Skip to content

Instantly share code, notes, and snippets.

@jarulsamy
Last active June 23, 2022 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarulsamy/08fae5c218a1f461b3c587215e745192 to your computer and use it in GitHub Desktop.
Save jarulsamy/08fae5c218a1f461b3c587215e745192 to your computer and use it in GitHub Desktop.
Reboot to Linux From Windows
:: _Sigh_ Windows dumbness...
powershell .\RebootLinux.ps1
# Figure this out with 'GET-CimInstance -query "SELECT * from Win32_DiskDrive"'
$LinuxRootDisk = "\\.\PHYSICALDRIVE0"
$LinuxRootPart = 2
# This needs to be run as admin, thus the funky Start-Process stuff.
$cmd = "wsl --mount $LinuxRootDisk -p $LinuxRootPart --bare"
Start-Process -Verb RunAs cmd.exe -Args '/c', $cmd
# Call the WSL script to set all the necessary GRUB stuff.
bash -c "sudo /home/joshua/RebootLinux.sh"
# Reboot the system
shutdown /r /t 5 /d p:0:0 /c "Rebooting to a much better OS :)"
pause
#!/usr/bin/env bash
# /home/USER/RebootLinux.sh
# UUID of Linux partition with GRUB installation at /boot/grub.
ROOT_UUID="UUID"
# Mount point within WSL.
MOUNT_POINT="/mnt/root"
# Desired GRUB default boot entry on reboot.
BOOT_ENTRY="0"
if [ "$#" -gt 1 ]; then
printf "%s" "Usage: $0 [ENTRY_NUM]"
exit 1
elif [ "$#" -eq 1 ]; then
BOOT_ENTRY="$1"
fi
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
printf "%s\n" "Rerun as root please."
exit 1
fi
if [ ! -d "$MOUNT_POINT" ]; then
printf "%s\n" "Mount point doesn't exist!"
exit 1
fi
if ! mount -U "$ROOT_UUID" "$MOUNT_POINT"; then
printf "%s\n" "Failed to mount root partition"
exit 1
fi
if ! grub-reboot --boot-directory="$MOUNT_POINT/boot" "$BOOT_ENTRY"; then
printf "%s\n" "Failed to call grub-reboot"
exit 1
fi
umount "$MOUNT_POINT"
printf "%s\n" " .--."
printf "%s\n" " |o_o |"
printf "%s\n" " |:_/ |"
printf "%s\n" " // \ \\"
printf "%s\n" " (| | )"
printf "%s\n" "/'|_ _/'\\"
printf "%s\n" "\___)=(___/"
printf "%s\n" "Done on the WSL side. See you in Linux friendo!"
@jarulsamy
Copy link
Author

jarulsamy commented Jun 23, 2022

This set of scripts is used to quickly reboot from Windows to Linux without having to select the other OS within the Grub menu. This is done by mounting the root partition in WSL, then using grub-reboot to set the necessary boot variables.

The bash and powershell scripts do all the heavy lifting. The batch script is just a convenient way to call it and reboot the system without having to manually open a powershell window. The fact that in 2022 you can double-click on an .exe, .bat, or .vbs script and run it just fine, but you can't with powerShell is mind boggling to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment