Identify UUID of target system even if it's SWRAID or a mountpoint
authorMichael Prokop <mika@grml.org>
Sat, 2 Dec 2017 08:25:19 +0000 (09:25 +0100)
committerMichael Prokop <mika@grml.org>
Fri, 8 Dec 2017 11:47:57 +0000 (12:47 +0100)
The `blkid -o udev` doesn't work for mountpoints.
Also we can't rely on setting the TARGET_UUID within
mkfs, as this might not even get executed.

Closes #100
Thanks: hex2a for reporting and testing

grml-debootstrap

index a21400f..135b660 100755 (executable)
@@ -1113,14 +1113,52 @@ mkfs() {
     # race conditions :-/
     sleep 2
 
     # race conditions :-/
     sleep 2
 
-    eval "$(blkid -o udev "$TARGET" 2>/dev/null)"
-    [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
-
     eend $RC
   fi
 }
 # }}}
 
     eend $RC
   fi
 }
 # }}}
 
+identify_target_uuid() {
+  local device="$1"
+
+  if ! [ -b "$device" ] ; then
+    return 1
+  fi
+
+  eval "$(blkid -o udev "$1" 2>/dev/null)"
+
+  if [ -n "$ID_FS_UUID" ] ; then
+    echo "$ID_FS_UUID"
+  else
+    return 1
+  fi
+}
+
+mountpoint_to_blockdevice() {
+  TARGET_UUID=''
+
+  TARGET_UUID=$(identify_target_uuid "$TARGET" 2>/dev/null || true)
+  if [ -n "$TARGET_UUID" ] ; then
+    einfo "Identified UUID $TARGET_UUID for $TARGET"
+    return 0
+  fi
+
+  # $TARGET might be a mountpoint and not a blockdevice, search for according entry
+  for file in /sys/block/*/*/dev ; do
+    if grep -q "^$(mountpoint -d "${TARGET}")$" "$file" ; then
+      local dev
+      dev="${file%/dev}"
+      dev="/dev/${dev##*/}"
+      TARGET_UUID=$(identify_target_uuid "$dev" 2>/dev/null || true)
+
+      if [ -n "$TARGET_UUID" ] ; then
+        einfo "Identified UUID $TARGET_UUID for $TARGET (via $file)"
+        return 0
+      fi
+    fi
+  done
+}
+
 # modify filesystem settings {{{
 tunefs() {
   if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
 # modify filesystem settings {{{
 tunefs() {
   if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
@@ -1660,7 +1698,8 @@ remove_configs() {
 # }}}
 
 # now execute all the functions {{{
 # }}}
 
 # now execute all the functions {{{
-for i in format_efi_partition prepare_vm mkfs tunefs mount_target debootstrap_system \
+for i in format_efi_partition prepare_vm mkfs tunefs \
+         mount_target mountpoint_to_blockdevice debootstrap_system \
          preparechroot execute_pre_scripts chrootscript execute_post_scripts \
          remove_configs umount_chroot finalize_vm fscktool ; do
     if stage "${i}" ; then
          preparechroot execute_pre_scripts chrootscript execute_post_scripts \
          remove_configs umount_chroot finalize_vm fscktool ; do
     if stage "${i}" ; then