Fri 18th Jan 2013 at 14:35

Rebooting into Windows from Linux

While trying to get my LiveScribe pen working with Ubuntu, I managed to completely wipe my scripts director. Essentially, I had downloaded the LibreScribe program for of zeal at the promise that it did exactly what it was after. However, executing make promptly erased all files under ~/bin/ and then screwed an error! So be warned.


However, this is not about the how I got to this stage, but and description of one of my most useful scripts... The one that allows be to go and make a coffee while my laptop reboots into Windows. It is a rare occasion that I need to launch Windose, but it happens and over the years I have gotten irritated by having to wait until my laptop has shutdown and the boot menu appears, so I can select Windows. I remember Mandrake having the menu option within KDE, however, I have not seen it as a default available option for a good number of years now. This caused me to write a script, a now lost script, and so I started again.

So, rather than wittering on about how I wrote the script, I'll just document what is needed to be done with a few explanations about the various commands as we go along.

Please Note: This will only word for Grub2


Preparing Grub

Firstly we need to ake a quick tweak to the Grub configuration to allow the defaulft highlight menu option to be set but not saved. We do this by editing /etc/default/grub as root:

sudo vim /etc/default/grub

and modify the "GRUB_DEFAULT=" value to "saved" so that it looks like this:


Once you have edited this file, you will need to let Grub know that the config has been chaged. To do this, you will need to execute the following command:

sudo update-grub

You will need to run this command every time you edit Grub's configuration files. However, do not worry, the edit you have just made will allows us to switch the default boot menu option without editing any config files.


The script

A lot of the exampes I found on the web refer to manually looking up the menu number and setting it. However, this is no good as it will mean your script will cease functioning when Ubuntu releases a new kernel update or some other reason why the menu order may change. So, the most robust method I found is to search for the boot menu option. Other examples that use the same method as I do search for "Windows", but I had several references to windows in to boot menu and so I prefer so search for the particition, but obviously you we need to tweak this to your needs.

~$ grep '^menuentry.*sda2' /boot/grub/grub.cfg
menuentry "Windows 7 (loader) (on /dev/sda2)" --class windows --class os {

What we need to do, is to pass the correct value to grub-reboot which allows you to reboot to a given option at the next reboot, but resets everything back afterwards. Some of the examples I found try to work out the numeric id of item's position in the menu list. I found this to be a bit hit and miss. It used to be very easy when there was a /boot/grub/menu.lst file, but with the new version of Grub, this no longer exists and thus you have to use the /boot/grub/grub.cfg file which contains, in my case, a lot of hidden menu options, so the numeric value never seems to be correct.

The command grub-reboot, however, supports the menu names and so the I can use the following:

sudo grub-reboot "Windows 7 (loader) (on /dev/sda2)"

So all we need to do now, is to strip all the extra text we do not need from the result of our grep and pass it to grub-reboot. This can be achieved all in one line and piping it to xargs.

grep menuentry /boot/grub/grub.cfg  | grep sda2 | sed 's/^menuentry //; s/ --.*$//' | xargs sudo grub-reboot
sudo reboot

This is oke to execute at the command line, but not very good if we want to trigger this command from our GUI (Gome or KDE) and so ideally we do not want to have a terminal window open and for sudo to prompt us for our password. Now, as ever with linux, there are several ways to do this but for the sake of this tutorial, we shall replace sudo with the command gksudo. The resulting script looks as follows:

grep '^menuentry.*sda2' /boot/grub/grub.cfg  | sed 's/^menuentry //; s/ --.*$//' | xargs gksudo --description="Reboot to Windows" grub-reboot
gksudo reboot


Creating an icon

You can then create a menu link either by using alacarte to create a menu item or as I prefer, creating an Shortcut on the desktop. To do the latter, you just need to create the file ~/Desktop/Windows.desktop and place the following code into it:

[Desktop Entry]
Name[en_GB]=Reboot to Windows
Name=Reboot to Windows



I obviously didn't come up with this without searching on the web first, so the following are the links that refreshed my memory, but didn't quite work.

This site uses cookies, please read my cookie policy.