Exploring the next generation GRUB bootloader

Reboot


The long-awaited GRUB 2 brings a new look and many new features to the GRUB bootloader.

By Andreas Bohle

Lars Lieber, Fotolia

The GRUB bootloader appeared on the scene several years ago as an alternative to the once-popular Lilo (Linux Loader), and since that time, GRUB has become the standard tool for loading and starting a Linux system. Many end users associate GRUB with the boot menu that appears during the startup process on computers with a multi-boot configuration; however, even if you don't interact directly with GRUB, it is still behind the scenes, finding and loading the kernel as the system comes to life.

The standard GNU GRUB bootloader is based on the original GRUB (Grand Unified Loader), which was created by Erich Stefan Boleyn. GRUB has served the community well over the years, but for some time, developers have been noticing signs of age. The code base had collected a number of complications over the course of time from new feature implementations, and, even with all the improvements, the software lacked several features that would be useful with the next generation of computers.

The PUPA project (Preliminary Universal Programming Architecture for GNU GRUB) [1] was a research initiative (sponsored by the Information-Technology Promotion Agency of Japan) with the purpose of re-envisioning and revitalizing GRUB. GNU developers have taken up this initiative and rolled out a new version of GRUB based on PUPA.

GRUB 2 [2] is appearing in new versions of the major Linux distros. (If it hasn't reached your distro yet, watch for it in an upcoming release.) According to the GNU project, GRUB 2 "...has been rewritten from scratch to clean up everything for modularity and portability."

The stated goals for GRUB 2 were to provide:

The result is a simpler structure, a modular model, and support for non-Latin letters in the Start menu. Add to this a graphical interface for the start screen that supports easy modification with the use of a theming system.

With the previous version of GRUB, if you wanted to add an entry to your boot menu or edit an existing entry, you had to load the /boot/grub/menu.lst file in an editor and customize the file. GRUB 2 puts an end to this.

Although you still find a grub.cfg file in the /boot/grub directory after loading the bootloader, the file just contains the configuration that you create calling update-grub. The developers do not envisage users modifying this file manually.

GRUB 2 generally automates many steps. Helper scripts search the partitions on a machine for known operating systems and create the required entries for them without any user interaction. In many cases, this is fine for a dual-boot system wherein Linux and Windows share a machine.

The new version also changes the file and directory structure (see Table 1). On an Ubuntu/Debian system, you can use /etc/grub/default to define the software's general behavior. For example, if necessary, you can disable the function that requires you to specify the partition to boot in the form of a UUID via the GRUB_DISABLE_LINUX_UUID=true function. This means that you can continue to carry on working with familiar device names.

Most options will not be of interest to most users. On a positive note, the developers now provide a function that lets you hold down the Shift key to display the menu onscreen, although you would not see it under normal circumstances. Then you can modify the configuration manually in case of an error.

GLOSSARY

UUID: Universally Unique Identifier, a standard for identifiers. A UUID comprises a 16-byte number, which typically resolves to five groups of hexadecimal numbers. Because of the huge range of numbers, the probability of duplicate UUIDs is virtually zero, and this removes the need for central assignment.

GRUB 2 in Ubuntu

The first attempts to release GRUB 2 into the wild were fraught with problems: Ubuntu, for instance, supplied version 1.97 beta 4 of GRUB with the current 9.10 release of Ubuntu "Karmic Koala" (Figure 1) [3], and many users reported problems, including unbootable systems. However, the Ubuntu wiki has solutions for many of these issues. One of the more serious problems involved password protection for the bootloader: A bug allows users to lift the block if they just guessed the first letter of the password [4]. The developers have now fixed this bug and released a new version. Considering this critical vulnerability, it is always a good idea to watch the typical channels for security updates.

The final Karmic Koala release provides a complete migration to GRUB 2. The new GRUB caused issues on some machines, especially for people updating from Ubuntu 9.04 [5]. If you want to install GRUB 2 on a previous version of Ubuntu (or, similarly, on other distros with a Apt-based packaging system), pop up a terminal and give the following command:

$ sudo apt-get install grub2

Then install the grub-pc package, which has a dependency on grub-common. After doing so, you have all the required files in place on your machine. When you install, you are shown a dialog informing you that you have GRUB Legacy (that is the previous version) installed on your machine. Confirm by pressing OK and, in the dialog that follows, press Yes to add an entry to your current menu that lets you go to GRUB 2 and test it.

After doing so, the configuration routine will identify all your start options in the existing GRUB configuration and display them to you for editing. This completes the setup. Following this, both bootloaders are installed in parallel on your system, although you will only see the legacy menu with an extra entry for GRUB 2 on booting.

Select this entry and then launch all your systems one after another from the GRUB 2 menu. If everything works as intended, pop up a terminal and give the following command:

$ sudo upgrade-from-grub-legacy

The system will ask you to choose the device from which you will be booting your machine. Make sure you select the right device by pressing the Space key. If you fail to do so, your system will boot into an Error 15 (start device not found) error message next time you restart.

If you have a system that shows you an Error 15 message, boot from a live CD, mount the root partition in /mnt, use the following commands to mount the dev filesystem, and then change to a chroot system:

$ sudo mount --bind /dev /mnt/dev
$ sudo chroot /mnt

Then give the dpkg-reconfigure grub-pc command to access the GRUB 2 configuration routine. After selecting the right entry, quit the chroot environment by pressing Ctrl+D, unmount the mounted filesystem, and reboot.

Figure 1: Karmic Koala was the first distribution to promote the new version of GRUB in a major way.

Adding a Menu Entry

Linux installers typically create a new boot menu when you install a new system, however, once in a while, the process goes awry. For instance, part of your configuration might have escaped auto-detection, or you might need to add a special boot parameter to load the system successfully. The first place to start when you want to make a change to your GRUB 2 configuration is to understand how GRUB 2 works: When GRUB starts, it launches all the scripts in /etc/grub.d/ (assuming you ran sudo chmod u+x script_name to set the right permissions. System scripts will have the right permissions set by default.

Numbers in the file name ensure the correct execution order. Listing 1 shows the contents of a custom start script. This example boots the System Rescue CD from the hard disk. (Note that Listing 1 is a here document, which you can use to output a string or to redirect a string into a file.)

Line 5 in Listing 1,

set root=(hd0,10)

assigns the 10th partition on the first hard disk as the root filesystem. In the new version, the partition numbering schema starts at 1, instead of 0 as did its predecessor. The linux and initrd commands let you define the paths to the kernel and the initial RAM disk; two parameters are passed to the kernel: subdir and setkmap. GRUB Legacy, the previous version, used a reserved word, root. The newer version now uses a variable. A complete list of GRUB 2 commands can be found on the GrubWiki CommandList page [6].

Listing 1: A Custom Startup Script
01 echo "Modified entry for launching a Rescue CD as an Image" >&2
02 cat << EOF
03
04 menuentry "Starting System Rescue CD from hard disc" {
05         set root=(hd0,10)
06         linux   /sysrcd/rescuecd subdir=sysrcd setkmap=de
07         initrd  /sysrcd/initram.igz
08 }
09 EOF

Visually Enhanced

When you boot a machine, the start menu normally appears with a fairly staid two-tone look, but GRUB 2 can use PNG- and TGA-formatted images as the background for the boot menu. On Ubuntu, just copy the images to the /usr/share/images/grub/ folder. To install a package with images to your disk, give the sudo apt-get install grub2-splashimages command.

Now just tell the system which of the images to use. To do so, use a different start file in /etc/grub.d/; on Ubuntu/Debian systems, it is 05_debian_theme (line 16, Listing 2). Now modify the path and the file name to reflect your system (as above) and run update-grub afterwards to apply the changes (Figure 2).

Listing 2: Specifying an Image
01 #!/bin/bash -e
02
03 source /usr/lib/grub/grub-mkconfig_lib
04
05 set_mono_theme()
06 {
07   cat << EOF
08 set menu_color_normal=white/black
09 set menu_color_highlight=black/white
10 EOF
11 }
12
13 # check for usable backgrounds
14 use_bg=false
15 if [ "$GRUB_TERMINAL_OUTPUT" = "gfxterm" ] ; then
16   for i in {/boot/grub,/usr/share/images/desktop-base}/moreblue-orbit-grub.{png,tga} ; do
17     if is_path_readable_by_grub $i ; then
18       bg=$i

Figure 2: Just a few simple steps let you add a background image to the boot menu.

Conclusions

GRUB 2 provides several new features and fixes, although many of the changes will not have an effect on the average user. A boot screen with flashy graphics is interesting from a marketing viewpoint, and it beautifies the desktop at an earlier stage of the startup process, but it does not really change the way the end user interacts with the system. In addition, it is hard to understand why you would need to wrap a simple menu entry in a script.

On the other hand, GRUB 2 development is still underway. Control structures and a modular structure give the configuration more flexibility and will someday make it easier to extend the software.

INFO
[1] PUPA project: http://www.nongnu.org/pupa/
[2] GRUB 2 project: http://www.gnu.org/software/grub/grub-2.en.html
[3] GRUB 2 in Karmic Koala: https://wiki.ubuntu.com/Grub2
[4] Password bug: https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/478806/
[5] Upgrade bug: https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/386789
[6] GRUB commands: http://grub.enbug.org/CommandList