Dual Boot Magic
I dual boot on two out of three computers but I really only spend less than 1% of the time in Windows. Occasionally I do find the occasion to bring the red headed stepchild out of the basement. As if this weren’t annoying enough I would often reboot, lack the patience to babysit the reboot, and come back just in time to see that I missed the grub menu and now I have to reboot again. Well some time ago I found a solution to this mess. Enter grub-set-default <num>—this nifty command sets the grub entry to load by default for the next boot only. So I count my grub entries and put the proper number in a nifty reboot script. Alas, by the next time I use this script various Ubuntu updates have changed my grub menu such that now I get some old kernel instead of Windows like I wanted. This clearly wouldn’t do, so I fixed it. Now you can do the same.
Save the following to a script, such as ~/bin/windows.
#! /bin/bash
TARGET=windows
MENU=/boot/grub/menu.lst
ENTRY=$[`grep ^title $MENU | grep -n -i $TARGET | cut -d: -f1` - 1]
sudo umount /media/surfer
sudo grub-set-default $ENTRY
sudo reboot
The umount /media/surfer line just unmounts my network media, speeding up the reboot process working around an annoying Ubuntu bug where the shutdown hangs waiting for network drives even as the network is already down, hence no response from the share and a long timeout.
The ENTRY= line is the interesting part. First grep ^title grabs all the grub entries from which we will count the indexes. Next, grep -n $TARGET prints the line number of the entry we are interested in (Windows) along with the line. cut -d: -f1 splits the line on ‘:’ and returns the first field, the number we want to boot. Finally, grep counts from 1, but grub counts from 0, so $[<stuff> - 1] does bash math, wich is like normal math mostly, and hence we have the proper argument to give grub-set-default, et voila.
You can even put an icon in your panel or desktop that will run this script, giving you Windows in one easy click.