Skip to content

Commit

Permalink
Move partition->device translation into a function
Browse files Browse the repository at this point in the history
The new function is called get_device_from_partition() and resides in
layout-functions.sh.

Also change variable names to conform to ReaR coding style:
local variables should be all lowercase.

Another place that could make use of the new function is
usr/share/rear/finalize/Linux-i386/610_EFISTUB_run_efibootmgr.sh
  • Loading branch information
pcahyna committed May 5, 2021
1 parent ec7fb6d commit fae98be
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
34 changes: 1 addition & 33 deletions usr/share/rear/finalize/Linux-i386/670_run_efibootmgr.sh
Expand Up @@ -32,39 +32,7 @@ BootEfiDev="$( mount | grep "$esp_mountpoint" | awk '{print $1}' )"
Dev=$( get_device_name $BootEfiDev )
# 1 (must anyway be a low nr <9)
ParNr=$( get_partition_number $Dev )
# /dev/sda or /dev/mapper/vol34_part or /dev/mapper/mpath99p or /dev/mmcblk0p
Disk=$( echo ${Dev%$ParNr} )

# Strip trailing partition remainders like '_part' or '-part' or 'p'
# if we have 'mapper' in disk device name:
if [[ ${Dev/mapper//} != $Dev ]] ; then
# we only expect mpath_partX or mpathpX or mpath-partX
case $Disk in
(*p) Disk=${Disk%p} ;;
(*-part) Disk=${Disk%-part} ;;
(*_part) Disk=${Disk%_part} ;;
(*) Log "Unsupported kpartx partition delimiter for $Dev"
esac
fi

# For eMMC devices the trailing 'p' in the Disk value
# (as in /dev/mmcblk0p that is derived from /dev/mmcblk0p1)
# needs to be stripped (to get /dev/mmcblk0), otherwise the
# efibootmgr call fails because of a wrong disk device name.
# See also https://github.com/rear/rear/issues/2103
if [[ $Disk = *'/mmcblk'+([0-9])p ]] ; then
Disk=${Disk%p}
fi

# For NVMe devices the trailing 'p' in the Disk value
# (as in /dev/nvme0n1p that is derived from /dev/nvme0n1p1)
# needs to be stripped (to get /dev/nvme0n1), otherwise the
# efibootmgr call fails because of a wrong disk device name.
# See also https://github.com/rear/rear/issues/1564
if [[ $Disk = *'/nvme'+([0-9])n+([0-9])p ]] ; then
Disk=${Disk%p}
fi

Disk=$( get_device_from_partition $Dev $ParNr )
# EFI\fedora\shim.efi
BootLoader=$( echo $UEFI_BOOTLOADER | cut -d"/" -f4- | sed -e 's;/;\\;g' )
LogPrint "Creating EFI Boot Manager entry '$OS_VENDOR $OS_VERSION' for '$BootLoader' (UEFI_BOOTLOADER='$UEFI_BOOTLOADER')"
Expand Down
49 changes: 48 additions & 1 deletion usr/share/rear/lib/layout-functions.sh
Expand Up @@ -316,7 +316,7 @@ get_child_components() {
done
}

# Return all ancestors of component $1 [ of type $2 ]
# Return all ancestors of component $1 [ of type $2 [ skipping types $3 during resolution ] ]
get_parent_components() {
declare -a ancestors devlist
declare current child parent
Expand Down Expand Up @@ -427,6 +427,53 @@ get_partition_number() {
echo $partition_number
}

# Extract the underlying device name from the full partition device name.
# Underlying device may be a disk, a multipath device or other devices that can be partitioned.
# Should we use the information in $LAYOUT_DEPS, like get_parent_component does,
# instead of string munging?
function get_device_from_partition() {
local partition_block_device
local device
local partition_number

partition_block_device=$1
partition_number=${2-$(get_partition_number $partition_block_device )}
# /dev/sda or /dev/mapper/vol34_part or /dev/mapper/mpath99p or /dev/mmcblk0p
device=${partition_block_device%$partition_number}

# Strip trailing partition remainders like '_part' or '-part' or 'p'
# if we have 'mapper' in disk device name:
if [[ ${partition_block_device/mapper//} != $partition_block_device ]] ; then
# we only expect mpath_partX or mpathpX or mpath-partX
case $device in
(*p) device=${device%p} ;;
(*-part) device=${device%-part} ;;
(*_part) device=${device%_part} ;;
(*) Log "Unsupported kpartx partition delimiter for $partition_block_device"
esac
fi

# For eMMC devices the trailing 'p' in the $device value
# (as in /dev/mmcblk0p that is derived from /dev/mmcblk0p1)
# needs to be stripped (to get /dev/mmcblk0), otherwise the
# efibootmgr call fails because of a wrong disk device name.
# See also https://github.com/rear/rear/issues/2103
if [[ $device = *'/mmcblk'+([0-9])p ]] ; then
device=${device%p}
fi

# For NVMe devices the trailing 'p' in the $device value
# (as in /dev/nvme0n1p that is derived from /dev/nvme0n1p1)
# needs to be stripped (to get /dev/nvme0n1), otherwise the
# efibootmgr call fails because of a wrong disk device name.
# See also https://github.com/rear/rear/issues/1564
if [[ $device = *'/nvme'+([0-9])n+([0-9])p ]] ; then
device=${device%p}
fi

echo $device
}

# Returns partition start block or 'unknown'
# sda/sda1 or
# dm-XX
Expand Down

0 comments on commit fae98be

Please sign in to comment.