Created
October 28, 2023 17:54
-
-
Save solariz/0fda3e95acfc4c089300afa63d6d7d98 to your computer and use it in GitHub Desktop.
Boot from Windows
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 | |
# Tiny little helper script to automatically grab the boot ID | |
# from grub.cfg and force to boot into windows next booth then reboot. | |
# https://tcpip.wtf | |
# Request sudo access | |
if [ "$EUID" -ne 0 ]; then | |
echo "Please run as root" | |
sudo "$0" | |
exit | |
fi | |
# Rest of script runs as root | |
# Path to grub config | |
grub_cfg="/boot/grub2/grub.cfg" | |
# Check if grub config file exists | |
if [ ! -f "$grub_cfg" ]; then | |
echo "Grub config $grub_cfg not found" >&2 | |
exit 1 | |
fi | |
# Search for Windows Boot Manager in grub config | |
if ! grep -q "Windows Boot Manager" "$grub_cfg"; then | |
echo "Windows Boot Manager not found in $grub_cfg" >&2 | |
exit 1 | |
fi | |
# Extract Windows boot ID from grub config | |
windows_boot_id="" | |
windows_boot_line=$(grep "Windows Boot Manager" "$grub_cfg") | |
if [[ $windows_boot_line =~ menuentry\ .*\ \$menuentry_id_option\ \'([^\']+)\' ]]; then | |
windows_boot_id="${BASH_REMATCH[1]}" | |
fi | |
# Validate extracted ID | |
if [ -z "$windows_boot_id" ]; then | |
echo "Failed to extract Windows boot ID" >&2 | |
exit 1 | |
fi | |
echo "Found Windows boot ID: $windows_boot_id" | |
# Confirm reboot to Windows | |
read -p "Reboot to Windows (Y/N)? " confirm | |
if [[ $confirm != [Yy] ]]; then | |
exit 0 | |
fi | |
# Reboot using grub-reboot or grub2-reboot | |
if command -v grub-reboot >/dev/null 2>&1; then | |
if ! sudo grub-reboot "$windows_boot_id"; then | |
echo "grub-reboot failed" >&2 | |
exit 1 | |
fi | |
elif command -v grub2-reboot >/dev/null 2>&1; then | |
if ! sudo grub2-reboot "$windows_boot_id"; then | |
echo "grub2-reboot failed" >&2 | |
exit 1 | |
fi | |
else | |
echo "Neither grub-reboot nor grub2-reboot found" >&2 | |
exit 1 | |
fi | |
sudo reboot --reboot --no-wall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment