96 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
			
		
		
	
	
			96 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
| #!/bin/sh
 | |
| 
 | |
| # This script set a specific grub entry (winLOOSE…) for the next boot.
 | |
| # Then reboot the host
 | |
| 
 | |
| # Vars {{{
 | |
| readonly PROGNAME=$(basename "${0}")
 | |
| readonly PROGDIR=$(readlink -m $(dirname "${0}"))
 | |
| readonly ARGS="${*}"
 | |
| readonly NBARGS="${#}"
 | |
| 
 | |
| readonly GRUB_DEFAULT_PATH='/etc/default/grub'
 | |
| readonly GRUB_DEFAULT_DIR='/etc/default/grub.d'
 | |
| readonly WIN_GRUB="2"
 | |
| 
 | |
| ## By default, the host should reboot at the end of the script
 | |
| REBOOT=0
 | |
| HALT=1
 | |
| 
 | |
| # }}}
 | |
| 
 | |
| main() {                                                        # {{{
 | |
| 
 | |
| 	# First check in Grub default dir if saved mode is set
 | |
| 	if grep -q -E -R -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_DIR}"
 | |
| 	then
 | |
| 		printf '%b' "Reboot to windaube partition\\n"
 | |
| 		sudo grub-reboot "${WIN_GRUB}"
 | |
| 		[ "${REBOOT}" -eq "0" ] && sudo systemctl reboot
 | |
| 		[ "${HALT}" -eq "0" ] && sudo systemctl poweroff
 | |
| 	fi
 | |
| 
 | |
| 	# Otherwise check the default grub file
 | |
| 	if grep -q -E -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_PATH}"
 | |
| 	then
 | |
| 		printf '%b' "Reboot to windaube partition\\n"
 | |
| 		sudo grub-reboot "${WIN_GRUB}"
 | |
| 		[ "${REBOOT}" -eq "0" ] && sudo systemctl reboot
 | |
| 		[ "${HALT}" -eq "0" ] && sudo systemctl poweroff
 | |
| 	else
 | |
| 
 | |
| 		printf '%b' "GRUB_DEFAULT is not set in 'saved' mode\\n"
 | |
| 		sudo sed -i 's/\(^GRUB_DEFAULT.*\)/#\1\nGRUB_DEFAULT=saved/' "${GRUB_DEFAULT_PATH}"
 | |
| 		GRUB_DEFAULT_ENTRY=$(grep -E -- "#GRUB_DEFAULT=" "${GRUB_DEFAULT_PATH}" | cut -d"=" -f2)
 | |
| 		sudo grub-set-default "${GRUB_DEFAULT_ENTRY}"
 | |
| 		sudo update-grub
 | |
| 		printf '%b' "Please launch this script (${PROGDIR}/${PROGNAME} ${ARGS}) once again.\\n"
 | |
| 	fi
 | |
| }
 | |
| # }}}
 | |
| 
 | |
| # 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
 | |
| 		--reboot )          ## A reboot was requested (default behaviour)
 | |
| 			REBOOT=0
 | |
| 			HALT=1
 | |
| 			;;
 | |
| 		--halt|--poweroff ) ## A poweroff was requested
 | |
| 			REBOOT=1
 | |
| 			HALT=0
 | |
| 			;;
 | |
| 		-- )                ## End of options list
 | |
| 			## End the while loop
 | |
| 			break
 | |
| 			;;
 | |
| 		* )                 ## unknow option
 | |
| 			printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
 | |
| 			printf '%b\n' "---"
 | |
| 			usage
 | |
| 			exit 1
 | |
| 			;;
 | |
| 	esac
 | |
| 
 | |
| 	## Move to the next argument
 | |
| 	shift
 | |
| 	manage_arg=$((manage_arg+1))
 | |
| 
 | |
| 	done
 | |
| 
 | |
| fi
 | |
| 
 | |
| # }}}
 | |
| 
 | |
| main
 | |
| 
 | |
| exit 0
 |