source: client/engine/UEFI.lib @ a90a051

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacion
Last change on this file since a90a051 was ff5e572, checked in by Irina Gómez <irinagomez@…>, 6 years ago

#802 #888 ogRefindInstall used wrong parameter when calling ogNvramGetOrder. ogNvramSetOrder use uppercase in hexadecimal number because efibootmgr need it.

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