etc/function: add detect_boot_device()

Add function to detect boot device. Start by checking
CONFIG_BOOT_DEV, then iterate thru all bootable partitions.
Check if partition is mountable, contains grub directory.

Update CONFIG_BOOT_DEV and mount on /boot if successful.

Signed-off-by: Matt DeVillier <matt.devillier@puri.sm>
This commit is contained in:
Matt DeVillier 2019-08-19 17:07:22 -05:00
parent c5999d9b12
commit 4f54a97cf2
No known key found for this signature in database
GPG Key ID: 2BBB776A35B978FD

View File

@ -293,3 +293,50 @@ update_checksums()
# switch back to ro mode
mount -o ro,remount /boot
}
# detect and set /boot device
# mount /boot if successful
detect_boot_device()
{
# unmount /boot to be safe
umount /boot 2>/dev/null
# check $CONFIG_BOOT_DEV if set/valid
if [ -e "$CONFIG_BOOT_DEV" ]; then
mount -o ro $CONFIG_BOOT_DEV /boot >/dev/null 2>&1
if [[ $? && -d /boot/grub ]]; then
# CONFIG_BOOT_DEV is valid device and contains an installed OS
return 0
fi
fi
# generate list of possible boot devices
fdisk -l | grep "Disk" | cut -f2 -d " " | cut -f1 -d ":" > /tmp/disklist
# filter out extraneous options
> /tmp/boot_device_list
for i in `cat /tmp/disklist`; do
# remove block device from list if numeric partitions exist, since not bootable
let DEV_NUM_PARTITIONS=`ls -1 $i* | wc -l`-1
if [ ${DEV_NUM_PARTITIONS} -eq 0 ]; then
echo $i >> /tmp/boot_device_list
else
ls $i* | tail -${DEV_NUM_PARTITIONS} >> /tmp/boot_device_list
fi
done
# iterate thru possible options and check for grub dir
for i in `cat /tmp/boot_device_list`; do
umount /boot 2>/dev/null
mount -o ro $i /boot >/dev/null 2>&1
if [[ $? && -d /boot/grub ]]; then
CONFIG_BOOT_DEV="$i"
return 0
fi
done
# no valid boot device found
echo "Unable to locate /boot files on any mounted disk"
umount /boot 2>/dev/null
return 1
}