source: client/engine/UEFI.lib @ 7e4b9e0

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 7e4b9e0 was 527cd97, checked in by Irina Gómez <irinagomez@…>, 6 years ago

#802 #888 #890 ogNvramAddEntry function allows disk and partition as params and creates a entry with 'Part-D-P' name that uses generic loader of OpenGnsys. ogGrubInstall uses shimx64.efi.signed of ogLive Ubuntu 18. Previously this function used shim of tftp of server.

  • Property mode set to 100644
File size: 21.0 KB
RevLine 
[39b84ff]1#!/bin/bash
2# Libreria provisional para uso de UEFI
3# Las funciones se incluirán las librerías ya existentes
4
5#/**
[b0a7050]6#         ogNvramActiveEntry
7#@brief   Activa entrada de la NVRAM identificada por la etiqueta o el orden
[b00e622]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#*/ ##
[b0a7050]14function ogNvramActiveEntry () {
[b00e622]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
[b0a7050]32if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
[b00e622]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
[b0a7050]43#/**
44#         ogNvramAddEntry
45#@brief   Crea nueva entrada en el gestor de arranque (NVRAM), opcionalmente la incluye al final del orden de arranque.
[527cd97]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.
[b0a7050]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 ]" \
[527cd97]60           "$FUNCNAME 1 2 TRUE" \
[b0a7050]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
[527cd97]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
[b0a7050]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
[b00e622]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/{$BOOTLABEL,Microsoft}/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/Boot ] && rm -rf $MNTDIR/Boot
138        DIRLOADER=$(realpath "${LOADER%/*}/..")
139        cp -r ${DIRLOADER}/Boot $MNTDIR
140        ;;
141esac
142}
143
144
145#/**
[b0a7050]146#         ogNvramDeleteEntry
[b00e622]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#*/ ##
[b0a7050]154function ogNvramDeleteEntry () {
155local NUMENTRY n
[b00e622]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
[b0a7050]172if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
[b00e622]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
[b0a7050]180for n in $NUMENTRY; do
181    efibootmgr -B -b $n &>/dev/null
182done
[b00e622]183}
184
185
186#/**
[b0a7050]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
[b00e622]190#@exception OG_ERR_NOTUEFI   UEFI no activa.
191#*/ ##
[b0a7050]192function ogNvramGetCurrent () {
[b00e622]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
[b0a7050]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))}'
[b00e622]205}
206
207
[b0a7050]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)
[b00e622]230#@return  Orden de las entradas
231#@exception OG_ERR_NOTUEFI   UEFI no activa.
232#*/ ##
[b0a7050]233function ogNvramGetOrder () {
[b00e622]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#/**
[b0a7050]249#         ogNvramGetTimeout
[b00e622]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#*/ ##
[b0a7050]254function ogNvramGetTimeout () {
[b00e622]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#/**
[30238ab]270#         ogGrubUefiConf int_ndisk int_part str_dir_grub
[39b84ff]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
[7dc06be9]274#@param   str_dir_grub prefijo del directorio de grub en la partición de sistema. ej: /boot/grubPARTITION
[39b84ff]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#*/ ##
[30238ab]280function ogGrubUefiConf () {
[7dc06be9]281local EFIDIR BOOTLABEL GRUBEFI UUID DEVICE PREFIXSECONDSTAGE EFIGRUBDIR
[39b84ff]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" \
[7dc06be9]287           "$FUNCNAME 1 3 /boot/grubPARTITION"
[39b84ff]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
[7dc06be9]295PREFIXSECONDSTAGE="$3"
[39b84ff]296
297EFIDIR=$(ogMount $(ogGetEsp)) || ogRaiseError $OG_ERR_PARTITION "ESP" || return $?
298BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
[7dc06be9]299EFIGRUBDIR="$EFIDIR/EFI/$BOOTLABEL/boot/grub"
[39b84ff]300# Comprobamos que existe directorio
[7dc06be9]301[ -d "$EFIGRUBDIR" ] || mkdir -p "$EFIGRUBDIR"
[39b84ff]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
[7dc06be9]306cat << EOT > $EFIGRUBDIR/grub.cfg
[39b84ff]307set root='$DEVICE'
[7dc06be9]308set prefix=(\$root)'${PREFIXSECONDSTAGE}/boot/grub'
[39b84ff]309configfile \$prefix/grub.cfg
310EOT
311
312# Provisional: confirmar si el segundo archivo se utiliza
[527cd97]313cp $EFIGRUBDIR/grub.cfg "$EFIDIR/EFI/$BOOTLABEL/grub.cfg"
[39b84ff]314}
315
[b00e622]316
[39b84ff]317#/**
[b0a7050]318#         ogNvramInactiveEntry
[b00e622]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)
[39b84ff]322#@exception OG_ERR_FORMAT    formato incorrecto.
323#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
[b00e622]324#@exception OG_ERR_NOTUEFI   UEFI no activa.
[39b84ff]325#*/ ##
[b0a7050]326function ogNvramInactiveEntry () {
[b00e622]327local NUMENTRY
[39b84ff]328
329# Si se solicita, mostrar ayuda.
330if [ "$*" == "help" ]; then
[b00e622]331    ogHelp "$FUNCNAME" "$FUNCNAME [ Num_order_entry | Label_entry ] " \
332           "$FUNCNAME 2" \
333           "$FUNCNAME \"Windows Boot Manager\""
[39b84ff]334    return
335fi
336
[b00e622]337# Error si no se recibe 1 parámetro.
338[ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME [ Num_order_entry | Label_entry ]" || return $?
[39b84ff]339
[b00e622]340# Si no es equipo UEFI salir con error
341ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
[39b84ff]342
[b00e622]343# Distingo si es número de orden o etiqueta
[b0a7050]344if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
[b00e622]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
[39b84ff]349
[b00e622]350[ "$NUMENTRY" == "" ] && return $(ogRaiseError $OG_ERR_NOTFOUND "NVRAM entry '$1'")
351
352efibootmgr -A -b $NUMENTRY &>/dev/null
[39b84ff]353}
[30238ab]354
[b00e622]355
[30238ab]356#/**
[b0a7050]357#         ogNvramList
[b00e622]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.
[30238ab]361#*/ ##
[b0a7050]362function ogNvramList () {
[30238ab]363
364# Si se solicita, mostrar ayuda.
365if [ "$*" == "help" ]; then
[b00e622]366    ogHelp "$FUNCNAME" "$FUNCNAME" \
367           "$FUNCNAME"
[30238ab]368    return
369fi
370
[b00e622]371# Si no es equipo UEFI salir con error
372ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
[30238ab]373
[b00e622]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}'
[30238ab]375}
376
[b00e622]377
[30238ab]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 $?
[e9601e1]404EFIDIR=$(ogMount $(ogGetEsp))
405if [ "$EFIDIR" == "" ]; then
406    ogFormat $(ogGetEsp) FAT32
407    EFIDIR=$(ogMount $(ogGetEsp)) || ogRaiseError $OG_ERR_PARTITION "ESP" || return $?
408fi
[30238ab]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/Boot/bootmgfw.efi)
417        [ -n "$LOADER" ] || ogRaiseError $OG_ERR_NOTOS "$1 $2 ($OSVERSION, EFI)" || return $?
418        [ -r $EFIDIR/$BOOTLABEL ] && rm -rf $EFIDIR/$BOOTLABEL
419        mkdir -p $EFIDIR/EFI/$BOOTLABEL
420        cp -r "${LOADER%/*}" $EFIDIR/EFI/$BOOTLABEL
[47d8ae8]421        # Nombre OpenGnsys para cargador
[40bd9f5]422        cp $LOADER $EFIDIR/EFI/$BOOTLABEL/Boot/ogloader.efi
[30238ab]423        ;;
424esac
425}
426
427
[b00e622]428#/**
[30238ab]429#         ogRestoreUuidPartitions
430#@brief   Restaura los uuid de las particiones y la tabla de particiones
431#@param   int_ndisk      nº de orden del disco
432#@param   int_nfilesys   nº de orden del sistema de archivos
433#@param   REPO|CACHE     repositorio
434#@param   str_imgname    nombre de la imagen
[b0a7050]435#@return  (nada)
[30238ab]436#@exception OG_ERR_FORMAT    Formato incorrecto.
437#@exception OG_ERR_NOTFOUND  No encontrado fichero de información de la imagen (con uuid)
[b00e622]438#*/ ##
[30238ab]439function ogRestoreUuidPartitions () {
[a31f7a9]440local DISK PART IMGNAME INFOFILE DEVICE DATA GUID UUID IMGGUID
441local EFIDEVICE EFIDATA EFIGUID EFIUUID EFIUUID IMGEFIGUID
[30238ab]442
443# Si se solicita, mostrar ayuda.
444if [ "$*" == "help" ]; then
445    ogHelp "$FUNCNAME" "$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" \
446           "$FUNCNAME REPO Windows 1 2"
447    return
448fi
449# Error si no se reciben 4 parámetros.
450[ $# -eq 4 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" || return $?
451
452# Sólo se ejecuta si es UEFI
[47d8ae8]453ogIsEfiActive || return
[30238ab]454
455# Parámetros de entrada
456IMGNAME="$2"
457INFOFILE="$OGIMG/.$IMGNAME.img.json"
458[ "${1^^}" == "CACHE" ] && INFOFILE="$OGCAC$INFOFILE"
459# TODO: que la función getPath soporte archivos ocultos
460ls $INFOFILE &>/dev/null || ogRaiseError $OG_ERR_NOTFOUND "$INFOFILE" || return $?
461DISK=$3
462PART=$4
463
464DEVICE=$(ogDiskToDev $DISK)
465read -e EFIDISK EFIPART <<<"$(ogGetEsp)"
466
467# Datos de la imagen
468IMGGUID=$(jq .guid $INFOFILE|tr -d \")
469IMGEFIGUID=$(jq .espguid $INFOFILE|tr -d \")
470
471# Datos actuales
472DATA=$(sfdisk -J $DEVICE)
473GUID=$(echo $DATA|jq ".partitiontable|.id"|tr -d \")
474
475if [ "$IMGGUID" != "$GUID" ]; then
476    echo sgdisk -U "$IMGGUID"  "$DEVICE"
477    sgdisk -U "$IMGGUID"  "$DEVICE"
478    partprobe
479fi
480
481if [ $DISK -eq $EFIDISK ]; then
482    EFIDATA=$DATA
483    EFIDEVICE=$DEVICE
484else
485    EFIDEVICE=$(ogDiskToDev $EFIDISK) || return $?
486    EFIDATA=$(sfdisk -J $EFIDEVICE)
487    EFIGUID=$(echo $EFIDATA|jq ".partitiontable|.id"|tr -d \")
488    if [ "$IMGEFIGUID" != "$EFIGUID" ]; then
489echo         sgdisk -U "$IMGEFIGUID"  "$EFIDEVICE"
490       sgdisk -U "$IMGEFIGUID"  "$EFIDEVICE"
491       partprobe
492   fi
493fi
494}
495
[b00e622]496
497#/**
[b0a7050]498#         ogNvramSetNext
499#@brief   Configura el próximo arranque con la entrada del gestor de arranque (NVRAM) identificada por la etiqueta o el orden.
500#@param    Num_order_entry | Label_entry Número de orden o la etiqueta de la entrada a borrar.
501#@return  (nada)
502#@exception OG_ERR_FORMAT    formato incorrecto.
503#@exception OG_ERR_NOTUEFI   UEFI no activa.
504#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
505#*/ ##
506function ogNvramSetNext () {
507local NUMENTRY
508
509# Si se solicita, mostrar ayuda.
510if [ "$*" == "help" ]; then
511    ogHelp "$FUNCNAME" "$FUNCNAME [ Num_order_entry | Label_entry ] " \
512           "$FUNCNAME 2" \
513           "$FUNCNAME \"Windows Boot Manager\""
514    return
515fi
516
517# Error si no se recibe 1 parámetro.
518[ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME [ Num_order_entry | Label_entry ]" || return $?
519
520# Si no es equipo UEFI salir con error
521ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
522
523# Distingo si es número de orden o etiqueta
524if [[ $1 =~ ^([0-9a-fA-F]+)$ ]]; then
525    NUMENTRY=$( efibootmgr |awk  -v NUM="$(printf %04x 0x$1|tr '[:lower:]' '[:upper:]')" '{ if($1~NUM) print substr($1,5,4)}')
526else
527    NUMENTRY=$(efibootmgr |awk  -v LABEL="$1" '{ if(substr($0, index($0,$2))==LABEL) print substr($1,5,4)}')
528fi
529
530[ "$NUMENTRY" == "" ] && return $(ogRaiseError $OG_ERR_NOTFOUND "NVRAM entry '$1'")
531
532efibootmgr -n $NUMENTRY &>/dev/null
533}
534
535#/**
536#         ogNvramSetOrder
[b00e622]537#@brief   Configura el orden de las entradas de la NVRAM
538#@param   Orden de las entradas separadas por espacios
539#@return  (nada)
540#@exception OG_ERR_FORMAT    formato incorrecto.
541#@exception OG_ERR_NOTUEFI   UEFI no activa.
542#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado (entrada NVRAM).
543#*/ ##
[b0a7050]544function ogNvramSetOrder () {
[b00e622]545# Si se solicita, mostrar ayuda.
546if [ "$*" == "help" ]; then
547    ogHelp "$FUNCNAME" "$FUNCNAME Num_order1 [ Num_order2 ] ... " \
548           "$FUNCNAME 1 3"
549    return
550fi
551#
552# Error si no se recibe al menos 1 parámetro.
553[ $# -ge 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME Num_order1 [ Num_order2 ] ..." || return $?
554
555# Si no es equipo UEFI salir con error
556ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
557
558# Comprobamos que sean números
[b0a7050]559[[ "$@" =~ ^([0-9a-fA-F ]+)$ ]] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME Num_order1 [ Num_order2 ] ..." || return $?
[b00e622]560
561# Entradas de la NVRAM actuales
[ff5e572]562NUMENTRYS=$(efibootmgr|awk '{ if ($1~/Boot[0-9a-fA-F]{4}/) printf "0%s ", substr($1,5,4)}')
[b00e622]563
564ORDER=""
565for ARG in $@; do
566    # Si no existe la entrada me salgo
[ff5e572]567    ARG=$(printf  %04X 0x$ARG)
[b00e622]568    echo $NUMENTRYS | grep "$ARG" &>/dev/null || ogRaiseError $OG_ERR_NOTFOUND "NVRAM entry order \"$ARG\"" || return $?
569    ORDER=${ORDER},$ARG
570done
571
572# Cambiamos el orden
573efibootmgr -o ${ORDER#,} &>/dev/null
574}
575
576
577#/**
[b0a7050]578#         ogNvramSetTimeout
[b00e622]579#@brief   Configura el tiempo de espera de la NVRAM
580#@param   Orden de las entradas separadas por espacios
581#@return  (nada)
582
583#@exception OG_ERR_FORMAT    formato incorrecto.
584#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
585#*/ ##
[b0a7050]586function ogNvramSetTimeout () {
[b00e622]587# Si se solicita, mostrar ayuda.
588if [ "$*" == "help" ]; then
589    ogHelp "$FUNCNAME" "$FUNCNAME int_Timeout (seg)" \
590           "$FUNCNAME 2"
591    return
592fi
593#
594# Si no es equipo UEFI salir con error
595ogIsEfiActive || ogRaiseError $OG_ERR_NOTUEFI || return $?
596
597# Error si no se recibe 1 parámetro.
598[ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_Timeout (seg)" || return $?
599
600# Comprobamos que sea  un número
601[[ "$1" =~ ^([0-9 ]+)*$ ]] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_Timeout (seg)" || return $?
602
603# Cambiamos el orden
604efibootmgr -t $1 &>/dev/null
605}
606
607
608#/**
609#         ogUuidChange int_ndisk str_repo
610#@brief   Reemplaza el UUID de un sistema de ficheros.
611#@param   int_ndisk    nº de orden del disco
612#@param   int_part     nº de partición
613#@return  (nada, por determinar)
614#@exception OG_ERR_FORMAT    formato incorrecto.
615#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
616#*/ ##
617function ogUuidChange () {
618local MNTDIR DEVICE UUID NEWUUID f
619
620# Si se solicita, mostrar ayuda.
621if [ "$*" == "help" ]; then
622    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
623           "$FUNCNAME 1 2"
624    return
625fi
626
627# Error si no se reciben al menos 2 parámetros.
628[ $# -eq 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_part" || return $?
629
630# Comprobamos que exista la partición
631MNTDIR=$(ogMount $1 $2) || ogRaiseError $OG_ERR_NOTFOUND "Device $1 $2" || return $?
632DEVICE=$(ogDiskToDev $1 $2)
633UUID=$(blkid -o value -s UUID $DEVICE)
634NEWUUID=$(cat /proc/sys/kernel/random/uuid)
635
636# Cambiamos UUID a la partición
637ogUnmount $1 $2
638tune2fs $DEVICE -U $NEWUUID
639
640# Cambiamos UUID en la configuración (fstab y grub)
641ogMount $1 $2
642for f in $MNTDIR/etc/fstab $MNTDIR/{,boot/}{{grubMBR,grubPARTITION}/boot/,}{grub{,2},{,efi/}EFI/*}/{menu.lst,grub.cfg}; do
643        [ -r $f ] && sed -i s/$UUID/$NEWUUID/g $f
644done
645}
Note: See TracBrowser for help on using the repository browser.