source: client/engine/UEFI.lib @ eeade34

v1.2.0
Last change on this file since eeade34 was e03141b9, checked in by Ramón M. Gómez <ramongomez@…>, 4 years ago

#983: Releasing OpenGnsys 1.1.1c

  • Property mode set to 100755
File size: 21.2 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#         ogRestoreEfiBootLoader int_ndisk str_repo
380#@brief   Copia el cargador de arranque de la partición de sistema a la partición EFI.
381#@param   int_ndisk    nº de orden del disco
382#@param   int_part     nº de partición
383#@return  (nada, por determinar)
384#@exception OG_ERR_FORMAT    formato incorrecto.
385#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado (partición de sistema o EFI).
386#@exception OG_ERR_NOTOS     sin sistema operativo.
387#*/ ##
388function ogRestoreEfiBootLoader () {
389# Variables locales
390local MNTDIR EFIDIR BOOTLABEL OSVERSION LOADER f UUID DEVICE
391
392# Si se solicita, mostrar ayuda.
393if [ "$*" == "help" ]; then
394    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
395           "$FUNCNAME 1 2"
396    return 
397fi
398
399# Error si no se reciben 2 arámetros.
400[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_part" || return $?
401
402# Comprobamos que exista partición de sistema y la  ESP
403MNTDIR=$(ogMount $1 $2) || ogRaiseError $OG_ERR_PARTITION "$DISK $PART" || return $?
404EFIDIR=$(ogMount $(ogGetEsp))
405if [ "$EFIDIR" == "" ]; then
406    ogFormat $(ogGetEsp) FAT32
407    EFIDIR=$(ogMount $(ogGetEsp)) || ogRaiseError $OG_ERR_PARTITION "ESP" || return $?
408fi
409
410# Comprobamos que exista el cargador
411#BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
412OSVERSION=$(ogGetOsVersion $1 $2)
413case $OSVERSION in
414    *Windows\ 10*)
415        BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
416        LOADER=$(ogGetPath $MNTDIR/ogBoot/bootmgfw.efi)
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
423
424        # Si existe subcarpeta Microsoft en la partición EFI la renombramos
425        [ "$(ogGetPath $EFIDIR/EFI/Microsoft)" == "" ] || mv $EFIDIR/EFI/{Microsoft,Microsoft.backup.og}
426        ;; 
427esac
428}
429
430
431#/**
432#         ogRestoreUuidPartitions
433#@brief   Restaura los uuid de las particiones y la tabla de particiones
434#@param   int_ndisk      nº de orden del disco
435#@param   int_nfilesys   nº de orden del sistema de archivos
436#@param   REPO|CACHE     repositorio
437#@param   str_imgname    nombre de la imagen
438#@return  (nada)
439#@exception OG_ERR_FORMAT    Formato incorrecto.
440#@exception OG_ERR_NOTFOUND  No encontrado fichero de información de la imagen (con uuid)
441#*/ ##
442function ogRestoreUuidPartitions () {
443local DISK PART IMGNAME INFOFILE DEVICE DATA GUID UUID IMGGUID
444local EFIDEVICE EFIDATA EFIGUID EFIUUID EFIUUID IMGEFIGUID
445
446# Si se solicita, mostrar ayuda.
447if [ "$*" == "help" ]; then
448    ogHelp "$FUNCNAME" "$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" \
449           "$FUNCNAME REPO Windows 1 2"
450    return
451fi
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 $?
454
455# Sólo se ejecuta si es UEFI
456ogIsEfiActive || return
457
458# Parámetros de entrada
459IMGNAME="$2"
460INFOFILE="$OGIMG/.$IMGNAME.img.json"
461[ "${1^^}" == "CACHE" ] && INFOFILE="$OGCAC$INFOFILE"
462# TODO: que la función getPath soporte archivos ocultos
463ls $INFOFILE &>/dev/null || ogRaiseError $OG_ERR_NOTFOUND "$INFOFILE" || return $?
464DISK=$3
465PART=$4
466
467DEVICE=$(ogDiskToDev $DISK)
468read -e EFIDISK EFIPART <<<"$(ogGetEsp)"
469
470# Datos de la imagen
471IMGGUID=$(jq .guid $INFOFILE|tr -d \")
472IMGEFIGUID=$(jq .espguid $INFOFILE|tr -d \")
473
474# Datos actuales
475DATA=$(sfdisk -J $DEVICE)
476GUID=$(echo $DATA|jq ".partitiontable|.id"|tr -d \")
477
478if [ "$IMGGUID" != "$GUID" ]; then
479    echo sgdisk -U "$IMGGUID"  "$DEVICE"
480    sgdisk -U "$IMGGUID"  "$DEVICE"
481    partprobe
482fi
483
484if [ $DISK -eq $EFIDISK ]; then
485    EFIDATA=$DATA
486    EFIDEVICE=$DEVICE
487else
488    EFIDEVICE=$(ogDiskToDev $EFIDISK) || return $?
489    EFIDATA=$(sfdisk -J $EFIDEVICE)
490    EFIGUID=$(echo $EFIDATA|jq ".partitiontable|.id"|tr -d \")
491    if [ "$IMGEFIGUID" != "$EFIGUID" ]; then
492echo         sgdisk -U "$IMGEFIGUID"  "$EFIDEVICE"
493       sgdisk -U "$IMGEFIGUID"  "$EFIDEVICE"
494       partprobe
495   fi
496fi
497}
498
499
500#/**
501#         ogNvramSetNext
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.
504#@return  (nada)
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.
508#*/ ##
509function ogNvramSetNext () {
510local NUMENTRY
511
512# Si se solicita, mostrar ayuda.
513if [ "$*" == "help" ]; then
514    ogHelp "$FUNCNAME" "$FUNCNAME [ Num_order_entry | Label_entry ] " \
515           "$FUNCNAME 2" \
516           "$FUNCNAME \"Windows Boot Manager\""
517    return
518fi
519
520# Error si no se recibe 1 parámetro.
521[ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME [ Num_order_entry | Label_entry ]" || return $?
522
523# Si no es equipo UEFI salir con error
524ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
525
526# Distingo si es número de orden o etiqueta
527if [[ $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)}')
529else
530    NUMENTRY=$(efibootmgr |awk  -v LABEL="$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
531fi
532
533[ "$NUMENTRY" == "" ] && return $(ogRaiseError $OG_ERR_NOTFOUND "NVRAM entry '$1'")
534
535efibootmgr -n $NUMENTRY &>/dev/null
536}
537
538#/**
539#         ogNvramSetOrder
540#@brief   Configura el orden de las entradas de la NVRAM
541#@param   Orden de las entradas separadas por espacios
542#@return  (nada)
543#@exception OG_ERR_FORMAT    formato incorrecto.
544#@exception OG_ERR_NOTUEFI   UEFI no activa.
545#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado (entrada NVRAM).
546#*/ ##
547function ogNvramSetOrder () {
548# Si se solicita, mostrar ayuda.
549if [ "$*" == "help" ]; then
550    ogHelp "$FUNCNAME" "$FUNCNAME Num_order1 [ Num_order2 ] ... " \
551           "$FUNCNAME 1 3"
552    return
553fi
554#
555# Error si no se recibe al menos 1 parámetro.
556[ $# -ge 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME Num_order1 [ Num_order2 ] ..." || return $?
557
558# Si no es equipo UEFI salir con error
559ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
560
561# Comprobamos que sean números
562[[ "$@" =~ ^([0-9a-fA-F ]+)$ ]] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME Num_order1 [ Num_order2 ] ..." || return $?
563
564# Entradas de la NVRAM actuales
565NUMENTRYS=$(efibootmgr|awk '{ if ($1~/Boot[0-9a-fA-F]{4}/) printf "0%s ", substr($1,5,4)}')
566
567ORDER=""
568for ARG in $@; do
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 $?
572    ORDER=${ORDER},$ARG
573done
574
575# Cambiamos el orden
576efibootmgr -o ${ORDER#,} &>/dev/null
577}
578
579
580#/**
581#         ogNvramSetTimeout
582#@brief   Configura el tiempo de espera de la NVRAM
583#@param   Orden de las entradas separadas por espacios
584#@return  (nada)
585
586#@exception OG_ERR_FORMAT    formato incorrecto.
587#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
588#*/ ##
589function ogNvramSetTimeout () {
590# Si se solicita, mostrar ayuda.
591if [ "$*" == "help" ]; then
592    ogHelp "$FUNCNAME" "$FUNCNAME int_Timeout (seg)" \
593           "$FUNCNAME 2"
594    return
595fi
596#
597# Si no es equipo UEFI salir con error
598ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
599
600# Error si no se recibe 1 parámetro.
601[ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_Timeout (seg)" || return $?
602
603# Comprobamos que sea  un número
604[[ "$1" =~ ^([0-9 ]+)*$ ]] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_Timeout (seg)" || return $?
605
606# Cambiamos el orden
607efibootmgr -t $1 &>/dev/null
608}
609
610
611#/**
612#         ogUuidChange int_ndisk str_repo
613#@brief   Reemplaza el UUID de un sistema de ficheros.
614#@param   int_ndisk    nº de orden del disco
615#@param   int_part     nº de partición
616#@return  (nada, por determinar)
617#@exception OG_ERR_FORMAT    formato incorrecto.
618#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
619#*/ ##
620function ogUuidChange () {
621local MNTDIR DEVICE UUID NEWUUID f
622
623# Si se solicita, mostrar ayuda.
624if [ "$*" == "help" ]; then
625    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
626           "$FUNCNAME 1 2"
627    return
628fi
629
630# Error si no se reciben al menos 2 parámetros.
631[ $# -eq 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_part" || return $?
632
633# Comprobamos que exista la partición
634MNTDIR=$(ogMount $1 $2) || ogRaiseError $OG_ERR_NOTFOUND "Device $1 $2" || return $?
635DEVICE=$(ogDiskToDev $1 $2)
636UUID=$(blkid -o value -s UUID $DEVICE)
637NEWUUID=$(cat /proc/sys/kernel/random/uuid)
638
639# Cambiamos UUID a la partición
640ogUnmount $1 $2
641tune2fs $DEVICE -U $NEWUUID
642
643# Cambiamos UUID en la configuración (fstab y grub)
644ogMount $1 $2
645for f in $MNTDIR/etc/fstab $MNTDIR/{,boot/}{{grubMBR,grubPARTITION}/boot/,}{grub{,2},{,efi/}EFI/*}/{menu.lst,grub.cfg}; do
646        [ -r $f ] && sed -i s/$UUID/$NEWUUID/g $f
647done
648}
Note: See TracBrowser for help on using the repository browser.