Wensveen's Blog

November 17, 2012

Reboot to Windows from Linux

Filed under: Uncategorized — Tags: , , , — wensveen @ 1:23 am

I, like many others, have a dual-boot system. I run Debian (unstable / sid) as my primary OS, and Windows 7 when I play games. Hopefully Steam coming to Linux means that I won’t have to reboot anymore, but for the moment I do.

Of course, it’s not really hard to reboot your system and then choose the correct option in th GRUB menu, but it does mean you have to stay around to prevent GRUB from booting the default OS, instead of walking to the kitchen to get a cup of coffee / tea  / [insert beverage of choice here]. Well, there’s a solution for that, a command called grub-reboot. For example:

sudo grub-reboot 6

This will set the 7th entry in the list of OSes as the default for one boot (it’s a 0-based index). Then reboot, et voila! You still have to know which entry to boot, of course, but you can just count it once and remember. Right? Well, it’s not always that simple.

The problem is that sometimes a new Linux kernel is installed and the old one is kept, to be able to reboot safely when the new kernel doesn’t work (which has actually never happened to me, but is a possibility when running Debian unstable). In that case, you might need to reboot to a different entry. Besides that, I wanted to have a nice icon to click on, that would set the grub entry and reboot me in one simple step.

I’ve created a simple shell script that reads the correct menu entry from grub.cfg and sets that up for the next boot. After that the script calls gnome-session-quit to request a reboot (you can choose between Cancel, Restart or Power Off):

#!/bin/sh

# Find the menuentry containing Windows
#entry=`grep menuentry /boot/grub/grub.cfg | grep -n Windows | cut -d":" -f 1`
#entry=`grep -E '^menuentry|submenu' /boot/grub/grub.cfg | grep -n Windows | cut -d":" -f 1`
entry=`grep -E '^menuentry' /boot/grub/grub.cfg | grep -n Windows | cut -d":" -f 1`

/usr/sbin/grub-reboot $(($entry - 1))
# Use line below for gnome
#gnome-session-quit --power-off

# Use line below for xfce
xfce4-session-logout --fast

Update (16-5-2013): The version of Grub I currently use, adds support for submenus. Only top-level menuitems and submenus should be counted.

Update (12-2-2014): The current version of Grub does not count submenu items as bootable items. Changed reboot command to match XFCE, which is my current DE (a script could/should be made to auto-detect this).

Save it as ~/bin/reboot-to-windows.sh (or some such). Use chmod to make it executable. Also, you will probably have to fiddle with write rights to /boot/grub/grubenv. Either create a grub-reboot group that you add all allowed users to, and set that as the group for the file. Or you can add yourself and other allowed users to the ‘root’ group (Beware: This might introduce serious security issues). Or you can just make the file world-writeable (which allows all users to fuck up your system by corrupting the file).

Now, to create an entry in the Gnome-shell menu (yes, I use Gnome-shell), create a file ~/.local/share/applications/RebootWindows.desktop with the following contents:

#!/usr/bin/env xdg-open
[Desktop Entry]
Encoding=UTF-8
Name=Reboot to Windows
Exec=/home/your-username/bin/reboot-to-windows.sh
Icon=/home/your-username/bin/Windows-reboot.svg
Type=Application
Categories=System;

Where ‘your-username’ is your username, obviously. I’m not sure ~/bin/reboot-to-windows.sh will work, instead of a full path. The Icon entry refers to a svg graphic I created, here it is:
Windows reboot desktop icon
Save the file. The entry should now show up and be clickable.

Enjoy!

Update (31-12-2012): I forgot a modification in /etc/default/grub:

# Enable grub-reboot
#GRUB_DEFAULT=0
GRUB_DEFAULT=saved

Otherwise, grub will just boot the first entry, no matter what.

About these ads

6 Comments »

  1. Hey nice one Matthijs, useful for me since I’ve the same problem all the time…
    I’m gonne try to modify this one for KDE (not today..)

    Comment by arno — January 2, 2013 @ 5:10 pm

    • Cool! Let me know how it works out.

      Comment by wensveen — January 2, 2013 @ 7:53 pm

    • It seems that KDE can already do this out of the box: How to use grub-reboot in KDE

      Comment by wensveen — January 3, 2013 @ 7:29 am

      • This worked for older versions of KDE, the option to set the boot manager doesn’t seem to exist any more.
        It can still work, just a matter of finding out how to modify the shutdown dialog…

        Comment by arno — January 5, 2013 @ 11:02 am

  2. What if you duel boot without grub? I use the BIOS to boot to Windows (seperate drive).

    Comment by zew — January 26, 2014 @ 2:18 am

    • It’s probably possible to configure other boot loaders as well. If you boot to windows, you’ll at least pass the windows boot loader (NTLDR), which is configurable as well but probably not from within your linux environment (I haven’t researched this, so don’t take my word for it). Other linux boot loaders like LILO might also be configurable, but I’m not sure they support the one-time default that GRUB does (boot once into Windows, then reset the default to what it was before).
      Selecting the boot drive from the BIOS (press F12, probably?) is way harder to program. The boot preferences are stored in CMOS/NVRAM or somewhere in the SMBIOS. Your best bet would be something like libsmbios2/smbios-utils or nvramtool. Be warned though, that you can easily screw up your system when mucking with NVRAM and SMBIOS, especially when writing to it.

      Installing GRUB would probably be your best and safest bet. Installing GRUB on your default boot drive, leaving the windows drive alone (if you want to be sure, take it out), is quite safe nowadays. Just don’t install GRUB on the MBR of the Windows partition.

      Comment by wensveen — January 27, 2014 @ 8:24 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: