20 Mar

Guff about Grub 2 – Changing default boot target

Category:Hacking, Howto's

Grub 2 (btw, why is it called that since the version reports as 1.97 or something?) has a fucked up way of doing things. A grand example of engineers designing something for themselves. Changing default boot target in Grub 1 was as easy as editing menu.lst, but this is not the case with Grub 2. Here, it’s a mish-mash, a hodge-podge, of different files that rely on each other, and are mudged together by various scripts to make a working grub boot menu.

Without further ado, to change the default boot target in Grub, i.e. get Windows to boot as default, or a specific kernel version (if you have many installed) would be done like this:

Open up a terminal, and open up the file /boot/grub/grub.cfg. My file looks like this:

[…the header is here, but omitted….]

### BEGIN /etc/grub.d/10_linux ###
menuentry “Ubuntu, Linux 2.6.31-19-generic-pae” {
if [ -n ${have_grubenv} ]; then save_env recordfail; fi
set quiet=1
insmod ext2
set root=(hd1,1)
search –no-floppy –fs-uuid –set c3ebcb50-f07e-4985-9734-471bb5c607da
linux   /boot/vmlinuz-2.6.31-19-generic-pae root=UUID=c3ebcb50-f07e-4985-9734-471bb5c607da ro   quiet splash
initrd  /boot/initrd.img-2.6.31-19-generic-pae
menuentry “Ubuntu, Linux 2.6.31-19-generic-pae (recovery mode)” {
if [ -n ${have_grubenv} ]; then save_env recordfail; fi
insmod ext2
set root=(hd1,1)
search –no-floppy –fs-uuid –set c3ebcb50-f07e-4985-9734-471bb5c607da
linux   /boot/vmlinuz-2.6.31-19-generic-pae root=UUID=c3ebcb50-f07e-4985-9734-471bb5c607da ro single
initrd  /boot/initrd.img-2.6.31-19-generic-pae
### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/20_memtest86+ ###
menuentry “Memory test (memtest86+)” {
linux16 /boot/memtest86+.bin
menuentry “Memory test (memtest86+, serial console 115200)” {
linux16 /boot/memtest86+.bin console=ttyS0,115200n8
### END /etc/grub.d/20_memtest86+ ###

### BEGIN /etc/grub.d/30_os-prober ###
menuentry “Windows 7 (loader) (on /dev/sda1)” {
insmod ntfs
set root=(hd0,1)
search –no-floppy –fs-uuid –set 561ed31d1ed2f4c9
chainloader +1
### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the ‘exec tail’ line above.
### END /etc/grub.d/40_custom ###

What you want to look at here is the name of the menu entry you wish to boot as default. In my example, we’ll use Windows 7. The relevant bit is in red.

Select and copy the relevant section, that is, whatever follows menuentry, and inside ” “, whatever the entry you want to boot as default.

Close the file without saving any changes, and open up (as sudo) the /etc/default/grub

In the beginning of the file, there’s a line that looks like:  GRUB_DEFAULT=0

The interwebs are full of instructions on changing the number to something other than 0, where 0 corresponds to the first entry in the grub.cfg file we looked at earlier. This will work just fine if you never ever install a new kernel, a second or third OS, or do any changing what so ever to your grub configuration. Why i like to use the actual menuentry name is that that will not change, unless you want to change it.

Paste in what you copied from grub.cfg replacing the 0. My new entry now looks like: GRUB_DEFAULT=”Windows 7 (loader) (on /dev/sda1)”

It’s very important to remember the quotes, because otherwise the next step will fail, since it doesn’t understand a line that ends with a parenthesis ).

Save and close the /etc/default/grub file, and run the following command: sudo update-grub

This will parse the /etc/default/grub file, and generate a new /boot/grub/grub.cfg file which controls the actual menu (you could call this the old menu.lst, but one you can’t edit by hand without fucking everything up).

If no errors are printed, you are good to go. Run sudo reboot or use the gui to reboot the computer. Your new default entry should now be highlighted in the grub menu, and if you don’t touch anything, should boot.

One thought on “Guff about Grub 2 – Changing default boot target”

  1. Endless thanks my friend. I had to dig through a ridiculous number of non-existent directories before finding this post.

Leave a Reply

Your email address will not be published. Required fields are marked *