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}' 388 function ogRestoreEfiBootLoader () {
390 local MNTDIR EFIDIR BOOTLABEL OSVERSION LOADER f UUID DEVICE
392 # Si se solicita, mostrar ayuda. 393 if [
"$*" ==
"help" ]; then
394 ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
399 # Error si no se reciben 2 arámetros. 400 [ $# == 2 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME int_ndisk int_part" ||
return $?
402 # Comprobamos que exista partición de sistema y la ESP 405 if [
"$EFIDIR" ==
"" ]; then
410 # Comprobamos que exista el cargador
411 #BOOTLABEL=$(printf
"Part-%02d-%02d" $1 $2)
412 OSVERSION=$(ogGetOsVersion $1 $2)
415 BOOTLABEL=$(printf
"Part-%02d-%02d" $1 $2)
417 [ -n
"$LOADER" ] ||
ogRaiseError $OG_ERR_NOTOS
"$1 $2 ($OSVERSION, EFI)" ||
return $?
418 [ -r $EFIDIR/EFI/$BOOTLABEL ] && rm -rf $EFIDIR/EFI/$BOOTLABEL
419 mkdir -p $EFIDIR/EFI/$BOOTLABEL
420 cp -r
"${LOADER%/*}" $EFIDIR/EFI/$BOOTLABEL/
Boot 421 # Nombre OpenGnsys para cargador 422 cp $LOADER $EFIDIR/EFI/$BOOTLABEL/
Boot/ogloader.efi
424 # Si existe subcarpeta Microsoft en la partición EFI la renombramos 425 [
"$(ogGetPath $EFIDIR/EFI/Microsoft)" ==
"" ] || mv $EFIDIR/EFI/{Microsoft,Microsoft.backup.og}
442 function ogRestoreUuidPartitions () {
443 local DISK PART IMGNAME INFOFILE DEVICE DATA GUID UUID IMGGUID
444 local EFIDEVICE EFIDATA EFIGUID EFIUUID EFIUUID IMGEFIGUID
446 # Si se solicita, mostrar ayuda. 447 if [
"$*" ==
"help" ]; then
448 ogHelp "$FUNCNAME" "$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" \
449 "$FUNCNAME REPO Windows 1 2" 452 # Error si no se reciben 4 parámetros. 453 [ $# -eq 4 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" ||
return $?
455 # Sólo se ejecuta si es UEFI 458 # Parámetros de entrada 460 INFOFILE=
"$OGIMG/.$IMGNAME.img.json" 461 [
"${1^^}" ==
"CACHE" ] && INFOFILE=
"$OGCAC$INFOFILE" 462 # TODO: que la función getPath soporte archivos ocultos 463 ls $INFOFILE &>/dev/null ||
ogRaiseError $OG_ERR_NOTFOUND
"$INFOFILE" ||
return $?
468 read -e EFIDISK EFIPART <<<
"$(ogGetEsp)" 471 IMGGUID=$(jq .guid $INFOFILE|tr -d \
") 472 IMGEFIGUID=$(jq .espguid $INFOFILE|tr -d \") 475 DATA=$(sfdisk -J $DEVICE) 476 GUID=$(echo $DATA|jq ".partitiontable|.
id"|tr -d \") 478 if [ "$IMGGUID
" != "$GUID
" ]; then 479 echo sgdisk -U "$IMGGUID
" "$DEVICE
" 480 sgdisk -U "$IMGGUID
" "$DEVICE
" 484 if [ $DISK -eq $EFIDISK ]; then 488 EFIDEVICE=$(ogDiskToDev $EFIDISK) || return $? 489 EFIDATA=$(sfdisk -J $EFIDEVICE) 490 EFIGUID=$(echo $EFIDATA|jq ".partitiontable|.
id"|tr -d \") 491 if [ "$IMGEFIGUID
" != "$EFIGUID
" ]; then 492 echo sgdisk -U "$IMGEFIGUID
" "$EFIDEVICE
" 493 sgdisk -U "$IMGEFIGUID
" "$EFIDEVICE
" 502 #@brief Configura el próximo arranque con la entrada del gestor de arranque (NVRAM) identificada por la etiqueta o el orden. 503 #@param Num_order_entry | Label_entry Número de orden o la etiqueta de la entrada a borrar. 505 #@exception OG_ERR_FORMAT formato incorrecto. 506 #@exception OG_ERR_NOTUEFI UEFI no activa. 507 #@exception OG_ERR_NOTFOUND fichero o dispositivo no encontrado. 509 function ogNvramSetNext () { 512 # Si se solicita, mostrar ayuda. 513 if [ "$*
" == "help
" ]; then 514 ogHelp "$FUNCNAME
" "$FUNCNAME [ Num_order_entry | Label_entry ]
" \ 516 "$FUNCNAME \
"Windows Boot Manager\"" 520 # Error si no se recibe 1 parámetro.
521 [ $# -eq 1 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME [ Num_order_entry | Label_entry ]" ||
return $?
523 # Si no es equipo UEFI salir con error
526 # Distingo si es número de orden o etiqueta
527 if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
528 NUMENTRY=$( efibootmgr |
awk -v NUM=
"$(printf %04x 0x$1|tr '[:lower:]' '[:upper:]')" '{ if($1~NUM) print substr($1,5,4)}')
530 NUMENTRY=$(efibootmgr |
awk -v LABEL=
"$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
533 [
"$NUMENTRY" ==
"" ] &&
return $(
ogRaiseError $OG_ERR_NOTFOUND
"NVRAM entry '$1'")
535 efibootmgr -n $NUMENTRY &>/dev/null
547 function ogNvramSetOrder () {
548 # Si se solicita, mostrar ayuda. 549 if [
"$*" ==
"help" ]; then
550 ogHelp "$FUNCNAME" "$FUNCNAME Num_order1 [ Num_order2 ] ... " \
555 # Error si no se recibe al menos 1 parámetro. 556 [ $# -ge 1 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME Num_order1 [ Num_order2 ] ..." ||
return $?
558 # Si no es equipo UEFI salir con error 561 # Comprobamos que sean números 562 [[
"$@" =~ ^([0-9a-fA-F ]+)$ ]] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME Num_order1 [ Num_order2 ] ..." ||
return $?
564 # Entradas de la NVRAM actuales
565 NUMENTRYS=$(efibootmgr|
awk '{ if ($1~/Boot[0-9a-fA-F]{4}/) printf "0%s ", substr($1,5,4)}')
569 # Si no existe la entrada me salgo 570 ARG=$(printf %04X 0x$ARG)
571 echo $NUMENTRYS | grep
"$ARG" &>/dev/null ||
ogRaiseError $OG_ERR_NOTFOUND
"NVRAM entry order \"$ARG\"" ||
return $?
576 efibootmgr -o ${ORDER#,} &>/dev/null
589 function ogNvramSetTimeout () {
590 # Si se solicita, mostrar ayuda. 591 if [
"$*" ==
"help" ]; then
592 ogHelp "$FUNCNAME" "$FUNCNAME int_Timeout (seg)" \
597 # Si no es equipo UEFI salir con error 600 # Error si no se recibe 1 parámetro. 601 [ $# -eq 1 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME int_Timeout (seg)" ||
return $?
603 # Comprobamos que sea un número 604 [[
"$1" =~ ^([0-9 ]+)*$ ]] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME int_Timeout (seg)" ||
return $?
607 efibootmgr -t $1 &>/dev/null
620 function ogUuidChange () {
621 local MNTDIR DEVICE UUID NEWUUID f
623 # Si se solicita, mostrar ayuda. 624 if [
"$*" ==
"help" ]; then
625 ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
630 # Error si no se reciben al menos 2 parámetros. 631 [ $# -eq 2 ] ||
ogRaiseError $OG_ERR_FORMAT
"$FUNCNAME int_ndisk int_part" ||
return $?
633 # Comprobamos que exista la partición 636 UUID=$(blkid -o value -s UUID $DEVICE)
637 NEWUUID=$(cat /proc/
sys/kernel/random/uuid)
639 # Cambiamos UUID a la partición 641 tune2fs $DEVICE -U $NEWUUID
643 # Cambiamos UUID en la configuración (fstab y grub) 645 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).