source: client/engine/UEFI.lib @ e9601e1

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

#802 #889 #890 ogGrubInstallPartitions, ogGrubInstallMbr y ogRestoreEfiBootLoader: Format EFI partition if it is not. ogGetEsp: Detecting the EFI partition does not require VFAT filesystem.

  • Property mode set to 100644
File size: 10.5 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#         ogGrubUefiConf int_ndisk int_part str_dir_grub
7#@brief   Genera el fichero grub.cfg de la ESP
8#@param   int_ndisk    nº de orden del disco
9#@param   int_part     nº de partición
10#@param   str_dir_grub prefijo del directorio de grub en la partición de sistema. ej: /boot/grubPARTITION
11#@return  (nada, por determinar)
12#@exception OG_ERR_FORMAT    formato incorrecto.
13#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
14#@TODO    Confirmar si el fichero "$EFIDIR/EFI/$BOOTLABEL/grub.cfg" es necesario.
15#*/ ##
16function ogGrubUefiConf () {
17local EFIDIR BOOTLABEL GRUBEFI UUID DEVICE PREFIXSECONDSTAGE EFIGRUBDIR
18
19# Si se solicita, mostrar ayuda.
20if [ "$*" == "help" ]; then
21    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part [ str_dir_grub ]" \
22           "$FUNCNAME 1 2" \
23           "$FUNCNAME 1 3 /boot/grubPARTITION"
24    return
25fi
26
27# Error si no se reciben al menos 2 parámetros.
28[ $# -ge 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_part [ str_dir_grub ]" || return $?
29
30# Directorio del grub en la partición de sistema
31PREFIXSECONDSTAGE="$3"
32
33EFIDIR=$(ogMount $(ogGetEsp)) || ogRaiseError $OG_ERR_PARTITION "ESP" || return $?
34BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
35EFIGRUBDIR="$EFIDIR/EFI/$BOOTLABEL/boot/grub"
36# Comprobamos que existe directorio
37[ -d "$EFIGRUBDIR" ] || mkdir -p "$EFIGRUBDIR"
38# Parcheamos uuid y particion en grub.cfg
39UUID=$(blkid -o value -s UUID $(ogDiskToDev $1 $2))
40DEVICE="hd$(expr $1 - 1 ),gpt$2"
41
42cat << EOT > $EFIGRUBDIR/grub.cfg
43set root='$DEVICE'
44set prefix=(\$root)'${PREFIXSECONDSTAGE}/boot/grub'
45configfile \$prefix/grub.cfg
46EOT
47
48# Provisional: confirmar si el segundo archivo se utiliza
49#cp $EFIGRUBDIR/grub.cfg "$EFIDIR/EFI/$BOOTLABEL/grub.cfg"
50}
51
52#/**
53#         ogUuidChange int_ndisk str_repo
54#@brief   Reemplaza el UUID de un sistema de ficheros.
55#@param   int_ndisk    nº de orden del disco
56#@param   int_part     nº de partición
57#@return  (nada, por determinar)
58#@exception OG_ERR_FORMAT    formato incorrecto.
59#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
60#*/ ##
61function ogUuidChange () {
62local MNTDIR DEVICE UUID NEWUUID f
63
64# Si se solicita, mostrar ayuda.
65if [ "$*" == "help" ]; then
66    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
67           "$FUNCNAME 1 2"
68    return
69fi
70
71# Error si no se reciben al menos 2 parámetros.
72[ $# -eq 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_part" || return $?
73
74# Comprobamos que exista la partición
75MNTDIR=$(ogMount $1 $2) || ogRaiseError $OG_ERR_NOTFOUND "Device $1 $2" || return $?
76DEVICE=$(ogDiskToDev $1 $2)
77UUID=$(blkid -o value -s UUID $DEVICE)
78NEWUUID=$(cat /proc/sys/kernel/random/uuid)
79
80# Cambiamos UUID a la partición
81ogUnmount $1 $2
82tune2fs $DEVICE -U $NEWUUID
83
84# Cambiamos UUID en la configuración (fstab y grub)
85ogMount $1 $2
86for f in $MNTDIR/etc/fstab $MNTDIR/{,boot/}{{grubMBR,grubPARTITION}/boot/,}{grub{,2},{,efi/}EFI/*}/{menu.lst,grub.cfg}; do
87        [ -r $f ] && sed -i s/$UUID/$NEWUUID/g $f
88done
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#         ogRestoreEfiBootLoader int_ndisk str_repo
138#@brief   Copia el cargador de arranque de la partición de sistema a la partición EFI.
139#@param   int_ndisk    nº de orden del disco
140#@param   int_part     nº de partición
141#@return  (nada, por determinar)
142#@exception OG_ERR_FORMAT    formato incorrecto.
143#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado (partición de sistema o EFI).
144#@exception OG_ERR_NOTOS     sin sistema operativo.
145#*/ ##
146function ogRestoreEfiBootLoader () {
147# Variables locales
148local MNTDIR EFIDIR BOOTLABEL OSVERSION LOADER f UUID DEVICE
149
150# Si se solicita, mostrar ayuda.
151if [ "$*" == "help" ]; then
152    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_part" \
153           "$FUNCNAME 1 2"
154    return
155fi
156
157# Error si no se reciben 2 arámetros.
158[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_part" || return $?
159
160# Comprobamos que exista partición de sistema y la  ESP
161MNTDIR=$(ogMount $1 $2) || ogRaiseError $OG_ERR_PARTITION "$DISK $PART" || return $?
162EFIDIR=$(ogMount $(ogGetEsp))
163if [ "$EFIDIR" == "" ]; then
164    ogFormat $(ogGetEsp) FAT32
165    EFIDIR=$(ogMount $(ogGetEsp)) || ogRaiseError $OG_ERR_PARTITION "ESP" || return $?
166fi
167
168# Comprobamos que exista el cargador
169#BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
170OSVERSION=$(ogGetOsVersion $1 $2)
171case $OSVERSION in
172    *Windows\ 10*)
173        BOOTLABEL=$(printf "Part-%02d-%02d" $1 $2)
174        LOADER=$(ogGetPath $MNTDIR/Boot/bootmgfw.efi)
175        [ -n "$LOADER" ] || ogRaiseError $OG_ERR_NOTOS "$1 $2 ($OSVERSION, EFI)" || return $?
176        [ -r $EFIDIR/$BOOTLABEL ] && rm -rf $EFIDIR/$BOOTLABEL
177        mkdir -p $EFIDIR/EFI/$BOOTLABEL
178        cp -r "${LOADER%/*}" $EFIDIR/EFI/$BOOTLABEL
179        ;;
180esac
181}
182
183
184#         ogRestoreUuidPartitions
185#@brief   Restaura los uuid de las particiones y la tabla de particiones
186#@param   int_ndisk      nº de orden del disco
187#@param   int_nfilesys   nº de orden del sistema de archivos
188#@param   REPO|CACHE     repositorio
189#@param   str_imgname    nombre de la imagen
190#@exception OG_ERR_FORMAT    Formato incorrecto.
191#@exception OG_ERR_NOTFOUND  No encontrado fichero de información de la imagen (con uuid)
192function ogRestoreUuidPartitions () {
193local DISK PART IMGNAME INFOFILE DEVICE DATA GUID UUID IMGGUID
194local EFIDEVICE EFIDATA EFIGUID EFIUUID EFIUUID IMGEFIGUID
195
196# Si se solicita, mostrar ayuda.
197if [ "$*" == "help" ]; then
198    ogHelp "$FUNCNAME" "$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" \
199           "$FUNCNAME REPO Windows 1 2"
200    return
201fi
202# Error si no se reciben 4 parámetros.
203[ $# -eq 4 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME REPO|CACHE str_imgname int_ndisk int_npart" || return $?
204
205# Sólo se ejecuta si es UEFI
206[ ogIsEfiActive ] || return
207
208# Parámetros de entrada
209IMGNAME="$2"
210INFOFILE="$OGIMG/.$IMGNAME.img.json"
211[ "${1^^}" == "CACHE" ] && INFOFILE="$OGCAC$INFOFILE"
212# TODO: que la función getPath soporte archivos ocultos
213ls $INFOFILE &>/dev/null || ogRaiseError $OG_ERR_NOTFOUND "$INFOFILE" || return $?
214DISK=$3
215PART=$4
216
217DEVICE=$(ogDiskToDev $DISK)
218read -e EFIDISK EFIPART <<<"$(ogGetEsp)"
219
220# Datos de la imagen
221IMGGUID=$(jq .guid $INFOFILE|tr -d \")
222IMGEFIGUID=$(jq .espguid $INFOFILE|tr -d \")
223
224# Datos actuales
225DATA=$(sfdisk -J $DEVICE)
226GUID=$(echo $DATA|jq ".partitiontable|.id"|tr -d \")
227
228if [ "$IMGGUID" != "$GUID" ]; then
229    echo sgdisk -U "$IMGGUID"  "$DEVICE"
230    sgdisk -U "$IMGGUID"  "$DEVICE"
231    partprobe
232fi
233
234if [ $DISK -eq $EFIDISK ]; then
235    EFIDATA=$DATA
236    EFIDEVICE=$DEVICE
237else
238    EFIDEVICE=$(ogDiskToDev $EFIDISK) || return $?
239    EFIDATA=$(sfdisk -J $EFIDEVICE)
240    EFIGUID=$(echo $EFIDATA|jq ".partitiontable|.id"|tr -d \")
241    if [ "$IMGEFIGUID" != "$EFIGUID" ]; then
242echo         sgdisk -U "$IMGEFIGUID"  "$EFIDEVICE"
243       sgdisk -U "$IMGEFIGUID"  "$EFIDEVICE"
244       partprobe
245   fi
246fi
247
248}
249
250#         ogSaveImageInfo
251#@brief   Crea un fichero con la información de la imagen.
252#@param   int_ndisk      nº de orden del disco
253#@param   int_nfilesys   nº de orden del sistema de archivos
254#@param   REPO|CACHE     repositorio
255#@param   str_imgname    nombre de la imagen
256#@exception OG_ERR_FORMAT    formato incorrecto.
257#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
258function ogSaveImageInfo () {
259local DISK PART IMGDIR IMGNAME INFO INFOFILE DEVICE DATA GUID
260local EFIPARTDEVICE EFIDEVICE EFIDATA EFIGUID
261
262# Si se solicita, mostrar ayuda.
263if [ "$*" == "help" ]; then
264    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys REPO|CACHE str_imgname" \
265           "$FUNCNAME 1 2 REPO Windows"
266    return
267fi
268# Error si no se reciben 4 parámetros.
269[ $# -eq 4 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_nfilesys REPO|CACHE str_imgname" || return $?
270
271DISK=$1
272PART=$2
273IMGDIR="$(ogGetParentPath "$3" "/$4")"
274# Si no existe el directorio de la imagen me salgo
275[ "$IMGDIR" != "" ] || ogRaiseError $OG_ERR_NOTFOUND "$3 $(dirname $4)" || return $?
276IMGNAME="$(basename "$4")"
277INFOFILE="$IMGDIR/.$IMGNAME.img.json"
278
279DEVICE=$(ogDiskToDev $DISK) || return $?
280DATA=$(sfdisk -J $DEVICE)
281GUID=$(echo $DATA|jq ".partitiontable|.id"|tr -d \")
282
283# Información de la imagen. Valor inicial de efi: false
284INFO=$(cat << EOT | jq .
285{"name":"$IMGNAME","efi":"false","guid":"$GUID"}
286EOT
287     )
288
289if ogIsEfiActive; then
290    # Cambio valor de efi a true
291    INFO=$(echo $INFO|  jq --arg aux true '. + {efi: $aux}')
292
293    # Obtener partición EFI.
294    read -e EFIDISK EFIPART <<<"$(ogGetEsp)"
295    EFIPARTDEVICE=$(ogDiskToDev $EFIDISK $EFIPART) || return $?
296    if [ $DISK -eq $EFIDISK ]; then
297        EFIDEVICE=$DEVICE
298        EFIDATA=$DATA
299        EFIGUID=$GUID
300    else
301        EFIDEVICE=$(ogDiskToDev $EFIDISK)
302        EFIDATA=$(sfdisk -J $EFIDEVICE)
303        EFIGUID=$(echo $EFIDATA|jq ".partitiontable|.id"|tr -d \")
304    fi
305
306    # Incluyo valor de EFIGUID (por si partición EFI en distinto disco que la de sistema)
307    INFO=$(echo $INFO|  jq --arg aux $EFIGUID '. + {espguid: $aux}')
308fi
309
310cat << EOT | jq . > $INFOFILE
311$INFO
312EOT
313}
Note: See TracBrowser for help on using the repository browser.