#!/bin/bash #/** #@file Boot.lib #@brief Librería o clase Boot #@class Boot #@brief Funciones para arranque y post-configuración de sistemas de archivos. #@version 0.9 #@warning License: GNU GPLv3+ #*/ #/** # ogBoot int_ndisk int_npartition #@brief Inicia el proceso de arranque de un sistema de archivos. #@arg \c int_ndisk nº de orden del disco #@arg \c int_npartition nº de orden de la partición #@return (activar el sistema de archivos). #@exception OG_ERR_FORMAT Formato incorrecto. #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. #@exception OG_ERR_NOTOS La partición no tiene instalado un sistema operativo. #@note En Linux, debe arrancarse la partición del directorio \c /boot #@version 0.9 - Adaptación para OpenGNSys. #@author Ramon Gomez, ETSII Universidad de Sevilla #@date 2009-09-11 #*/ function ogBoot () { # Variables locales. local PART TYPE MNTDIR PARAMS KERNEL INITRD APPEND FILE LOADER #/// Si se solicita, mostrar ayuda. if [ "$*" == "help" ]; then ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ "$FUNCNAME 1 1" return fi #/// Error si no se reciben 2 parámetros. [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? #/// Detectar tipo de sistema de archivos y montarlo. PART=$(ogDiskToDev $1 $2) || return $? TYPE=$(ogGetFsType $1 $2) || return $? MNTDIR=$(ogMount $1 $2) 2>/dev/null [ -z "$MNTDIR" ] && ogRaiseError OG_ERR_PARTITION "$1, $2" && return $? case "$TYPE" in EXT[234]|REISERFS|REISER4|JFS|XFS) #/// Obtiene los parámetros de arranque para Linux. PARAMS=$(ogLinuxBootParameters $1 $2) || return $? read -e KERNEL INITRD APPEND <<<"$PARAMS" #/// Si no hay kernel, no hay sistema operativo. [ -z "$KERNEL" ] && ogRaiseError $OG_ERR_NOTOS && return $? #/// Arrancar de partición distinta a la original. [ -e "$MNTDIR/etc" ] && APPEND=$(echo $APPEND | awk -v P="$PART " '{sub (/root=[-+=_/a-zA-Z0-9]* /,"root="P);print}') # Configurar kernel Linux con los parámetros leídos de su GRUB. kexec -l "${MNTDIR}${KERNEL}" --append="$APPEND" --initrd="${MNTDIR}${INITRD}" ;; NTFS|HNTFS|FAT32|HFAT32) #/// Compruebar si hay un cargador de Windows. for f in io.sys ntldr bootmgr; do FILE="$(ogGetPath $1 $2 $f 2>/dev/null)" [ -n "$FILE" ] && LOADER="$(basename $FILE)" done [ -z "$LOADER" ] && ogRaiseError $OG_ERR_NOTOS && return $? # Activar la partición y copiar Grub4DOS. ogSetPartitionActive $1 $2 cp $OGLIB/grub4dos/* $MNTDIR # */ (necesario para Doxygen) kexec -l $MNTDIR/grub.exe --append=--config-file="find --set-root /$LOADER; chainloader /$LOADER; tpm --init" ;; *) ogRaiseError $OG_ERR_PARTITION "$1, $2" return $? ;; esac #/// Arrancar. kexec -e } #/** # ogGetRegistryValue path_mountpoint str_registrytype str_valuename #@brief Devuelve el dato de un valor del registro de Windows. #@arg \c path_mountpoint directorio donde está montado el sistema Windows #@arg \c str_registrytype tipo de registro a leer #@arg \c str_valuename valor de registro #@return str_valuedata - valor de la clave. #@exception OG_ERR_FORMAT Formato incorrecto. #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo. #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. #@note registrytype = { default, sam, security, software, system, components } #@warning Requisitos: chntpw, awk #@warning La partición de Windows debe estar montada previamente. #@version 0.9 - Adaptación para OpenGNSys. #@author Ramon Gomez, ETSII Universidad de Sevilla #@date 2009-09-11 #*/ function ogGetRegistryValue () { # Variables locales. local FILE FILENT FILEXP #/// Si se solicita, mostrar ayuda. if [ "$*" == "help" ]; then ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_registrytype str_key" return fi #/// Error si no se reciben 3 parámetros. [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? #/// Camino del fichero de registro en NT/2000 o XP/Vista/7. FILENT=$(ogGetPath "/$1/winnt/system32/config/$2") [ -f $FILENT ] && FILE="$FILENT" FILEXP=$(ogGetPath "/$1/windows/system32/config/$2") [ -f $FLEHXP ] && FILE="$FILEXP" [ ! -f $FILE ] && ogRaiseError OG_ERR_NOTFOUND "$1,$2" && return $? #/// Devolver el dato del valor de registro. chntpw $FILE << FIN 2>/dev/null | awk '/> Value/ {getline;print $0;}' cd ${3%\\*} cat ${3##*\\} q FIN } #/** # ogGetWindowsName int_ndisk int_npartition #@brief Muestra el nombre del equipo en el registro de Windows. #@arg \c int_ndisk nº de orden del disco #@arg \c int_npartition nº de orden de la partición #@return str_name - nombre del equipo #@exception OG_ERR_FORMAT Formato incorrecto. #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. #@version 0.9 - Adaptación para OpenGNSys. #@author Ramon Gomez, ETSII Universidad de Sevilla #@date 2009-09-23 #*/ function ogGetWindowsName () { # Variables locales. local PART MNTDIR #/// Si se solicita, mostrar ayuda. if [ "$*" == "help" ]; then ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ "$FUNCNAME 1 1 ==> PRACTICA-PC" return fi #/// Error si no se reciben 2 parámetros. [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? #/// Montar el sistema de archivos. MNTDIR=$(ogMount $1 $2) 2>/dev/null [ -z "$MNTDIR" ] && ogRaiseError OG_ERR_PARTITION "$1, $2" && return $? #/// Obtener dato del valor de registro. ogGetRegistryValue $MNTDIR system '\ControlSet001\Control\ComputerName\ComputerName\ComputerName' } #/** # ogListRegistryKeys path_mountpoint str_registrytype str_key #@brief Lista los nombres de claves de una determinada clave del registro de Windows. #@arg \c path_mountpoint directorio donde está montado el sistema Windows #@arg \c str_registrytype tipo de registro a leer #@arg \c str_key clave de registro #@return #@exception OG_ERR_FORMAT Formato incorrecto. #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. #@note registrytype = { default, sam, security, software, system, components } #@warning Requisitos: chntpw, awk #@warning La partición de Windows debe estar montada previamente. #@version 0.9 - Adaptación para OpenGNSys. #@author Ramon Gomez, ETSII Universidad de Sevilla #@date 2009-09-23 #*/ function ogListRegistryKeys () { # Variables locales. local FILE FILENT FILEXP #/// Si se solicita, mostrar ayuda. if [ "$*" == "help" ]; then ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_registrytype str_key" return fi #/// Error si no se reciben 3 parámetros. [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? #/// Camino del fichero de registro en NT/2000 o XP/Vista/7. FILENT=$(ogGetPath "/$1/winnt/system32/config/$2") [ -f $FILENT ] && FILE="$FILENT" FILEXP=$(ogGetPath "/$1/windows/system32/config/$2") [ -f $FLEHXP ] && FILE="$FILEXP" [ ! -f $FILE ] && ogRaiseError OG_ERR_NOTFOUND "$1,$2" && return $? #/// Devolver la lista de claves de registro. chntpw $FILE << FIN 2>/dev/null | awk 'BEGIN {FS="[<>]"} $1~/^ $/ {print $2}' ls $3 q FIN } #/** # ogLinuxBootParameters int_ndisk int_npartition #@brief Muestra los parámetros de arranque de un sistema de archivos Linux. #@arg \c int_ndisk nº de orden del disco #@arg \c int_npartition nº de orden de la partición #@return kernel initrd parámetros ... #@exception OG_ERR_FORMAT Formato incorrecto. #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. #@warning Función básica usada por \c ogBoot #@version 0.9 - Primera adaptación para OpenGNSys. #@author Ramon Gomez, ETSII Universidad de Sevilla #@date 2009-09-11 #*/ function ogLinuxBootParameters () { # Variables locales. local MNTDIR CONF #/// Si se solicita, mostrar ayuda. if [ "$*" == "help" ]; then ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ "$FUNCNAME 1 2 ==> ..." return fi #/// Error si no se reciben 2 parámetros. [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? #/// Detectar id. de tipo de partición y codificar al mnemonico. MNTDIR=$(ogMount $1 $2) 2>/dev/null [ -z "$MNTDIR" ] && ogRaiseError OG_ERR_PARTITION "$1, $2" && return $? # Fichero de configuración de GRUB. CONF="$MNTDIR/boot/grub/menu.lst" [ ! -e $CONF ] && CONF="$MNTDIR/boot/grub/grub.cfg" [ ! -e $CONF ] && ogRaiseError $OG_ERR_NOTFOUND "grub.cfg" && return $? #/** Toma del fichero de configuracion los valores del kernel, initrd # y parámetros de arranque usando las cláusulas por defecto # ("default" en GRUB1, "set default" en GRUB2) # y los formatea para que sean compatibles con \c kexec . */ #/* (ncesario para Doxygen) awk 'BEGIN {cont=-1;} $1~/^default/ {sub(/=/," "); def=$2;} $1~/^set/ && $2~/^default/ {gsub(/[="]/," "); def=$3;} $1~/^title|^menuentry/ {cont++} $1~/^kernel|^linux/ {if (def==cont) { kern=$2; sub($1,"");sub($1,"");sub(/^[ \t]*/,"");app=$0} } $1~/^initrd/ {if (def==cont) init=$2} END {if (kern!="") printf("%s %s %s", kern,init,app)} ' $CONF } #/** # ogSetRegistryValue path_mountpoint str_registrytype str_valuename str_valuedata #@brief Establece el dato asociado a un valor del registro de Windows. #@arg \c path_mountpoint directorio donde está montado el sistema Windows #@arg \c str_registrytype tipo de registro #@arg \c str_valuename nombre del valor de registro #@arg \c str_valuedata dato del valor de registro #@return (nada) #@exception OG_ERR_FORMAT Formato incorrecto. #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo. #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. #@note registrytype = { default, sam, security, software, system, components } #@warning Requisitos: chntpw, awk #@warning La partición de Windows debe estar montada previamente. #@version 0.9 - Adaptación para OpenGNSys. #@author Ramon Gomez, ETSII Universidad de Sevilla #@date 2009-09-24 #*/ function ogSetRegistryValue () { # Variables locales. local FILE FILENT FILEXP #/// Si se solicita, mostrar ayuda. if [ "$*" == "help" ]; then ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_registrytype str_key" return fi #/// Error si no se reciben 4 parámetros. [ $# == 4 ] || ogRaiseError $OG_ERR_FORMAT || return $? #/// Camino del fichero de registro en NT/2000 o XP/Vista/7. FILENT=$(ogGetPath "/$1/winnt/system32/config/$2") [ -f $FILENT ] && FILE="$FILENT" FILEXP=$(ogGetPath "/$1/windows/system32/config/$2") [ -f $FLEHXP ] && FILE="$FILEXP" [ ! -f $FILE ] && ogRaiseError OG_ERR_NOTFOUND "$1,$2" && return $? #/// Cambiar el dato del valor de registro. chntpw $FILE << FIN &>/dev/null ed $3 $4 q y FIN } #/** # ogSetWindowsName int_ndisk int_npartition str_name #@brief Establece el nombre del equipo en el registro de Windows. #@arg \c int_ndisk nº de orden del disco #@arg \c int_npartition nº de orden de la partición #@arg \c str_name nombre asignado #@return (nada) #@exception OG_ERR_FORMAT Formato incorrecto. #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. #@version 0.9 - Adaptación a OpenGNSys. #@author Ramon Gomez, ETSII Universidad de Sevilla #@date 2009-09-24 #*/ function ogSetWindowsName () { # Variables locales. local PART MNTDIR NAME #/// Si se solicita, mostrar ayuda. if [ "$*" == "help" ]; then ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ "$FUNCNAME 1 1 PRACTICA-PC" return fi #/// Error si no se reciben 3 parámetros. [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? #/// Montar el sistema de archivos. MNTDIR=$(ogMount $1 $2) 2>/dev/null [ -z "$MNTDIR" ] && ogRaiseError OG_ERR_PARTITION "$1, $2" && return $? NAME="$3" #/// Modificar datos de los valores de registro. ogSetRegistryValue $MNTDIR system '\ControlSet001\Control\ComputerName\ComputerName\ComputerName' "$NAME" ogSetRegistryValue $MNTDIR system '\ControlSet001\Services\Tcpip\Parameters\Hostname' "$NAME" ogSetRegistryValue $MNTDIR system '\ControlSet001\Services\Tcpip\Parameters\NV Hostname' "$NAME" } function ogNewMbrXP () { #/** @function NewMbrXP: @brief Genera un nuevo Master Boot Record en el disco duro indicado, compatible con los SO tipo Windows #@param $1 obligatorio int_numdisk #@return salida del programa my-sys #@warning no definidos #@attention Requisitos: my-sys, compilado ubicado en /var/EAC/admin/source #@note #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga #*/ # if [ $# = 0 ] then Msg "sintaxis: ogNewMbrXP int_disco " red return fi if [ $# = 1 ] then particion=`ogDiskToDev $1` ms-sys -z -f $particion ms-sys -m -f $particion fi }