source: client/engine/Boot.lib @ 6e139c9

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since 6e139c9 was 6e139c9, checked in by adv <adv@…>, 15 years ago

ogNewMbrXP 0.9 adaptado a sintáxis OpenGNsys

git-svn-id: https://opengnsys.es/svn/trunk@656 a21b9725-9963-47de-94b9-378ad31fedc9

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