2017-07-04 23:49:14 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# Save these options to be the persistent default
|
2017-07-12 04:17:45 +00:00
|
|
|
set -e -o pipefail
|
2018-12-06 23:24:28 +00:00
|
|
|
. /tmp/config
|
2017-07-04 23:49:14 +00:00
|
|
|
. /etc/functions
|
|
|
|
|
2017-07-22 20:32:10 +00:00
|
|
|
while getopts "b:d:p:i:" arg; do
|
2017-07-04 23:49:14 +00:00
|
|
|
case $arg in
|
|
|
|
b) bootdir="$OPTARG" ;;
|
|
|
|
d) paramsdev="$OPTARG" ;;
|
|
|
|
p) paramsdir="$OPTARG" ;;
|
|
|
|
i) index="$OPTARG" ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2017-07-08 20:59:37 +00:00
|
|
|
if [ -z "$bootdir" -o -z "$index" ]; then
|
2017-07-17 16:43:14 +00:00
|
|
|
die "Usage: $0 -b /boot -i menu_option "
|
2017-07-04 23:49:14 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$paramsdev" ]; then
|
|
|
|
paramsdev="$bootdir"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$paramsdir" ]; then
|
|
|
|
paramsdir="$bootdir"
|
|
|
|
fi
|
|
|
|
|
2017-07-22 18:25:39 +00:00
|
|
|
bootdir="${bootdir%%/}"
|
|
|
|
paramsdev="${paramsdev%%/}"
|
|
|
|
paramsdir="${paramsdir%%/}"
|
|
|
|
|
2017-07-08 20:59:37 +00:00
|
|
|
TMP_MENU_FILE="/tmp/kexec/kexec_menu.txt"
|
2017-07-04 23:49:14 +00:00
|
|
|
ENTRY_FILE="$paramsdir/kexec_default.$index.txt"
|
|
|
|
HASH_FILE="$paramsdir/kexec_default_hashes.txt"
|
|
|
|
|
2017-07-08 20:59:37 +00:00
|
|
|
if [ ! -r "$TMP_MENU_FILE" ]; then
|
|
|
|
die "No menu options available, please run kexec-select-boot"
|
|
|
|
fi
|
|
|
|
|
|
|
|
entry=`head -n $index $TMP_MENU_FILE | tail -1`
|
|
|
|
if [ -z "$entry" ]; then
|
|
|
|
die "Invalid menu index $index"
|
|
|
|
fi
|
|
|
|
|
2017-07-29 17:24:34 +00:00
|
|
|
KEY_DEVICES="$paramsdir/kexec_key_devices.txt"
|
|
|
|
KEY_LVM="$paramsdir/kexec_key_lvm.txt"
|
|
|
|
save_key="n"
|
2019-11-21 22:01:50 +00:00
|
|
|
if [[ "$CONFIG_TPM" = "y" && "$CONFIG_TPM_NO_LUKS_DISK_UNLOCK" != "y" ]]; then
|
2017-12-05 08:29:07 +00:00
|
|
|
if [ ! -r "$KEY_DEVICES" ]; then
|
|
|
|
read \
|
|
|
|
-n 1 \
|
|
|
|
-p "Do you wish to add a disk encryption to the TPM [y/N]: " \
|
|
|
|
add_key_confirm
|
|
|
|
echo
|
|
|
|
|
|
|
|
if [ "$add_key_confirm" = "y" \
|
|
|
|
-o "$add_key_confirm" = "Y" ]; then
|
|
|
|
lvm_suggest="e.g. qubes_dom0 or blank"
|
|
|
|
devices_suggest="e.g. /dev/sda2 or blank"
|
|
|
|
save_key="y"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
read \
|
|
|
|
-n 1 \
|
|
|
|
-p "Do you want to reseal a disk key to the TPM [y/N]: " \
|
|
|
|
change_key_confirm
|
|
|
|
echo
|
|
|
|
|
|
|
|
if [ "$change_key_confirm" = "y" \
|
|
|
|
-o "$change_key_confirm" = "Y" ]; then
|
|
|
|
old_lvm_volume_group=""
|
|
|
|
if [ -r "$KEY_LVM" ]; then
|
|
|
|
old_lvm_volume_group=`cat $KEY_LVM` || true
|
|
|
|
old_key_devices=`cat $KEY_DEVICES \
|
|
|
|
| cut -d\ -f1 \
|
|
|
|
| grep -v "$old_lvm_volume_group" \
|
|
|
|
| xargs` || true
|
|
|
|
else
|
|
|
|
old_key_devices=`cat $KEY_DEVICES \
|
|
|
|
| cut -d\ -f1 | xargs` || true
|
|
|
|
fi
|
|
|
|
|
|
|
|
lvm_suggest="was '$old_lvm_volume_group'"
|
|
|
|
devices_suggest="was '$old_key_devices'"
|
|
|
|
save_key="y"
|
2017-07-22 20:32:10 +00:00
|
|
|
fi
|
2017-07-29 17:24:34 +00:00
|
|
|
fi
|
|
|
|
|
2017-12-05 08:29:07 +00:00
|
|
|
if [ "$save_key" = "y" ]; then
|
|
|
|
echo "+++ LVM volume groups (lvm vgscan): "
|
|
|
|
lvm vgscan || true
|
2017-07-29 17:24:34 +00:00
|
|
|
|
2017-12-05 08:29:07 +00:00
|
|
|
read \
|
|
|
|
-p "Encrypted LVM group? ($lvm_suggest): " \
|
|
|
|
lvm_volume_group
|
2017-07-29 17:24:34 +00:00
|
|
|
|
2017-12-05 08:29:07 +00:00
|
|
|
echo "+++ Block devices (blkid): "
|
|
|
|
blkid || true
|
2017-07-29 17:24:34 +00:00
|
|
|
|
2017-12-05 08:29:07 +00:00
|
|
|
read \
|
|
|
|
-p "Encrypted devices? ($devices_suggest): " \
|
|
|
|
key_devices
|
2017-07-29 17:24:34 +00:00
|
|
|
|
2017-12-05 08:29:07 +00:00
|
|
|
save_key_params="-s -p $paramsdev"
|
|
|
|
if [ -n "$lvm_volume_group" ]; then
|
|
|
|
save_key_params="$save_key_params -l $lvm_volume_group $key_devices"
|
|
|
|
else
|
|
|
|
save_key_params="$save_key_params $key_devices"
|
|
|
|
fi
|
|
|
|
echo "Running kexec-save-key with params: $save_key_params"
|
|
|
|
kexec-save-key $save_key_params \
|
|
|
|
|| die "Failed to save the disk key"
|
2017-07-22 20:32:10 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2017-07-04 23:49:14 +00:00
|
|
|
# try to switch to rw mode
|
|
|
|
mount -o rw,remount $paramsdev
|
|
|
|
|
|
|
|
if [ ! -d $paramsdir ]; then
|
2017-07-08 20:59:37 +00:00
|
|
|
mkdir -p $paramsdir \
|
|
|
|
|| die "Failed to create params directory"
|
2017-07-04 23:49:14 +00:00
|
|
|
fi
|
2020-08-26 11:21:57 +00:00
|
|
|
rm $paramsdir/kexec_default.*.txt 2>/dev/null || true
|
2017-07-04 23:49:14 +00:00
|
|
|
echo "$entry" > $ENTRY_FILE
|
2023-01-13 19:53:11 +00:00
|
|
|
( cd $bootdir && kexec-boot -b "$bootdir" -e "$entry" -f| \
|
2017-07-12 04:17:45 +00:00
|
|
|
xargs sha256sum > $HASH_FILE \
|
2023-01-13 19:53:11 +00:00
|
|
|
) || die "Failed to create hashes of boot files"
|
2017-07-04 23:49:14 +00:00
|
|
|
if [ ! -r $ENTRY_FILE -o ! -r $HASH_FILE ]; then
|
|
|
|
die "Failed to write default config"
|
|
|
|
fi
|
|
|
|
|
2017-07-08 20:59:37 +00:00
|
|
|
# sign and auto-roll config counter
|
2017-12-05 08:29:07 +00:00
|
|
|
extparam=
|
|
|
|
if [ "$CONFIG_TPM" = "y" ]; then
|
2020-10-08 13:16:08 +00:00
|
|
|
extparam=-r
|
2017-12-05 08:29:07 +00:00
|
|
|
fi
|
|
|
|
kexec-sign-config -p $paramsdir $extparam \
|
2017-07-08 20:59:37 +00:00
|
|
|
|| die "Failed to sign default config"
|
2017-07-04 23:49:14 +00:00
|
|
|
|
|
|
|
# switch back to ro mode
|
|
|
|
mount -o ro,remount $paramsdev
|