nextboot: merge picksdboot and pickefi into one function

This commit is contained in:
David Thurstenson 2021-12-01 18:12:19 -06:00
parent 9952d04056
commit 692c361290
1 changed files with 36 additions and 49 deletions

View File

@ -10,38 +10,18 @@ bootctl is-installed &> /dev/null && sdboot=1
[[ "$(bootctl reboot-to-firmware)" == "supported" ]] && fwboot=1 [[ "$(bootctl reboot-to-firmware)" == "supported" ]] && fwboot=1
picksdboot() { selectentry() {
# Usage: picksdboot <title> <prompt> # Usage: selectentry <title> <prompt>
local entry PS3 COLUMNS local sel PS3 COLUMNS
PS3="$2 "
COLUMNS=1
# Print title to stderr
printf "\n%s\n\n" "$1" >&2
# Put all systemd-boot loder entry confs into an array
mapfile -t sdbootlist < <(systemctl --boot-loader-entry=help)
# Only add the firmware interface to the list if booting to it is supported
[[ -v fwboot ]] && sdbootlist+=("$biosname")
select entry in "${sdbootlist[@]}"; do
break
done
printf "%s" "$entry"
}
pickefi() {
# Usage: pickefi <title> <prompt>
local bootnext PS3 COLUMNS
PS3="$2 " PS3="$2 "
COLUMNS=1 COLUMNS=1
# Print title to stderr # Print title to stderr
printf "\n%s\n\n" "$1" >&2 printf "\n%s\n\n" "$1" >&2
# Populate entry list array
# If we're in EFI mode
if [[ -v efimode ]]; then
# This one is fun # This one is fun
# mapfile splits stdin on \n to an array # mapfile splits stdin on \n to an array
# The sed regex looks for an asterisk in the 8th position (0 indexed), # The sed regex looks for an asterisk in the 8th position (0 indexed),
@ -49,28 +29,35 @@ pickefi() {
# we care to add to the list. # we care to add to the list.
# It *seems* like one could put mapfile at the end of the pipeline, but # It *seems* like one could put mapfile at the end of the pipeline, but
# each command gets run in a subshell, so the array is lost. (ref: SC2031) # each command gets run in a subshell, so the array is lost. (ref: SC2031)
mapfile -t efientries < <(efibootmgr | sed -n '/.\{8\}\*/p') mapfile -t entrylist < <(efibootmgr | sed -n '/.\{8\}\*/p')
# Rebooting to the firmware interface only works with systemd-boot, so only add # Add firmware interface entry if supported
# it to the EFI mode list if systemd-boot is available and it's supported [[ -v fwboot ]] && entrylist+=("$biosname")
[[ -v sdboot && -v fwboot ]] && efientries+=("$biosname")
select efiselection in "${efientries[@]}"; do # If we're in systemd-boot mode
elif [[ ! -v efimode ]]; then
# Put all systemd-boot loder entry confs into an array
mapfile -t entrylist < <(systemctl --boot-loader-entry=help)
# If they chose the firmware interface # Only add the firmware interface to the list if booting to it is supported
if [[ "$efiselection" == "$biosname" ]]; then [[ -v fwboot ]] && entrylist+=("$biosname")
break
else
# Take only character 4-8 of the entry, which is the BOOTNUM
bootnext="${efiselection:4:4}"
break
fi fi
# Get user selection
select sel in "${entrylist[@]}"; do
break
done done
printf "%s" "$bootnext" # If in efi mode and the user did not select the firmware config menu item,
# then drop everything except for the BOOTNUM (character 4-8)
[[ -v efimode && "$sel" != "$biosname" ]] && sel="${sel:4:4}"
# Print result to stdout
printf "%s" "$sel"
} }
# Parse command line options
while getopts ":en" opt; do while getopts ":en" opt; do
case $opt in case $opt in
e) e)
@ -107,7 +94,7 @@ if [[ -v efimode ]]; then
fi fi
# Prompt for the user's choice # Prompt for the user's choice
choice="$(pickefi "$title" "$prompt")" choice="$(selectentry "$title" "$prompt")"
# A special case to boot into firmware interface # A special case to boot into firmware interface
if [[ "$choice" == "$biosname" ]]; then if [[ "$choice" == "$biosname" ]]; then
@ -131,7 +118,7 @@ elif [[ ! -v efimode ]]; then
fi fi
# Prompt for the user's choice # Prompt for the user's choice
choice="$(picksdboot "$title" "$prompt")" choice="$(selectentry "$title" "$prompt")"
# A special case to boot into firmware interface # A special case to boot into firmware interface
if [[ "$choice" == "$biosname" ]]; then if [[ "$choice" == "$biosname" ]]; then