Blame SOURCES/rear-bz1945869.patch

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