2017-07-03 03:01:04 +00:00
|
|
|
#!/bin/sh
|
2017-07-08 20:59:37 +00:00
|
|
|
# Generic configurable boot script via kexec
|
2017-07-12 04:17:45 +00:00
|
|
|
set -e -o pipefail
|
2017-07-04 23:49:14 +00:00
|
|
|
. /etc/config
|
2017-07-03 03:01:04 +00:00
|
|
|
. /etc/functions
|
|
|
|
|
2017-07-04 23:49:14 +00:00
|
|
|
add=""
|
|
|
|
remove=""
|
|
|
|
config="*.cfg"
|
|
|
|
unique="n"
|
2017-07-08 20:59:37 +00:00
|
|
|
valid_hash="n"
|
|
|
|
valid_global_hash="n"
|
|
|
|
valid_rollback="n"
|
2017-07-22 18:57:46 +00:00
|
|
|
force_menu="n"
|
2018-02-22 21:18:16 +00:00
|
|
|
gui_menu="n"
|
|
|
|
while getopts "b:d:p:a:r:c:uimg" arg; do
|
2017-07-04 23:49:14 +00:00
|
|
|
case $arg in
|
|
|
|
b) bootdir="$OPTARG" ;;
|
|
|
|
d) paramsdev="$OPTARG" ;;
|
|
|
|
p) paramsdir="$OPTARG" ;;
|
|
|
|
a) add="$OPTARG" ;;
|
|
|
|
r) remove="$OPTARG" ;;
|
|
|
|
c) config="$OPTARG" ;;
|
|
|
|
u) unique="y" ;;
|
2017-07-22 18:57:46 +00:00
|
|
|
m) force_menu="y" ;;
|
2017-07-08 20:59:37 +00:00
|
|
|
i) valid_hash="y"; valid_rollback="y" ;;
|
2018-02-22 21:18:16 +00:00
|
|
|
g) gui_menu="y" ;;
|
2017-07-04 23:49:14 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "$bootdir" ]; then
|
2017-07-17 16:43:14 +00:00
|
|
|
die "Usage: $0 -b /boot"
|
2017-07-04 23:49:14 +00:00
|
|
|
fi
|
2017-07-03 03:01:04 +00:00
|
|
|
|
2017-07-04 23:49:14 +00:00
|
|
|
if [ -z "$paramsdev" ]; then
|
|
|
|
paramsdev="$bootdir"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$paramsdir" ]; then
|
|
|
|
paramsdir="$bootdir"
|
|
|
|
fi
|
2017-07-03 03:01:04 +00:00
|
|
|
|
2017-07-22 18:25:39 +00:00
|
|
|
bootdir="${bootdir%%/}"
|
|
|
|
paramsdev="${paramsdev%%/}"
|
|
|
|
paramsdir="${paramsdir%%/}"
|
|
|
|
|
2017-07-08 20:59:37 +00:00
|
|
|
verify_global_hashes()
|
|
|
|
{
|
|
|
|
echo "+++ Checking verified boot hash file "
|
|
|
|
# Check the hashes of all the files
|
|
|
|
if cd $bootdir && sha256sum -c "$TMP_HASH_FILE" ; then
|
|
|
|
echo "+++ Verified boot hashes "
|
|
|
|
valid_hash='y'
|
|
|
|
valid_global_hash='y'
|
|
|
|
else
|
|
|
|
die "$TMP_HASH_FILE: boot hash mismatch"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
verify_rollback_counter()
|
|
|
|
{
|
|
|
|
TPM_COUNTER=`grep counter $TMP_ROLLBACK_FILE | cut -d- -f2`
|
|
|
|
if [ -z "$TPM_COUNTER" ]; then
|
|
|
|
die "$TMP_ROLLBACK_FILE: TPM counter not found?"
|
|
|
|
fi
|
|
|
|
|
|
|
|
read_tpm_counter $TPM_COUNTER \
|
|
|
|
|| die "Failed to read TPM counter"
|
|
|
|
|
|
|
|
sha256sum -c $TMP_ROLLBACK_FILE \
|
|
|
|
|| die "Invalid TPM counter state"
|
|
|
|
|
|
|
|
valid_rollback="y"
|
|
|
|
}
|
|
|
|
|
2017-07-03 17:07:03 +00:00
|
|
|
first_menu="y"
|
2017-07-03 03:01:04 +00:00
|
|
|
get_menu_option() {
|
2017-07-03 17:07:03 +00:00
|
|
|
num_options=`cat $TMP_MENU_FILE | wc -l`
|
|
|
|
if [ $num_options -eq 0 ]; then
|
2017-07-08 20:59:37 +00:00
|
|
|
die "No boot options"
|
2017-07-03 17:07:03 +00:00
|
|
|
fi
|
2017-07-03 03:01:04 +00:00
|
|
|
|
2017-07-03 17:07:03 +00:00
|
|
|
if [ $num_options -eq 1 -a $first_menu = "y" ]; then
|
|
|
|
option_index=1
|
2018-02-22 21:18:16 +00:00
|
|
|
elif [ "$gui_menu" = "y" ]; then
|
|
|
|
MENU_OPTIONS=""
|
|
|
|
n=0
|
|
|
|
while read option
|
|
|
|
do
|
|
|
|
parse_option
|
|
|
|
n=`expr $n + 1`
|
|
|
|
name=$(echo $name | tr " " "_")
|
|
|
|
kernel=$(echo $kernel | cut -f2 -d " ")
|
|
|
|
MENU_OPTIONS="$MENU_OPTIONS $n ${name}_[$kernel]"
|
|
|
|
done < $TMP_MENU_FILE
|
|
|
|
|
|
|
|
whiptail --clear --title "Select your boot option" \
|
|
|
|
--menu "Choose the boot option [1-$n, a to abort]:" 20 120 8 \
|
|
|
|
-- $MENU_OPTIONS \
|
|
|
|
2>/tmp/whiptail || die "Aborting boot attempt"
|
|
|
|
|
|
|
|
option_index=$(cat /tmp/whiptail)
|
2017-07-03 17:07:03 +00:00
|
|
|
else
|
|
|
|
echo "+++ Select your boot option:"
|
|
|
|
n=0
|
|
|
|
while read option
|
|
|
|
do
|
|
|
|
parse_option
|
|
|
|
n=`expr $n + 1`
|
|
|
|
echo "$n. $name [$kernel]"
|
|
|
|
done < $TMP_MENU_FILE
|
|
|
|
|
|
|
|
read \
|
|
|
|
-p "Choose the boot option [1-$n, a to abort]: " \
|
|
|
|
option_index
|
2017-07-03 03:01:04 +00:00
|
|
|
|
2017-07-03 17:07:03 +00:00
|
|
|
if [ "$option_index" = "a" ]; then
|
2017-07-08 20:59:37 +00:00
|
|
|
die "Aborting boot attempt"
|
2017-07-03 17:07:03 +00:00
|
|
|
fi
|
2017-07-03 03:01:04 +00:00
|
|
|
fi
|
2017-07-03 17:07:03 +00:00
|
|
|
first_menu="n"
|
2017-07-03 03:01:04 +00:00
|
|
|
|
|
|
|
option=`head -n $option_index $TMP_MENU_FILE | tail -1`
|
|
|
|
parse_option
|
|
|
|
}
|
|
|
|
|
|
|
|
confirm_menu_option() {
|
2018-02-22 21:18:16 +00:00
|
|
|
if [ "$gui_menu" = "y" ]; then
|
|
|
|
whiptail --clear --title "Confirm boot details" \
|
|
|
|
--menu "Confirm the boot details for $name:\n\n$option\n\n" 20 120 8 \
|
|
|
|
-- 'y' "Boot $name" 'd' "Make $name the default" \
|
|
|
|
2>/tmp/whiptail || die "Aborting boot attempt"
|
|
|
|
|
|
|
|
option_confirm=$(cat /tmp/whiptail)
|
|
|
|
else
|
|
|
|
echo "+++ Please confirm the boot details for $name:"
|
|
|
|
echo $option
|
|
|
|
|
|
|
|
read \
|
|
|
|
-n 1 \
|
|
|
|
-p "Confirm selection by pressing 'y', make default with 'd': " \
|
|
|
|
option_confirm
|
|
|
|
echo
|
|
|
|
fi
|
2017-07-03 03:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parse_option() {
|
|
|
|
name=`echo $option | cut -d\| -f1`
|
|
|
|
kernel=`echo $option | cut -d\| -f3`
|
|
|
|
}
|
|
|
|
|
2017-07-04 23:49:14 +00:00
|
|
|
scan_options() {
|
2017-07-03 03:01:04 +00:00
|
|
|
echo "+++ Scanning for unsigned boot options"
|
|
|
|
option_file="/tmp/kexec_options.txt"
|
2017-07-04 23:49:14 +00:00
|
|
|
if [ -r $option_file ]; then rm $option_file; fi
|
|
|
|
for i in `find $bootdir -name "$config"`; do
|
2017-07-12 04:17:45 +00:00
|
|
|
kexec-parse-boot "$bootdir" "$i" >> $option_file
|
2017-07-03 03:01:04 +00:00
|
|
|
done
|
|
|
|
if [ ! -r $option_file ]; then
|
2017-07-08 20:59:37 +00:00
|
|
|
die "Failed to parse any boot options"
|
2017-07-03 03:01:04 +00:00
|
|
|
fi
|
2017-07-04 23:49:14 +00:00
|
|
|
if [ "$unique" = 'y' ]; then
|
|
|
|
sort $option_file | uniq > $TMP_MENU_FILE
|
|
|
|
else
|
|
|
|
cp $option_file $TMP_MENU_FILE
|
|
|
|
fi
|
|
|
|
}
|
2017-07-03 03:01:04 +00:00
|
|
|
|
2017-07-08 20:59:37 +00:00
|
|
|
save_default_option() {
|
|
|
|
read \
|
|
|
|
-n 1 \
|
|
|
|
-p "Saving a default will modify the disk. Proceed? (y/n): " \
|
|
|
|
default_confirm
|
|
|
|
echo
|
|
|
|
|
|
|
|
if [ "$default_confirm" = "y" ]; then
|
|
|
|
if kexec-save-default \
|
|
|
|
-b "$bootdir" \
|
|
|
|
-d "$paramsdev" \
|
|
|
|
-p "$paramsdir" \
|
|
|
|
-i "$option_index" \
|
|
|
|
; then
|
|
|
|
echo "+++ Saved defaults to device"
|
|
|
|
sleep 2
|
|
|
|
default_failed="n"
|
2017-07-22 18:57:46 +00:00
|
|
|
force_menu="n"
|
2017-07-08 20:59:37 +00:00
|
|
|
return
|
|
|
|
else
|
|
|
|
echo "Failed to save defaults"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
option_confirm="n"
|
|
|
|
}
|
|
|
|
|
2017-07-04 23:49:14 +00:00
|
|
|
default_select() {
|
|
|
|
# Attempt boot with expected parameters
|
|
|
|
|
|
|
|
# Check that entry matches that which is expected from menu
|
|
|
|
default_index=`basename "$TMP_DEFAULT_FILE" | cut -d. -f 2`
|
|
|
|
|
|
|
|
# Check to see if entries have changed - useful for detecting grub update
|
|
|
|
expectedoption=`cat $TMP_DEFAULT_FILE`
|
|
|
|
option=`head -n $default_index $TMP_MENU_FILE | tail -1`
|
|
|
|
if [ "$option" != "$expectedoption" ]; then
|
2017-07-22 18:57:46 +00:00
|
|
|
die "!!! Boot entry has changed - please set a new default"
|
2017-07-04 23:49:14 +00:00
|
|
|
fi
|
|
|
|
parse_option
|
|
|
|
|
|
|
|
# Enforce that default option hashes are valid
|
|
|
|
echo "+++ Checking verified default boot hash file "
|
|
|
|
# Check the hashes of all the files
|
|
|
|
if cd $bootdir && sha256sum -c "$TMP_DEFAULT_HASH_FILE" ; then
|
|
|
|
echo "+++ Verified default boot hashes "
|
2017-07-08 20:59:37 +00:00
|
|
|
valid_hash='y'
|
2017-07-04 23:49:14 +00:00
|
|
|
else
|
2017-07-22 18:57:46 +00:00
|
|
|
die "!!! $TMP_DEFAULT_HASH_FILE: default boot hash mismatch"
|
2017-07-04 23:49:14 +00:00
|
|
|
fi
|
2017-07-03 03:01:04 +00:00
|
|
|
|
2017-07-04 23:49:14 +00:00
|
|
|
echo "+++ Executing default boot for $name:"
|
2017-07-08 20:59:37 +00:00
|
|
|
do_boot
|
|
|
|
warn "Failed to boot default option"
|
2017-07-04 23:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user_select() {
|
|
|
|
# No default expected boot parameters, ask user
|
|
|
|
|
|
|
|
option_confirm=""
|
|
|
|
while [ "$option_confirm" != "y" -a "$option_confirm" != "d" ]
|
|
|
|
do
|
|
|
|
get_menu_option
|
|
|
|
confirm_menu_option
|
|
|
|
|
|
|
|
if [ "$option_confirm" = 'd' ]; then
|
2017-07-08 20:59:37 +00:00
|
|
|
save_default_option
|
2017-07-04 23:49:14 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "$option_confirm" = "d" ]; then
|
2017-07-22 18:57:46 +00:00
|
|
|
if [ ! -r "$TMP_KEY_DEVICES" ]; then
|
|
|
|
# rerun primary boot loop to boot the new default option
|
|
|
|
continue
|
|
|
|
else
|
|
|
|
echo "+++ Rebooting to start the new default option"
|
|
|
|
sleep 2
|
|
|
|
reboot \
|
|
|
|
|| die "!!! Failed to reboot system"
|
|
|
|
fi
|
2017-07-04 23:49:14 +00:00
|
|
|
fi
|
|
|
|
|
2017-07-08 20:59:37 +00:00
|
|
|
do_boot
|
|
|
|
}
|
|
|
|
|
|
|
|
do_boot()
|
|
|
|
{
|
|
|
|
if [ "$CONFIG_BOOT_REQ_ROLLBACK" = "y" -a "$valid_rollback" = "n" ]; then
|
2017-07-22 18:57:46 +00:00
|
|
|
die "!!! Missing required rollback counter state"
|
2017-07-08 20:59:37 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$CONFIG_BOOT_REQ_HASH" = "y" -a "$valid_hash" = "n" ]; then
|
2017-07-22 18:57:46 +00:00
|
|
|
die "!!! Missing required boot hashes"
|
2017-07-04 23:49:14 +00:00
|
|
|
fi
|
|
|
|
|
2017-07-12 04:17:45 +00:00
|
|
|
if [ -r "$TMP_KEY_DEVICES" ]; then
|
|
|
|
INITRD=`kexec-boot -b "$bootdir" -e "$option" -i` \
|
|
|
|
|| die "!!! Failed to extract the initrd from boot option"
|
|
|
|
if [ -z "$INITRD" ]; then
|
|
|
|
die "!!! No initrd file found in boot option"
|
|
|
|
fi
|
|
|
|
|
|
|
|
kexec-insert-key $INITRD \
|
|
|
|
|| die "!!! Failed to insert disk key into a new initrd"
|
|
|
|
|
|
|
|
kexec-boot -b "$bootdir" -e "$option" \
|
|
|
|
-a "$add" -r "$remove" -o "/tmp/secret/initrd.cpio" \
|
|
|
|
|| die "!!! Failed to boot w/ options: $option"
|
|
|
|
else
|
|
|
|
kexec-boot -b "$bootdir" -e "$option" -a "$add" -r "$remove" \
|
|
|
|
|| die "!!! Failed to boot w/ options: $option"
|
|
|
|
fi
|
2017-07-04 23:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while true; do
|
2017-07-08 20:59:37 +00:00
|
|
|
check_config $paramsdir
|
2017-07-12 04:17:45 +00:00
|
|
|
TMP_DEFAULT_FILE=`find /tmp/kexec/kexec_default.*.txt 2>/dev/null | head -1` || true
|
2017-07-04 23:49:14 +00:00
|
|
|
TMP_MENU_FILE="/tmp/kexec/kexec_menu.txt"
|
|
|
|
TMP_HASH_FILE="/tmp/kexec/kexec_hashes.txt"
|
|
|
|
TMP_DEFAULT_HASH_FILE="/tmp/kexec/kexec_default_hashes.txt"
|
2017-07-08 20:59:37 +00:00
|
|
|
TMP_ROLLBACK_FILE="/tmp/kexec/kexec_rollback.txt"
|
2017-07-12 04:17:45 +00:00
|
|
|
TMP_KEY_DEVICES="/tmp/kexec/kexec_key_devices.txt"
|
|
|
|
TMP_KEY_LVM="/tmp/kexec/kexec_key_lvm.txt"
|
|
|
|
|
|
|
|
if [ ! -r "$TMP_KEY_DEVICES" ]; then
|
|
|
|
# Extend PCR4 as soon as possible
|
|
|
|
tpm extend -ix 4 -ic generic \
|
|
|
|
|| die "Failed to extend PCR 4"
|
|
|
|
fi
|
2017-07-04 23:49:14 +00:00
|
|
|
|
|
|
|
# if no saved options, scan the boot directory and generate
|
2017-07-08 20:59:37 +00:00
|
|
|
if [ ! -r "$TMP_MENU_FILE" ]; then
|
2017-07-04 23:49:14 +00:00
|
|
|
scan_options
|
|
|
|
fi
|
|
|
|
|
2017-07-08 20:59:37 +00:00
|
|
|
# Optionally enforce device file hashes
|
|
|
|
if [ -r "$TMP_HASH_FILE" ]; then
|
|
|
|
valid_global_hash="n"
|
|
|
|
|
|
|
|
verify_global_hashes
|
|
|
|
|
|
|
|
if [ "$valid_global_hash" = "n" ]; then
|
|
|
|
die "Failed to verify global hashes"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -r "$TMP_ROLLBACK_FILE" ]; then
|
|
|
|
# in the case of iso boot with a rollback file, do not assume valid
|
|
|
|
valid_rollback="n"
|
|
|
|
|
|
|
|
verify_rollback_counter
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$default_failed" != "y" \
|
2017-07-22 18:57:46 +00:00
|
|
|
-a "$force_menu" = "n" \
|
2017-07-08 20:59:37 +00:00
|
|
|
-a -r "$TMP_DEFAULT_FILE" \
|
|
|
|
-a -r "$TMP_DEFAULT_HASH_FILE" ] \
|
|
|
|
; then
|
2017-07-04 23:49:14 +00:00
|
|
|
default_select
|
2017-07-08 20:59:37 +00:00
|
|
|
default_failed="y"
|
2017-07-04 23:49:14 +00:00
|
|
|
else
|
|
|
|
user_select
|
|
|
|
fi
|
|
|
|
done
|
2017-07-08 20:59:37 +00:00
|
|
|
|
|
|
|
die "!!! Shouldn't get here""
|