2023-02-08 21:01:48 +00:00
|
|
|
#!/bin/bash
|
2017-07-12 04:17:45 +00:00
|
|
|
# Generate a TPM key used to unlock LUKS disks
|
2023-08-22 18:34:29 +00:00
|
|
|
|
|
|
|
. /etc/functions
|
|
|
|
|
|
|
|
TRACE "kexec-save-key: start"
|
2017-07-12 04:17:45 +00:00
|
|
|
set -e -o pipefail
|
|
|
|
. /etc/functions
|
|
|
|
|
|
|
|
lvm_volume_group=""
|
2017-07-22 20:32:10 +00:00
|
|
|
skip_sign="n"
|
|
|
|
while getopts "sp:d:l:" arg; do
|
2017-07-12 04:17:45 +00:00
|
|
|
case $arg in
|
2023-09-01 19:18:36 +00:00
|
|
|
s) skip_sign="y" ;;
|
|
|
|
p) paramsdir="$OPTARG" ;;
|
|
|
|
d) paramsdev="$OPTARG" ;;
|
|
|
|
l) lvm_volume_group="$OPTARG" ;;
|
2017-07-12 04:17:45 +00:00
|
|
|
esac
|
|
|
|
done
|
2023-08-22 18:34:29 +00:00
|
|
|
|
|
|
|
DEBUG "kexec-save-key prior of parsing: paramsdir: $paramsdir, paramsdev: $paramsdev, lvm_volume_group: $lvm_volume_group"
|
|
|
|
|
2023-09-01 19:18:36 +00:00
|
|
|
shift $(expr $OPTIND - 1)
|
2017-07-12 04:17:45 +00:00
|
|
|
key_devices="$@"
|
|
|
|
|
2023-08-22 18:34:29 +00:00
|
|
|
DEBUG "kexec-save-key: key_devices: $key_devices"
|
|
|
|
|
2017-07-12 04:17:45 +00:00
|
|
|
if [ -z "$paramsdir" ]; then
|
2017-07-22 20:32:10 +00:00
|
|
|
die "Usage: $0 [-s] -p /boot [-l qubes_dom0] [/dev/sda2 /dev/sda5 ...] "
|
2017-07-12 04:17:45 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$paramsdev" ]; then
|
|
|
|
paramsdev="$paramsdir"
|
2023-08-22 18:34:29 +00:00
|
|
|
DEBUG "kexec-save-key: paramsdev modified to : $paramsdev"
|
2017-07-12 04:17:45 +00:00
|
|
|
fi
|
|
|
|
|
2017-07-22 18:25:39 +00:00
|
|
|
paramsdev="${paramsdev%%/}"
|
|
|
|
paramsdir="${paramsdir%%/}"
|
|
|
|
|
2023-08-22 18:34:29 +00:00
|
|
|
DEBUG "kexec-save-key prior of last override: paramsdir: $paramsdir, paramsdev: $paramsdev, lvm_volume_group: $lvm_volume_group"
|
|
|
|
|
2017-07-12 04:17:45 +00:00
|
|
|
if [ -n "$lvm_volume_group" ]; then
|
2023-09-01 19:18:36 +00:00
|
|
|
lvm vgchange -a y $lvm_volume_group ||
|
|
|
|
die "Failed to activate the LVM group"
|
|
|
|
#TODO: why reuse key_devices for lvm devices?
|
2017-07-12 04:17:45 +00:00
|
|
|
for dev in /dev/$lvm_volume_group/*; do
|
|
|
|
key_devices="$key_devices $dev"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$key_devices" ]; then
|
|
|
|
die "No devices specified for TPM key insertion"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# try to switch to rw mode
|
|
|
|
mount -o rw,remount $paramsdev
|
|
|
|
|
|
|
|
rm -f $paramsdir/kexec_key_lvm.txt || true
|
|
|
|
if [ -n "$lvm_volume_group" ]; then
|
2023-08-22 18:34:29 +00:00
|
|
|
DEBUG "kexec-save-key saving under $paramsdir/kexec_key_lvm.txt : lvm_volume_group: $lvm_volume_group"
|
2023-09-01 19:18:36 +00:00
|
|
|
echo "$lvm_volume_group" >$paramsdir/kexec_key_lvm.txt ||
|
|
|
|
die "Failed to write lvm group to key config "
|
2017-07-12 04:17:45 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
rm -f $paramsdir/kexec_key_devices.txt || true
|
2017-07-12 04:43:08 +00:00
|
|
|
for dev in $key_devices; do
|
2023-09-01 19:18:36 +00:00
|
|
|
DEBUG "Getting UUID for $dev"
|
|
|
|
uuid=$(cryptsetup luksUUID "$dev" 2>/dev/null) ||
|
|
|
|
die "Failed to get UUID for device $dev"
|
|
|
|
DEBUG "Saving under $paramsdir/kexec_key_devices.txt : dev: $dev, uuid: $uuid"
|
|
|
|
echo "$dev $uuid" >>$paramsdir/kexec_key_devices.txt ||
|
|
|
|
die "Failed to add $dev:$uuid to key devices config"
|
2017-07-12 04:17:45 +00:00
|
|
|
done
|
|
|
|
|
2023-09-01 19:18:36 +00:00
|
|
|
kexec-seal-key $paramsdir ||
|
|
|
|
die "Failed to save and generate key in TPM"
|
2017-07-12 04:17:45 +00:00
|
|
|
|
2017-07-22 20:32:10 +00:00
|
|
|
if [ "$skip_sign" != "y" ]; then
|
2022-08-25 18:43:31 +00:00
|
|
|
extparam=
|
|
|
|
if [ "$CONFIG_IGNORE_ROLLBACK" != "y" ]; then
|
|
|
|
extparam=-r
|
|
|
|
fi
|
2017-07-22 20:32:10 +00:00
|
|
|
# sign and auto-roll config counter
|
2023-09-01 19:18:36 +00:00
|
|
|
kexec-sign-config -p $paramsdir $extparam ||
|
|
|
|
die "Failed to sign updated config"
|
2017-07-22 20:32:10 +00:00
|
|
|
fi
|
2017-07-12 04:17:45 +00:00
|
|
|
|
|
|
|
# switch back to ro mode
|
2023-09-01 19:18:36 +00:00
|
|
|
mount -o ro,remount $paramsdev
|