Blame SOURCES/rear-bz1945869.patch

f88787
diff --git a/usr/share/rear/finalize/Linux-i386/670_run_efibootmgr.sh b/usr/share/rear/finalize/Linux-i386/670_run_efibootmgr.sh
f88787
old mode 100644
f88787
new mode 100755
f88787
index cc646359..33d87767
f88787
--- a/usr/share/rear/finalize/Linux-i386/670_run_efibootmgr.sh
f88787
+++ b/usr/share/rear/finalize/Linux-i386/670_run_efibootmgr.sh
f88787
@@ -8,6 +8,10 @@ is_true $USING_UEFI_BOOTLOADER || return 0
f88787
 # (cf. finalize/Linux-i386/610_EFISTUB_run_efibootmgr.sh): 
f88787
 is_true $EFI_STUB && return
f88787
 
f88787
+LogPrint "Creating EFI Boot Manager entries..."
f88787
+
f88787
+local esp_mountpoint esp_mountpoint_inside boot_efi_parts boot_efi_dev
f88787
+
f88787
 # When UEFI_BOOTLOADER is not a regular file in the restored target system
f88787
 # (cf. how esp_mountpoint is set below) it means BIOS is used
f88787
 # (cf. rescue/default/850_save_sysfs_uefi_vars.sh)
f88787
@@ -15,64 +19,80 @@ is_true $EFI_STUB && return
f88787
 # because when UEFI_BOOTLOADER is empty the test below evaluates to
f88787
 #   test -f /mnt/local/
f88787
 # which also returns false because /mnt/local/ is a directory
f88787
-# (cf. https://github.com/rear/rear/pull/2051/files#r258826856):
f88787
-test -f "$TARGET_FS_ROOT/$UEFI_BOOTLOADER" || return 0
f88787
+# (cf. https://github.com/rear/rear/pull/2051/files#r258826856)
f88787
+# but using BIOS conflicts with USING_UEFI_BOOTLOADER is true
f88787
+# i.e. we should create EFI Boot Manager entries but we cannot:
f88787
+if ! test -f "$TARGET_FS_ROOT/$UEFI_BOOTLOADER" ; then
f88787
+    LogPrintError "Failed to create EFI Boot Manager entries (UEFI bootloader '$UEFI_BOOTLOADER' not found under target $TARGET_FS_ROOT)"
f88787
+    return 1
f88787
+fi
f88787
 
f88787
 # Determine where the EFI System Partition (ESP) is mounted in the currently running recovery system:
f88787
-esp_mountpoint=$( df -P "$TARGET_FS_ROOT/$UEFI_BOOTLOADER" | tail -1 | awk '{print $6}' )
f88787
-# Use TARGET_FS_ROOT/boot/efi as fallback ESP mountpoint:
f88787
-test "$esp_mountpoint" || esp_mountpoint="$TARGET_FS_ROOT/boot/efi"
f88787
+esp_mountpoint=$( filesystem_name "$TARGET_FS_ROOT/$UEFI_BOOTLOADER" )
f88787
+# Use TARGET_FS_ROOT/boot/efi as fallback ESP mountpoint (filesystem_name returns "/"
f88787
+# if mountpoint not found otherwise):
f88787
+if [ "$esp_mountpoint" = "/" ] ; then
f88787
+    esp_mountpoint="$TARGET_FS_ROOT/boot/efi"
f88787
+    LogPrint "Mountpoint of $TARGET_FS_ROOT/$UEFI_BOOTLOADER not found, trying $esp_mountpoint"
f88787
+fi
f88787
 
f88787
 # Skip if there is no esp_mountpoint directory (e.g. the fallback ESP mountpoint may not exist).
f88787
 # Double quotes are mandatory here because 'test -d' without any (possibly empty) argument results true:
f88787
-test -d "$esp_mountpoint" || return 0
f88787
-
f88787
-BootEfiDev="$( mount | grep "$esp_mountpoint" | awk '{print $1}' )"
f88787
-# /dev/sda1 or /dev/mapper/vol34_part2 or /dev/mapper/mpath99p4
f88787
-Dev=$( get_device_name $BootEfiDev )
f88787
-# 1 (must anyway be a low nr <9)
f88787
-ParNr=$( get_partition_number $Dev )
f88787
-# /dev/sda or /dev/mapper/vol34_part or /dev/mapper/mpath99p or /dev/mmcblk0p
f88787
-Disk=$( echo ${Dev%$ParNr} )
f88787
-
f88787
-# Strip trailing partition remainders like '_part' or '-part' or 'p'
f88787
-# if we have 'mapper' in disk device name:
f88787
-if [[ ${Dev/mapper//} != $Dev ]] ; then
f88787
-    # we only expect mpath_partX or mpathpX or mpath-partX
f88787
-    case $Disk in
f88787
-        (*p)     Disk=${Disk%p} ;;
f88787
-        (*-part) Disk=${Disk%-part} ;;
f88787
-        (*_part) Disk=${Disk%_part} ;;
f88787
-        (*)      Log "Unsupported kpartx partition delimiter for $Dev"
f88787
-    esac
f88787
+if ! test -d "$esp_mountpoint" ; then
f88787
+    LogPrintError "Failed to create EFI Boot Manager entries (no ESP mountpoint directory $esp_mountpoint)"
f88787
+    return 1
f88787
 fi
f88787
 
f88787
-# For eMMC devices the trailing 'p' in the Disk value
f88787
-# (as in /dev/mmcblk0p that is derived from /dev/mmcblk0p1)
f88787
-# needs to be stripped (to get /dev/mmcblk0), otherwise the
f88787
-# efibootmgr call fails because of a wrong disk device name.
f88787
-# See also https://github.com/rear/rear/issues/2103
f88787
-if [[ $Disk = *'/mmcblk'+([0-9])p ]] ; then
f88787
-    Disk=${Disk%p}
f88787
-fi
f88787
+# Mount point inside the target system,
f88787
+# accounting for possible trailing slashes in TARGET_FS_ROOT
f88787
+esp_mountpoint_inside="${esp_mountpoint#${TARGET_FS_ROOT%%*(/)}}"
f88787
 
f88787
-# For NVMe devices the trailing 'p' in the Disk value
f88787
-# (as in /dev/nvme0n1p that is derived from /dev/nvme0n1p1)
f88787
-# needs to be stripped (to get /dev/nvme0n1), otherwise the
f88787
-# efibootmgr call fails because of a wrong disk device name.
f88787
-# See also https://github.com/rear/rear/issues/1564
f88787
-if [[ $Disk = *'/nvme'+([0-9])n+([0-9])p ]] ; then
f88787
-    Disk=${Disk%p}
f88787
+boot_efi_parts=$( find_partition "fs:$esp_mountpoint_inside" fs )
f88787
+if ! test "$boot_efi_parts" ; then
f88787
+    LogPrint "Unable to find ESP $esp_mountpoint_inside in layout"
f88787
+    LogPrint "Trying to determine device currently mounted at $esp_mountpoint as fallback"
f88787
+    boot_efi_dev="$( mount | grep "$esp_mountpoint" | awk '{print $1}' )"
f88787
+    if ! test "$boot_efi_dev" ; then
f88787
+        LogPrintError "Cannot create EFI Boot Manager entry (unable to find ESP $esp_mountpoint among mounted devices)"
f88787
+        return 1
f88787
+    fi
f88787
+    if test $(get_component_type "$boot_efi_dev") = part ; then
f88787
+        boot_efi_parts="$boot_efi_dev"
f88787
+    else
f88787
+        boot_efi_parts=$( find_partition "$boot_efi_dev" )
f88787
+    fi
f88787
+    if ! test "$boot_efi_parts" ; then
f88787
+        LogPrintError "Cannot create EFI Boot Manager entry (unable to find partition for $boot_efi_dev)"
f88787
+        return 1
f88787
+    fi
f88787
+    LogPrint "Using fallback EFI boot partition(s) $boot_efi_parts (unable to find ESP $esp_mountpoint_inside in layout)"
f88787
 fi
f88787
 
f88787
+local bootloader partition_block_device partition_number disk efipart
f88787
+
f88787
 # EFI\fedora\shim.efi
f88787
-BootLoader=$( echo $UEFI_BOOTLOADER | cut -d"/" -f4- | sed -e 's;/;\\;g' )
f88787
-LogPrint "Creating  EFI Boot Manager entry '$OS_VENDOR $OS_VERSION' for '$BootLoader' (UEFI_BOOTLOADER='$UEFI_BOOTLOADER')"
f88787
-Log efibootmgr --create --gpt --disk ${Disk} --part ${ParNr} --write-signature --label \"${OS_VENDOR} ${OS_VERSION}\" --loader \"\\${BootLoader}\"
f88787
-if efibootmgr --create --gpt --disk ${Disk} --part ${ParNr} --write-signature --label "${OS_VENDOR} ${OS_VERSION}" --loader "\\${BootLoader}" ; then
f88787
-    # ok, boot loader has been set-up - tell rear we are done using following var.
f88787
-    NOBOOTLOADER=''
f88787
-    return
f88787
-fi
f88787
+bootloader=$( echo $UEFI_BOOTLOADER | cut -d"/" -f4- | sed -e 's;/;\\;g' )
f88787
+
f88787
+for efipart in $boot_efi_parts ; do
f88787
+    # /dev/sda1 or /dev/mapper/vol34_part2 or /dev/mapper/mpath99p4
f88787
+    partition_block_device=$( get_device_name $efipart )
f88787
+    # 1 or 2 or 4 for the examples above
f88787
+    partition_number=$( get_partition_number $partition_block_device )
f88787
+    if ! disk=$( get_device_from_partition $partition_block_device $partition_number ) ; then
f88787
+        LogPrintError "Cannot create EFI Boot Manager entry for ESP $partition_block_device (unable to find the underlying disk)"
f88787
+        # do not error out - we may be able to locate other disks if there are more of them
f88787
+        continue
f88787
+    fi
f88787
+    LogPrint "Creating  EFI Boot Manager entry '$OS_VENDOR $OS_VERSION' for '$bootloader' (UEFI_BOOTLOADER='$UEFI_BOOTLOADER') "
f88787
+    Log efibootmgr --create --gpt --disk $disk --part $partition_number --write-signature --label \"${OS_VENDOR} ${OS_VERSION}\" --loader \"\\${bootloader}\"
f88787
+    if efibootmgr --create --gpt --disk $disk --part $partition_number --write-signature --label "${OS_VENDOR} ${OS_VERSION}" --loader "\\${bootloader}" ; then
f88787
+        # ok, boot loader has been set-up - continue with other disks (ESP can be on RAID)
f88787
+        NOBOOTLOADER=''
f88787
+    else
f88787
+        LogPrintError "efibootmgr failed to create EFI Boot Manager entry on $disk partition $partition_number (ESP $partition_block_device )"
f88787
+    fi
f88787
+done
f88787
 
f88787
-LogPrintError "efibootmgr failed to create EFI Boot Manager entry for '$BootLoader' (UEFI_BOOTLOADER='$UEFI_BOOTLOADER')"
f88787
+is_true $NOBOOTLOADER || return 0
f88787
+LogPrintError "efibootmgr failed to create EFI Boot Manager entry for '$bootloader' (UEFI_BOOTLOADER='$UEFI_BOOTLOADER')"
f88787
+return 1
f88787
diff --git a/usr/share/rear/lib/layout-functions.sh b/usr/share/rear/lib/layout-functions.sh
f88787
index 54ddb50f..cdd81a14 100644
f88787
--- a/usr/share/rear/lib/layout-functions.sh
f88787
+++ b/usr/share/rear/lib/layout-functions.sh
f88787
@@ -302,12 +302,20 @@ get_child_components() {
f88787
     done
f88787
 }
f88787
 
f88787
-# Return all ancestors of component $1 [ of type $2 ]
f88787
+# Return all ancestors of component $1 [ of type $2 [ skipping types $3 during resolution ] ]
f88787
 get_parent_components() {
f88787
-    declare -a ancestors devlist
f88787
-    declare current child parent
f88787
+    declare -a ancestors devlist ignoretypes
f88787
+    declare current child parent parenttype
f88787
 
f88787
     devlist=( "$1" )
f88787
+    if [[ "$3" ]] ; then
f88787
+        # third argument should, if present, be a space-separated list
f88787
+        # of types to ignore when walking up the dependency tree.
f88787
+        # Convert it to array
f88787
+        ignoretypes=( $3 )
f88787
+    else
f88787
+        ignoretypes=()
f88787
+    fi
f88787
     while (( ${#devlist[@]} )) ; do
f88787
         current=${devlist[0]}
f88787
 
f88787
@@ -318,6 +326,13 @@ get_parent_components() {
f88787
                 if IsInArray "$parent" "${ancestors[@]}" ; then
f88787
                     continue
f88787
                 fi
f88787
+                ### ...test if parent is of a correct type if requested...
f88787
+                if [[ ${#ignoretypes[@]} -gt 0 ]] ; then
f88787
+                    parenttype=$(get_component_type "$parent")
f88787
+                    if IsInArray "$parenttype" "${ignoretypes[@]}" ; then
f88787
+                        continue
f88787
+                    fi
f88787
+                fi
f88787
                 ### ...and add them to the list
f88787
                 devlist+=( "$parent" )
f88787
                 ancestors+=( "$parent" )
f88787
@@ -345,22 +360,24 @@ get_parent_components() {
f88787
 }
f88787
 
f88787
 # find_devices <other>
f88787
+# ${2+"$2"} in the following functions ensures that $2 gets passed down quoted if present
f88787
+# and ignored if not present
f88787
 # Find the disk device(s) component $1 resides on.
f88787
 find_disk() {
f88787
-    get_parent_components "$1" "disk"
f88787
+    get_parent_components "$1" "disk" ${2+"$2"}
f88787
 }
f88787
 
f88787
 find_multipath() {
f88787
-    get_parent_components "$1" "multipath"
f88787
+    get_parent_components "$1" "multipath" ${2+"$2"}
f88787
 }
f88787
 
f88787
 find_disk_and_multipath() {
f88787
-    find_disk "$1"
f88787
-    is_true "$AUTOEXCLUDE_MULTIPATH" || find_multipath "$1"
f88787
+    find_disk "$1" ${2+"$2"}
f88787
+    is_true "$AUTOEXCLUDE_MULTIPATH" || find_multipath "$1" ${2+"$2"}
f88787
 }
f88787
 
f88787
 find_partition() {
f88787
-    get_parent_components "$1" "part"
f88787
+    get_parent_components "$1" "part" ${2+"$2"}
f88787
 }
f88787
 
f88787
 # The get_partition_number function
f88787
@@ -413,6 +430,54 @@ get_partition_number() {
f88787
     echo $partition_number
f88787
 }
f88787
 
f88787
+# Extract the underlying device name from the full partition device name.
f88787
+# Underlying device may be a disk, a multipath device or other devices that can be partitioned.
f88787
+# Should we use the information in $LAYOUT_DEPS, like get_parent_component does,
f88787
+# instead of string munging?
f88787
+function get_device_from_partition() {
f88787
+    local partition_block_device
f88787
+    local device
f88787
+    local partition_number
f88787
+
f88787
+    partition_block_device=$1
f88787
+    test -b "$partition_block_device" || BugError "get_device_from_partition called with '$partition_block_device' that is no block device"
f88787
+    partition_number=${2-$(get_partition_number $partition_block_device )}
f88787
+    # /dev/sda or /dev/mapper/vol34_part or /dev/mapper/mpath99p or /dev/mmcblk0p
f88787
+    device=${partition_block_device%$partition_number}
f88787
+
f88787
+    # Strip trailing partition remainders like '_part' or '-part' or 'p'
f88787
+    # if we have 'mapper' in disk device name:
f88787
+    if [[ ${partition_block_device/mapper//} != $partition_block_device ]] ; then
f88787
+        # we only expect mpath_partX or mpathpX or mpath-partX
f88787
+        case $device in
f88787
+            (*p)     device=${device%p} ;;
f88787
+            (*-part) device=${device%-part} ;;
f88787
+            (*_part) device=${device%_part} ;;
f88787
+            (*)      Log "Unsupported kpartx partition delimiter for $partition_block_device"
f88787
+        esac
f88787
+    fi
f88787
+
f88787
+    # For eMMC devices the trailing 'p' in the $device value
f88787
+    # (as in /dev/mmcblk0p that is derived from /dev/mmcblk0p1)
f88787
+    # needs to be stripped (to get /dev/mmcblk0), otherwise the
f88787
+    # efibootmgr call fails because of a wrong disk device name.
f88787
+    # See also https://github.com/rear/rear/issues/2103
f88787
+    if [[ $device = *'/mmcblk'+([0-9])p ]] ; then
f88787
+        device=${device%p}
f88787
+    fi
f88787
+
f88787
+    # For NVMe devices the trailing 'p' in the $device value
f88787
+    # (as in /dev/nvme0n1p that is derived from /dev/nvme0n1p1)
f88787
+    # needs to be stripped (to get /dev/nvme0n1), otherwise the
f88787
+    # efibootmgr call fails because of a wrong disk device name.
f88787
+    # See also https://github.com/rear/rear/issues/1564
f88787
+    if [[ $device = *'/nvme'+([0-9])n+([0-9])p ]] ; then
f88787
+        device=${device%p}
f88787
+    fi
f88787
+
f88787
+    test -b "$device" && echo $device
f88787
+}
f88787
+
f88787
 # Returns partition start block or 'unknown'
f88787
 # sda/sda1 or
f88787
 # dm-XX