From 05ed1a4c5039c78a991d55fd93296e620c6bf900 Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Sat, 2 Dec 2017 09:25:19 +0100 Subject: [PATCH] Identify UUID of target system even if it's SWRAID or a mountpoint 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 | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/grml-debootstrap b/grml-debootstrap index a21400f..135b660 100755 --- a/grml-debootstrap +++ b/grml-debootstrap @@ -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 -- 2.1.4