Last active
March 22, 2022 09:53
-
-
Save Thesola10/93ec2d8c3af73e3f198b24c1ccc092ae to your computer and use it in GitHub Desktop.
Reboot to an arbitrary UEFI entry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Reboot-EFI (c) Karim Vergnes <me@thesola.io> | |
# Wrapper to reboot to a set UEFI entry | |
# Requires efibootmgr and fzy | |
rebootcmd="shutdown -r now" | |
trap _exit KILL INT TERM | |
_exit() { | |
exit 20 | |
} | |
_get_bootcode() | |
{ | |
typeset -A entries | |
local match | |
local smatch | |
if ! which -p efibootmgr >/dev/null 2>&1 | |
then | |
>&2 echo "'efibootmgr' is missing, cannot proceed." | |
exit 1 | |
fi | |
{ | |
IFS=$'\n' | |
for entry in $(efibootmgr | grep -iF "$1") | |
do | |
[[ "$entry" =~ Boot([0-9A-Fa-f]{4})(\*|\ )\ (.*) ]] &&\ | |
entries["${BASH_REMATCH[3]}"]="${BASH_REMATCH[1]}" | |
match="${BASH_REMATCH[1]}" | |
smatch="${BASH_REMATCH[3]}" | |
done | |
} | |
if [[ ${#entries[@]} > 1 ]] | |
then | |
sel=$(printf '%s\n' "${!entries[@]}" | fzy) | |
[[ ${#sel} == 0 ]] && exit 20 # fzy got killed | |
>&2 echo "Rebooting to \`$sel'..." | |
echo ${entries[$sel]} | |
elif [[ ${#entries[@]} == 1 ]] | |
then | |
>&2 echo "Rebooting to \`$smatch'..." | |
echo $match | |
else | |
>&2 echo "no target found for \`$1'" | |
exit 1 | |
fi | |
} | |
_on_help() | |
{ | |
>&2 echo "usage: reboot [target]" | |
exit 1 | |
} | |
if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]] | |
then | |
_on_help | |
fi | |
if [[ $# < 1 ]] | |
then | |
exec $rebootcmd | |
elif [[ $# == 1 ]] | |
then | |
bcode=$(_get_bootcode $1) | |
[[ ${#bcode} == 0 ]] && exit 20 # no bootcode found | |
efibootmgr -n $bcode >/dev/null 2>&1 | |
exec $rebootcmd | |
else | |
_on_help | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment