Refactor dmsetup handling
authorColin Watson <cjwatson@debian.org>
Wed, 18 Jan 2017 12:18:54 +0000 (12:18 +0000)
committerColin Watson <cjwatson@debian.org>
Thu, 19 Jan 2017 21:01:49 +0000 (21:01 +0000)
Move common code into a do_dmsetup function, and use it from
linux_mount_boot as well.

common.sh
linux-boot-probes/common/50mounted-tests
os-probes/common/50mounted-tests

index 1b9f8a3..f17a72c 100644 (file)
--- a/common.sh
+++ b/common.sh
@@ -210,15 +210,37 @@ find_uuid () {
        fi
 }
 
-# Sets $mountboot as output variable.  (We do this rather than requiring a
-# subshell so that we can run ro_partition without the cleanup trap firing
-# when the subshell exits.)
+do_dmsetup () {
+       local prefix partition dm_device partition_name size_p
+       prefix="$1"
+       partition="$2"
+       dm_device=
+
+       if type dmsetup >/dev/null 2>&1 && \
+          type blockdev >/dev/null 2>&1; then
+               partition_name="osprober-linux-${partition##*/}"
+               dm_device="/dev/mapper/$partition_name"
+               size_p=$(blockdev --getsize $partition )
+               if [ -e "$dm_device" ]; then
+                       error "$dm_device already exists"
+                       dm_device=
+               else
+                       debug "creating device mapper device $dm_device"
+                       echo "0 $size_p linear $partition 0" | dmsetup create -r $partition_name
+               fi
+       fi
+       echo "$dm_device"
+}
+
+# Sets $mountboot and $dm_device as output variables.  This is very messy,
+# but POSIX shell isn't really up to the task of doing it more cleanly.
 linux_mount_boot () {
        partition="$1"
        tmpmnt="$2"
 
        bootpart=""
        mounted=""
+       dm_device=""
        if [ -e "$tmpmnt/etc/fstab" ]; then
                # Try to mount any /boot partition.
                bootmnt=$(parsefstab < "$tmpmnt/etc/fstab" | grep " /boot ") || true
@@ -289,11 +311,18 @@ linux_mount_boot () {
                                        if type grub-mount >/dev/null 2>&1 && \
                                           grub-mount "$boottomnt" "$tmpmnt/boot" 2>/dev/null; then
                                                mounted=1
+                                       elif dm_device="$(do_dmsetup osprober-linux "$boottomnt")" && [ "$dm_device" ]; then
+                                               if mountinfo=`mount -o ro "$dm_device" "$tmpmnt/boot" -t "$3"`; then
+                                                       debug "mounted as $3 filesystem"
+                                                       mounted=1
+                                               else
+                                                       error "failed to mount $dm_device on $tmpmnt/boot: $mountinfo"
+                                               fi
                                        else
                                                ro_partition "$boottomnt"
                                                if mount -o ro "$boottomnt" "$tmpmnt/boot" -t "$3"; then
-                                                       mounted=1
-                                               else
+                                                       mounted=1
+                                               else
                                                        error "failed to mount $boottomnt on $tmpmnt/boot"
                                                fi
                                        fi
index 3a58456..407c9c4 100755 (executable)
@@ -10,11 +10,12 @@ do_unmount() {
                        warn "failed to umount $tmpmnt"
                fi
        fi
-       if [ -e "$dm_device" ]
-       then
-               debug "remove device mapper device $dm_device"
-               dmsetup remove $dm_device
-       fi
+       for dm_device in $dm_devices; do
+               if [ -e "$dm_device" ]; then
+                       debug "remove device mapper device $dm_device"
+                       dmsetup remove $dm_device
+               fi
+       done
        rmdir "$tmpmnt" || true
 }
 
@@ -53,52 +54,45 @@ if [ ! -d "$tmpmnt" ]; then
 fi
 
 mounted=
+dm_devices=
 if type grub-mount >/dev/null 2>&1 && \
    type grub-probe >/dev/null 2>&1 && \
    grub-mount "$partition" "$tmpmnt" 2>/dev/null; then
        mounted=1
        type="$(grub-probe -d "$partition" -t fs)"
        [ "$type" ] || type=fuseblk
+elif dm_device="$(do_dmsetup osprober-linux "$partition")" && \
+     [ "$dm_device" ]; then
+       dm_devices="$dm_device"
+       for type in $types; do
+               if mountinfo=`mount -o ro -t "$type" "$dm_device" "$tmpmnt" 2>&1`; then
+                       debug "mounted as $type filesystem"
+                       mounted=1
+                       break
+               else
+                       debug "mounting $dm_device ($partition) as $type failed: $mountinfo"
+               fi
+       done
 else
-       if type dmsetup >/dev/null 2>&1 && \
-          type blockdev >/dev/null 2>&1; then
-               partition_name="osprober-linux-${partition##*/}"
-               dm_device="/dev/mapper/$partition_name"
-               size_p=$(blockdev --getsize $partition )
-               if [ -e "$dm_device" ]; then
-                       error "$dm_device already exists"
-                       dm_device=
+       ro_partition "$partition"
+       for type in $types; do
+               if mountinfo=`mount -o ro -t "$type" "$partition" "$tmpmnt" 2>&1`; then
+                       debug "mounted as $type filesystem"
+                       mounted=1
+                       break
                else
-                       debug "creating device mapper device $dm_device"
-                       echo "0 $size_p linear $partition 0" | dmsetup create -r $partition_name
-                       for type in $types; do
-                               if mountinfo=`mount -o ro -t "$type" "$dm_device" "$tmpmnt" 2>&1`; then
-                                       debug "mounted as $type filesystem"
-                                       mounted=1
-                                       break
-                               else
-                                       debug "mounting $dm_device ($partition) as $type failed: $mountinfo"
-                               fi
-                       done
+                       debug "mounting $partition as $type failed: $mountinfo"
                fi
-       else
-               ro_partition "$partition"
-               for type in $types; do
-                       if mountinfo=`mount -o ro -t "$type" "$partition" "$tmpmnt" 2>&1`; then
-                               debug "mounted as $type filesystem"
-                               mounted=1
-                               break
-                       else
-                               debug "mounting $partition as $type failed: $mountinfo"
-                       fi
-               done
-       fi
+       done
 fi
 
 if [ "$mounted" ]; then
        linux_mount_boot "$partition" "$tmpmnt"
        bootpart="${mountboot%% *}"
        mounted="${mountboot#* }"
+       if [ "$dm_device" ]; then
+               dm_devices="$dm_device $dm_devices"
+       fi
 
        for test in /usr/lib/linux-boot-probes/mounted/*; do
                if [ -f "$test" ] && [ -x "$test" ]; then
index c65feef..51ee254 100755 (executable)
@@ -71,40 +71,28 @@ if type grub-mount >/dev/null 2>&1 && \
                debug "mounted using GRUB, but unknown filesystem?"
                type=fuseblk
        fi
+elif dm_device="$(do_dmsetup osprober "$partition")" && \
+     [ "$dm_device" ]; then
+       for type in $types $delaytypes; do
+               if mountinfo=`mount -o ro -t "$type" "$dm_device" "$tmpmnt" 2>&1`; then
+                       debug "mounted as $type filesystem"
+                       mounted=1
+                       break
+               else
+                       debug "mounting $dm_device ($partition) as $type failed: $mountinfo"
+               fi
+       done
 else
-       if type dmsetup >/dev/null 2>&1 && \
-          type blockdev >/dev/null 2>&1; then
-               partition_name="osprober-${partition##*/}"
-               dm_device="/dev/mapper/$partition_name"
-               size_p=$(blockdev --getsize $partition )
-               if [ -e "$dm_device" ]; then
-                       error "$dm_device already exists"
-                       dm_device=
+       ro_partition "$partition"
+       for type in $types $delaytypes; do
+               if mountinfo=`mount -o ro -t "$type" "$partition" "$tmpmnt" 2>&1`; then
+                       debug "mounted as $type filesystem"
+                       mounted=1
+                       break
                else
-                       debug "creating device mapper device $dm_device"
-                       echo "0 $size_p linear $partition 0" | dmsetup create -r $partition_name
-                       for type in $types $delaytypes; do
-                               if mountinfo=`mount -o ro -t "$type" "$dm_device" "$tmpmnt" 2>&1`; then
-                                       debug "mounted as $type filesystem"
-                                       mounted=1
-                                       break
-                               else
-                                       debug "mounting $dm_device ($partition) as $type failed: $mountinfo"
-                               fi
-                       done
+                       debug "mounting $partition as $type failed: $mountinfo"
                fi
-       else
-               ro_partition "$partition"
-               for type in $types $delaytypes; do
-                       if mountinfo=`mount -o ro -t "$type" "$partition" "$tmpmnt" 2>&1`; then
-                               debug "mounted as $type filesystem"
-                               mounted=1
-                               break
-                       else
-                               debug "mounting $partition as $type failed: $mountinfo"
-                       fi
-               done
-       fi
+       done
 fi
 
 if [ "$mounted" ]; then