Thursday, May 6, 2010

Changing GRUB's Boot Order

When you install GNU/Linux, chances are you installed GRUB (the GRand Unified Bootloader) as well, especially if you have multiple operating systems installed on the computer. GRUB is that menu that appears shortly after you boot up your computer that allows you to select which operating system you want to boot into.



By default, GRUB's first choice is the GNU/Linux distribution used to install it. For example, say you have a computer with Windows 7 on it, and decide you want to give Linux a spin. After the installation is complete booting your computer will take you to a rather simple menu that asks you which operating system you want to boot into, the Linux distribution you've installed, or Windows.


It's here a problem arises. After a certain number of seconds without user interaction, GRUB will automatically choose the first option listed, which is generally the OS used to install it. This means that if you have a dual-boot Windows/Ubuntu setup, and you want to boot into Windows, you'll have to monitor your computer while starting to prevent it from automatically launching into Ubuntu. This guide will cover how to re-arrange the order of the items that appear on the GRUB boot-menu, as well as other minor customizations.


First things first: There are two versions of GRUB out there, grub, and grub2. Newer distributions such as Ubuntu 10.04 have switched to GRUB 2, but GRUB (now referred to as the 'Legacy Edition') is still fairly common. This guide will cover the procedure for both GRUB versions. Of course you still have to tell which version of GRUB you are using. Run "grub-install -v" in the terminal and it will give you a version number. .97 is the Legacy edition, anything above that is GRUB2. Ubuntu 10.04 ships with version 1.98 which is GRUB 2.


GRUB Legacy


Modifying GRUB is fairly straight-forward, you can make changes by editing the GRUB Menu's configuration file: '/boot/grub/menu.lst'. After altering the file, you will need to save it, and in order to save it, you need superuser privileges. If you try to open it in gedit or kate you won't be able to save it. You'll need to launch your preferred text editor as a superuser in order to save the file after revision. Try using these in terminal to accomplish that.



GNOME:
gksu gedit /boot/grub/menu.lst
KDE:
kdesu kate /boot/grub/menu.lst

Now for the actual configuration file itself, here's mine:





# Config file for GRUB - The GNU GRand Unified Bootloader
# /boot/grub/menu.lst


# DEVICE NAME CONVERSIONS
#
# Linux Grub
# -------------------------
# /dev/fd0 (fd0)

# /dev/sda (hd0)
# /dev/sdb2 (hd1,1)
# /dev/sda3 (hd0,2)
#

# FRAMEBUFFER RESOLUTION SETTINGS

# +-------------------------------------------------+
# | 640x480 800x600 1024x768 1280x1024
# ----+--------------------------------------------
# 256 | 0x301=769 0x303=771 0x305=773 0x307=775
# 32K | 0x310=784 0x313=787 0x316=790 0x319=793
# 64K | 0x311=785 0x314=788 0x317=791 0x31A=794

# 16M | 0x312=786 0x315=789 0x318=792 0x31B=795
# +-------------------------------------------------+
# for more details and different resolutions see
# http://wiki.archlinux.org/index.php/GRUB#Framebuffer_Resolution

# general configuration:

timeout 30
default 0
color light-blue/black light-cyan/blue

# boot sections follow
# each is implicitly numbered from 0 in the order of appearance below

#
# TIP: If you want a 1024x768 framebuffer, add "vga=773" to your kernel line.
#
#-*

# (0) Arch Linux

title Arch Linux
root (hd0,2)
kernel /vmlinuz26 root=/dev/disk/by-uuid/a8a2e826-2f66-47df-b522-9f842964897f ro
initrd /kernel26.img

# (1) Arch Linux

title Arch Linux Fallback
root (hd0,2)
kernel /vmlinuz26 root=/dev/disk/by-uuid/a8a2e826-2f66-47df-b522-9f842964897f ro
initrd /kernel26-fallback.img

# (2) Windows

title Windows 7 x64
rootnoverify (hd0,0)
makeactive
chainloader +1



Now at the bottom you see the actual menu items. Arch Linux, Arch Linux Fallback, and Windows 7 x64. Each of the items has a few lines of information, and separate menu items are separated from each other by a blank line. Let's take a look at the first menu entry.




# (0) Arch Linux

title Arch Linux

root (hd0,2)

kernel /vmlinuz26 root=/dev/disk/by-uuid/a8a2e826-2f66-47df-b522-9f842964897f ro

initrd /kernel26.img



The first line, the one that starts with '#' is for your information. It is not read by GRUB, but serves as more of a comment for the user reading the configuration file to make it a little more organized.


The line under it, the 'title' line, holds the text that will be in the GRUB Menu's entry for that selection. I.e. My GRUB Menu will give me the choices Arch Linux, Arch Linux Fallback, and Windows 7 x64. If I changed the line that reads 'title Arch Linux' to 'title Foobar', my Grub Menu would present me with the choices Foobar, Arch Linux Fallback, and Windows 7 x64. This is useful if the default title GRUB chooses is not to your liking. For example, GRUB frequently identifies Windows Vista as 'Windows Vista/Longhorn'.


You can also change the boot sequence fairly easily. For example, changing:



# (0) Arch Linux
title Arch Linux

root (hd0,2)
kernel /vmlinuz26 root=/dev/disk/by-uuid/a8a2e826-2f66-47df-b522-9f842964897f ro
initrd /kernel26.img

# (1) Arch Linux
title Arch Linux Fallback

root (hd0,2)
kernel /vmlinuz26 root=/dev/disk/by-uuid/a8a2e826-2f66-47df-b522-9f842964897f ro
initrd /kernel26-fallback.img

# (2) Windows
title Windows 7 x64

rootnoverify (hd0,0)
makeactive
chainloader +1

to



# (0) Windows

title Windows 7 x64
rootnoverify (hd0,0)
makeactive
chainloader +1

# (1) Arch Linux

title Arch Linux
root (hd0,2)
kernel /vmlinuz26 root=/dev/disk/by-uuid/a8a2e826-2f66-47df-b522-9f842964897f ro
initrd /kernel26.img

# (2) Arch Linux

title Arch Linux Fallback
root (hd0,2)
kernel /vmlinuz26 root=/dev/disk/by-uuid/a8a2e826-2f66-47df-b522-9f842964897f ro
initrd /kernel26-fallback.img

will change the order of the menu entries in GRUB's Menu. Since Windows 7 is first on the list, after 30 seconds without user intervention, GRUB will default to booting into Windows. You can change the amount of time before this happens if you desire by altering the 'timeout 30' line to read the amount of seconds you wish (i.e. 'timeout 15').



GRUB 2


GRUB 2's configuration files are a bit more complex, and offer much more versatility. Start by taking a look at your /boot/grub/grub.cfg file. Note: Do not edit this file. You'll notice right away that this is not a pretty document. Look for the lines that start with 'menuentry', Ctrl-F is your friend here. You should find something that resembles this:



menuentry 'Ubuntu, with Linux 2.6.32-21-generic' --class ubuntu --class gnu-linux --class gnu --class os {
    recordfail

    insmod ext2
    set root='(hd0,1)'
    search --no-floppy --fs-uuid --set e6948097-4d7c-4bc5-a4fe-8e760a11f5c5
    linux /boot/vmlinuz-2.6.32-21-generic root=UUID=e6948097-4d7c-4bc5-a4fe-8e760a11f5c5 ro quiet splash
    initrd /boot/initrd.img-2.6.32-21-generic
}

menuentry 'Ubuntu, with Linux 2.6.32-21-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
    recordfail
    insmod ext2
    set root='(hd0,1)'
    search --no-floppy --fs-uuid --set e6948097-4d7c-4bc5-a4fe-8e760a11f5c5
    echo 'Loading Linux 2.6.32-21-generic ...'

    linux /boot/vmlinuz-2.6.32-21-generic root=UUID=e6948097-4d7c-4bc5-a4fe-8e760a11f5c5 ro single
    echo 'Loading initial ramdisk ...'
    initrd /boot/initrd.img-2.6.32-21-generic
}

Each line that begins with 'menuentry' is precisely that. Above we see two of them, Ubuntu, and it's recovery mode. Now, the first one we'll refer to as menu entry 0, and the second one will be menu entry 1. This may seem counter-intuitive as most human beings start counting with 1, but with computers starting with 0 isn't as uncommon as one might think. Now say you wanted to configure the GRUB 2 Menu so that the recovery mode is the default option. For this we'll need to edit the /etc/default/grub file.



Like with GRUB legacy, you'll also need superuser privileges to save the configuration file after editing it, so you'll need to launch your text editor with elevated privileges. Run the following in the terminal depending on your DE of choice:



GNOME:
gksu gedit /etc/default/grub

KDE:
kdesu gedit /etc/default/grub

This configuration file is more human-readable. Look for "GRUB_DEFAULT=0", which should be relatively close to the top. By changing this to "GRUB_DEFAULT=1", The second, or menu entry #1 will be selected by default in the GRUB menu, becoming the default choice. Say Windows was installed as well, and occupied a third (or #2) menu entry. By setting "GRUB_DEFAULT=2" Windows will be automatically highlighted on GRUB's Menu, and will be launched after the timer runs out. After editing this file to your liking, save it, and run 'sudo update-grub'.


That's it, you're done! Restart your computer and see the changes for yourself.

Recommended Reading:
GRUB 2 Documentation
GRUB 2 Guide
GRUB 2 Title Tweaks


No comments:

Post a Comment