The bootloader displays the boot menu that appears during Linux startup. Bootloaders are not unique to Linux. They are the bridge between the BIOS and an operating system, whether it is Linux, Windows, or UNIX.
The bootloader loads the Linux kernel and initial ram disk and then executes the kernel. The BIOS determines which source (hard disk, floppy, CD, etc.) to boot from. The Master Boot Record (MBR) is then loaded, and the bootloader is executed from the selected device.
The operating system load programs or bootloaders covered in this chapter are the GRand Unified Bootloader (GRUB) and LInux LOader (LILO), and we concentrate on the Red Hat and SUSE distributions. This section explains how the bootloaders work, what parts they have, and how to fix common problems with them. This section also discusses how to boot when the bootloader fails.
GRUB is the bootloader most commonly used to start installed Linux systems. GRUB identifies the Linux kernel that should be used to boot the system and loads and then executes the kernel. If you installed Linux recently, there is a good chance that GRUB was installed too and serves as the bootloader.
This section examines the features of GRUB and how to fix problems with GRUB. We start with an overview of how GRUB works. Next, we demonstrate the features used for troubleshooting and resolving boot problems. We include examples to show how to boot to single user mode, how to correct a bad GRUB configuration, and how to repair the MBR when it is overwritten or corrupted. GRUB has rich configuration features that are covered well in the GRUB info manual. We won't try to duplicate that information here.
Before discussing GRUB, we need to briefly explain the MBR. The MBR of a hard disk is located in the first sector and is used to load and start the operating system. The MBR contains the partition table and an extremely small program called the bootloader. More information about the MBR can be found in Chapter 6, "Disk Partitions and Filesystems."
GRUB is a two-stage bootloader:
The menu choices are from an ASCII configuration file named /boot/grub/grub.conf for Red Hat and /boot/grub/menu.lst for SUSE. The GRUB configuration file can be edited as needed. Figure 1-1 shows a GRUB configuration with two Linux installations. Each has a /boot partition and a grub.conf or menu.lst configuration file. Whichever Linux install wrote the MBR is the one whose /boot is used at startup. The GRUB menu can be customized using different backgrounds and colors. The screenshots in this chapter show GRUB output from a serial console window. Typically, there is a graphical menu of kernels to boot. Each menu choice has a group of lines consisting of a menu item title and the kernel location for this choice. The highlighted Red Hat entry in Figure 1-1 consists of the following lines in grub.conf.
title Red Hat Linux (2.4.20-8) Original Kernel root (hd0,0) kernel /vmlinuz-2.4.20-8 ro root=LABEL=/ initrd /initrd-2.4.20-8.img
This is an example of a very simple kernel definition in grub.conf. Each grub.conf line begins with a keyword. The keywords used in Figure 1-1 are:
All the GRUB options are identified in the GRUB info file.
Please notice the disk partition (hd0,0) that is identified as the location of the boot partition. With GRUB, the disks are numbered starting from zero, as are the partitions. The second disk would be hd1, the third hd2, and so on. The root partition in the previous example is the first partition on the first hard disk. Floppy disks are identified as fd rather than hd.
A complete sample grub.conf file is shown here:
# Set up the serial terminal, first of all. serial --unit=0 --speed=9600 --word=8 --parity=no --stop=1 terminal --timeout=10 serial console # Set default kernel selection. Numbering starts at 0. default=1 # 10 second delay before autoboot timeout=10 # Comment out graphical menu # splashimage=(hd0,0)/grub/splash.xpm.gz title Red Hat Linux (2.4.20-8) root (hd0,0) kernel /bzImage ro root=LABEL=/ initrd /initrd-2.4.20-8.img title Red Hat Linux (2.4.20-8) Original Kernel root (hd0,0) kernel /vmlinuz-2.4.20-8 ro root=LABEL=/ initrd /initrd-2.4.20-8.img title Red Hat Linux (2.4.20-8) test Kernel root (hd0,0) kernel /vmlinuz.tset ro root=LABEL=/ initrd /initrd-2.4.20-8.img title SuSe Linux kernel (hd1,0)/vmlinuz root=/dev/hdb3 splash=silent text desktop \ showopts initrd (hd1,0)/initrd
The focus of this chapter is on troubleshooting, not on thoroughly explaining how GRUB works. That information is already available. GRUB has an excellent user manual that explains all the different options and syntax. Visit http://www.gnu.org/software/grub/ to obtain the manual and get the latest GRUB news.
GRUB provides a whole lot more than just the capability to select different kernels from a menu. GRUB allows the menu choices to be modified and even allows a shell-like command interface to boot from kernels not listed on the menu. GRUB makes it easy to correct problems that keep Linux from booting.
Editing the Menu Choices with GRUB
GRUB allows the boot menu choices to be edited by pressing e. GRUB enables users to edit the configuration of the menu choices. This means users can correct problems with grub.conf that prevent Linux from starting. Figure 1-2 shows a GRUB screen after pressing e.
Figure 1-2. GRUB menu edit screen
Let's see how this feature can help us resolve a boot problem.
Figure 1-3 is a console message that no system administrator wants to see. Pressing the space bar just brings up the GRUB menu again. The timer might be restarted too. GRUB tries to boot the same kernel again when the timer expires. If this attempt fails, the screen is displayed again without the timer.
Figure 1-3. GRUB boot error message
The Error 15 tells us that the kernel specified in grub.conf can't be found. Fortunately, GRUB permits editing the configuration. Pressing e gets the commands for the selected boot entry, as shown in Figure 1-4.
Figure 1-4. GRUB kernel configuration editing
If we arrow down to the kernel line and press e, we get the edit screen shown in Figure 1-5.
Figure 1-5. GRUB shell interface
We can use the arrow keys and Backspace to make changes just like the BASH shell. Press Enter when done to return to the previous screen. Press Esc to exit to the previous screen without keeping changes. We fix the typo by changing vmlinuz.tset to vmlinuz.test and press Enter. Now, the menu choice in Figure 1-6 looks better.
Figure 1-6. GRUB kernel configuration editing
Press b to boot. Hopefully it works and Linux starts. If it still doesn't work, GRUB lets us try again. The kernel line can also be used to boot Linux to single user or emergency mode.
Booting to Single User Mode and Emergency Mode
Occasionally it is necessary to perform system maintenance in a minimalist environment. Linux provides single user mode for this purpose. In single user mode (runlevel 1), Linux boots to a root shell. Networking is disabled, and few processes are running. Single user mode can be used to restore configuration files, move user data, fix filesystem corruption, and so on. It is important to know how to boot Linux to single user mode for the times when the boot to multiuser mode fails. Figure 1-7 is a typical SUSE console screen when booting to single user mode.
Figure 1-7. SUSE single user mode boot console output
Note that SUSE requires the root password in single user mode. Red Hat, however, does not, which makes it easy to change the root password if it is lost. We explain later in this chapter how to reset a lost root password with a rescue CD-ROM.
If Linux boots from the kernel but then hangs, encounters errors during the startup scripts, or cannot boot to multiuser mode for some other reason, try single user mode. Just interrupt the GRUB auto boot, edit the kernel line, and add single to the end. Figure 1-8 is a screenshot of a Red Hat single user mode boot.
Figure 1-8. GRUB single user mode boot
Booting to emergency mode is accomplished by adding emergency to the end of the command line. Emergency mode is a minimalist environment. The root filesystem is mounted in read-only mode, no other filesystems are mounted, and init is not started. Figure 1-9 shows a Red Hat emergency mode boot.
Figure 1-9. GRUB emergency mode boot
What if we want to boot a kernel that is not on the menu? The next section looks at the editor features provided with GRUB.
Command-Line Editing with GRUB
The GRUB command line can be invoked by pressing c, and it can be used to boot a kernel that is not on the menu. Users can enter their own root, kernel, and initrd lines. Press c and you get
GRUB supports tab line completion to list matches for device files and kernels. The previous Red Hat menu example can be used as a template for commands that could be used to boot the system from the GRUB command line.
For example, press Tab after typing the following, and GRUB completes the device if only one choice is available or lists all the matches if multiple matches exist:
grub> root (h
For a single-disk Linux installation with one IDE drive, GRUB fills in
grub> root (hd0,
Complete the rest of the root definition so that the line reads
grub> root (hd0,0)
Press Enter, and GRUB responds
Filesystem type is ext2fs, partition type 0x83
Now choose a kernel. Enter the following and press Tab:
grub> kernel /v
GRUB responds by filling in the rest of the unique characters (vmlinu) and showing the matches:
Possible files are: vmlinuz vmlinux-2.4.20-8 vmlinuz-2.4.20-8 vmlinuz.good vmlinuz-2.4.20-dave grub> kernel /vmlinu
All the kernels in /boot do not necessarily have entries in the grub.conf file.
Tab completion makes it easy to boot a kernel even when the exact spelling isn't known. After the commands are entered, just type boot to boot Linux. This technique can also be used if the grub.conf file was renamed or erased.
Problems with the MBR
We mentioned earlier that GRUB inserts its stage1 file in the MBR. It is important to know how to restore the MBR if it becomes corrupted.
Reinstall MBR with GRUB stage1
Creating a dual-boot Linux system such as the Red Hat/SUSE example in Figure 1-1 is a nice way to create a fallback position for system changes and to test a new Linux distribution. A small downside is that the GRUB stage1 information in the MBR can be overwritten by the second install. In our example, Red Hat is installed on the first disk, and SUSE is installed on the second. After SUSE is installed, however, the SUSE GRUB menu is displayed instead of the Red Hat menu that we are used to and that has been customized for our installation. An easy way exists to fix the MBR, though. In Figure 1-10, we've reinstalled the GRUB stage1 file to the MBR following the instructions in the GRUB manual, which is available at http://www.gnu.org/software/grub/manual/.
Figure 1-10. Installing GRUB
The root (hd0,0) command sets the (hd0,0) partition as the location of the boot directory. This command tells GRUB in which partition the stage2 and grub.conf or menu.lst files are located.
The find /boot/grub/stage1 command in Figure 1-10 returned the first stage1 entry it found. Both disks should have this file. In this instance, GRUB shows the stage1 file from the second disk. Because we want GRUB to format the MBR on the first disk, /dev/hd0 is used.
The setup (hd0) command writes the MBR of the selected disk or partition.
Using a Boot Floppy to Repair the MBR
It is a good idea to create a GRUB boot floppy or CD and print or archive the GRUB configuration file (/boot/grub/grub.conf for Red Hat and /boot/grub/menu.lst for SUSE) for use when GRUB won't start or won't display the GRUB menu. The following code illustrates how to create the boot floppy, as explained in Section 3.1 of the GRUB manual (http://www.gnu.org/software/grub/manual/grub.pdf):
cd /usr/share/grub/i386-pc # dd if=stage1 of=/dev/fd0 bs=512 count=1 1+0 records in 1+0 records out # dd if=stage2 of=/dev/fd0 bs=512 seek=1 153+1 records in 153+1 records out #
The dd if=stage1 of=/dev/fd0 bs=512 count=1 command copies the GRUB MBR file (stage1) to the beginning of the floppy to make it bootable. The command dd if=stage2 of=/dev/fd0 bs=512 seek=1 skips one 512-byte block from the beginning of the floppy and writes the stage2 file.
If GRUB fails to run when the computer is started, you can use this floppy to boot to the GRUB prompt. Enter the commands from the grub.conf file at the GRUB command line to boot Linux. Use tab completion to find a good kernel if there is no grub.conf archive to which to refer.
Creating a boot CD is just as easy. Section 3.4 of the GRUB manual contains the instructions for making a GRUB boot CD. Here are the instructions (without the comments):
$ mkdir iso $ mkdir -p iso/boot/grub $ cp /usr/lib/grub/i386-pc/stage2_eltorito iso/boot/grub $ mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot \ -boot-load-size 4 -boot-info-table -o grub.iso iso
Now just burn the grub.iso file created by mkisofs to a CD. The instructions are for GRUB version 0.97. If an earlier version of GRUB is installed on your Linux system, the /usr/lib/grub/i386-pc/stage2_eltorito file might not exist. In that case, download version 0.97 of GRUB from http://www.gnu.org/software/grub/ and follow the INSTALL file instructions for running configure and make, which produces the stage2_eltorito file. Running configure and make does not affect the version of GRUB installed in /boot on your Linux system.
The LILO bootloader is similar to GRUB in that it provides menu-based kernel selection. LILO is a two-stage bootloader. Both Stage 1 and Stage 2 are kept in one file, usually /boot/boot.b. The first stage of the LILO bootloader occupies the boot sector, usually the MBR. It relies on the BIOS to load the following:
The key to LILO is the map file (/boot/map). This file is created by the /sbin/lilo command. LILO does not understand filesystems. The physical location of the files is stored in the map file. Thus, if the files move, /sbin/lilo must be run. If a new kernel is built, /sbin/lilo must be run to map the new location and size. Because this information is encoded in the map file, LILO doesn't provide a shell-like environment as GRUB does to manually enter kernel location information at boot time. The /sbin/lilo command reinstalls LILO because it writes the MBR.
The /etc/lilo.conf configuration file specifies kernel locations and LILO configuration. The following is a very basic /etc/lilo.conf file for a two-disk configuration with Red Hat on the first disk and SUSE on the second:
prompt serial=0,9600 # wait 10 seconds to autoboot timeout=100 # location of boot sector to write boot=/dev/hda # location to write map file map=/boot/map # identify bootloader location install=/boot/boot.b linear # set default kernel for autoboot default=SuSE # RedHat image=/boot/vmlinuz-2.4.20-8 label=RedHat initrd=/boot/initrd-2.4.20-8.img read-only append="root=LABEL=/ console=ttyS0,9600" # SuSE image=/suse_root_hdb/boot/vmlinuz label=SuSE initrd=/suse_root_hdb/boot/initrd append="root=/dev/hdb3 splash=silent text desktop showopts \ console=ttyS0,9600"
The /etc/lilo.conf file has many options, which are explained in the lilo.conf(5) man page. Lines starting with # are comments and are ignored by /sbin/lilo. Table 1-1 provides a description of the global entries used in this file.
Many more keywords exist, and explaining them all is beyond the scope of this chapter. Our goal is to show how LILO works and how to fix problems. LILO is well documented in the lilo.conf(5) and lilo(8) man pages, as well as the excellent LILO README supplied with the LILO package.
Most LILO installations display a nice graphical menu at boot that lists all the kernels from /etc/lilo.conf. The kernels are listed by using the message option:
The examples we use are from the text LILO output from a serial console. Figure 1-11 shows what the normal boot screen looks like if the message line is not included in /etc/lilo.conf.
Figure 1-11. LILO boot screen
If no keys are pressed, LILO boots the default entry from /etc/lilo.conf. If the default variable is not set, the first image entry in /etc/lilo.conf is booted. Press Tab to interrupt autoboot and see the list of boot entries. Figure 1-12 shows the display after Tab is pressed.
Figure 1-12. LILO boot choices
It is easy to pick a different kernel. Just type the name of the entry and press Enter. The SUSE kernel is chosen in Figure 1-13.
Figure 1-13. Selecting a kernel to boot with LILO
We can append parameters to the kernel command line too. Figure 1-14 demonstrates how to boot to single user mode (init runlevel 1).
Figure 1-14. Booting single user mode with LILO
Booting to emergency mode is achieved the same way. Just add emergency to the command line. As we stated earlier, emergency mode is a minimalist environment. The root filesystem is mounted in read-only mode, no other filesystems are mounted, and init is not started.
Booting When GRUB or LILO Doesn't Work
A boot floppy can be created to boot a Linux box when the /boot filesystem is damaged or missing files.
Red Hat provides the command mkbootdisk to create a bootable floppy. The root filesystem that is mounted when booting from this floppy is specified in /etc/fstab. Thus, the root filesystem must be in good condition. Otherwise, the box starts to boot but then fails when trying to mount /. This is not a rescue utilities disk. It is just a way to boot Linux when /boot is missing files or is damaged. See the mkbootdisk(8) man page for full details. This command works with both LILO and GRUB bootloaders.
Here is an example of making the boot floppy:
# mkbootdisk --device /dev/fd0 -v 2.4.20-8 Insert a disk in /dev/fd0. Any information on the disk will be lost. Press <Enter> to continue or ^C to abort: Formatting /tmp/mkbootdisk.zRbsi0... done. Copying /boot/vmlinuz-2.4.20-8... done. Copying /boot/initrd-2.4.20-8.img... done. Configuring bootloader... done. 20+0 records in 20+0 records out
Here is what the console shows when booting from this floppy:
SYSLINUX 2.00 2002-10-25 Copyright (C) 1994-2002 H. Peter Anvin Press <return> (or wait 10 seconds) to boot your Red Hat Linux system from /dev/hda2. You may override the default linux kernel parameters by typing "linux <params>", followed by <return> if you like. boot:
Boot to single user mode by appending single to the boot command like this:
boot: linux single
The mkbootdisk floppy makes repairing /boot easy. For example, suppose LILO displays only the following during boot:
This result means LILO encountered a problem while starting. During boot, LILO displays L I L O one letter at a time to indicate its progress. The meaning of each is described in Chapter 6. When only LI is displayed, the first stage bootloader could not execute the second stage loader. Maybe the file was moved or deleted. What now? We can use the mkbootdisk floppy. The floppy boots Linux, mounts / from the hard disk, and Linux runs normally. After fixing the problem in /boot, don't forget to run lilo -v to update the MBR.
A mkbootdisk floppy is a good recovery tool. We discuss recovery CDs later in this chapter.