2 # Libreria provisional para uso de UEFI 3 # Las funciones se incluirán las librerías ya existentes 17 # Si se solicita, mostrar ayuda. 18 if [
"$*" ==
"help" ]; then
19 ogHelp "$FUNCNAME" "$FUNCNAME [ Num_order_entry | Label_entry ] " \
21 "$FUNCNAME \"Windows Boot Manager\"" 25 # Error si no se recibe 1 parámetro. 26 [ $# -eq 1 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME [ Num_order_entry | Label_entry ]" ||
return $?
28 # Si no es equipo UEFI salir con error 31 # Distingo si es número de orden o etiqueta 32 if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
33 NUMENTRY=$( efibootmgr |
awk -v NUM=
"$(printf %04x 0x$1|tr '[:lower:]' '[:upper:]')" '{ if($1~NUM) print substr($1,5,4)}')
35 NUMENTRY=$(efibootmgr |
awk -v LABEL=
"$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
38 [
"$NUMENTRY" ==
"" ] &&
return $(
ogRaiseError $OG_ERR_NOTFOUND
"NVRAM entry '$1'")
40 efibootmgr -a -b $NUMENTRY &>/dev/null
55 local EFIDISK EFIPART BOOTLABEL BOOTLOADER ADDORDER
57 # Si se solicita, mostrar ayuda. 58 if [
"$*" ==
"help" ]; then
59 ogHelp "$FUNCNAME" "$FUNCNAME Str_label_entry Str_boot_loader [ Bool_add_bootorder ]" \
60 "$FUNCNAME 1 2 TRUE" \
61 "$FUNCNAME grub /EFI/grub/grubx64.efi TRUE" \
62 "$FUNCNAME Windows /EFI/Microsoft/Boot/bootmgfw.efi" 66 # Error si no se recibe 1 parámetro. 67 [ $# -ge 2 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME Str_label_entry Str_boot_locader" ||
return $?
69 # Si no es equipo UEFI salir con error 72 read -e EFIDISK EFIPART <<<
"$(ogGetEsp)" 73 [ -n
"$EFIPART" ] ||
ogRaiseError $OG_ERR_NOTFOUND
"ESP" ||
return $?
75 # Recogemos parámetros 76 # Distinguimos si es disco/partición o etiqueta/cargador 77 if [[
"$1$2" =~ ^([0-9]+)$ ]]; then
78 BOOTLABEL=$(printf
"Part-%02d-%02d" $1 $2)
79 BOOTLOADER=
"/EFI/$BOOTLABEL/Boot/ogloader.efi" 86 # Si existe entrada con la misma etiqueta la borramos
89 efibootmgr -C -d $(
ogDiskToDev $EFIDISK) -p $EFIPART -L
"$BOOTLABEL" -l
"$BOOTLOADER" &>/dev/null
91 # Incluimos la entrada en el orden de arranque (opcional) 92 if [
"${3^^}" ==
"TRUE" ]; then
93 NUMENTRY=$(efibootmgr |
awk -v LABEL=
"$BOOTLABEL" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
111 local MNTDIR EFIDIR BOOTLABEL OSVERSION LOADER f
113 # Si se solicita, mostrar ayuda. 114 if [
"$*" ==
"help" ]; then
115 ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
120 # Error si no se reciben 2 arámetros. 121 [ $# == 2 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME int_ndisk int_part" ||
return $?
123 # Comprobamos que exista partición de sistema y la ESP 127 # Comprobamos que exista el cargador
128 BOOTLABEL=$(printf
"Part-%02d-%02d" $1 $2)
129 OSVERSION=$(ogGetOsVersion $1 $2)
132 for f in $EFIDIR/EFI/{Microsoft,$BOOTLABEL}/
Boot/bootmgfw.efi;
do 133 [ -r $f ] && LOADER=$f
135 [ -n
"$LOADER" ] ||
ogRaiseError $OG_ERR_NOTOS
"$1 $2 ($OSVERSION, EFI)" ||
return $?
136 # Si existe el directorio Boot lo borramos 138 DIRLOADER=$(realpath
"${LOADER%/*}/..")
157 # Si se solicita, mostrar ayuda. 158 if [
"$*" ==
"help" ]; then
159 ogHelp "$FUNCNAME" "$FUNCNAME [ Num_order_entry | Label_entry ] " \
161 "$FUNCNAME \"Windows Boot Manager\"" 165 # Error si no se recibe 1 parámetro. 166 [ $# -eq 1 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME [ Num_order_entry | Label_entry ]" ||
return $?
168 # Si no es equipo UEFI salir con error 171 # Distingo si es número de orden o etiqueta 172 if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
173 NUMENTRY=$( efibootmgr |
awk -v NUM=
"$(printf %04x 0x$1|tr '[:lower:]' '[:upper:]')" '{ if($1~NUM) print substr($1,5,4)}')
175 NUMENTRY=$(efibootmgr |
awk -v LABEL=
"$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
178 [
"$NUMENTRY" ==
"" ] &&
return $(
ogRaiseError $OG_ERR_NOTFOUND
"NVRAM entry '$1'")
180 for n in $NUMENTRY;
do 181 efibootmgr -B -b $n &>/dev/null
194 # Si se solicita, mostrar ayuda. 195 if [
"$*" ==
"help" ]; then
196 ogHelp "$FUNCNAME" "$FUNCNAME" \
201 # Si no es equipo UEFI salir con error 204 efibootmgr|
awk -v bootentry=99999
'{if ($1~/BootCurrent/) bootentry=$2; if ($1~bootentry) printf "%s %s %s\n", gensub(/^0{1,3}/,"",1,substr($1,5,4))," ", substr($0, index($0,$2))}' 209 #@brief Muestra la entrada del gestor de arranque (NVRAM) que se utilizará en el próximo arranque. 210 #@return Entrada que se utilizará en el próximo arranque 211 #@exception OG_ERR_NOTUEFI UEFI no activa. 214 # Si se solicita, mostrar ayuda. 215 if [
"$*" ==
"help" ]; then
216 ogHelp "$FUNCNAME" "$FUNCNAME" \
221 # Si no es equipo UEFI salir con error 224 efibootmgr|
awk '{ if ($1 == "BootNext:") print $2}' 229 #@brief Muestra el orden de las entradas del gestor de arranque (NVRAM) 230 #@return Orden de las entradas 231 #@exception OG_ERR_NOTUEFI UEFI no activa. 234 # Si se solicita, mostrar ayuda. 235 if [
"$*" ==
"help" ]; then
236 ogHelp "$FUNCNAME" "$FUNCNAME" \
241 # Si no es equipo UEFI salir con error 244 efibootmgr|
awk '{ if ($1 == "BootOrder:") print $2}' 255 # Si se solicita, mostrar ayuda. 256 if [
"$*" ==
"help" ]; then
257 ogHelp "$FUNCNAME" "$FUNCNAME" \
262 # Si no es equipo UEFI salir con error 265 efibootmgr|
awk '{ if ($1 == "Timeout:") print substr($0, index($0,$2))}' 281 local EFIDIR BOOTLABEL GRUBEFI UUID DEVICE PREFIXSECONDSTAGE EFIGRUBDIR
283 # Si se solicita, mostrar ayuda. 284 if [
"$*" ==
"help" ]; then
285 ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part [ str_dir_grub ]" \
287 "$FUNCNAME 1 3 /boot/grubPARTITION" 291 # Error si no se reciben al menos 2 parámetros. 292 [ $# -ge 2 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME int_ndisk int_part [ str_dir_grub ]" ||
return $?
294 # Directorio del grub en la partición de sistema 295 PREFIXSECONDSTAGE=
"$3" 298 BOOTLABEL=$(printf
"Part-%02d-%02d" $1 $2)
299 EFIGRUBDIR=
"$EFIDIR/EFI/$BOOTLABEL/boot/grub" 300 # Comprobamos que existe directorio
301 [ -d
"$EFIGRUBDIR" ] || mkdir -p
"$EFIGRUBDIR" 302 # Parcheamos uuid y particion en grub.cfg
303 UUID=$(blkid -o value -s UUID $(
ogDiskToDev $1 $2))
304 DEVICE=
"hd$(expr $1 - 1 ),gpt$2" 306 cat << EOT > $EFIGRUBDIR/grub.cfg
308 set prefix=(\$root)
'${PREFIXSECONDSTAGE}/boot/grub' 309 configfile \$prefix/grub.cfg
312 # Provisional: confirmar si el segundo archivo se utiliza 313 cp $EFIGRUBDIR/grub.cfg
"$EFIDIR/EFI/$BOOTLABEL/grub.cfg" 329 # Si se solicita, mostrar ayuda. 330 if [
"$*" ==
"help" ]; then
331 ogHelp "$FUNCNAME" "$FUNCNAME [ Num_order_entry | Label_entry ] " \
333 "$FUNCNAME \"Windows Boot Manager\"" 337 # Error si no se recibe 1 parámetro. 338 [ $# -eq 1 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME [ Num_order_entry | Label_entry ]" ||
return $?
340 # Si no es equipo UEFI salir con error 343 # Distingo si es número de orden o etiqueta 344 if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
345 NUMENTRY=$( efibootmgr |
awk -v NUM=
"$(printf %04x 0x$1|tr '[:lower:]' '[:upper:]')" '{ if($1~NUM) print substr($1,5,4)}')
347 NUMENTRY=$(efibootmgr |
awk -v LABEL=
"$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
350 [
"$NUMENTRY" ==
"" ] &&
return $(
ogRaiseError $OG_ERR_NOTFOUND
"NVRAM entry '$1'")
352 efibootmgr -A -b $NUMENTRY &>/dev/null
364 # Si se solicita, mostrar ayuda. 365 if [
"$*" ==
"help" ]; then
366 ogHelp "$FUNCNAME" "$FUNCNAME" \
371 # Si no es equipo UEFI salir con error 374 efibootmgr |
awk '{if($1~/Boot[[:digit:]]/) ; active="" ;if ($1~/*/) active="*"; if($1~/Boot[[:digit:]]/) printf "%4s %s %s %s\n", gensub(/^0{1,3}/,"",1,substr($1,5,4))," ", substr($0, index($0,$2)), active}' 384 function ogNvramPxeFirstEntry (){
387 # Si se solicita, mostrar ayuda. 388 if [
"$*" ==
"help" ]; then
389 ogHelp "$FUNCNAME" "$FUNCNAME" \
394 # Si no es equipo UEFI salir con error 397 NUMENTRY=$(printf %04X 0x$(efibootmgr|
awk '/IP[vV]{0,1}4/ {print gensub(/^0{1,3}/,"",1,substr($1,5,4))}'))
399 # Si la entrada es la primera nos salimos. 402 # Si la entrada ya existe la borramos.
403 ORDER=
"$NUMENTRY $(ogNvramGetOrder| sed -e s/$NUMENTRY//g -e s/,/' '/g)" 405 ogNvramSetOrder $ORDER
419 function ogRestoreEfiBootLoader () {
421 local MNTDIR EFIDIR BOOTLABEL OSVERSION LOADER f UUID DEVICE
423 # Si se solicita, mostrar ayuda. 424 if [
"$*" ==
"help" ]; then
425 ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
430 # Error si no se reciben 2 arámetros. 431 [ $# == 2 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME int_ndisk int_part" ||
return $?
433 # Comprobamos que exista partición de sistema y la ESP 436 if [
"$EFIDIR" ==
"" ]; then
441 # Comprobamos que exista el cargador
442 #BOOTLABEL=$(printf
"Part-%02d-%02d" $1 $2)
443 OSVERSION=$(ogGetOsVersion $1 $2)
446 BOOTLABEL=$(printf
"Part-%02d-%02d" $1 $2)
448 [ -n
"$LOADER" ] ||
ogRaiseError $OG_ERR_NOTOS
"$1 $2 ($OSVERSION, EFI)" ||
return $?
449 [ -r $EFIDIR/EFI/$BOOTLABEL ] && rm -rf $EFIDIR/EFI/$BOOTLABEL
450 mkdir -p $EFIDIR/EFI/$BOOTLABEL
451 cp -r
"${LOADER%/*}" $EFIDIR/EFI/$BOOTLABEL/
Boot 452 # Nombre OpenGnsys para cargador 453 cp $LOADER $EFIDIR/EFI/$BOOTLABEL/
Boot/ogloader.efi
455 # Si existe subcarpeta Microsoft en la partición EFI la renombramos 456 [
"$(ogGetPath $EFIDIR/EFI/Microsoft)" ==
"" ] || mv $EFIDIR/EFI/{Microsoft,Microsoft.backup.og}
473 function ogRestoreUuidPartitions () {
474 local DISK PART IMGNAME INFOFILE DEVICE DATA GUID UUID IMGGUID
475 local EFIDEVICE EFIDATA EFIGUID EFIUUID EFIUUID IMGEFIGUID
477 # Si se solicita, mostrar ayuda. 478 if [
"$*" ==
"help" ]; then
479 ogHelp "$FUNCNAME" "$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" \
480 "$FUNCNAME REPO Windows 1 2" 483 # Error si no se reciben 4 parámetros. 484 [ $# -eq 4 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" ||
return $?
486 # Sólo se ejecuta si es UEFI 489 # Parámetros de entrada 491 INFOFILE=
"$OGIMG/.$IMGNAME.img.json" 492 [
"${1^^}" ==
"CACHE" ] && INFOFILE=
"$OGCAC$INFOFILE" 493 # TODO: que la función getPath soporte archivos ocultos 494 ls $INFOFILE &>/dev/null ||
ogRaiseError $OG_ERR_NOTFOUND
"$INFOFILE" ||
return $?
499 read -e EFIDISK EFIPART <<<
"$(ogGetEsp)" 502 IMGGUID=$(jq .guid $INFOFILE|tr -d \
") 503 IMGEFIGUID=$(jq .espguid $INFOFILE|tr -d \") 506 DATA=$(sfdisk -J $DEVICE) 507 GUID=$(echo $DATA|jq ".partitiontable|.
id"|tr -d \") 509 if [ "$IMGGUID
" != "$GUID
" ]; then 510 echo sgdisk -U "$IMGGUID
" "$DEVICE
" 511 sgdisk -U "$IMGGUID
" "$DEVICE
" 515 if [ $DISK -eq $EFIDISK ]; then 519 EFIDEVICE=$(ogDiskToDev $EFIDISK) || return $? 520 EFIDATA=$(sfdisk -J $EFIDEVICE) 521 EFIGUID=$(echo $EFIDATA|jq ".partitiontable|.
id"|tr -d \") 522 if [ "$IMGEFIGUID
" != "$EFIGUID
" ]; then 523 echo sgdisk -U "$IMGEFIGUID
" "$EFIDEVICE
" 524 sgdisk -U "$IMGEFIGUID
" "$EFIDEVICE
" 533 #@brief Configura el próximo arranque con la entrada del gestor de arranque (NVRAM) identificada por la etiqueta o el orden. 534 #@param Num_order_entry | Label_entry Número de orden o la etiqueta de la entrada a borrar. 536 #@exception OG_ERR_FORMAT formato incorrecto. 537 #@exception OG_ERR_NOTUEFI UEFI no activa. 538 #@exception OG_ERR_NOTFOUND fichero o dispositivo no encontrado. 540 function ogNvramSetNext () { 543 # Si se solicita, mostrar ayuda. 544 if [ "$*
" == "help
" ]; then 545 ogHelp "$FUNCNAME
" "$FUNCNAME [ Num_order_entry | Label_entry ]
" \ 547 "$FUNCNAME \
"Windows Boot Manager\"" 551 # Error si no se recibe 1 parámetro.
552 [ $# -eq 1 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME [ Num_order_entry | Label_entry ]" ||
return $?
554 # Si no es equipo UEFI salir con error
557 # Distingo si es número de orden o etiqueta
558 if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
559 NUMENTRY=$( efibootmgr |
awk -v NUM=
"$(printf %04x 0x$1|tr '[:lower:]' '[:upper:]')" '{ if($1~NUM) print substr($1,5,4)}')
561 NUMENTRY=$(efibootmgr |
awk -v LABEL=
"$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
564 [
"$NUMENTRY" ==
"" ] &&
return $(
ogRaiseError $OG_ERR_NOTFOUND
"NVRAM entry '$1'")
566 efibootmgr -n $NUMENTRY &>/dev/null
578 function ogNvramSetOrder () {
579 # Si se solicita, mostrar ayuda. 580 if [
"$*" ==
"help" ]; then
581 ogHelp "$FUNCNAME" "$FUNCNAME Num_order1 [ Num_order2 ] ... " \
586 # Error si no se recibe al menos 1 parámetro. 587 [ $# -ge 1 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME Num_order1 [ Num_order2 ] ..." ||
return $?
589 # Si no es equipo UEFI salir con error 592 # Comprobamos que sean números 593 [[
"$@" =~ ^([0-9a-fA-F ]+)$ ]] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME Num_order1 [ Num_order2 ] ..." ||
return $?
595 # Entradas de la NVRAM actuales
596 NUMENTRYS=$(efibootmgr|
awk '{ if ($1~/Boot[0-9a-fA-F]{4}/) printf "0%s ", substr($1,5,4)}')
600 # Si no existe la entrada me salgo 601 ARG=$(printf %04X 0x$ARG)
602 echo $NUMENTRYS | grep
"$ARG" &>/dev/null ||
ogRaiseError $OG_ERR_NOTFOUND
"NVRAM entry order \"$ARG\"" ||
return $?
607 efibootmgr -o ${ORDER#,} &>/dev/null
620 function ogNvramSetTimeout () {
621 # Si se solicita, mostrar ayuda. 622 if [
"$*" ==
"help" ]; then
623 ogHelp "$FUNCNAME" "$FUNCNAME int_Timeout (seg)" \
628 # Si no es equipo UEFI salir con error 631 # Error si no se recibe 1 parámetro. 632 [ $# -eq 1 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME int_Timeout (seg)" ||
return $?
634 # Comprobamos que sea un número 635 [[
"$1" =~ ^([0-9 ]+)*$ ]] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME int_Timeout (seg)" ||
return $?
638 efibootmgr -t $1 &>/dev/null
651 function ogUuidChange () {
652 local MNTDIR DEVICE UUID NEWUUID f
654 # Si se solicita, mostrar ayuda. 655 if [
"$*" ==
"help" ]; then
656 ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
661 # Error si no se reciben al menos 2 parámetros. 662 [ $# -eq 2 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME int_ndisk int_part" ||
return $?
664 # Comprobamos que exista la partición 667 UUID=$(blkid -o value -s UUID $DEVICE)
668 NEWUUID=$(cat /proc/
sys/kernel/random/uuid)
670 # Cambiamos UUID a la partición 672 tune2fs $DEVICE -U $NEWUUID
674 # Cambiamos UUID en la configuración (fstab y grub) 676 for f in $MNTDIR/etc/fstab $MNTDIR/{,boot/}{{grubMBR,grubPARTITION}/boot/,}{grub{,2},{,efi/}EFI
function ogDiskToDev()
Devuelve la equivalencia entre el nº de orden del dispositivo (dicso o partición) y el nombre de fich...
function ogIsEfiActive()
Comprueba si el sistema tiene activo el arranque EFI.
function ogNvramActiveEntry()
Activa entrada de la NVRAM identificada por la etiqueta o el orden #.
function ogNvramAddEntry()
Crea nueva entrada en el gestor de arranque (NVRAM), opcionalmente la incluye al final del orden de a...
function ogNvramGetTimeout()
Muestra el tiempo de espera del gestor de arranque (NVRAM) #.
Funciones para arranque y post-configuración de sistemas de archivos. #.
function ogRaiseError()
Devuelve el mensaje y el código de error correspondiente. #.
function ogHelp()
Muestra mensaje de ayuda para una función determinda. #.
function ogCopyEfiBootLoader()
Copia el cargador de arranque desde la partición EFI a la de sistema. #.
function ogGetPath()
Inicia el proceso de arranque de un sistema de archivos. #.
function ogNvramGetCurrent()
Muestra la entrada del gestor de arranque (NVRAM) que ha iniciado el equipo. #.
function ogNvramGetNext()
function ogNvramGetOrder()
function ogGrubUefiConf()
Genera el fichero grub.cfg de la ESP #.
function ogNvramList()
Lista las entradas de la NVRAN (sólo equipos UEFI) #.
function ogNvramDeleteEntry()
Borra entrada de la NVRAM identificada por la etiqueta o el orden #.
function ogBoot()
Inicia el proceso de arranque de un sistema de archivos. #.
function ogNvramInactiveEntry()
Inactiva entrada de la NVRAM identificada por la etiqueta o el orden #.
function ogGetEsp()
Devuelve números de disco y partición para la partición EFI (ESP).