Grub The Linux Boot Loader

A boot loader is a very small program that the bootstrap (page 1026) process uses as it brings a computer from off or reset to a fully functional state. The boot loader frequently resides on the starting sectors of a hard disk called the master boot record (MBR).

BIOS The BIOS (page 1025), which is stored in an EEPROM (page 1035) on the system's motherboard, gains control of a system when you turn on or reset the computer. After testing the hardware, the BIOS transfers control to the MBR, which usually passes control to the partition boot record. This transfer of control starts the boot loader, which is responsible for locating the operating system kernel (kept in the /boot directory), loading that kernel into memory, and starting it running. The /boot directory, which may be mounted on a separate partition, must be present for the system to boot Linux. Refer to "Booting the System" on page 511 for more information on what happens from this point forward.

/boot You can place the /boot directory on a very small filesystem that is located near the beginning of the hard drive, where the BIOS can access it. With this setup, the root (/) filesystem can be anywhere on any hard drive that Linux can access and that perhaps the BIOS cannot. If you are using grub (discussed next), make the filesystem that holds the /boot directory an ext2 filesystem.

grub The name grub stands for Grand Unified Boot loader. A product of the GNU project, the grub loader conforms to the multiboot specification (page 1048), which allows it to load many free operating systems directly as well as to chain load

(page 1028) proprietary operating systems. The grub loader can recognize various types of filesystems and kernel executable formats, allowing it to load an arbitrary operating system. You must specify the kernel's filename and location (drive and partition) so grub knows where to find the kernel. You can pass this information to grub via either the command line or the menu. When you boot the system, grub can display a menu of choices that is generated by the /boot/grub/menu.lst file (next). At this point you can modify a menu selection, choose which operating system or kernel to boot, or do nothing and allow grub to boot the default system.

When you install grub at the time you install Linux, the installation program configures grub automatically. See the grub info page, www.gnu.org/software/grub, and www.gnu.org/software/grub/manual/grub.html for more information on grub.

menu.lst: Configures grub

The /boot/grub/menu.lst file is the default grub configuration file. This file is a grub shell script that holds grub commands that tell grub which kernel and associated files and which options to use when it boots the system. It can display a menu of kernels for you to chose from when the system boots.

As generated by update-grub (which is run when you install Ubuntu or install or upgrade a kernel using APT; see page 651), menu.lst comprises three sections:

• The file starts with many comments and a few interspersed global commands such as timeout, which controls how long grub waits for you to respond to its prompt before automatically booting the default kernel.

• It continues with the Automagic Kernels List, which is divided into two sections. The first section holds default options (directives) that updategrub uses to configure menu.lst (page 651). (The menu.lst file holds directives that control the generation of a replacement menu.lst file.)

• The second part of the Automagic Kernels List holds update-grubgenerated specifications for the grub boot-time menu (page 512). Each boot-time menu entry (boot specification) starts with a title keyword, which is followed by several lines that instruct grub which files to load and which options to use when you choose that menu entry to boot the system.

Following is a single menu entry from a menu.lst file. Following the title, the root command tells grub which device the /boot directory is located on (hd0,0 means hard disk drive 0, partition 0, typically hdal). Next the kernel command specifies the name of the file that holds the kernel that grub is to boot. The grub loader passes the arguments following this filename to the kernel. The root= argument tells the kernel which partition the root filesystem (/) is mounted on. This partition is usually initially mounted in readonly (ro) mode and is remounted in read-write mode later, after being checked. The noapic option, which turns off the advanced programmable interrupt controllers, is required by the system this kernel is running on (you may not need to use this option). The initrd (initial RAM disk; page 1056)

command specifies the name of the file that holds the initial RAM disk image. The grub boot loader loads this image and mounts it as the root filesystem as the first step in booting Linux. The savedefault command comes into play when the system boots and you choose a grub menu entry other than the default. If that entry includes a savedefault command, the menu entry becomes the default entry the next time the system boots. The boot command tells grub to boot the system and is not normally required.

title Ubuntu, kernel 2.6.17.14-ubuntu13.00

kernel /vm1inuz-2.6.17.14-ubuntu13.00 root=/dev/sda2 ro noapic initrd /initrd.img-2.6.17.14-ubuntu13.00

savedefault boot

The menu.lst file in the next example is from a system that had its kernel replaced (there are two versions of vmlinuz and initrd). This system has a separate boot partition or directory, as do most systems, so all kernel and initrd image paths are relative to /boot.

The menu.lst file that update-grub configures when you install Ubuntu Linux starts with comments that explain many of the commands you can use in the file. Most of the comments have been removed from the following file. The first of the global commands, default, specifies the ordinal number of the default menu entry. Menu entries are numbered starting with 0. Although this number does not change, the default boot specification changes when you select a boot specification that includes a savedefault command (as explained earlier). The timeout command specifies the number of seconds that grub waits after it has prompted you for a boot specification before it automatically boots the system using the default boot specification. When you specify hiddenmenu, grub boots the default entry and does not display its menu unless you press ESCAPE while the timeout counter is ticking down. This option is commented out in the following listing, which causes grub to display its menu. The color command causes grub to display its menu in color.

$ cat /boot/grub/menu.lst default timeout

#hiddenmenu color cyan/blue white/blue

### BEGIN AUTOMAGIC KERNELS LIST

title root

Ubuntu, kernel 2.6.17.14-ubuntu13.00 (hd0,0)

/vm1inuz-2.6.17.14-ubuntu13.00 root=/dev/sda2 ro noapic /initrd.img-2.6.17.14-ubuntu13.00

kernel initrd savedefault boot title root kernel initrd boot

Ubuntu, kernel 2.6.17.14-ubuntu13.00 (recovery mode) (hd0,0)

/vmlinuz-2.6.17.14-ubuntu13.00 root=/dev/sda2 ro noapic single /initrd.img-2.6.17.14-ubuntu13.00

title root kernel initrd quiet savedefault boot

Ubuntu, kernel 2.6.17-10-generic (hd0,0)

/vmlinuz-2.6.17-10-generic root=/dev/sda2 ro quiet splash noapic /initrd.img-2.6.17-10-generic title root kernel initrd boot

Ubuntu, kernel 2.6.17-10-generic (recovery mode) (hd0,0)

/vmlinuz-2.6.17-10-generic root=/dev/sda2 ro single /initrd.img-2.6.17-10-generic title root kernel quiet boot

Ubuntu, memtest86+ (hd0,0)

/memtest86+.bin

### END DEBIAN AUTOMAGIC KERNELS LIST

Each menu entry/boot specification in the menu.lst file, called a stanza, starts with a title command. If you do not specify hiddenmenu, or if you press ESCAPE while the timeout counter is ticking down, grub displays a menu of these title strings and allows you to a select one.

The preceding menu.lst file includes five boot specifications: The first, numbered 0, is for the 2.6.17.14-ubuntu13.00 kernel; the second, numbered 1, is for the same kernel brought up in recovery (single-user) mode. The next two entries are for generic kernels—those installed when Ubuntu was installed. The final entry is for memtest86 (page 62) and allows you to run this memory test utility directly from the grub menu.

The menu entries that bring the system up in multiuser mode have a few more entries than those that bring the system up in recovery mode. The quiet option following the kernel command causes the kernel to produce less output so the user can more easily tell what is happening. The splash option causes grub to display the Ubuntu logo as it boots the system. The single option in the recovery mode entries brings the system up in recovery (single-user) mode.

Make sure that when you install a new kernel manually, its title line is different from the other title lines in menu.lst.

You must add a kernel that is not managed by update-grub before or after the Automagic Kernels List section of menu.lst or update-grub will replace it next time update-grub runs. The Automagic Kernels List section is delimited with the following comments: BEGIN AUTOMAGIC KERNELS LIST and END DEBIAN AUTOMAGIC KERNELS LIST.

update-grub: Updates the menu.lst file

The update-grub utility updates the /boot/grub/menu.lst file if the file exists; otherwise, update-grub creates this file. This utility first looks for all files in the /boot directory whose names start with the string vmlinuz- and assumes that each of these files holds a kernel. If the memtest86 directive (page 653) is set to true, it also looks for a file that holds that utility. It creates a menu entry (boot specification) in menu.lst for each kernel it finds (and optionally for memtest86). It also adds an initrd (initial RAM disk) line to menu.lst for each file in /boot whose name starts with the string initrd- and whose version number matches one of the kernel files it found. For example, if update-grub finds the kernel file named vmlinuz-2.6.17-11-generic in /boot and then finds initrd.img-2.6.17-11-generic, it creates an initrd line in menu.lst for that RAM disk image file.

After update-grub runs for the first time, such as when you install Ubuntu, and creates a menu.lst file, you can edit menu.lst. The previous section discussed some of the options, located near the beginning of menu.lst, that you can edit. This section discusses the portion of this file that starts and ends with the following comments:

### BEGIN AUTOMAGIC KERNELS LIST

## lines between the AUTOMAGIC KERNELS LIST markers will be modified ## by the debian update-grub script except for the default options below

The update-grub utility uses the directives in this part of menu.lst to configure menu.lst when it updates this file. This part of the file is well commented; comments begin with two pound signs (##). Directives begin with one pound sign (#). This section of the chapter discusses the directives you are most likely to want to change. Following is an example of three comments followed by one directive:

## should update-grub create memtest86 boot option ## e.g. memtest86=true ## memtest86=fa1se

# memtest86=true

The directive memtest86=true tells update-grub to include in menu.lst an entry to run a memory test (memtest86) instead of booting a kernel. If you do not want update-grub to include a memory test entry, change the true in the last line to false:

# memtest86=fa1se

Do not remove the pound sign from the beginning of the line because a single pound sign indicates a line that holds a directive. (As mentioned earlier, comment lines start with two pound signs.)

When you run update-grub, it updates the menu.lst file based on the kernels in the /boot directory and the directives in the Automagic Kernels List. In the following example, update-grub finds three kernels and the memtest86 utility in /boot:

$ sudo update-grub

Searching for GRUB installation directory ... found: /boot/grub

Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst

Searching for splash image ... none found, skipping ...

Found kernel: /boot/vm1inuz-2.6.20-15-generic

Found kernel: /boot/vm1inuz-2.6.20-13-generic

Found kernel: /boot/vm1inuz-2.6.20-12-generic

Found kernel: /boot/memtest86+.bin

Updating /boot/grub/menu.lst ... done

List of Directives

The following directives can appear within the Automagic Kernels List section of menu.lst.

alternative=true Ifalse

When set to true (default), causes update-grub to create alternative stanzas for each kernel. This option works in conjunction with altoption.

altoption=(string) options

Specifies information that update-grub provides in alternative stanzas: string, which must be enclosed within parentheses, is appended to the title line, and options is a SPACE-separated list of options that are appended to the kernel line. This option has meaning only if alternatives is set to true. The default value of (recovery mode) single provides recovery mode menu entries for each kernel. See kopt and defoptions for more information on options.

defoptions=options

The options is a string that update-grub appends to the kernel line in the default stanzas but not in the alternative stanzas. See kopt and altoption for more information on options.

groot=(devX,Y) Specifies the default device that update-grub places in the root command of each stanza. This device holds the /boot directory that contains the kernel file. This directive starts numbering partitions at 0, while in most other situations partitions are numbered starting at 1. For example, groot=(hd0,0) would generate the line root (hd0,0), specifying that the /boot directory is located on first hard disk, first partition (hard disk drive 0, partition 1)—typically sdal. The string (hd1,4) refers to the second hard disk, first logical partition (logical partitions usually start at 5, even if there are fewer than four primary partitions; see page 31).

howmany=n«m lall

The num specifies the maximum number of kernels update-grub creates stanzas for. Does not count alternative stanzas. Setting howmany to all includes all kernels.

kopt=root=dev options

Specifies the part of the kernel command of each stanza that tells the kernel the location of the root device (/). The dev can be the device name of a partition or a

UUID (page 576), which the following example uses. The comments in the example explain how to specify kernel-specific devices and options. The options, such as ro (readonly) or acip=off (disables ACIP), is a SPACE-separated list that grub passes to the kernel in all stanzas. The options are the same as the parameters you can specify on the boot command line (page 63). See also defoptions and altoption.

## default kernel options for automagic boot options

## If you want special options for specific kernels use kopt_x_y_z

## where x.y.z is kernel version. Minor versions can be omitted.

# kopt=root=UUID=46f9dd87-4a2c-45f5-9177-3327567da56e ro memtest86=true false

When set to true (default), creates a stanza that calls the memory testing program memtest86, instead of booting a kernel.

updatedefaultentry=true | false

When set to false (default), update-grub sets the default line in menu.lst so that it always specifies the newest kernel. As a result, grub automatically boots the newest kernel by default. When set to true, update-grub sets the default line in menu.lst so that it always specifies the same kernel, even when it adds newer kernels to menu.lst.

grub-install: Installs the MBR and grub Files

The grub-install utility installs the MBR and the files, such as the *stage* files (the grub images), that grub needs to boot the system. This utility takes a single argument, the name of the device that is to hold the MBR. You can specify the device name as a grub device name (e.g., hd0) or a device filename (e.g., /dev/hda). The /boot/grub/device.map lists both forms of the name of the device that holds the MBR. The following example shows grub-install installing files in the default location (/boot/grub) and the MBR on device /dev/hda:

$ sudo grub-install /dev/hda

Installation finished. No error reported.

This is the contents of the device map /boot/grub/device.map. Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script 'grub-install'.

$ cat /boot/grub/device.map

Reinstalling the MBR

The following procedure reinstalls the MBR, as is necessary when it gets overwritten by a Windows installation:

1. Boot the system using an Ubuntu live (installation) CD/DVD.

2. Open a terminal emulator window (Menubar: Applications ^Accessories^ Terminal).

3. Give the following commands, substituting the name of the device that holds the root partition (e.g., /dev/sda2) for /dev/xxx. Substitute the name of the drive that you want to install the MBR on (e.g., /dev/sda) for /dev/yyy. Do not forget to unmount /mnt when you are done.

a. If the system does not have a separate boot partition, use these commands:

$ sudo grub-install --root-directory=/mnt /dev/yyy

Installation finished. No error reported.

This is the contents of the device map /mnt/boot/grub/device.map. Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script 'grub-install'. $ cat /boot/grub/device.map (hd0) /dev/hda $ sudo umount /mnt b. If the system does have a separate boot partition, use these commands, substituting the name of the device that holds the boot partition (e.g., /dev/hdal) for /dev/zzz:

$ sudo mount /dev/xxx /mnt $ sudo mount /dev/zzz /mnt/boot

$ sudo grub-install --root-directory=/mnt /dev/yyy

Installation finished. No error reported.

This is the contents of the device map /mnt/boot/grub/device.map. Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script 'grub-install'. $ cat /boot/grub/device.map (hd0) /dev/hda $ sudo umount /mnt

4. Reboot the system. Remove the CD/DVD when the system asks you to.

5. Continue to reboot the system from the hard drive you specified in place of /dev/xxx.

Was this article helpful?

0 0
The Ultimate Computer Repair Guide

The Ultimate Computer Repair Guide

Read how to maintain and repair any desktop and laptop computer. This Ebook has articles with photos and videos that show detailed step by step pc repair and maintenance procedures. There are many links to online videos that explain how you can build, maintain, speed up, clean, and repair your computer yourself. Put the money that you were going to pay the PC Tech in your own pocket.

Get My Free Ebook


Post a comment