Oct
30

Reboot or Hibernate into Windows from Ubuntu Easily

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!

4 Comments to “Reboot or Hibernate into Windows from Ubuntu Easily”

  • Roger November 1, 2010 at 2:38 pm

    Heh, I had never really thought of doing that before, but that is a really neat way to do it. Kind of like the best of both worlds!

    In truth, I am more likely to use a Virtual Machine now, as this is the most efficient for work related stuff, but I do also have a Windows 7 Dual boot setup for a game, so I am going to do a little playing with it. There are times when I need to switch back to Linux for a sec or two, but doing the full reboot is a pain…

  • infinitree May 27, 2011 at 1:15 am

    Is there a way to add an option to “hibernate to windows” or “reboot to windows” to the power dropdown menu?

    • mr reply September 7, 2012 at 4:45 pm

      I know it`s outdated but in case someone stumbles upon this: If you are using Linux Mint (ie. Cinnamon) you might consider using ShutdownApplet@shelley which is a nice one-click restart/shutdown etc. menu. Then just edit applet.js in the ShutdownApplet@shelley folder (usually in your home directory -> .local/share/cinnamon/applets) like this:

      this.menu.addAction(_(“Boot to Windows”), function(event) {
      Util.spawnCommandLine(“gksudo /usr/local/bin/win-reboot”);
      });

      Of course you may need to change the path to your script. When you open the applet.js it will be quite clear, where you have to put this peace. Just add it somewhere between the other entries that look alike.

  • Sammie I. Curry June 23, 2013 at 6:46 am

    allows you to mount a SMB file share. Once a SMB share is mounted it acts similar to a local hard drive (you can access the SMB share with your file browser (nautilus, konqueror, thunar, other).

Post comment

%d bloggers like this: