Skip to content

Commit 5771820

Browse files
committed
root-hashes-gui.sh: generalize to work on debian/ubuntu and pureos
TODO: tpm prompt for password is silenced still.... Signed-off-by: Thierry Laurion <insurgo@riseup.net>
1 parent 16a083e commit 5771820

3 files changed

Lines changed: 74 additions & 37 deletions

File tree

initrd/bin/root-hashes-gui.sh

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ROOT_MOUNT="/root"
1313
export CONFIG_ROOT_DIRLIST_PRETTY=$(echo $CONFIG_ROOT_DIRLIST | sed -e 's/^/\//;s/ / \//g')
1414

1515
update_root_checksums() {
16+
TRACE_FUNC
1617
if ! detect_root_device; then
1718
whiptail_error --title 'ERROR: No Valid Root Disk Found' \
1819
--msgbox "No Valid Root Disk Found" 0 80
@@ -31,6 +32,7 @@ update_root_checksums() {
3132
mount -o rw,remount /boot
3233
fi
3334

35+
DEBUG "calculating hashes for $CONFIG_ROOT_DIRLIST_PRETTY on $ROOT_MOUNT"
3436
echo "+++ Calculating hashes for all files in $CONFIG_ROOT_DIRLIST_PRETTY "
3537
# Intentional wordsplit
3638
# shellcheck disable=SC2086
@@ -47,6 +49,8 @@ update_root_checksums() {
4749
unmount_root_device
4850
}
4951
check_root_checksums() {
52+
TRACE_FUNC
53+
DEBUG "verifying existing hash file for $CONFIG_ROOT_DIRLIST_PRETTY"
5054
if ! detect_root_device; then
5155
whiptail_error --title 'ERROR: No Valid Root Disk Found' \
5256
--msgbox "No Valid Root Disk Found" 0 80
@@ -74,6 +78,7 @@ check_root_checksums() {
7478
update_root_checksums
7579
return 0
7680
else
81+
DEBUG "user chose not to create root hash file, returning"
7782
exit 1
7883
fi
7984
fi
@@ -124,6 +129,7 @@ check_root_checksums() {
124129

125130
return 0
126131
else
132+
DEBUG "user chose not to update signatures after new-files warning"
127133
return 1
128134
fi
129135
fi
@@ -154,6 +160,7 @@ check_root_checksums() {
154160
update_root_checksums
155161
return 0
156162
else
163+
DEBUG "user chose not to update signatures after hash-check-failure"
157164
return 1
158165
fi
159166
fi
@@ -170,15 +177,33 @@ open_block_device_lvm() {
170177
return 1
171178
fi
172179

173-
# Use the LV 'root'. This is the default name used by Qubes. There's no
174-
# way to configure this at the moment.
175-
if ! [ -e "/dev/mapper/$VG-root" ]; then
176-
DEBUG "LVM volume group does not have 'root' logical volume"
180+
# Prefer an LV named 'root' (used by Qubes), but fall back to any LV
181+
# in the VG. This ensures Ubuntu-style names (e.g. ubuntu-vg/ubuntu-root)
182+
# also work.
183+
local LV="/dev/$VG/root"
184+
if ! [ -e "$LV" ]; then
185+
MAPPER_VG="${VG//-/--}"
186+
LV="/dev/mapper/${MAPPER_VG}-root"
187+
fi
188+
if ! [ -e "$LV" ]; then
189+
DEBUG "LVM VG $VG has no 'root' LV, enumerating all LVs"
190+
# list LV names and try each one
191+
for name in $(lvm lvs --noheadings -o lv_name --separator ' ' "$VG" 2>/dev/null); do
192+
lvpath="/dev/$VG/$name"
193+
[ -e "$lvpath" ] || lvpath="/dev/mapper/${VG//-/--}-$name"
194+
if [ -e "$lvpath" ]; then
195+
DEBUG "trying LV $lvpath"
196+
LV="$lvpath"
197+
break
198+
fi
199+
done
200+
fi
201+
if ! [ -e "$LV" ]; then
202+
DEBUG "no usable LV found in VG $VG"
177203
return 1
178204
fi
179-
180-
# Use the root LV now
181-
open_block_device_layers "/dev/mapper/$VG-root"
205+
# Use selected LV
206+
open_block_device_layers "$LV"
182207
}
183208

184209
# Open a LUKS device, then continue looking for more layers.
@@ -368,6 +393,7 @@ detect_root_device()
368393

369394
# generate list of possible boot devices
370395
fdisk -l 2>/dev/null | grep "Disk /dev/" | cut -f2 -d " " | cut -f1 -d ":" > /tmp/disklist
396+
DEBUG "detect_root_device: initial disklist=$(cat /tmp/disklist | tr '\n' ' ')"
371397

372398
# filter out extraneous options
373399
> /tmp_root_device_list

initrd/etc/functions

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,34 +1298,28 @@ find_lvm_vg_name() {
12981298
# GPT-partitioned disk.
12991299
is_gpt_bios_grub() {
13001300
TRACE_FUNC
1301-
DEBUG "is_gpt_bios_grub: PART_DEV=$1"
1302-
1303-
local PART_DEV="$1" DEVICE NUMBER
1304-
1305-
# Figure out the partitioned device containing this device (if there is
1306-
# one) from /sys/class/block.
1307-
local DEVICE_MATCHES=("/sys/class/block/"*"/$(basename "$PART_DEV")")
1308-
DEBUG "is_gpt_bios_grub: DEVICE_MATCHES=${DEVICE_MATCHES[*]}"
1309-
1310-
DEVICE="$(echo "${DEVICE_MATCHES[0]}" | cut -d/ -f5)"
1311-
if [ "${#DEVICE_MATCHES[@]}" -ne 1 ] || [ "$DEVICE" = "*" ]; then
1312-
DEBUG "is_gpt_bios_grub: ambiguous DEVICE, returning false"
1313-
return 0
1314-
fi
1315-
1316-
# Extract the partition number
1317-
if ! [[ $(basename "$PART_DEV") =~ ([0-9]+)$ ]]; then
1318-
DEBUG "is_gpt_bios_grub: cannot parse partition number"
1319-
return 0 # Can't figure out the partition number
1301+
# $1 is the device path being tested (e.g. /dev/vda1)
1302+
local PART_DEV="$1"
1303+
DEBUG "PART_DEV=$PART_DEV"
1304+
1305+
# identify the base device and partition number with a regex
1306+
local partname device number
1307+
partname=$(basename "$PART_DEV")
1308+
1309+
# match nvme style (p&lt;num&gt;) or normal (digit suffix)
1310+
if [[ $partname =~ ^(.+?)(p?[0-9]+)$ ]]; then
1311+
device="${BASH_REMATCH[1]}"
1312+
number="${BASH_REMATCH[2]}"
1313+
number="${number#p}"
1314+
else
1315+
DEBUG "cannot parse partition name '$partname'"
1316+
return 0 # not a recognised partition
13201317
fi
13211318

1322-
NUMBER="${BASH_REMATCH[1]}"
1323-
DEBUG "is_gpt_bios_grub: DEVICE=$DEVICE NUMBER=$NUMBER"
1319+
DEBUG "DEVICE=$device NUMBER=$number"
13241320

1325-
# Now we know the device and partition number, get the type. This is
1326-
# specific to GPT disks, MBR disks are shown differently by fdisk.
1327-
TRACE "$PART_DEV is partition $NUMBER of $DEVICE"
1328-
if [ "$(fdisk -l "/dev/$DEVICE" 2>/dev/null | awk '$1 == '"$NUMBER"' {print $5}')" == grub ]; then
1321+
# GPT disks list type in column 5; fall through to 1 otherwise
1322+
if [ "$(fdisk -l "/dev/$device" 2>/dev/null | awk '$1 == '"$number"' {print $5}')" == grub ]; then
13291323
return 0
13301324
fi
13311325
return 1
@@ -1354,9 +1348,17 @@ mount_possible_boot_device() {
13541348

13551349
# Skip bios-grub partitions on GPT disks, LUKS partitions, and LVM PVs,
13561350
# we can't mount these as /boot.
1357-
if is_gpt_bios_grub "$BOOT_DEV" || cryptsetup isLuks "$BOOT_DEV" ||
1358-
find_lvm_vg_name "$BOOT_DEV" >/dev/null; then
1359-
TRACE "$BOOT_DEV is not a mountable partition for /boot"
1351+
# Skip partitions we definitely can't mount for /boot. Log each reason.
1352+
if is_gpt_bios_grub "$BOOT_DEV"; then
1353+
DEBUG "$BOOT_DEV is GPT BIOS/GRUB partition, skipping"
1354+
return 1
1355+
fi
1356+
if cryptsetup isLuks "$BOOT_DEV"; then
1357+
DEBUG "$BOOT_DEV is a LUKS volume, skipping"
1358+
return 1
1359+
fi
1360+
if find_lvm_vg_name "$BOOT_DEV" >/dev/null; then
1361+
DEBUG "$BOOT_DEV is an LVM PV, skipping"
13601362
return 1
13611363
fi
13621364

@@ -1388,6 +1390,15 @@ mount_possible_boot_device() {
13881390
detect_boot_device() {
13891391
TRACE_FUNC
13901392
local devname
1393+
DEBUG "CONFIG_BOOT_DEV=$CONFIG_BOOT_DEV"
1394+
# If /boot is already mounted and appears to be a valid boot tree, just
1395+
# use its device. This avoids remount churn and makes the later lookup
1396+
# fast.
1397+
mounted_boot_dev="$(awk '$2=="/boot" {print $1; exit}' /proc/mounts)"
1398+
if [ -n "$mounted_boot_dev" ] && ls -d /boot/grub* >/dev/null 2>&1; then
1399+
CONFIG_BOOT_DEV="$mounted_boot_dev"
1400+
return 0
1401+
fi
13911402
# unmount /boot to be safe
13921403
cd / && umount /boot 2>/dev/null
13931404

@@ -1425,6 +1436,7 @@ detect_boot_device() {
14251436

14261437
# no valid boot device found
14271438
echo "Unable to locate /boot files on any mounted disk"
1439+
DEBUG "detect_boot_device: failed to find a bootable device"
14281440
return 1
14291441
}
14301442

initrd/etc/gui_functions

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ show_system_info() {
183183
EC_VER: ${EC_VER}"
184184

185185
local disk_info="$(disk_info_sysfs)"
186-
TRACE_FUNC
187-
DEBUG "show_system_info: disk_info=\n${disk_info}"
186+
DEBUG "disk_info=\n${disk_info}"
188187

189188
local msgbox="${BOARD_NAME}
190189

0 commit comments

Comments
 (0)