opengnsys/client/engine/Boot.lib

942 lines
31 KiB
Bash

#!/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 1.0.4
#@warning License: GNU GPLv3+
#*/
#/**
# ogBoot int_ndisk int_npartition
#@brief Inicia el proceso de arranque de un sistema de archivos.
#@param int_ndisk nº de orden del disco
#@param 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.1 - Integración para OpenGnSys. - EAC: HDboot; BootLinuxEX en Boot.lib
#@author Antonio J. Doblas Viso, Universidad de Malaga
#@date 2008-10-27
#@version 0.9 - Adaptación para OpenGnSys.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2009-09-11
#@version 1.0.4 - Soporta modo de arranque Windows (parámetro de inicio "winboot").
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2012-04-12
#*/ ##
function ogBoot ()
{
# Variables locales.
local PART TYPE MNTDIR PARAMS KERNEL INITRD APPEND FILE LOADER f
# 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}"
kexec -e &
;;
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="$f"
done
[ -z "$LOADER" ] && ogRaiseError $OG_ERR_NOTOS && return $?
# Activar la partición.
ogSetPartitionActive $1 $2
ogSetRegistryValue $MNTDIR SYSTEM '\ControlSet001\services\Cliente Opengnsys\Start' '2'
ogSetRegistryValue $MNTDIR SYSTEM '\ControlSet002\services\Cliente Opengnsys\Start' '2'
ogSetRegistryValue $MNTDIR SYSTEM '\ControlSet003\services\Cliente Opengnsys\Start' '2'
if [ "$winboot" == "kexec" ]; then
# Modo de arranque en caliente (con kexec).
cp $OGLIB/grub4dos/* $MNTDIR # */ (Comentario Doxygen)
kexec -l $MNTDIR/grub.exe --append=--config-file="root (hd$[$1-1],$[$2-1]); chainloader (hd$[$1-1],$[$2-1])/$LOADER; tpm --init"
kexec -e &
else
# Modo de arranque por reinicio (con reboot).
dd if=/dev/zero of=${MNTDIR}/ogboot.me bs=1024 count=3
dd if=/dev/zero of=${MNTDIR}/ogboot.firstboot bs=1024 count=3
dd if=/dev/zero of=${MNTDIR}/ogboot.secondboot bs=1024 count=3
ogLoadHiveWindows $1 $2
ogHiveNTRunMachine "cmd /c del c:\ogboot.*" ogcleanboot
ogUpdateHiveWindows
reboot
fi
;;
*) ogRaiseError $OG_ERR_PARTITION "$1, $2"
return $?
;;
esac
# Parar Browser para evitar cuelgues.
pkill browser
}
#/**
# ogGetWindowsName int_ndisk int_npartition
#@brief Muestra el nombre del equipo en el registro de Windows.
#@param int_ndisk nº de orden del disco
#@param 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'
}
#/**
# ogLinuxBootParameters int_ndisk int_npartition
#@brief Muestra los parámetros de arranque de un sistema de archivos Linux.
#@param int_ndisk nº de orden del disco
#@param int_npartition nº de orden de la partición
#@return str_kernel str_initrd str_parameters ...
#@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
#@version 0.9.2 - Soporta partición /boot independiente.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010-07-20
#*/ ##
function ogLinuxBootParameters ()
{
# Variables locales.
local MNTDIR CONFDIR CONFFILE
# 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.
CONFDIR=$MNTDIR # Partición de arranque /boot.
[ -d $MNTDIR/boot ] && CONFDIR=$MNTDIR/boot # Partición raíz con directorio boot.
CONFFILE="$CONFDIR/grub/menu.lst"
[ ! -e $CONFFILE ] && CONFFILE="$CONFDIR/grub/grub.cfg"
[ ! -e $CONFFILE ] && 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 . */
# /* (comentario 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} # /* (comentario Doxygen)
}
$1~/^initrd/ {if (def==cont) init=$2}
END {if (kern!="") printf("%s %s %s", kern,init,app)}
' $CONFFILE
# */ (comentario Doxygen)
}
#/**
# ogSetWindowsName int_ndisk int_npartition str_name
#@brief Establece el nombre del equipo en el registro de Windows.
#@param int_ndisk nº de orden del disco
#@param int_npartition nº de orden de la partición
#@param 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 str_name" \
"$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" 2>/dev/null
ogSetRegistryValue $MNTDIR system '\ControlSet001\Services\Tcpip\Parameters\Hostname' "$NAME" 2>/dev/null
ogSetRegistryValue $MNTDIR system '\ControlSet001\services\Tcpip\Parameters\Hostname' "$NAME" 2>/dev/null
ogSetRegistryValue $MNTDIR system '\ControlSet001\Services\Tcpip\Parameters\NV Hostname' "$NAME" 2>/dev/null
ogSetRegistryValue $MNTDIR system '\ControlSet001\services\Tcpip\Parameters\NV Hostname' "$NAME" 2>/dev/null
}
#/**
# ogSetWinlogonUser int_ndisk int_npartition str_username
#@brief Establece el nombre de usuario por defecto en la entrada de Windows.
#@param int_ndisk nº de orden del disco
#@param int_npartition nº de orden de la partición
#@param str_username nombre de usuario por defecto
#@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.2 - Adaptación a OpenGnSys.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010-07-20
#*/ ##
function ogSetWinlogonUser ()
{
# Variables locales.
local PART MNTDIR NAME
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition str_username" \
"$FUNCNAME 1 1 USUARIO"
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 en el registro.
ogSetRegistryValue $MNTDIR SOFTWARE '\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName' "$3"
}
#/**
# ogBootMbrXP int_ndisk
#@brief Genera un nuevo Master Boot Record en el disco duro indicado, compatible con los SO tipo Windows
#@param int_ndisk nº de orden del disco
#@return salida del programa my-sys
#@exception OG_ERR_FORMAT Formato incorrecto.
#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
#@version 0.9 - Adaptación a OpenGnSys.
#@author Antonio J. Doblas Viso. Universidad de Málaga
#@date 2009-09-24
#*/ ##
function ogBootMbrXP ()
{
# Variables locales.
local PART
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk " \
"$FUNCNAME 1 "
return
fi
# Error si no se reciben 1 parámetros.
[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
PART="$(ogDiskToDev $1)" || return $?
ms-sys -z -f $PART
ms-sys -m -f $PART
}
#/**
# ogBootMbrGeneric int_ndisk
#@brief Genera un nuevo Codigo de arranque en el MBR del disco indicado, compatible con los SO tipo Windows, Linux.
#@param int_ndisk nº de orden del disco
#@return salida del programa my-sys
#@exception OG_ERR_FORMAT Formato incorrecto.
#@exception OG_ERR_NOTFOUND Tipo de partición desconocido o no se puede montar.
#@version 0.9 - Adaptación a OpenGnSys.
#@author Antonio J. Doblas Viso. Universidad de Málaga
#@date 2009-09-24
#*/ ##
function ogBootMbrGeneric ()
{
# Variables locales.
local PART
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk " \
"$FUNCNAME 1 "
return
fi
# Error si no se reciben 1 parámetros.
[ $# == 1 ] || return $(ogRaiseError $OG_ERR_FORMAT; echo $?)
PART="$(ogDiskToDev $1)" || return $(ogRaiseError $OG_ERR_NOTFOUND; echo $?)
ms-sys -z -f $PART
ms-sys -s -f $PART
}
#/**
# ogFixBootSector int_ndisk int_parition
#@brief Corrige el boot sector de una particion activa para MS windows/dos -fat-ntfs
#@param int_ndisk nº de orden del disco
#@param int_partition nº de particion
#@return
#@exception OG_ERR_FORMAT Formato incorrecto.
#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
#@version 0.9 - Adaptación a OpenGnSys.
#@author Antonio J. Doblas Viso. Universidad de Málaga
#@date 2009-09-24
#*/ ##
function ogFixBootSector ()
{
# Variables locales.
local PARTYPE DISK PART FILE
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_partition " \
"$FUNCNAME 1 1 "
return
fi
# Error si no se reciben 2 parámetros.
[ $# == 2 ] || return $(ogRaiseError $OG_ERR_FORMAT; echo $?)
#TODO, solo si la particion existe
#TODO, solo si es ntfs o fat
PARTYPE=$(ogGetPartitionId $1 $2)
case "$PARTYPE" in
1|4|6|7|b|c|e|f)
;;
*)
return $(ogRaiseError $OG_ERR_PARTITION; echo $?)
;;
esac
ogUnmount $1 $2 || return $(ogRaiseError $OG_ERR_PARTITION; echo $?)
#Preparando instruccion
let DISK=$1-1
PART=$2
FILE=/tmp/temp$$
cat > $FILE <<EOF
disk=$DISK
main_part=$PART
fix_first_sector=yes
EOF
spartlnx.run -cui -nm -a -f $FILE &
sleep 5
ps aux > /dev/null | grep $! | grep -E "T|S" | kill -9 $! > /dev/null
rm -f $FILE
}
#/**
# ogWindowsBootParameters int_ndisk int_parition
#@brief Configura el gestor de arranque de windows 7 / vista / XP / 2000
#@param int_ndisk nº de orden del disco
#@param int_partition nº de particion
#@return
#@exception OG_ERR_FORMAT Formato incorrecto.
#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
#@version 0.9 - Integración desde EAC para OpenGnSys.
#@author Antonio J. Doblas Viso. Universidad de Málaga
#@date 2009-09-24
#@version 1.0.1 - Adapatacion para OpenGnsys.
#@author Antonio J. Doblas Viso. Universidad de Málaga
#@date 2011-05-20
#*/ ##
function ogWindowsBootParameters ()
{
# Variables locales.
local PART DISK FILE VERSION WINVER MOUNT
#Preparando variables adaptadas a sintaxis windows.
let DISK=$1-1
PART=$2
FILE=/tmp/temp$$
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_partition " \
"$FUNCNAME 1 1 "
return
fi
# Error si no se reciben 2 parámetros.
[ $# == 2 ] || return $(ogRaiseError $OG_ERR_FORMAT; echo $?)
ogDiskToDev $1 $2 || return $(ogRaiseError $OG_ERR_PARTITION; echo $?)
VERSION=$(ogGetOsVersion $1 $2)
if echo "$VERSION" | grep "Windows 7"
then
WINVER="Windows 7"
elif echo "$VERSION" | grep "Windows Seven"
then
WINVER="Windows Vista"
elif echo "$VERSION" | grep "XP"
then
MOUNT=$(ogMount $1 $2)
[ -f ${MOUNT}/boot.ini ] || return $(ogRaiseError $OG_ERR_NOTOS; echo $?)
cat ${MOUNT}/boot.ini | sed s/partition\([0-9]\)/partition\($PART\)/g | sed s/rdisk\([0-9]\)/rdisk\($DISK\)/g > ${MOUNT}/tmp.boot.ini; mv ${MOUNT}/tmp.boot.ini ${MOUNT}/boot.ini
return 0
else
return $(ogRaiseError $OG_ERR_NOTOS; echo $?)
fi
ogUnmount $1 $2 || return $(ogRaiseError $OG_ERR_PARTITION; echo $?)
#Preparando instruccion Windows Resume Application
cat > $FILE <<EOF
boot_disk=$DISK
boot_main_part=$PART
disk=$DISK
main_part=$PART
boot_entry=Windows Resume Application
EOF
spartlnx.run -cui -nm -w -f $FILE &
sleep 5
ps aux > /dev/null | grep $! | grep -E "T|S" | kill -9 $! > /dev/null
#Preparando instruccion tipo windows
cat > $FILE <<EOF
boot_disk=$DISK
boot_main_part=$PART
disk=$DISK
main_part=$PART
boot_entry=$WINVER
EOF
spartlnx.run -cui -nm -w -f $FILE &
sleep 5
ps aux > /dev/null | grep $! | grep -E "T|S" | kill -9 $! > /dev/null
##Preparando instruccion Ramdisk Options
cat > $FILE <<EOF
boot_disk=$DISK
boot_main_part=$PART
disk=$DISK
main_part=$PART
boot_entry=Ramdisk Options
EOF
spartlnx.run -cui -nm -w -f $FILE &
sleep 5
ps aux > /dev/null | grep $! | grep -E "T|S" | kill -9 $! > /dev/null
#Preparando instruccion Windows Boot Manager
cat > $FILE <<EOF
boot_disk=$DISK
boot_main_part=$PART
disk=$DISK
main_part=$PART
boot_entry=Windows Boot Manager
EOF
spartlnx.run -cui -nm -w -f $FILE &
sleep 5
ps aux > /dev/null | grep $! | grep -E "T|S" | kill -9 $! > /dev/null
#Preparando instruccion Herramienta de diagnóstico de memoria de Windows
cat > $FILE <<EOF
boot_disk=$DISK
boot_main_part=$PART
disk=$DISK
main_part=$PART
boot_entry=Herramienta de diagnóstico de memoria de Windows
EOF
spartlnx.run -cui -nm -w -f $FILE &
sleep 5
ps aux > /dev/null | grep $! | grep -E "T|S" | kill -9 $! > /dev/null
}
# ogWindowsRegisterPartition int_ndisk int_partiton str_volume int_disk int_partition
#@brief Registra una partición en windows con un determinado volumen.
#@param int_ndisk nº de orden del disco a registrar
#@param int_partition nº de particion a registrar
#@param str_volumen volumen a resgistar
#@param int_ndisk_windows nº de orden del disco donde esta windows
#@param int_partition_windows nº de particion donde esta windows
#@return
#@exception OG_ERR_FORMAT Formato incorrecto.
#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
#@version 0.9 - Adaptación a OpenGnSys.
#@author Antonio J. Doblas Viso. Universidad de Málaga
#@date 2009-09-24
#*/ ##
function ogWindowsRegisterPartition ()
{
# Variables locales.
local PART DISK FILE REGISTREDDISK REGISTREDPART REGISTREDVOL VERSION SYSTEMROOT
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk_TO_registre int_partition_TO_registre str_NewVolume int_disk int_parition " \
"$FUNCNAME 1 1 c: 1 1"
return
fi
# Error si no se reciben 5 parámetros.
[ $# == 5 ] || return $(ogRaiseError $OG_ERR_FORMAT; echo $?)
REGISTREDDISK=$1
REGISTREDPART=$2
REGISTREDVOL=$(echo $3 | cut -c1 | tr '[:lower:]' '[:upper:]')
DISK=$4
PART=$5
FILE=/tmp/temp$$
ogDiskToDev $REGISTREDDISK $REGISTREDPART || return $(ogRaiseError $OG_ERR_PARTITION "particion a registrar "; echo $?)
ogDiskToDev $DISK $PART || return $(ogRaiseError $OG_ERR_PARTITION "particion de windows"; echo $?)
ogGetOsType $DISK $PART | grep "Windows" || return $(ogRaiseError $OG_ERR_NOTOS "no es windows"; echo $?)
VERSION=$(ogGetOsVersion $DISK $PART)
#Systemroot
if ogGetPath $DISK $PART WINDOWS
then
SYSTEMROOT="Windows"
elif ogGetPath $DISK $PART WINNT
then
SYSTEMROOT="winnt"
else
return $(ogRaiseError $OG_ERR_NOTOS; echo $?)
fi
ogUnmount $DISK $PART
let DISK=$DISK-1
let REGISTREDDISK=$REGISTREDDISK-1
#Preparando instruccion Windows Boot Manager
cat > $FILE <<EOF
windows_disk=$DISK
windows_main_part=$PART
windows_dir=$SYSTEMROOT
disk=$REGISTREDDISK
main_part=$REGISTREDPART
;ext_part
part_letter=$REGISTREDVOL
EOF
spartlnx.run -cui -nm -u -f $FILE &
sleep 5
ps aux > /dev/null | grep $! | grep -E "T|S" | kill -9 $! > /dev/null
}
# ogGrubInstallMbr int_disk_GRUBCFG int_partition_GRUBCFG #@param bolean_Check_Os_installed_and_Configure_2ndStage true | false[default]
#@brief Instala y actualiza el gestor grub el el MBR del disco duro donde se encuentra el fichero grub.cfg
#@param int_disk_SecondStage
#@param int_part_SecondStage
#@param bolean_Check_Os_installed_and_Configure_2ndStage true | false[default]
#@return
#@exception OG_ERR_FORMAT Formato incorrecto.
#@version 1.0.2 - Primeras pruebas.
#@author Antonio J. Doblas Viso. Universidad de Malaga.
#@date 2011-10-29
#@version 1.0.3 - Soporte para linux de 32 y 64 bits
#@author Antonio J. Doblas Viso. Universidad de Malaga.
#@date 2012-03-13
#@version 1.0.3 - Ficheros de configuracion independientes segun ubicación de la primera etapa
#@author Antonio J. Doblas Viso. Universidad de Malaga.
#@date 2012-03-13
#*/ ##
function ogGrubInstallMbr {
# Variables locales.
local PART DISK VERSION FIRSTAGE SECONSTAGE CHECKOS KERNELPARAM
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndiskSecondStage int_partitionSecondStage bolean_Configure_2ndStage \"param param \" " \
"$FUNCNAME 1 1 FALSE " \
"$FUNCNAME 1 1 TRUE \"nomodeset irqpoll pci=noacpi quiet splash \" "
return
fi
# Error si no se reciben 2 parámetros.
[ $# -ge 2 ] || return $(ogRaiseError $OG_ERR_FORMAT; echo $?)
DISK=$1; PART=$2;
CHECKOS=${3:-"FALSE"}
KERNELPARAM=$4
#Error si no es linux.
#TODO: comprobar si se puede utilizar la particion windows como contenedor de grub.
VERSION=$(ogGetOsVersion $DISK $PART)
echo $VERSION | grep "Linux" || return $(ogRaiseError $OG_ERR_NOTOS "no es linux"; echo $?)
#Localizar primera etapa del grub
FIRSTSTAGE=$(ogDiskToDev $DISK)
#localizar disco segunda etapa del grub
SECONDSTAGE=$(ogMount $DISK $PART)
#Localizar directorio segunda etapa del grub
PREFIXSECONDSTAGE="/boot/grubMBR"
# Si Reconfigurar segunda etapa (grub.cfg) == FALSE
if [ -f ${SECONDSTAGE}/boot/grub/grub.cfg ]
then
if [ "$CHECKOS" == "false" -o "$CHECKOS" == "FALSE" ]
then
grub-install --force --root-directory=${SECONDSTAGE} $FIRSTSTAGE
return $?
fi
fi
# SI Reconfigurar segunda etapa (grub.cfg) == TRUE
#Configur la sintaxis grub para evitar menus de "recovery" en el OGLive
echo "GRUB_DISABLE_RECOVERY=\"true\"" >> /etc/default/grub
echo "GRUB_DISABLE_LINUX_UUID=\"true\"" >> /etc/default/grub
#Evitar detectar modo recovery - mover grub.cfg original a grub.cfg.backup
[ -f ${SECONDSTAGE}/boot/grub/grub.cfg ] && mv ${SECONDSTAGE}/boot/grub/grub.cfg ${SECONDSTAGE}/boot/grub/grub.cfg.backup
#Preparar configuración segunda etapa: crear ubicacion
mkdir -p ${SECONDSTAGE}${PREFIXSECONDSTAGE}/boot/grub/
#Preparar configuración segunda etapa: crear cabecera del fichero
/etc/grub.d/00_header > ${SECONDSTAGE}${PREFIXSECONDSTAGE}/boot/grub/grub.cfg
#Preparar configuración segunda etapa: crear entrada del sistema operativo
grubSyntax "$KERNELPARAM" >> ${SECONDSTAGE}${PREFIXSECONDSTAGE}/boot/grub/grub.cfg
#Instalar el grub
grub-install --force --root-directory=${SECONDSTAGE}${PREFIXSECONDSTAGE} $FIRSTSTAGE
}
# ogGrubInstallPartition int_disk_SECONDSTAGE int_partition_SECONDSTAGE bolean_Check_Os_installed_and_Configure_2ndStage
#@brief Instala y actualiza el gestor grub en el bootsector de la particion indicada
#@param int_disk_SecondStage
#@param int_part_SecondStage
#@param bolean_Check_Os_installed_and_Configure_2ndStage true | false[default]
#@param str "kernel param "
#@return
#@exception OG_ERR_FORMAT Formato incorrecto.
#@version 1.0.2 - Primeras pruebas.
#@author Antonio J. Doblas Viso. Universidad de Malaga.
#@date 2011-10-29
#@version 1.0.3 - Soporte para linux de 32 y 64 bits
#@author Antonio J. Doblas Viso. Universidad de Malaga.
#@date 2012-03-13
#@version 1.0.3 - Ficheros de configuracion independientes segun ubicación de la priemra etapa
#@author Antonio J. Doblas Viso. Universidad de Malaga.
#@date 2012-03-13
#*/ ##
function ogGrubInstallPartition {
# Variables locales.
local PART DISK VERSION FIRSTAGE SECONSTAGE CHECKOS KERNELPARAM
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndiskSecondStage int_partitionSecondStage bolean_Configure_2ndStage \"param param \" " \
"$FUNCNAME 1 1 FALSE " \
"$FUNCNAME 1 1 TRUE \"nomodeset irqpoll pci=noacpi quiet splash \" "
return
fi
# Error si no se reciben 2 parámetros.
[ $# -ge 2 ] || return $(ogRaiseError $OG_ERR_FORMAT; echo $?)
DISK=$1; PART=$2;
CHECKOS=${3:-"FALSE"}
KERNELPARAM=$4
#error si no es linux.
#TODO: comprobar si se puede utilizar la particion windows como contenedor de grub.
VERSION=$(ogGetOsVersion $DISK $PART)
echo $VERSION | grep "Linux" || return $(ogRaiseError $OG_ERR_NOTOS "no es linux"; echo $?)
#Localizar primera etapa del grub
FIRSTSTAGE=$(ogDiskToDev $DISK $PART)
#localizar disco segunda etapa del grub
SECONDSTAGE=$(ogMount $DISK $PART)
#Localizar directorio segunda etapa del grub
PREFIXSECONDSTAGE="/boot/grubPARTITION"
# Si Reconfigurar segunda etapa (grub.cfg) == FALSE
if [ -f ${SECONDSTAGE}/boot/grub/grub.cfg ]
then
if [ "$CHECKOS" == "false" -o "$CHECKOS" == "FALSE" ]
then
grub-install --force --root-directory=${SECONDSTAGE} $FIRSTSTAGE
return $?
fi
fi
# SI Reconfigurar segunda etapa (grub.cfg) == TRUE
#Configur la sintaxis grub para evitar menus de "recovery" en el OGLive
echo "GRUB_DISABLE_RECOVERY=\"true\"" >> /etc/default/grub
echo "GRUB_DISABLE_LINUX_UUID=\"true\"" >> /etc/default/grub
#Evitar detectar modo recovery - mover grub.cfg original a grub.cfg.backup
[ -f ${SECONDSTAGE}/boot/grub/grub.cfg ] && mv ${SECONDSTAGE}/boot/grub/grub.cfg ${SECONDSTAGE}/boot/grub/grub.cfg.backup
#Preparar configuración segunda etapa: crear ubicacion
mkdir -p ${SECONDSTAGE}${PREFIXSECONDSTAGE}/boot/grub/
#Preparar configuración segunda etapa: crear cabecera del fichero
/etc/grub.d/00_header > ${SECONDSTAGE}${PREFIXSECONDSTAGE}/boot/grub/grub.cfg
#Preparar configuración segunda etapa: crear entrada del sistema operativo
grubSyntax $DISK $PART "$KERNELPARAM" >> ${SECONDSTAGE}${PREFIXSECONDSTAGE}/boot/grub/grub.cfg
#Instalar el grub
grub-install --force --root-directory=${SECONDSTAGE}${PREFIXSECONDSTAGE} $FIRSTSTAGE
}
###
#En pruebas
##
#/**
# ogConfigureFstab int_ndisk int_npartition str_name
#@brief Establece el nombre del equipo en los ficheros hostname y hosts.
#@param int_ndisk nº de orden del disco
#@param int_npartition nº de orden de la partición
#@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 .
#@author
#@date
#*/ ##
function ogConfigureFstab {
# Variables locales.
local PART MNTDIR MNTLINUX DEFROOT PARTROOT PARTSWAP
# 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 $?
MNTLINUX=$(ogMount $1 $2)
#Test fstab . pasar a funcion tipo ogLNXfstab
cp ${MNTLINUX}/etc/fstab ${MNTLINUX}/etc/fstab.backup
cat ${MNTLINUX}/etc/fstab | egrep -v " was on" > ${MNTLINUX}/etc/fstab.new
DEFROOT=$(cat ${MNTLINUX}/etc/fstab.new | grep " / " | awk '{print $1}')
PARTROOT=$(ogDiskToDev $1 $2)
#PARTROOT=LABEL=Sistema
#configuracion de la swap
PARTSWAP=$(fdisk -l $(ogDiskToDev $1 ) | grep swap | awk '{print $1}')
if [ -n "$PARTSWAP" ]
then
ogFormat $(ogDevToDisk $PARTSWAP)
#swapnew=$(fdisk -l | grep swap | awk '{print $1}')
echo "Existe partición swap en $PARTSWAP "
DEFSWAP=$(grep swap ${MNTLINUX}/etc/fstab.new | awk '{print $1}')
if [ -n "$DEFSWAP" ]
then
#swapold=$(cat ${MNTLINUX}/etc/fstab.new | grep "none" | awk '{print $1}')
echo "Hay definicion de swap en el fstab $DEFSWAP -> modificamos fichero con nuevo valor $DEFSWAP->$PARTSWAP"
sed "s|$DEFSWAP|$PARTSWAP|g ; s|$DEFROOT|$PARTROOT|g" ${MNTLINUX}/etc/fstab.new > ${MNTLINUX}/etc/fstab
else
echo "No hay definicion de swap y si hay partición swap -> moficamos fichero"
sed "s|$DEFROOT|$PARTROOT|g" ${MNTLINUX}/etc/fstab.renew > ${MNTLINUX}/etc/fstab
echo "$PARTSWAP none swap sw 0 0" >> ${MNTLINUX}/etc/fstab
fi
else
echo "No hay partición swap -> configuramos fstba"
cat ${MNTLINUX}/etc/fstab.new | egrep -v "none" > ${MNTLINUX}/etc/fstab.renew
sed "s|$DEFROOT|$PARTROOT|g" ${MNTLINUX}/etc/fstab.renew > ${MNTLINUX}/etc/fstab
fi
}
###
#En pruebas
##
#/**
# ogSetLinuxName int_ndisk int_npartition str_name
#@brief Establece el nombre del equipo en los ficheros hostname y hosts.
#@param int_ndisk nº de orden del disco
#@param int_npartition nº de orden de la partición
#@param 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 .
#@author
#@date
#*/ ##
function ogSetLinuxName ()
{
# Variables locales.
local PART MNTDIR NAME
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition str_name" \
"$FUNCNAME 1 1 PRACTICA-PC"
return
fi
# Error si no se reciben 3 parámetros.
[ $# -ge 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 $?
[ $# == 3 ] && NAME="$3" || NAME="$(ogGetHostname)"
NAME=${NAME:-"pc"}
ETC=$(ogGetPath $1 $2 /etc)
if [ -d "$ETC" ]; then
#cambio de nombre en hostname
echo "$NAME" >$ETC/hostname 2>/dev/null
#Opcion A para cambio de nombre en hosts
#sed "/127.0.1.1/ c\127.0.1.1 \t $HOSTNAME" $ETC/hosts > /tmp/hosts && cp /tmp/hosts $ETC/ && rm /tmp/hosts
#Opcion B para cambio de nombre en hosts
cat > $ETC/hosts <<EOF
127.0.0.1 localhost
127.0.1.1 $NAME
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
fi
}
###
#En pruebas
##
#/**
# ogCleanLinuxDevices int_ndisk int_npartition
#@brief Limpia los dispositivos del equipo de referencia. Interfaz de red ...
#@param int_ndisk nº de orden del disco
#@param int_npartition nº de orden de la partición
#@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 .
#@author
#@date
#*/ ##
function ogCleanLinuxDevices ()
{
# Variables locales.
local PART MNTDIR NAME
# 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 $?
# Montar el sistema de archivos.
MNTDIR=$(ogMount $1 $2) 2>/dev/null
[ -z "$MNTDIR" ] && ogRaiseError OG_ERR_PARTITION "$1, $2" && return $?
ETC=$(ogGetPath $1 $2 /etc)
# Clean old network id in udev configuration
UDEVNET="${ETC}/udev/rules.d/70-persistent-net.rules"
[ -f $UDEVNET ] && echo " " > $UDEVNET
}