A Better Way of Booting

On those rare occasions when you have to reboot your Linux system, the first thing you probably see is the terse prompt of LILO, the most common bootloader for Linux on Intel-compatible systems. Its familiar prompt LILO: is itself a diagnostic indicating the progress of the booting process.

On those rare occasions when you have to reboot your Linux system, the first thing you probably see is the terse prompt of LILO, the most common bootloader for Linux on Intel-compatible systems. Its familiar prompt LILO: is itself a diagnostic indicating the progress of the booting process.

It’s always sad to say goodbye to an old friend, but there’s a newer, more general and flexible kid on the block named GRUB, and there are lots of good reasons to switch.

LILO has done its job reliably and well for many years, but it has always had one significant drawback; whenever you replace a kernel or change the boot configuration in any non-trivial way, you must remember to reinstall LILO into the computer’s Master Boot Record.

If you forget to do this, you can end up with a system that can be booted only from a floppy disk (after which you can rerun LILO). For more information on using LILO, see the December 1999 issue (http://www.linux-mag.com/1999-12/guru_01.html).

GRUB is the GRand Unified Bootloader (http://www.gnu.org/software/grub), which was originally written by Erich Boleyn and is now part of the GNU project. As of version 7.2, Red Hat Linux installs GRUB by default (although it still offers LILO as an option for those who prefer it).

Using GRUB

GRUB is designed to be operating system-independent and more flexible than earlier bootloaders. One of its main advantages is that it can operate as a boot-time shell, providing an environment where you can enter any boot command you want. In this way, GRUB allows you to select an alternate configuration or kernel without requiring any prior preparation.

When a system starts to boot, GRUB displays an attractive splash screen offering a menu of predefined boot choices. At this point, there are several options: select one of the standard choices, modify one of them, or enter raw GRUB commands.

Selecting one of the standard options is easy and performed in the obvious way. Alternatively, you can select one of the listed menu items and then press the E key to edit that choice before it is booted. For example, you could boot the default Linux configuration in single-user mode in this way:

  1. Select the top choice from the menu.

  2. Press E (for “Edit”). The configuration commands corresponding to that choice will appear.

  3. Move the cursor to the line containing the kernel keyword and edit it so that the parameter single is included at the end of the line.

  4. Move the cursor back to the first command in the window and then press B to begin the boot process.

Alternatively, you can enter boot commands manually instead of selecting one of the existing menu choices, effectively defining and then running a new configuration on the fly. You do this by pressing the C key from the main menu to reach the GRUB interactive command prompt.

Figure One shows an example sequence of commands that could be used to boot a Linux operating system installed on the first hard disk.

Figure One: Simple Booting Using GRUB

grub> root (hd0,0)
grub> kernel /vmlinuz ro root=/dev/hda2
grub> boot

The first command (root) specifies the location of the root directory for the boot process using GRUB’s unique numbering system. In this case, the Linux installation uses a separate partition that is the first one on the first disk, typically mounted as /boot under Linux. (See the Mapping Device Names sidebar for more on the mapping of partitions to mounted filesystems.)

Mapping Device Names

Each disk partition can be mapped in a variety of ways. GRUB has one method, the Linux Device List another, and finally, the superuser can mount it with an arbitrary filesystem name. Table One shows how the mappings work for the example given in Figure One.

Table One: Partitions, Devices, and Filesystems

Device Name GRUB Designation Filesystem (from /etc/fstab)
/dev/hda1 (hd0,0) /boot
/dev/hda2 (hd0,1) /

The BootPrompt-HOWTO specifies the range of legal devices for mounting as the Linux root filesystem. These depend on whether you use EIDE disks ( /dev/hda , /dev/hdb , etc.) or SCSI ( /dev/ sda , /dev/sdb , etc.). The root filesystem need not be on one of the drive’s four permissible primary partitions ( /dev/hda1 through /dev/hda4 , for example), but may be on an extended partition ( /dev/hda5 and up). You can also specify a floppy, a RAM disk, or certain other disk types.

Once Linux has located the partition for its root filesystem and begun the boot process, it refers to /etc/fstab to mount partitions at mount points you choose. See last month’s Tech Support column (http://www.linux-mag.com/2002-03/tech_support_01.html) for more information. In practice, you may want to store your kernel image and associated files in a relatively small partition at the start of your first disk, while your Linux root filesystem is in a much larger partition that follows on the first disk. That’s the idea behind the example in Figure One.

If you’re using EIDE disks, the small kernel image is in /dev/ hda1 and the large root filesystem is in /dev/hda2 . Your /etc/ fstab might include lines like:

/dev/hda1 /boot ext2 defaults 0 0
/dev/hda2 / ext2 defaults 1 1

With Linux up and running, you can make any necessary changes to the kernel files in the /boot directory. When you reboot, GRUB will map the /dev/hda1 primary partition (which was mounted as /boot under Linux) to its (hd0,0) logical designator and boot from there. Meanwhile, the kernel will receive the appropriate BootPrompt parameter, root=/dev/hda2, telling it to mount /dev/hda2 as the root filesystem.

The second command specifies the location of the kernel and passes any parameters desired for the boot process as specified in the BootPrompt-HOWTO, which can be found online at http://www.linuxdoc.org/HOWTO/BootPrompt-HOWTO.html. The ro parameter, for “read only”, is standard. You also have to tell the Linux kernel which partition to mount as its root (/) filesystem, which is done via the root= parameter.

The two uses of “root” are unrelated. The root command uses GRUB’s numbering system to designate the boot partition, while the root parameter of the kernel command specifies the location of the root filesystem using Linux’s device naming system.

Note that the path to the kernel is relative to GRUB’s root partition (in the first command), not to Linux’s root filesystem. The kernel can be found at /vmlinuz on the partition that will eventually be mounted at /boot; after system has booted, the kernel will be located at /boot/vmlinuz.

The final command (boot) begins booting the Linux kernel.

The root command also illustrates the way that GRUB refers to hard disk partitions; it refers to the first hard disk’s first partition (where both numbering schemes begin at 0). Extended partition numbering always begins at 4, regardless of the number of primary partitions. (Each disk may have up to four primary partitions.)

The GRUB package includes the installation tool grub-install. For example, this installs GRUB into the Master Boot Record on the first disk:

# grub-install /dev/hda

GRUB informs you of the mappings between physical disks and its own device names. Usually these are correct, but there may be discrepancies from what you are expecting (most often when there is a mixture of EIDE and SCSI disks in the system). If there is something amiss, edit the device map (the path is indicated in the output) and then run grub-install again.

You can also install GRUB on a floppy disk:

# mke2fs /dev/fd0
# mount -t ext2 /dev/fd0 /mnt
# grub-install \
–root-directory=/mnt ‘(fd0)’
# umount /mnt

Configuring GRUB

GRUB’s menu choices and other attributes are defined in its configuration file, grub.conf, usually stored in the /boot/grub directory. The configuration file entries illustrate methods for booting a variety of operating systems.

Figure Two shows a typical opening section of grub.conf that applies to all boot options defined later in the file.

Figure Two: General grub.conf Settings

# general section
splashimage (hd0,0)/grub/splash.xpm.gz
default 0
timeout 30
password -md5 encoded-password

These lines specify the location of the initial splash screen image (a file in XPM format) using the GRUB disk notation to specify the path, the default menu item (where numbering begins at 0), the menu selection timeout period before the default choice is automatically booted (provided in seconds), and a password which is required before the system will boot. Use the grub-md5-crypt command to encode the desired password and enter the resulting string into the file in place of the encoded-password parameter shown.

Figure Three shows a few sample entries that may be used to boot Linux installations. The first entry boots a Linux installation whose kernel image and other associated files are stored in the first partition of the first disk. Linux’s root filesystem is located on /dev/ hda2, which is the second partition on the first disk (indicated via the root= parameter of the kernel entry). The menu will list the name “Linux” for this configuration.

Figure Three: Sample grub.conf Kernel Entries

title Linux
root (hd0,0)
kernel /vmlinuz ro root=/dev/hda2

title Test-Linux
root (hd1,1)
kernel /boot/vmlinuz-test ro root=/dev/hdb2

The second entry boots a Linux installation on the second partition of the second hard disk (indicated by root (hd1,1)). In this case, the Linux root (/) and /boot directories are both located on that partition. Thus, the full path to files under /boot must be given. The menu will list this option as “Test-Linux.”

Booting Non-Linux Operating Systems

GRUB is designed for booting Linux and the BSD variants. It can also be used to boot other operating systems through its chainloader command. (For details, see the GRUB Manual at http://www.gnu.org/manual/grub/html_mono/grub.html.)

For example, the following entry could be used to boot a Microsoft Windows operating system on the third partition of the first hard disk:

title Win2K
rootnoverify (hd0,2)
chainloader +1

This will appear in the menu as “Win2K.” The rootnoverify command is a variant of the root command, specifying the boot partition but telling GRUB not to perform its usual checks. The makeactive command designates the just-specified partition as bootable. The final command, chainloader, tells GRUB to hand off the boot process to the boot program in the first block (indicated by the +1 parameter) of the specified partition (in this case, the MS Windows bootloader).

An entry to boot a Windows operating system from the third partition on the second disk might look like the following:

title WinXP
map (hd0) (hd1)
map (hd1) (hd0)
root (hd0,2)
chainloader +1

This menu item will be labeled as “WinXP”. The two map commands serve to logically swap the first and second drives with respect to the boot process, because the Windows bootloader can use only the first disk. (LILO uses a similar technique to fool the BIOS for such cases.) The remaining three commands designate the boot partition and hand off the boot process.

GRUB is also well suited for booting other Open Source operating systems. For example, here is an entry that may be used to boot FreeBSD:

title FreeBSD
root (hd0,1,a)
kernel /boot/loader

The first command defines the configuration’s menu item as “FreeBSD.” The second command sets the GRUB root partition to the a slice of the FreeBSD partition (itself the second partition on the first hard disk). This entry follows the FreeBSD’s documentation’s recommendation to boot by using the final-stage bootloader rather than directly invoking the FreeBSD kernel. This is accomplished via the final kernel command.

Dynamic Defaults

GRUB has another nice feature: a dynamic default configuration. It can remember what you selected the last time you booted, and it will automatically make the same option the default selection at the next boot.

To enable this capability, insert a default saved command and add a savedefault directive to the end of each bootable kernel you want to participate in this process. Omit the savedefault directive from kernels you don’t want to become the default (like test versions). Figure Four shows a simple example.

Figure Four: Dynamic Default Configuration

#general section

default saved

title Linux
root (hd1,1)
kernel /boot/vmlinuz ro root=/dev/hdb2

For Novices and Pros

GRUB is able to provide both the pre-configured boot choices that unsophisticated users need and the flexibility and dynamic configurability that system administrators will find useful and convenient. And when you design and install your own splash screen, the customization possibilities are endless.

Æleen Frisch is the author of Essential System Administration. She can be reached at aefrisch@lorentzian.com.

Comments are closed.