Monday 8 August 2011

Changing Ubuntu Natty 11.04 grub default booting operating system

Recently an acquaintance's computer needed significant repair. It had been used by several of his immediate family and some of them had downloaded significant amounts of dubious materials from dubious sources resulting in virus infection and system slowdown. I reinstalled a fresh copy of Windows XP + Ubuntu 11.04, dual booting, on the basis that both provided for his needs, namely office applications, email and internet. Ubuntu, as similar as its interface is to Windows from a basic perspective, was not to his taste, so I had to change the default boot order of Grub.

Ubuntu, by default, sets the most recently installed kernel as the default boot option, and then scans the hard disk for other bootable partitions, which it lists in order. Every time a new kernel is installed, the boot order is updated and subsequently for any lasting changes to be made, they need to be added to Ubuntu's grub autoconfiguration.

Ubuntu generates its grub configuration from a series of rules in the /etc/grub.d folder. A default install will have something similar to:
/etc/grub.d$ ls -1
00_header
05_debian_theme
10_linux
20_linux_xen
20_memtest86+
30_os-prober
40_custom
41_custom
README
The README file in this folder states:
All executable files in this directory are processed in shell expansion order.

00_*: Reserved for 00_header.
10_*: Native boot entries.
20_*: Third party apps (e.g. memtest86+).

The number namespace in-between is configurable by system installer and/or administrator. For example, you can add an entry to boot another OS as 01_otheros, 11_otheros, etc, depending on the position you want it to occupy in the menu; and then adjust the default setting via /etc/default/grub.
Based upon this, I decided to create a file, 07_windows_default and, copying the existing Windows stanza created by the autodetect routine in the existing /boot/grub/grub.cfg (look for the section entitled "### BEGIN /etc/grub.d/30_os-prober ###") with some slight tweaks to the name to make it stand out from the auto options, I ended up with something along the lines of:
#!/bin/sh
cat <<EOF
menuentry "Start Windows 7" --class windows --class os {
        insmod part_msdos
        insmod ntfs
        set root='(/dev/sda,msdos1)'
        search --no-floppy --fs-uuid --set=root DRIVEUUID
        chainloader +1
}
EOF
I've used Windows 7 here as my home system doesn't have XP but Windows 7 as a dual boot option. The caveat here is that if there are any special shell characters, like $, need to be backslash escaped. I've also removed my XP partition's UUID in the above example - again, I'd recommend people leverage the autodetect routine of Ubuntu and just copy the Windows stanza out of the existing auto-generated config.

Once this file is in place, and has been made executable (Eg. chmod a+x 07_windows_default), running sudo update-grub will trigger Ubuntu to re-run its rules and insert the above Windows stanza above the Ubuntu kernel entries.

Reboot - et voila, Windows is now your default boot option and Ubuntu kernel upgrades won't break it.

No comments:

Post a Comment

Hey... thanks for leaving a comment! Due to Casino spam, I've had to turn on moderation for some of the posts. Apologies - I do read every comment left!