Identify UUID of target system even if it's SWRAID or a mountpoint
[grml-debootstrap.git] / grml-debootstrap
index 9eb6b9e..135b660 100755 (executable)
@@ -463,7 +463,7 @@ if [ -n "$CONFIGFILE" ] ; then
 fi
 # }}}
 
-# backwards compability checks {{{
+# backwards compatibility checks {{{
 if [ -n "$GROOT" ] ; then
    eerror "Error: you seem to have \$GROOT configured."
    eerror "This variable is no longer supported, please visit the"
@@ -774,7 +774,7 @@ format_efi_partition() {
   if fsck.vfat -bn "$EFI" >/dev/null; then
     einfo "EFI partition $EFI seems to have a FAT filesystem, not modifying." ; eend 0
   else
-    einfo "EFI partition $EFI doesn't seem to be formated, creating filesystem."
+    einfo "EFI partition $EFI doesn't seem to be formatted, creating filesystem."
     mkfs.fat -F32 -n "EFI System Partition" "$EFI"
     RC=$?
     if [ $RC -eq 0 ] ; then
@@ -1113,14 +1113,52 @@ mkfs() {
     # 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
 }
 # }}}
 
+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
@@ -1660,7 +1698,8 @@ remove_configs() {
 # }}}
 
 # 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