It’s often the case that I’m working on something in Ubuntu and need to switch over to Windows. For whatever reason, I got it in my head that it was too difficult to select hibernate or reboot from the power menu, wait for the system to power down, select Windows from the boot menu list, and wait for Windows to boot up. I wanted to be able to click on an icon, go get a sandwich, and walk back to a Windows login.

So, here are two scripts that accomplish this. When run, they will locate the boot entry for your Windows system, select that as the default boot entry, and either reboot or hibernate (with a restart after hibernation).



There are a couple preliminary things that must be done to allow this to happen. First, the hibernate script requires ‘s2disk’ in order to be able to instruct the system to hibernate and reboot afterwards. This is in the ‘uswsusp’ package and can be installed with sudo apt-get install uswsusp .

Secondly, in order to avoid needing a password prompt to reboot the machine, you need to add permissions for the user to run some commands without needing a password. This is accomplished by adding some lines to the /etc/sudoers file. Note: Editing the /etc/sudoers file is a potentially dangerous thing. If you get any reported syntax errors from visudo when you save and exit the editor, do not ignore them! If you mess up the /etc/sudoers file you may end up with a system that will not allow you to use sudo, and this can only be fixed by booting from a live-cd.

To edit the sudoers file, run sudo visudo . It is very important that you use this method; it checks the syntax of the file when you save it, and will prevent you from messing up your ability to use sudo (which can happen if you ignore error messages or edit /etc/sudoers directly).

So, open up the file and add these lines

# Needed for hibernate script user ALL=NOPASSWD: /usr/sbin/grub-reboot user ALL=NOPASSWD: /usr/sbin/s2disk user ALL=NOPASSWD: /sbin/reboot

This will allow ‘user’ to run those three commands with sudo without needing a password. Then, put the following scripts into /usr/local/bin

reboot_to_windows

#!/bin/bash WINDOWS_ENTRY = ` grep menuentry / boot / grub / grub.cfg | grep --line-number Windows ` MENU_NUMBER =$ ( ( ` echo $WINDOWS_ENTRY | sed -e "s/:.*//" ` - 1 ) ) sudo -n grub-reboot $MENU_NUMBER sudo reboot

hibernate_to_windows

#!/bin/bash WINDOWS_ENTRY = ` grep menuentry / boot / grub / grub.cfg | grep --line-number Windows ` MENU_NUMBER =$ ( ( ` echo $WINDOWS_ENTRY | sed -e "s/:.*//" ` - 1 ) ) sudo -n grub-reboot $MENU_NUMBER gnome-screensaver-command --lock sudo -n s2disk --parameter "shutdown method=reboot"

Then, all you need to do is add a launcher icon to the desktop, and when run the system will shutdown or hibernate, set the default entry to Windows, and reboot!

Like this: Like Loading...

Related