source: client/engine/UEFI.lib

lgromero-new-oglive
Last change on this file was f9ed0c1, checked in by Natalia Serrano <nserrano@…>, 10 months ago

refs #457 ogCopyEfiBootLoader and ogRestoreEfiBootLoader detect Windows 11

  • Property mode set to 100755
File size: 22.1 KB
Line 
1#!/bin/bash
2# Libreria provisional para uso de UEFI
3# Las funciones se incluirán las librerías ya existentes
4
5#/**
6#         ogNvramActiveEntry
7#@brief   Activa entrada de la NVRAM identificada por la etiqueta o el orden
8#@param    Num_order_entry | Label_entry Número de orden o la etiqueta de la entrada a borrar.
9#@return  (nada)
10#@exception OG_ERR_FORMAT    formato incorrecto.
11#@exception OG_ERR_NOTUEFI   UEFI no activa.
12#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
13#*/ ##
14function ogNvramActiveEntry () {
15local NUMENTRY
16
17# Si se solicita, mostrar ayuda.
18if [ "$*" == "help" ]; then
19    ogHelp "$FUNCNAME" "$FUNCNAME [ Num_order_entry | Label_entry ] " \
20           "$FUNCNAME 2" \
21           "$FUNCNAME \"Windows Boot Manager\""
22    return
23fi
24
25# Error si no se recibe 1 parámetro.
26[ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME [ Num_order_entry | Label_entry ]" || return $?
27
28# Si no es equipo UEFI salir con error
29ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
30
31# Distingo si es número de orden o etiqueta
32if [[ $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)}')
34else
35    NUMENTRY=$(efibootmgr |awk  -v LABEL="$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
36fi
37
38[ "$NUMENTRY" == "" ] && return $(ogRaiseError $OG_ERR_NOTFOUND "NVRAM entry '$1'")
39
40efibootmgr -a -b $NUMENTRY &>/dev/null
41}
42
43#/**
44#         ogNvramAddEntry
45#@brief   Crea nueva entrada en el gestor de arranque (NVRAM), opcionalmente la incluye al final del orden de arranque.
46#@param    Str_Label_entry Número de disco o etiqueta de la entrada a crear.
47#@param    Str_BootLoader  Número de partición o cargador de arranque.
48#@param    Bool_Incluir_Arranque  Incluir en el orden de arranque (por defecto FALSE) (opcional)
49#@return  (nada)
50#@exception OG_ERR_FORMAT    formato incorrecto.
51#@exception OG_ERR_NOTUEFI   UEFI no activa.
52#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
53#*/ ##
54function ogNvramAddEntry () {
55local EFIDISK EFIPART BOOTLABEL BOOTLOADER ADDORDER
56
57# Si se solicita, mostrar ayuda.
58if [ "$*" == "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"
63    return
64fi
65
66# Error si no se recibe 1 parámetro.
67[ $# -ge 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME Str_label_entry Str_boot_locader" || return $?
68
69# Si no es equipo UEFI salir con error
70ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
71
72read -e EFIDISK EFIPART <<<"$(ogGetEsp)"
73[ -n "$EFIPART" ] || ogRaiseError $OG_ERR_NOTFOUND "ESP" || return $?
74
75# Recogemos parámetros
76# Distinguimos si es disco/partición o etiqueta/cargador
77if [[ "$1$2" =~ ^([0-9]+)$ ]]; then
78    BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
79    BOOTLOADER="/EFI/$BOOTLABEL/Boot/ogloader.efi"
80else
81    BOOTLABEL="$1"
82    BOOTLOADER="$2"
83fi
84
85
86# Si existe entrada con la misma etiqueta la borramos
87ogNvramDeleteEntry "$BOOTLABEL" 2>/dev/null
88
89efibootmgr -C -d $(ogDiskToDev $EFIDISK) -p $EFIPART -L "$BOOTLABEL" -l "$BOOTLOADER" &>/dev/null
90
91# Incluimos la entrada en el orden de arranque (opcional)
92if [ "${3^^}" == "TRUE" ]; then
93    NUMENTRY=$(efibootmgr |awk  -v LABEL="$BOOTLABEL" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
94    ogNvramSetOrder $(ogNvramGetOrder |tr , " ") $NUMENTRY
95fi
96}
97
98
99#/**
100#         ogCopyEfiBootLoader int_ndisk str_repo path_image
101#@brief   Copia el cargador de arranque desde la partición EFI a la de sistema.
102#@param   int_ndisk    nº de orden del disco
103#@param   int_part     nº de partición
104#@return  (nada, por determinar)
105#@exception OG_ERR_FORMAT    formato incorrecto.
106#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
107#@note    Si existe el cargador en la partición de sistema no es válido
108#*/ ##
109function ogCopyEfiBootLoader () {
110# Variables locales
111local MNTDIR EFIDIR BOOTLABEL OSVERSION LOADER f
112
113# Si se solicita, mostrar ayuda.
114if [ "$*" == "help" ]; then
115    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
116           "$FUNCNAME 1 2"
117    return
118fi
119
120# Error si no se reciben 2 arámetros.
121[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_part" || return $?
122
123# Comprobamos que exista partición de sistema y la  ESP
124MNTDIR=$(ogMount $1 $2) || ogRaiseError $OG_ERR_PARTITION "$DISK $PART" || return $?
125EFIDIR=$(ogMount $(ogGetEsp)) || ogRaiseError $OG_ERR_PARTITION "ESP" || return $?
126
127# Comprobamos que exista el cargador
128BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
129OSVERSION=$(ogGetOsVersion $1 $2)
130case $OSVERSION in
131    # jcgarcia by carmelo 12-04-2024
132    *Windows\ 1*)
133        for f in $EFIDIR/EFI/{Microsoft,$BOOTLABEL}/Boot/bootmgfw.efi; do
134            [ -r $f ] && LOADER=$f
135        done
136        [ -n "$LOADER" ] || ogRaiseError $OG_ERR_NOTOS "$1 $2 ($OSVERSION, EFI)" || return $?
137        # Si existe el directorio Boot lo borramos
138        [ -d $MNTDIR/ogBoot ] && rm -rf $MNTDIR/ogBoot
139        DIRLOADER=$(realpath "${LOADER%/*}/..")
140        cp -r ${DIRLOADER}/Boot $MNTDIR/ogBoot
141        ;;
142esac
143}
144
145
146#/**
147#         ogNvramDeleteEntry
148#@brief   Borra entrada de la NVRAM identificada por la etiqueta o el orden
149#@param    Num_order_entry | Label_entry Número de orden o la etiqueta de la entrada a borrar.
150#@return  (nada)
151#@exception OG_ERR_FORMAT    formato incorrecto.
152#@exception OG_ERR_NOTUEFI   UEFI no activa.
153#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado (entrada en NVRAM).
154#*/ ##
155function ogNvramDeleteEntry () {
156local NUMENTRY n
157
158# Si se solicita, mostrar ayuda.
159if [ "$*" == "help" ]; then
160    ogHelp "$FUNCNAME" "$FUNCNAME [ Num_order_entry | Label_entry ] " \
161           "$FUNCNAME 2" \
162           "$FUNCNAME \"Windows Boot Manager\""
163    return
164fi
165
166# Error si no se recibe 1 parámetro.
167[ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME [ Num_order_entry | Label_entry ]" || return $?
168
169# Si no es equipo UEFI salir con error
170ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
171
172# Distingo si es número de orden o etiqueta
173if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
174    NUMENTRY=$( efibootmgr |awk  -v NUM="$(printf %04x 0x$1|tr '[:lower:]' '[:upper:]')" '{ if($1~NUM) print substr($1,5,4)}')
175else
176    NUMENTRY=$(efibootmgr |awk  -v LABEL="$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
177fi
178
179[ "$NUMENTRY" == "" ] && return $(ogRaiseError $OG_ERR_NOTFOUND "NVRAM entry '$1'")
180
181for n in $NUMENTRY; do
182    efibootmgr -B -b $n &>/dev/null
183done
184}
185
186
187#/**
188#         ogNvramGetCurrent
189#@brief   Muestra la entrada del gestor de arranque (NVRAM) que ha iniciado el equipo.
190#@return  Entrada con la que se ha iniciado el equipo
191#@exception OG_ERR_NOTUEFI   UEFI no activa.
192#*/ ##
193function ogNvramGetCurrent () {
194
195# Si se solicita, mostrar ayuda.
196if [ "$*" == "help" ]; then
197    ogHelp "$FUNCNAME" "$FUNCNAME" \
198           "$FUNCNAME"
199    return
200fi
201
202# Si no es equipo UEFI salir con error
203ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
204
205efibootmgr| 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))}'
206}
207
208
209#         ogNvramGetNext
210#@brief   Muestra la entrada del gestor de arranque (NVRAM) que se utilizará en el próximo arranque.
211#@return  Entrada que se utilizará en el próximo arranque
212#@exception OG_ERR_NOTUEFI   UEFI no activa.
213#*/ ##
214function ogNvramGetNext () {
215# Si se solicita, mostrar ayuda.
216if [ "$*" == "help" ]; then
217    ogHelp "$FUNCNAME" "$FUNCNAME" \
218           "$FUNCNAME"
219    return
220fi
221
222# Si no es equipo UEFI salir con error
223ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
224
225efibootmgr|awk '{ if ($1 == "BootNext:") print $2}'
226}
227
228
229#         ogNvramGetOrder
230#@brief   Muestra el orden de las entradas del gestor de arranque (NVRAM)
231#@return  Orden de las entradas
232#@exception OG_ERR_NOTUEFI   UEFI no activa.
233#*/ ##
234function ogNvramGetOrder () {
235# Si se solicita, mostrar ayuda.
236if [ "$*" == "help" ]; then
237    ogHelp "$FUNCNAME" "$FUNCNAME" \
238           "$FUNCNAME"
239    return
240fi
241
242# Si no es equipo UEFI salir con error
243ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
244
245efibootmgr|awk '{ if ($1 == "BootOrder:") print $2}'
246}
247
248
249#/**
250#         ogNvramGetTimeout
251#@brief   Muestra el tiempo de espera del gestor de arranque (NVRAM)
252#@return  Timeout de la NVRAM
253#@exception OG_ERR_NOTUEFI   UEFI no activa.
254#*/ ##
255function ogNvramGetTimeout () {
256# Si se solicita, mostrar ayuda.
257if [ "$*" == "help" ]; then
258    ogHelp "$FUNCNAME" "$FUNCNAME" \
259           "$FUNCNAME"
260    return
261fi
262
263# Si no es equipo UEFI salir con error
264ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
265
266efibootmgr|awk '{ if ($1 == "Timeout:") print substr($0, index($0,$2))}'
267}
268
269
270#/**
271#         ogGrubUefiConf int_ndisk int_part str_dir_grub
272#@brief   Genera el fichero grub.cfg de la ESP
273#@param   int_ndisk    nº de orden del disco
274#@param   int_part     nº de partición
275#@param   str_dir_grub prefijo del directorio de grub en la partición de sistema. ej: /boot/grubPARTITION
276#@return  (nada, por determinar)
277#@exception OG_ERR_FORMAT    formato incorrecto.
278#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
279#@TODO    Confirmar si el fichero "$EFIDIR/EFI/$BOOTLABEL/grub.cfg" es necesario.
280#*/ ##
281function ogGrubUefiConf () {
282local EFIDIR BOOTLABEL GRUBEFI UUID DEVICE PREFIXSECONDSTAGE EFIGRUBDIR
283
284# Si se solicita, mostrar ayuda.
285if [ "$*" == "help" ]; then
286    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part [ str_dir_grub ]" \
287           "$FUNCNAME 1 2" \
288           "$FUNCNAME 1 3 /boot/grubPARTITION"
289    return
290fi
291
292# Error si no se reciben al menos 2 parámetros.
293[ $# -ge 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_part [ str_dir_grub ]" || return $?
294
295# Directorio del grub en la partición de sistema
296PREFIXSECONDSTAGE="$3"
297
298EFIDIR=$(ogMount $(ogGetEsp)) || ogRaiseError $OG_ERR_PARTITION "ESP" || return $?
299BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
300EFIGRUBDIR="$EFIDIR/EFI/$BOOTLABEL/boot/grub"
301# Comprobamos que existe directorio
302[ -d "$EFIGRUBDIR" ] || mkdir -p "$EFIGRUBDIR"
303# Parcheamos uuid y particion en grub.cfg
304UUID=$(blkid -o value -s UUID $(ogDiskToDev $1 $2))
305DEVICE="hd$(expr $1 - 1 ),gpt$2"
306
307cat << EOT > $EFIGRUBDIR/grub.cfg
308set root='$DEVICE'
309set prefix=(\$root)'${PREFIXSECONDSTAGE}/boot/grub'
310configfile \$prefix/grub.cfg
311EOT
312
313# Provisional: confirmar si el segundo archivo se utiliza
314cp $EFIGRUBDIR/grub.cfg "$EFIDIR/EFI/$BOOTLABEL/grub.cfg"
315}
316
317
318#/**
319#         ogNvramInactiveEntry
320#@brief   Inactiva entrada de la NVRAM identificada por la etiqueta o el orden
321#@param    Num_order_entry | Label_entry Número de orden o la etiqueta de la entrada a borrar.
322#@return  (nada)
323#@exception OG_ERR_FORMAT    formato incorrecto.
324#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
325#@exception OG_ERR_NOTUEFI   UEFI no activa.
326#*/ ##
327function ogNvramInactiveEntry () {
328local NUMENTRY
329
330# Si se solicita, mostrar ayuda.
331if [ "$*" == "help" ]; then
332    ogHelp "$FUNCNAME" "$FUNCNAME [ Num_order_entry | Label_entry ] " \
333           "$FUNCNAME 2" \
334           "$FUNCNAME \"Windows Boot Manager\""
335    return
336fi
337
338# Error si no se recibe 1 parámetro.
339[ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME [ Num_order_entry | Label_entry ]" || return $?
340
341# Si no es equipo UEFI salir con error
342ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
343
344# Distingo si es número de orden o etiqueta
345if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
346    NUMENTRY=$( efibootmgr |awk  -v NUM="$(printf %04x 0x$1|tr '[:lower:]' '[:upper:]')" '{ if($1~NUM) print substr($1,5,4)}')
347else
348    NUMENTRY=$(efibootmgr |awk  -v LABEL="$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
349fi
350
351[ "$NUMENTRY" == "" ] && return $(ogRaiseError $OG_ERR_NOTFOUND "NVRAM entry '$1'")
352
353efibootmgr -A -b $NUMENTRY &>/dev/null
354}
355
356
357#/**
358#         ogNvramList
359#@brief   Lista las entradas de la NVRAN (sólo equipos UEFI)
360#@return  Entradas de la NVRAM con el formato: orden etiqueta [* (si está activa) ]
361#@exception OG_ERR_NOTUEFI  UEFI no activa.
362#*/ ##
363function ogNvramList () {
364
365# Si se solicita, mostrar ayuda.
366if [ "$*" == "help" ]; then
367    ogHelp "$FUNCNAME" "$FUNCNAME" \
368           "$FUNCNAME"
369    return
370fi
371
372# Si no es equipo UEFI salir con error
373ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
374
375efibootmgr |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}'
376}
377
378
379#/**
380#         ogNvramPxeFirstEntry
381#@brief   Sitúa la entrada de la tarjeta de red en el primer lugar en la NVRAM.
382#@return  (nada)
383#@exception OG_ERR_NOTUEFI  UEFI no activa.
384#*/ ##
385function ogNvramPxeFirstEntry (){
386local NUMENTRY ORDER
387
388# Si se solicita, mostrar ayuda.
389if [ "$*" == "help" ]; then
390    ogHelp "$FUNCNAME" "$FUNCNAME" \
391           "$FUNCNAME"
392    return
393fi
394
395# Si no es equipo UEFI salir con error
396ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
397
398NUMENTRY=$(printf  %04X 0x$(efibootmgr|awk '/IP[vV]{0,1}4/ {print gensub(/^0{1,3}/,"",1,substr($1,5,4))}'))
399
400# Si la entrada es la primera nos salimos.
401[[ $(ogNvramGetOrder) =~ ^$NUMENTRY ]] && return
402
403# Si la entrada ya existe la borramos.
404ORDER="$NUMENTRY $(ogNvramGetOrder| sed -e  s/$NUMENTRY//g -e s/,/' '/g)"
405
406ogNvramSetOrder $ORDER
407}
408
409
410#/**
411#         ogRestoreEfiBootLoader int_ndisk str_repo
412#@brief   Copia el cargador de arranque de la partición de sistema a la partición EFI.
413#@param   int_ndisk    nº de orden del disco
414#@param   int_part     nº de partición
415#@return  (nada, por determinar)
416#@exception OG_ERR_FORMAT    formato incorrecto.
417#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado (partición de sistema o EFI).
418#@exception OG_ERR_NOTOS     sin sistema operativo.
419#*/ ##
420function ogRestoreEfiBootLoader () {
421# Variables locales
422local MNTDIR EFIDIR BOOTLABEL OSVERSION LOADER f UUID DEVICE
423
424# Si se solicita, mostrar ayuda.
425if [ "$*" == "help" ]; then
426    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
427           "$FUNCNAME 1 2"
428    return
429fi
430
431# Error si no se reciben 2 arámetros.
432[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_part" || return $?
433
434# Comprobamos que exista partición de sistema y la  ESP
435MNTDIR=$(ogMount $1 $2) || ogRaiseError $OG_ERR_PARTITION "$DISK $PART" || return $?
436EFIDIR=$(ogMount $(ogGetEsp))
437if [ "$EFIDIR" == "" ]; then
438    ogFormat $(ogGetEsp) FAT32
439    EFIDIR=$(ogMount $(ogGetEsp)) || ogRaiseError $OG_ERR_PARTITION "ESP" || return $?
440fi
441
442# Comprobamos que exista el cargador
443#BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
444OSVERSION=$(ogGetOsVersion $1 $2)
445case $OSVERSION in
446    # jcgarcia by carmelo 12-04-2024
447    *Windows\ 1*)
448        BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
449        LOADER=$(ogGetPath $MNTDIR/ogBoot/bootmgfw.efi)
450        [ -n "$LOADER" ] || ogRaiseError $OG_ERR_NOTOS "$1 $2 ($OSVERSION, EFI)" || return $?
451        [ -r $EFIDIR/EFI/$BOOTLABEL ] && rm -rf $EFIDIR/EFI/$BOOTLABEL
452        mkdir -p $EFIDIR/EFI/$BOOTLABEL
453        cp -r "${LOADER%/*}" $EFIDIR/EFI/$BOOTLABEL/Boot
454        # Nombre OpenGnsys para cargador
455        cp $LOADER $EFIDIR/EFI/$BOOTLABEL/Boot/ogloader.efi
456
457        # Si existe subcarpeta Microsoft en la partición EFI la renombramos
458        [ "$(ogGetPath $EFIDIR/EFI/Microsoft)" == "" ] || mv $EFIDIR/EFI/{Microsoft,Microsoft.backup.og}
459        ;;
460esac
461}
462
463
464#/**
465#         ogRestoreUuidPartitions
466#@brief   Restaura los uuid de las particiones y la tabla de particiones
467#@param   int_ndisk      nº de orden del disco
468#@param   int_nfilesys   nº de orden del sistema de archivos
469#@param   REPO|CACHE     repositorio
470#@param   str_imgname    nombre de la imagen
471#@return  (nada)
472#@exception OG_ERR_FORMAT    Formato incorrecto.
473#@exception OG_ERR_NOTFOUND  No encontrado fichero de información de la imagen (con uuid)
474#*/ ##
475function ogRestoreUuidPartitions () {
476local DISK PART IMGNAME INFOFILE DEVICE DATA GUID UUID IMGGUID
477local EFIDEVICE EFIDATA EFIGUID EFIUUID EFIUUID IMGEFIGUID
478
479# Si se solicita, mostrar ayuda.
480if [ "$*" == "help" ]; then
481    ogHelp "$FUNCNAME" "$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" \
482           "$FUNCNAME REPO Windows 1 2"
483    return
484fi
485# Error si no se reciben 4 parámetros.
486[ $# -eq 4 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" || return $?
487
488# Sólo se ejecuta si es UEFI
489ogIsEfiActive || return
490
491# Parámetros de entrada
492IMGNAME="$2"
493INFOFILE="$OGIMG/.$IMGNAME.img.json"
494[ "${1^^}" == "CACHE" ] && INFOFILE="$OGCAC$INFOFILE"
495# TODO: que la función getPath soporte archivos ocultos
496ls $INFOFILE &>/dev/null || ogRaiseError $OG_ERR_NOTFOUND "$INFOFILE" || return $?
497DISK=$3
498PART=$4
499
500DEVICE=$(ogDiskToDev $DISK)
501read -e EFIDISK EFIPART <<<"$(ogGetEsp)"
502
503# Datos de la imagen
504IMGGUID=$(jq .guid $INFOFILE|tr -d \")
505IMGEFIGUID=$(jq .espguid $INFOFILE|tr -d \")
506
507# Datos actuales
508DATA=$(sfdisk -J $DEVICE)
509GUID=$(echo $DATA|jq ".partitiontable|.id"|tr -d \")
510
511if [ "$IMGGUID" != "$GUID" ]; then
512    echo sgdisk -U "$IMGGUID"  "$DEVICE"
513    sgdisk -U "$IMGGUID"  "$DEVICE"
514    partprobe
515fi
516
517if [ $DISK -eq $EFIDISK ]; then
518    EFIDATA=$DATA
519    EFIDEVICE=$DEVICE
520else
521    EFIDEVICE=$(ogDiskToDev $EFIDISK) || return $?
522    EFIDATA=$(sfdisk -J $EFIDEVICE)
523    EFIGUID=$(echo $EFIDATA|jq ".partitiontable|.id"|tr -d \")
524    if [ "$IMGEFIGUID" != "$EFIGUID" ]; then
525echo         sgdisk -U "$IMGEFIGUID"  "$EFIDEVICE"
526       sgdisk -U "$IMGEFIGUID"  "$EFIDEVICE"
527       partprobe
528   fi
529fi
530}
531
532
533#/**
534#         ogNvramSetNext
535#@brief   Configura el próximo arranque con la entrada del gestor de arranque (NVRAM) identificada por la etiqueta o el orden.
536#@param    Num_order_entry | Label_entry Número de orden o la etiqueta de la entrada a borrar.
537#@return  (nada)
538#@exception OG_ERR_FORMAT    formato incorrecto.
539#@exception OG_ERR_NOTUEFI   UEFI no activa.
540#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
541#*/ ##
542function ogNvramSetNext () {
543local NUMENTRY
544
545# Si se solicita, mostrar ayuda.
546if [ "$*" == "help" ]; then
547    ogHelp "$FUNCNAME" "$FUNCNAME [ Num_order_entry | Label_entry ] " \
548           "$FUNCNAME 2" \
549           "$FUNCNAME \"Windows Boot Manager\""
550    return
551fi
552
553# Error si no se recibe 1 parámetro.
554[ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME [ Num_order_entry | Label_entry ]" || return $?
555
556# Si no es equipo UEFI salir con error
557ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
558
559# Distingo si es número de orden o etiqueta
560if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
561    NUMENTRY=$( efibootmgr |awk  -v NUM="$(printf %04x 0x$1|tr '[:lower:]' '[:upper:]')" '{ if($1~NUM) print substr($1,5,4)}')
562else
563    NUMENTRY=$(efibootmgr |awk  -v LABEL="$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
564fi
565
566[ "$NUMENTRY" == "" ] && return $(ogRaiseError $OG_ERR_NOTFOUND "NVRAM entry '$1'")
567
568efibootmgr -n $NUMENTRY &>/dev/null
569}
570
571#/**
572#         ogNvramSetOrder
573#@brief   Configura el orden de las entradas de la NVRAM
574#@param   Orden de las entradas separadas por espacios
575#@return  (nada)
576#@exception OG_ERR_FORMAT    formato incorrecto.
577#@exception OG_ERR_NOTUEFI   UEFI no activa.
578#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado (entrada NVRAM).
579#*/ ##
580function ogNvramSetOrder () {
581# Si se solicita, mostrar ayuda.
582if [ "$*" == "help" ]; then
583    ogHelp "$FUNCNAME" "$FUNCNAME Num_order1 [ Num_order2 ] ... " \
584           "$FUNCNAME 1 3"
585    return
586fi
587#
588# Error si no se recibe al menos 1 parámetro.
589[ $# -ge 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME Num_order1 [ Num_order2 ] ..." || return $?
590
591# Si no es equipo UEFI salir con error
592ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
593
594# Comprobamos que sean números
595[[ "$@" =~ ^([0-9a-fA-F ]+)$ ]] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME Num_order1 [ Num_order2 ] ..." || return $?
596
597# Entradas de la NVRAM actuales
598NUMENTRYS=$(efibootmgr|awk '{ if ($1~/Boot[0-9a-fA-F]{4}/) printf "0%s ", substr($1,5,4)}')
599
600ORDER=""
601for ARG in $@; do
602    # Si no existe la entrada me salgo
603    ARG=$(printf  %04X 0x$ARG)
604    echo $NUMENTRYS | grep "$ARG" &>/dev/null || ogRaiseError $OG_ERR_NOTFOUND "NVRAM entry order \"$ARG\"" || return $?
605    ORDER=${ORDER},$ARG
606done
607
608# Cambiamos el orden
609efibootmgr -o ${ORDER#,} &>/dev/null
610}
611
612
613#/**
614#         ogNvramSetTimeout
615#@brief   Configura el tiempo de espera de la NVRAM
616#@param   Orden de las entradas separadas por espacios
617#@return  (nada)
618
619#@exception OG_ERR_FORMAT    formato incorrecto.
620#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
621#*/ ##
622function ogNvramSetTimeout () {
623# Si se solicita, mostrar ayuda.
624if [ "$*" == "help" ]; then
625    ogHelp "$FUNCNAME" "$FUNCNAME int_Timeout (seg)" \
626           "$FUNCNAME 2"
627    return
628fi
629#
630# Si no es equipo UEFI salir con error
631ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
632
633# Error si no se recibe 1 parámetro.
634[ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_Timeout (seg)" || return $?
635
636# Comprobamos que sea  un número
637[[ "$1" =~ ^([0-9 ]+)*$ ]] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_Timeout (seg)" || return $?
638
639# Cambiamos el orden
640efibootmgr -t $1 &>/dev/null
641}
642
643
644#/**
645#         ogUuidChange int_ndisk str_repo
646#@brief   Reemplaza el UUID de un sistema de ficheros.
647#@param   int_ndisk    nº de orden del disco
648#@param   int_part     nº de partición
649#@return  (nada, por determinar)
650#@exception OG_ERR_FORMAT    formato incorrecto.
651#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
652#*/ ##
653function ogUuidChange () {
654local MNTDIR DEVICE UUID NEWUUID f
655
656# Si se solicita, mostrar ayuda.
657if [ "$*" == "help" ]; then
658    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
659           "$FUNCNAME 1 2"
660    return
661fi
662
663# Error si no se reciben al menos 2 parámetros.
664[ $# -eq 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_part" || return $?
665
666# Comprobamos que exista la partición
667MNTDIR=$(ogMount $1 $2) || ogRaiseError $OG_ERR_NOTFOUND "Device $1 $2" || return $?
668DEVICE=$(ogDiskToDev $1 $2)
669UUID=$(blkid -o value -s UUID $DEVICE)
670NEWUUID=$(cat /proc/sys/kernel/random/uuid)
671
672# Cambiamos UUID a la partición
673ogUnmount $1 $2
674tune2fs $DEVICE -U $NEWUUID
675
676# Cambiamos UUID en la configuración (fstab y grub)
677ogMount $1 $2
678for f in $MNTDIR/etc/fstab $MNTDIR/{,boot/}{{grubMBR,grubPARTITION}/boot/,}{grub{,2},{,efi/}EFI/*}/{menu.lst,grub.cfg}; do
679        [ -r $f ] && sed -i s/$UUID/$NEWUUID/g $f
680done
681}
Note: See TracBrowser for help on using the repository browser.