1 | #!/bin/bash |
---|
2 | #/** |
---|
3 | #@file Boot.lib |
---|
4 | #@brief Librería o clase Boot |
---|
5 | #@class Boot |
---|
6 | #@brief Funciones para arranque y post-configuración de sistemas de archivos. |
---|
7 | #@version 0.9 |
---|
8 | #@warning License: GNU GPLv3+ |
---|
9 | #*/ |
---|
10 | |
---|
11 | |
---|
12 | #/** |
---|
13 | # ogBoot int_ndisk int_npartition |
---|
14 | #@brief Inicia el proceso de arranque de un sistema de archivos. |
---|
15 | #@arg \c int_ndisk nº de orden del disco |
---|
16 | #@arg \c int_npartition nº de orden de la partición |
---|
17 | #@return (activar el sistema de archivos). |
---|
18 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
19 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
20 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
21 | #@exception OG_ERR_NOTOS La partición no tiene instalado un sistema operativo. |
---|
22 | #@note En Linux, debe arrancarse la partición del directorio \c /boot |
---|
23 | #@version 0.9 - Adaptación para OpenGNSys. |
---|
24 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
25 | #@date 2009-09-11 |
---|
26 | #*/ |
---|
27 | function ogBoot () { |
---|
28 | |
---|
29 | # Variables locales. |
---|
30 | local PART TYPE MNTDIR PARAMS KERNEL INITRD APPEND FILE LOADER |
---|
31 | |
---|
32 | #/// Si se solicita, mostrar ayuda. |
---|
33 | if [ "$*" == "help" ]; then |
---|
34 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
35 | "$FUNCNAME 1 1" |
---|
36 | return |
---|
37 | fi |
---|
38 | #/// Error si no se reciben 2 parámetros. |
---|
39 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
40 | |
---|
41 | #/// Detectar tipo de sistema de archivos y montarlo. |
---|
42 | PART=$(ogDiskToDev $1 $2) || return $? |
---|
43 | TYPE=$(ogGetFsType $1 $2) || return $? |
---|
44 | MNTDIR=$(ogMount $1 $2) 2>/dev/null |
---|
45 | [ -z "$MNTDIR" ] && ogRaiseError OG_ERR_PARTITION "$1, $2" && return $? |
---|
46 | |
---|
47 | case "$TYPE" in |
---|
48 | EXT[234]|REISERFS|REISER4|JFS|XFS) |
---|
49 | #/// Obtiene los parámetros de arranque para Linux. |
---|
50 | PARAMS=$(ogLinuxBootParameters $1 $2) || return $? |
---|
51 | read -e KERNEL INITRD APPEND <<<"$PARAMS" |
---|
52 | #/// Si no hay kernel, no hay sistema operativo. |
---|
53 | [ -z "$KERNEL" ] && ogRaiseError $OG_ERR_NOTOS && return $? |
---|
54 | #/// Arrancar de partición distinta a la original. |
---|
55 | [ -e "$MNTDIR/etc" ] && APPEND=$(echo $APPEND | awk -v P="$PART " '{sub (/root=[-+=_/a-zA-Z0-9]* /,"root="P);print}') |
---|
56 | # Configurar kernel Linux con los parámetros leídos de su GRUB. |
---|
57 | kexec -l "${MNTDIR}${KERNEL}" --append="$APPEND" --initrd="${MNTDIR}${INITRD}" |
---|
58 | ;; |
---|
59 | NTFS|HNTFS|FAT32|HFAT32) |
---|
60 | #/// Compruebar si hay un cargador de Windows. |
---|
61 | for f in io.sys ntldr bootmgr; do |
---|
62 | FILE="$(ogGetPath $1 $2 $f 2>/dev/null)" |
---|
63 | [ -n "$FILE" ] && LOADER="$(basename $FILE)" |
---|
64 | done |
---|
65 | [ -z "$LOADER" ] && ogRaiseError $OG_ERR_NOTOS && return $? |
---|
66 | # Activar la partición y copiar Grub4DOS. |
---|
67 | ogSetPartitionActive $1 $2 |
---|
68 | cp $OGLIB/grub4dos/* $MNTDIR # */ (necesario para Doxygen) |
---|
69 | kexec -l $MNTDIR/grub.exe --append=--config-file="find --set-root /$LOADER; chainloader /$LOADER; tpm --init" |
---|
70 | ;; |
---|
71 | *) ogRaiseError $OG_ERR_PARTITION "$1, $2" |
---|
72 | return $? |
---|
73 | ;; |
---|
74 | esac |
---|
75 | |
---|
76 | #/// Arrancar. |
---|
77 | kexec -e |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | #/** |
---|
82 | # ogGetRegistryValue path_mountpoint str_registrytype str_valuename |
---|
83 | #@brief Devuelve el dato de un valor del registro de Windows. |
---|
84 | #@arg \c path_mountpoint directorio donde está montado el sistema Windows |
---|
85 | #@arg \c str_registrytype tipo de registro a leer |
---|
86 | #@arg \c str_valuename valor de registro |
---|
87 | #@return str_valuedata - valor de la clave. |
---|
88 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
89 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo. |
---|
90 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
91 | #@note registrytype = { default, sam, security, software, system, components } |
---|
92 | #@warning Requisitos: chntpw, awk |
---|
93 | #@warning La partición de Windows debe estar montada previamente. |
---|
94 | #@version 0.9 - Adaptación para OpenGNSys. |
---|
95 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
96 | #@date 2009-09-11 |
---|
97 | #*/ |
---|
98 | function ogGetRegistryValue () { |
---|
99 | |
---|
100 | # Variables locales. |
---|
101 | local FILE FILENT FILEXP |
---|
102 | |
---|
103 | #/// Si se solicita, mostrar ayuda. |
---|
104 | if [ "$*" == "help" ]; then |
---|
105 | ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_registrytype str_key" |
---|
106 | return |
---|
107 | fi |
---|
108 | #/// Error si no se reciben 3 parámetros. |
---|
109 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
110 | |
---|
111 | #/// Camino del fichero de registro en NT/2000 o XP/Vista/7. |
---|
112 | FILENT=$(ogGetPath "/$1/winnt/system32/config/$2") |
---|
113 | [ -f $FILENT ] && FILE="$FILENT" |
---|
114 | FILEXP=$(ogGetPath "/$1/windows/system32/config/$2") |
---|
115 | [ -f $FLEHXP ] && FILE="$FILEXP" |
---|
116 | [ ! -f $FILE ] && ogRaiseError OG_ERR_NOTFOUND "$1,$2" && return $? |
---|
117 | |
---|
118 | #/// Devolver el dato del valor de registro. |
---|
119 | chntpw $FILE << FIN 2>/dev/null | awk '/> Value/ {getline;print $0;}' |
---|
120 | cd ${3%\\*} |
---|
121 | cat ${3##*\\} |
---|
122 | q |
---|
123 | FIN |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | #/** |
---|
128 | # ogGetWindowsName int_ndisk int_npartition |
---|
129 | #@brief Muestra el nombre del equipo en el registro de Windows. |
---|
130 | #@arg \c int_ndisk nº de orden del disco |
---|
131 | #@arg \c int_npartition nº de orden de la partición |
---|
132 | #@return str_name - nombre del equipo |
---|
133 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
134 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
135 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
136 | #@version 0.9 - Adaptación para OpenGNSys. |
---|
137 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
138 | #@date 2009-09-23 |
---|
139 | #*/ |
---|
140 | function ogGetWindowsName () { |
---|
141 | |
---|
142 | # Variables locales. |
---|
143 | local PART MNTDIR |
---|
144 | |
---|
145 | #/// Si se solicita, mostrar ayuda. |
---|
146 | if [ "$*" == "help" ]; then |
---|
147 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
148 | "$FUNCNAME 1 1 ==> PRACTICA-PC" |
---|
149 | return |
---|
150 | fi |
---|
151 | #/// Error si no se reciben 2 parámetros. |
---|
152 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
153 | |
---|
154 | #/// Montar el sistema de archivos. |
---|
155 | MNTDIR=$(ogMount $1 $2) 2>/dev/null |
---|
156 | [ -z "$MNTDIR" ] && ogRaiseError OG_ERR_PARTITION "$1, $2" && return $? |
---|
157 | |
---|
158 | #/// Obtener dato del valor de registro. |
---|
159 | ogGetRegistryValue $MNTDIR system '\ControlSet001\Control\ComputerName\ComputerName\ComputerName' |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | #/** |
---|
164 | # ogListRegistryKeys path_mountpoint str_registrytype str_key |
---|
165 | #@brief Lista los nombres de claves de una determinada clave del registro de Windows. |
---|
166 | #@arg \c path_mountpoint directorio donde está montado el sistema Windows |
---|
167 | #@arg \c str_registrytype tipo de registro a leer |
---|
168 | #@arg \c str_key clave de registro |
---|
169 | #@return |
---|
170 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
171 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
172 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
173 | #@note registrytype = { default, sam, security, software, system, components } |
---|
174 | #@warning Requisitos: chntpw, awk |
---|
175 | #@warning La partición de Windows debe estar montada previamente. |
---|
176 | #@version 0.9 - Adaptación para OpenGNSys. |
---|
177 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
178 | #@date 2009-09-23 |
---|
179 | #*/ |
---|
180 | function ogListRegistryKeys () { |
---|
181 | |
---|
182 | # Variables locales. |
---|
183 | local FILE FILENT FILEXP |
---|
184 | |
---|
185 | #/// Si se solicita, mostrar ayuda. |
---|
186 | if [ "$*" == "help" ]; then |
---|
187 | ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_registrytype str_key" |
---|
188 | return |
---|
189 | fi |
---|
190 | #/// Error si no se reciben 3 parámetros. |
---|
191 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
192 | |
---|
193 | #/// Camino del fichero de registro en NT/2000 o XP/Vista/7. |
---|
194 | FILENT=$(ogGetPath "/$1/winnt/system32/config/$2") |
---|
195 | [ -f $FILENT ] && FILE="$FILENT" |
---|
196 | FILEXP=$(ogGetPath "/$1/windows/system32/config/$2") |
---|
197 | [ -f $FLEHXP ] && FILE="$FILEXP" |
---|
198 | [ ! -f $FILE ] && ogRaiseError OG_ERR_NOTFOUND "$1,$2" && return $? |
---|
199 | |
---|
200 | #/// Devolver la lista de claves de registro. |
---|
201 | chntpw $FILE << FIN 2>/dev/null | awk 'BEGIN {FS="[<>]"} $1~/^ $/ {print $2}' |
---|
202 | ls $3 |
---|
203 | q |
---|
204 | FIN |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | #/** |
---|
209 | # ogLinuxBootParameters int_ndisk int_npartition |
---|
210 | #@brief Muestra los parámetros de arranque de un sistema de archivos Linux. |
---|
211 | #@arg \c int_ndisk nº de orden del disco |
---|
212 | #@arg \c int_npartition nº de orden de la partición |
---|
213 | #@return kernel initrd parámetros ... |
---|
214 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
215 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
216 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
217 | #@warning Función básica usada por \c ogBoot |
---|
218 | #@version 0.9 - Primera adaptación para OpenGNSys. |
---|
219 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
220 | #@date 2009-09-11 |
---|
221 | #*/ |
---|
222 | function ogLinuxBootParameters () { |
---|
223 | |
---|
224 | # Variables locales. |
---|
225 | local MNTDIR CONF |
---|
226 | |
---|
227 | #/// Si se solicita, mostrar ayuda. |
---|
228 | if [ "$*" == "help" ]; then |
---|
229 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
230 | "$FUNCNAME 1 2 ==> ..." |
---|
231 | return |
---|
232 | fi |
---|
233 | #/// Error si no se reciben 2 parámetros. |
---|
234 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
235 | |
---|
236 | #/// Detectar id. de tipo de partición y codificar al mnemonico. |
---|
237 | MNTDIR=$(ogMount $1 $2) 2>/dev/null |
---|
238 | [ -z "$MNTDIR" ] && ogRaiseError OG_ERR_PARTITION "$1, $2" && return $? |
---|
239 | |
---|
240 | # Fichero de configuración de GRUB. |
---|
241 | CONF="$MNTDIR/boot/grub/menu.lst" |
---|
242 | [ ! -e $CONF ] && CONF="$MNTDIR/boot/grub/grub.cfg" |
---|
243 | [ ! -e $CONF ] && ogRaiseError $OG_ERR_NOTFOUND "grub.cfg" && return $? |
---|
244 | |
---|
245 | #/** Toma del fichero de configuracion los valores del kernel, initrd |
---|
246 | # y parámetros de arranque usando las cláusulas por defecto |
---|
247 | # ("default" en GRUB1, "set default" en GRUB2) |
---|
248 | # y los formatea para que sean compatibles con \c kexec . */ |
---|
249 | #/* (ncesario para Doxygen) |
---|
250 | awk 'BEGIN {cont=-1;} |
---|
251 | $1~/^default/ {sub(/=/," "); def=$2;} |
---|
252 | $1~/^set/ && $2~/^default/ {gsub(/[="]/," "); def=$3;} |
---|
253 | $1~/^title|^menuentry/ {cont++} |
---|
254 | $1~/^kernel|^linux/ {if (def==cont) { |
---|
255 | kern=$2; |
---|
256 | sub($1,"");sub($1,"");sub(/^[ \t]*/,"");app=$0} |
---|
257 | } |
---|
258 | $1~/^initrd/ {if (def==cont) init=$2} |
---|
259 | END {if (kern!="") printf("%s %s %s", kern,init,app)} |
---|
260 | ' $CONF |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | |
---|
265 | #/** |
---|
266 | # ogSetRegistryValue path_mountpoint str_registrytype str_valuename str_valuedata |
---|
267 | #@brief Establece el dato asociado a un valor del registro de Windows. |
---|
268 | #@arg \c path_mountpoint directorio donde está montado el sistema Windows |
---|
269 | #@arg \c str_registrytype tipo de registro |
---|
270 | #@arg \c str_valuename nombre del valor de registro |
---|
271 | #@arg \c str_valuedata dato del valor de registro |
---|
272 | #@return (nada) |
---|
273 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
274 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo. |
---|
275 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
276 | #@note registrytype = { default, sam, security, software, system, components } |
---|
277 | #@warning Requisitos: chntpw, awk |
---|
278 | #@warning La partición de Windows debe estar montada previamente. |
---|
279 | #@version 0.9 - Adaptación para OpenGNSys. |
---|
280 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
281 | #@date 2009-09-24 |
---|
282 | #*/ |
---|
283 | function ogSetRegistryValue () { |
---|
284 | |
---|
285 | # Variables locales. |
---|
286 | local FILE FILENT FILEXP |
---|
287 | |
---|
288 | #/// Si se solicita, mostrar ayuda. |
---|
289 | if [ "$*" == "help" ]; then |
---|
290 | ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_registrytype str_key" |
---|
291 | return |
---|
292 | fi |
---|
293 | #/// Error si no se reciben 4 parámetros. |
---|
294 | [ $# == 4 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
295 | |
---|
296 | #/// Camino del fichero de registro en NT/2000 o XP/Vista/7. |
---|
297 | FILENT=$(ogGetPath "/$1/winnt/system32/config/$2") |
---|
298 | [ -f $FILENT ] && FILE="$FILENT" |
---|
299 | FILEXP=$(ogGetPath "/$1/windows/system32/config/$2") |
---|
300 | [ -f $FLEHXP ] && FILE="$FILEXP" |
---|
301 | [ ! -f $FILE ] && ogRaiseError OG_ERR_NOTFOUND "$1,$2" && return $? |
---|
302 | |
---|
303 | #/// Cambiar el dato del valor de registro. |
---|
304 | chntpw $FILE << FIN &>/dev/null |
---|
305 | ed $3 |
---|
306 | $4 |
---|
307 | q |
---|
308 | y |
---|
309 | FIN |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | #/** |
---|
314 | # ogSetWindowsName int_ndisk int_npartition str_name |
---|
315 | #@brief Establece el nombre del equipo en el registro de Windows. |
---|
316 | #@arg \c int_ndisk nº de orden del disco |
---|
317 | #@arg \c int_npartition nº de orden de la partición |
---|
318 | #@arg \c str_name nombre asignado |
---|
319 | #@return (nada) |
---|
320 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
321 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
322 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
323 | #@version 0.9 - Adaptación a OpenGNSys. |
---|
324 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
325 | #@date 2009-09-24 |
---|
326 | #*/ |
---|
327 | function ogSetWindowsName () { |
---|
328 | |
---|
329 | # Variables locales. |
---|
330 | local PART MNTDIR NAME |
---|
331 | |
---|
332 | #/// Si se solicita, mostrar ayuda. |
---|
333 | if [ "$*" == "help" ]; then |
---|
334 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
335 | "$FUNCNAME 1 1 PRACTICA-PC" |
---|
336 | return |
---|
337 | fi |
---|
338 | #/// Error si no se reciben 3 parámetros. |
---|
339 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
340 | |
---|
341 | #/// Montar el sistema de archivos. |
---|
342 | MNTDIR=$(ogMount $1 $2) 2>/dev/null |
---|
343 | [ -z "$MNTDIR" ] && ogRaiseError OG_ERR_PARTITION "$1, $2" && return $? |
---|
344 | NAME="$3" |
---|
345 | |
---|
346 | #/// Modificar datos de los valores de registro. |
---|
347 | ogSetRegistryValue $MNTDIR system '\ControlSet001\Control\ComputerName\ComputerName\ComputerName' "$NAME" |
---|
348 | ogSetRegistryValue $MNTDIR system '\ControlSet001\Services\Tcpip\Parameters\Hostname' "$NAME" |
---|
349 | ogSetRegistryValue $MNTDIR system '\ControlSet001\Services\Tcpip\Parameters\NV Hostname' "$NAME" |
---|
350 | } |
---|
351 | |
---|
352 | |
---|
353 | function ogNewMbrXP () { |
---|
354 | #/** @function NewMbrXP: @brief Genera un nuevo Master Boot Record en el disco duro indicado, compatible con los SO tipo Windows |
---|
355 | #@param $1 obligatorio int_numdisk |
---|
356 | #@return salida del programa my-sys |
---|
357 | #@warning no definidos |
---|
358 | #@attention Requisitos: my-sys, compilado ubicado en /var/EAC/admin/source |
---|
359 | #@note |
---|
360 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
361 | #*/ |
---|
362 | # |
---|
363 | if [ $# = 0 ] |
---|
364 | then |
---|
365 | Msg "sintaxis: ogNewMbrXP int_disco " red |
---|
366 | return |
---|
367 | fi |
---|
368 | if [ $# = 1 ] |
---|
369 | then |
---|
370 | particion=`ogDiskToDev $1` |
---|
371 | ms-sys -z -f $particion |
---|
372 | ms-sys -m -f $particion |
---|
373 | fi |
---|
374 | } |
---|