source: client/engine/UEFI.lib

qndtest
Last change on this file was aabe31a, checked in by Irina Gómez <irinagomez@…>, 2 years ago

#1066 #1071 Rename the function to ogNvramPxeFirstentry and include help messages.

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