Skip to content

Commit 14fa71c

Browse files
authored
add support for detached LUKS header and UUID fallbacks (#378)
Two hard failures prevented 41-snapshots-btrfs from generating a snapshot submenu when the root LUKS header is detached and cryptdevice= uses a /dev/disk/by-id path: * grub-probe --target=fs_uuid aborted on detached headers. * grep-based extraction of UUID from GRUB_CMDLINE_LINUX_DEFAULT failed when cryptdevice= did not contain “UUID=…”. This patch: 1. Wraps grub-probe in a try/blkid/lsblk cascade that always returns the filesystem UUID or prints a clear error. 2. Replaces the fixed “cryptomount -u $(grep …UUID=…)” line with logic that: • accepts both UUID=… and /dev/disk/by-id/… syntaxes, • resolves paths to a canonical UUID with blkid, • emits ‘cryptomount -u <uuid>’ when possible, • falls back to ‘cryptomount -a’ only if no UUID can be extracted. 3. Keeps the previous behavior unchanged for unencrypted systems or for installations that already worked. Result: snapshot menu is produced and boots correctly on standard (setup with inline header), detached-header, and by-id configurations; no regression for existing users.
1 parent 2fcfbe9 commit 14fa71c

1 file changed

Lines changed: 73 additions & 29 deletions

File tree

‎41_snapshots-btrfs‎

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -107,38 +107,82 @@ esac
107107
if [ -n "${GRUB_BTRFS_PROTECTION_AUTHORIZED_USERS}" ] ; then
108108
protection_authorized_users="--users ${GRUB_BTRFS_PROTECTION_AUTHORIZED_USERS} "
109109
fi
110-
111110
## Probe information of Root and Boot devices
112-
# Probe info "Root partition"
113-
root_device=$(${grub_probe} --target=device /) # Root device
114-
root_uuid=$(${grub_probe} --device ${root_device} --target="fs_uuid" 2>/dev/null) # UUID of the root device
115-
root_uuid_subvolume=$(btrfs subvolume show / 2>/dev/null) || print_error "UUID of the root subvolume is not available"; # If UUID of root subvolume is not available, then exit
116-
root_uuid_subvolume=$(awk -F":" 'match($1, /(^[ \t]+UUID)/) {sub(/^[ \t]+/, "", $2); print $2}' <<< "$root_uuid_subvolume") # UUID of the root subvolume '
117-
# Probe info "Boot partition"
118-
boot_device=$(${grub_probe} --target=device ${boot_directory}) # Boot device
119-
boot_uuid=$(${grub_probe} --device ${boot_device} --target="fs_uuid" 2>/dev/null) # UUID of the boot device
120-
boot_uuid_subvolume=$(btrfs subvolume show "$boot_directory" 2>/dev/null) || boot_uuid_subvolume=" UUID: $root_uuid_subvolume"; # If boot folder isn't a subvolume, then UUID=root_uuid_subvolume
121-
boot_uuid_subvolume=$(awk -F":" 'match($1, /(^[ \t]+UUID)/) {sub(/^[ \t]+/, "", $2); print $2}' <<< "$boot_uuid_subvolume") # UUID of the boot subvolume '
122-
boot_hs=$(${grub_probe} --device ${boot_device} --target="hints_string" 2>/dev/null) # hints string
123-
boot_fs=$(${grub_probe} --device ${boot_device} --target="fs" 2>/dev/null) # Type filesystem of boot device
124-
125-
# Enable LUKS encrypted devices support
111+
# -----------------------------------------------------------
112+
113+
# ---------- Root partition ----------
114+
root_device="$(${grub_probe} --target=device /)" # e.g. /dev/mapper/enc
115+
root_uuid="$(${grub_probe} --device "${root_device}" --target=fs_uuid 2>/dev/null)" || true
116+
117+
# Fallback when grub-probe fails (encrypted container, detached header…)
118+
if [ -z "$root_uuid" ]; then
119+
root_uuid="$(blkid -s UUID -o value "${root_device}" 2>/dev/null)"
120+
fi
121+
[ -z "$root_uuid" ] && print_error "Cannot determine UUID of ${root_device}"
122+
123+
# Root subvolume UUID
124+
root_uuid_subvolume="$(btrfs subvolume show / 2>/dev/null | \
125+
awk -F':' '/^\s*UUID/ {gsub(/^[ \t]+/, "", $2); print $2}')"
126+
[ -z "$root_uuid_subvolume" ] && print_error "UUID of the root subvolume is not available"
127+
128+
# ---------- Boot partition ----------
129+
boot_device="$(${grub_probe} --target=device "${boot_directory}")" # e.g. /dev/sdb1
130+
boot_uuid="$(${grub_probe} --device "${boot_device}" --target=fs_uuid 2>/dev/null)" || true
131+
132+
# Fallback for boot UUID
133+
if [ -z "$boot_uuid" ]; then
134+
boot_uuid="$(blkid -s UUID -o value "${boot_device}" 2>/dev/null)"
135+
fi
136+
[ -z "$boot_uuid" ] && print_error "Cannot determine UUID of ${boot_device}"
137+
138+
# If /boot is not a Btrfs subvolume, reuse root subvol UUID
139+
boot_uuid_subvolume="$(btrfs subvolume show "${boot_directory}" 2>/dev/null | \
140+
awk -F':' '/^\s*UUID/ {gsub(/^[ \t]+/, "", $2); print $2}')" \
141+
|| boot_uuid_subvolume="UUID: $root_uuid_subvolume"
142+
143+
# Extra data for GRUB commands
144+
boot_hs="$(${grub_probe} --device "${boot_device}" --target=hints_string 2>/dev/null)"
145+
boot_fs="$(${grub_probe} --device "${boot_device}" --target=fs 2>/dev/null)"
146+
# -----------------------------------------------------------
147+
148+
## Enable LUKS encrypted devices support
126149
case "$(echo "$GRUB_BTRFS_ENABLE_CRYPTODISK" | tr '[:upper:]' '[:lower:]')" in
127150
true)
128-
list_insmods=()
129-
list_insmods+=("insmod gzio")
130-
list_insmods+=("insmod part_gpt")
131-
list_insmods+=("insmod cryptodisk")
132-
list_insmods+=("insmod luks")
133-
list_insmods+=("insmod gcry_rijndael")
134-
list_insmods+=("insmod gcry_rijndael")
135-
list_insmods+=("insmod gcry_sha256")
136-
list_insmods+=("insmod ${boot_fs}")
137-
list_insmods+=("cryptomount -u $(echo $GRUB_CMDLINE_LINUX_DEFAULT | grep -o -P '(?<=cryptdevice=UUID=).*(?=:cryptdev)')")
138-
;;
151+
list_insmods=(
152+
"insmod gzio"
153+
"insmod part_gpt"
154+
"insmod cryptodisk"
155+
"insmod luks"
156+
"insmod gcry_rijndael"
157+
"insmod gcry_rijndael"
158+
"insmod gcry_sha256"
159+
"insmod ${boot_fs}"
160+
)
161+
162+
# Extract the <source> field of cryptdevice=<source>:<name>[:header]
163+
crypt_source="$(printf '%s %s\n' "$GRUB_CMDLINE_LINUX_DEFAULT" "$GRUB_CMDLINE_LINUX" \
164+
| grep -o -P 'cryptdevice=\K[^:]+' || true)"
165+
166+
# Turn the source into a UUID that cryptomount -u understands
167+
crypt_uuid=""
168+
if [[ "$crypt_source" =~ ^UUID=.* ]]; then # already UUID=…
169+
crypt_uuid="${crypt_source#UUID=}"
170+
elif [[ "$crypt_source" == /dev/* ]]; then # path → resolve → blkid
171+
real_dev=$(readlink -f "$crypt_source" 2>/dev/null || true)
172+
[ -b "$real_dev" ] && crypt_uuid=$(blkid -s UUID -o value "$real_dev" 2>/dev/null || true)
173+
fi
174+
175+
# Emit the proper cryptomount command
176+
if [[ "$crypt_uuid" =~ ^[0-9a-fA-F-]{36}$ ]]; then
177+
list_insmods+=("cryptomount -u ${crypt_uuid}")
178+
else
179+
# last-resort: scan all crypto containers (works but a bit slower)
180+
list_insmods+=("cryptomount -a")
181+
fi
182+
;;
139183
*)
140-
list_insmods=("insmod ${boot_fs}")
141-
;;
184+
list_insmods=("insmod ${boot_fs}")
185+
;;
142186
esac
143187

144188
## Parameters passed to the kernel
@@ -351,7 +395,7 @@ snapshot_list()
351395
# Parse Snapper & timeshift & yabsnap information
352396
local type_snapshot="N/A"
353397
local description_snapshot="N/A"
354-
398+
355399
# path to yabsnap snapshot meta data
356400
local yabsnap_info="$grub_btrfs_mount_point/${path_snapshot%"/"*}/$(echo "${snap[13]}" | awk -F'/' '{print $3 "-meta.json"}')"
357401

0 commit comments

Comments
 (0)