diff options
-rwxr-xr-x | modules.d/dnbd3-rootfs/hooks/s3-dnbd3root.sh | 76 |
1 files changed, 57 insertions, 19 deletions
diff --git a/modules.d/dnbd3-rootfs/hooks/s3-dnbd3root.sh b/modules.d/dnbd3-rootfs/hooks/s3-dnbd3root.sh index 6c62480a..e79938f6 100755 --- a/modules.d/dnbd3-rootfs/hooks/s3-dnbd3root.sh +++ b/modules.d/dnbd3-rootfs/hooks/s3-dnbd3root.sh @@ -111,32 +111,70 @@ if [ -z "$read_only_device" ]; then fi # endregion -# region find system partition within dnbd3 image -if [ -z "$SLX_SYSTEM_PARTITION_PREPARATION_SCRIPT" ]; then - # Find requested root partition - default to SLX_SYS label - read_only_partition="$( slx-tools dev_find_partitions \ - "$read_only_device" "${SLX_SYSTEM_PARTITION_IDENTIFIER:-SLX_SYS}" )" - if [ -z "$SLX_SYSTEM_PARTITION_IDENTIFIER" ]; then - if [ -n "$read_only_partition" ]; then - # Defaulting to SLX_SYS worked - echo "SLX_SYSTEM_PARTITION_IDENTIFIER='SLX_SYS'" >> /etc/openslx - else - # Aussume there is no partition table - read_only_partition="$read_only_device" +# region find system partition within image +if [[ "$SLX_SYSTEM_PARTITION_IDENTIFIER" =~ ^\+[0-9]+$ ]]; then + # Partition number, e.g. +2 for second partition + # + num="${SLX_SYSTEM_PARTITION_IDENTIFIER#+}" + if [ -b "${read_only_device}p${num}" ]; then + read_only_partition="${read_only_device}p${num}" + elif [ -b "${read_only_device}${num}" ]; then + read_only_partition="${read_only_device}${num}" + fi +else + # Find requested root partition by MBRID, GPT label, or GPT type - default to SLX_SYS label + # + declare -a parts + parts=( "${read_only_device}"?* ) + if [ -b "${parts[0]}" ]; then + # There is at least one partition on the device, scan + mapfile -t parts < <( slx-tools dev_find_partitions \ + "$read_only_device" "${SLX_SYSTEM_PARTITION_IDENTIFIER:-SLX_SYS}" ) + if [ "${#parts[@]}" = 1 ]; then + # One match, perfect + read_only_partition="${parts[0]}" + echo "Found read-only partition by identifier: $read_only_partition" + elif [ "${#parts[@]}" = 0 ]; then + # Nothing found, scan all partitions. This will include the device itself. + parts=( "$read_only_device"* ) fi + # If we found 2 or more matching partitions, they'll be probed below + else + # No partitions on device - check if it's a file-system + parts=( "$read_only_device" ) fi +fi -else - eval "$SLX_SYSTEM_PARTITION_PREPARATION_SCRIPT" +if [ -z "$read_only_partition" ]; then + # Do a scan + echo "Don't know which partition to use, trying all candidates (${parts[*]})" + p="/tmp/mounttest" + mkdir -p "$p" + for part in "${parts[@]}"; do + if mount -v -o ro "$part" "$p"; then + # See if it looks like a system partition + if [ -x "$p/sbin/init" ] || [ -x "$p/lib/systemd/systemd" ]; then + umount -lf "$p" + echo "Found init on $part, will try to boot off of it." + read_only_partition="$part" + break + fi + umount -lf "$p" + fi + done fi -if [[ -z "$read_only_partition" ]]; then - echo "Failed to find unique device with identifier" \ - "\"${SLX_SYSTEM_PARTITION_IDENTIFIER}\"; matched devices:" \ - "\"${read_only_partition}\"" +# endregion + +if [ -z "$read_only_partition" ]; then + echo "Failed to find bootable partition" + exit 1 +fi +if ! [ -b "$read_only_partition" ]; then + echo "Bootable partition $read_only_partition does not exist, or is not a block device" exit 1 fi + echo "Using read-only partition: $read_only_partition" -# endregion # region add rw layer to dnbd3 image # don't be fooled to think we are done, the next part is crucial |