scripts/winboot

169 lines
4.0 KiB
Plaintext
Raw Normal View History

2016-12-19 17:45:17 +01:00
#!/bin/sh
2020-07-15 17:40:55 +02:00
# This script set a specific grub entry (winLOOSE…) for the next boot.
2020-07-15 18:27:15 +02:00
# Then reboot the host (by default) or just poweroff (if requested).
2020-07-15 17:40:55 +02:00
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}"
readonly NBARGS="${#}"
2020-07-15 18:03:00 +02:00
## Debug mode to print specific messages
DEBUG=1
## Grub specific vars
2020-07-15 17:40:55 +02:00
readonly GRUB_DEFAULT_PATH='/etc/default/grub'
readonly GRUB_DEFAULT_DIR='/etc/default/grub.d'
readonly WIN_GRUB="2"
2020-07-15 17:48:09 +02:00
## By default, the host should reboot at the end of the script
REBOOT=0
HALT=1
2020-07-15 17:48:09 +02:00
2020-07-15 18:03:00 +02:00
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [-d,--debug|-h,--help|--halt,--poweroff|--reboot]
Try to set a specific grub's entry for the next boot and
reboot the host (default behaviour).
EXAMPLES:
- Set the grub entry ${WIN_GRUB} (Windows by default) and reboot
${PROGNAME}
- Set the grub entry ${WIN_GRUB} (Windows by default) and shutdown
${PROGNAME} --halt
OPTIONS:
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
--halt,--poweroff
Ask to shutdown at the end of the script.
--reboot
Ask to reboot at the end of the script (default behaviour).
EOF
}
2020-07-15 17:40:55 +02:00
# }}}
2020-07-15 18:03:00 +02:00
debug_message() { # {{{
2020-07-15 17:40:55 +02:00
2020-07-15 18:03:00 +02:00
local_debug_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_debug_message}"
}
# }}}
2020-07-15 17:40:55 +02:00
main() { # {{{
# First check in Grub default dir if saved mode is set
if grep -q -E -R -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_DIR}"
then
2020-07-15 18:27:15 +02:00
debug_message "saved mode is enable in ${GRUB_DEFAULT_DIR} dir"
2020-07-15 17:40:55 +02:00
2020-07-15 18:27:15 +02:00
# Second check the default grub file
elif grep -q -E -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_PATH}"
2020-07-15 17:40:55 +02:00
then
2020-07-15 18:27:15 +02:00
debug_message "saved mode is enable in ${GRUB_DEFAULT_PATH} file"
# Otherwise enable saved mode
2020-07-15 17:40:55 +02:00
else
2020-07-15 18:27:15 +02:00
printf '%b' "GRUB_DEFAULT is not set in 'saved' mode\n"
debug_message "Enable grub saved mode in ${GRUB_DEFAULT_PATH} file"
2020-07-15 17:40:55 +02:00
sudo sed -i 's/\(^GRUB_DEFAULT.*\)/#\1\nGRUB_DEFAULT=saved/' "${GRUB_DEFAULT_PATH}"
2020-07-15 18:27:15 +02:00
2020-07-15 17:40:55 +02:00
GRUB_DEFAULT_ENTRY=$(grep -E -- "#GRUB_DEFAULT=" "${GRUB_DEFAULT_PATH}" | cut -d"=" -f2)
2020-07-15 18:27:15 +02:00
## Set current GRUB_DEFAULT as default grub's entry
## Exit if fails
sudo grub-set-default "${GRUB_DEFAULT_ENTRY}" \
|| exit 1
## Try to update grub's configuration
## Exit if fails
sudo update-grub \
|| exit 2
2020-07-15 17:40:55 +02:00
fi
2020-07-15 18:27:15 +02:00
printf '%b' "Reboot to windaube partition\\n"
sudo grub-reboot "${WIN_GRUB}"
[ "${REBOOT}" -eq "0" ] && sudo systemctl reboot
[ "${HALT}" -eq "0" ] && sudo systemctl poweroff
2020-07-15 17:40:55 +02:00
}
# }}}
2020-07-15 17:48:09 +02:00
# Manage arguments # {{{
# This code can't be in a function due to arguments
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
case "${1}" in
2020-07-15 18:03:00 +02:00
-d|--debug ) ## debug
DEBUG=0
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
2020-07-15 17:48:09 +02:00
;;
--halt|--poweroff ) ## A poweroff was requested
REBOOT=1
HALT=0
;;
2020-07-15 18:03:00 +02:00
--reboot ) ## A reboot was requested (default behaviour)
REBOOT=0
HALT=1
;;
-- ) ## End of options list
2020-07-15 17:48:09 +02:00
## End the while loop
break
;;
* ) ## unknow option
2020-07-15 17:48:09 +02:00
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
2020-07-15 18:03:00 +02:00
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
2020-07-15 17:48:09 +02:00
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
2020-07-15 18:03:00 +02:00
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
2020-07-15 17:48:09 +02:00
fi
# }}}
2020-07-15 17:40:55 +02:00
main
2016-12-19 17:45:17 +01:00
exit 0