@@ -1298,34 +1298,28 @@ find_lvm_vg_name() {
12981298# GPT-partitioned disk.
12991299is_gpt_bios_grub () {
13001300 TRACE_FUNC
1301- DEBUG " is_gpt_bios_grub: PART_DEV=$1 "
1302-
1303- local PART_DEV=" $1 " DEVICE NUMBER
1304-
1305- # Figure out the partitioned device containing this device (if there is
1306- # one) from /sys/class/block.
1307- local DEVICE_MATCHES=(" /sys/class/block/" * " /$( basename " $PART_DEV " ) " )
1308- DEBUG " is_gpt_bios_grub: DEVICE_MATCHES=${DEVICE_MATCHES[*]} "
1309-
1310- DEVICE=" $( echo " ${DEVICE_MATCHES[0]} " | cut -d/ -f5) "
1311- if [ " ${# DEVICE_MATCHES[@]} " -ne 1 ] || [ " $DEVICE " = " *" ]; then
1312- DEBUG " is_gpt_bios_grub: ambiguous DEVICE, returning false"
1313- return 0
1314- fi
1315-
1316- # Extract the partition number
1317- if ! [[ $( basename " $PART_DEV " ) =~ ([0-9]+)$ ]]; then
1318- DEBUG " is_gpt_bios_grub: cannot parse partition number"
1319- return 0 # Can't figure out the partition number
1301+ # $1 is the device path being tested (e.g. /dev/vda1)
1302+ local PART_DEV=" $1 "
1303+ DEBUG " PART_DEV=$PART_DEV "
1304+
1305+ # identify the base device and partition number with a regex
1306+ local partname device number
1307+ partname=$( basename " $PART_DEV " )
1308+
1309+ # match nvme style (p<num>) or normal (digit suffix)
1310+ if [[ $partname =~ ^(.+? )(p? [0-9]+)$ ]]; then
1311+ device=" ${BASH_REMATCH[1]} "
1312+ number=" ${BASH_REMATCH[2]} "
1313+ number=" ${number# p} "
1314+ else
1315+ DEBUG " cannot parse partition name '$partname '"
1316+ return 0 # not a recognised partition
13201317 fi
13211318
1322- NUMBER=" ${BASH_REMATCH[1]} "
1323- DEBUG " is_gpt_bios_grub: DEVICE=$DEVICE NUMBER=$NUMBER "
1319+ DEBUG " DEVICE=$device NUMBER=$number "
13241320
1325- # Now we know the device and partition number, get the type. This is
1326- # specific to GPT disks, MBR disks are shown differently by fdisk.
1327- TRACE " $PART_DEV is partition $NUMBER of $DEVICE "
1328- if [ " $( fdisk -l " /dev/$DEVICE " 2> /dev/null | awk ' $1 == ' " $NUMBER " ' {print $5}' ) " == grub ]; then
1321+ # GPT disks list type in column 5; fall through to 1 otherwise
1322+ if [ " $( fdisk -l " /dev/$device " 2> /dev/null | awk ' $1 == ' " $number " ' {print $5}' ) " == grub ]; then
13291323 return 0
13301324 fi
13311325 return 1
@@ -1354,9 +1348,17 @@ mount_possible_boot_device() {
13541348
13551349 # Skip bios-grub partitions on GPT disks, LUKS partitions, and LVM PVs,
13561350 # we can't mount these as /boot.
1357- if is_gpt_bios_grub " $BOOT_DEV " || cryptsetup isLuks " $BOOT_DEV " ||
1358- find_lvm_vg_name " $BOOT_DEV " > /dev/null; then
1359- TRACE " $BOOT_DEV is not a mountable partition for /boot"
1351+ # Skip partitions we definitely can't mount for /boot. Log each reason.
1352+ if is_gpt_bios_grub " $BOOT_DEV " ; then
1353+ DEBUG " $BOOT_DEV is GPT BIOS/GRUB partition, skipping"
1354+ return 1
1355+ fi
1356+ if cryptsetup isLuks " $BOOT_DEV " ; then
1357+ DEBUG " $BOOT_DEV is a LUKS volume, skipping"
1358+ return 1
1359+ fi
1360+ if find_lvm_vg_name " $BOOT_DEV " > /dev/null; then
1361+ DEBUG " $BOOT_DEV is an LVM PV, skipping"
13601362 return 1
13611363 fi
13621364
@@ -1388,6 +1390,15 @@ mount_possible_boot_device() {
13881390detect_boot_device () {
13891391 TRACE_FUNC
13901392 local devname
1393+ DEBUG " CONFIG_BOOT_DEV=$CONFIG_BOOT_DEV "
1394+ # If /boot is already mounted and appears to be a valid boot tree, just
1395+ # use its device. This avoids remount churn and makes the later lookup
1396+ # fast.
1397+ mounted_boot_dev=" $( awk ' $2=="/boot" {print $1; exit}' /proc/mounts) "
1398+ if [ -n " $mounted_boot_dev " ] && ls -d /boot/grub* > /dev/null 2>&1 ; then
1399+ CONFIG_BOOT_DEV=" $mounted_boot_dev "
1400+ return 0
1401+ fi
13911402 # unmount /boot to be safe
13921403 cd / && umount /boot 2> /dev/null
13931404
@@ -1425,6 +1436,7 @@ detect_boot_device() {
14251436
14261437 # no valid boot device found
14271438 echo " Unable to locate /boot files on any mounted disk"
1439+ DEBUG " detect_boot_device: failed to find a bootable device"
14281440 return 1
14291441}
14301442
0 commit comments