8.4. Using GRUB to Set Up the Boot Process

8.4.1. Introduction

Boot loading can be a complex area, so a few cautionary words are in order. Be familiar with the current boot loader and any other operating systems present on the hard drive(s) that need to be bootable. Make sure that an emergency boot disk is ready to “rescue” the computer if the computer becomes unusable (un-bootable).

The procedure involves writing some special GRUB files to specific locations on the hard drive. We highly recommend creating a GRUB boot floppy diskette as a backup. Insert a blank floppy diskette and run the following commands:

cd /tmp
grub-mkrescue --image-type=floppy floppy.img
dd if=floppy.img of=/dev/fd0 bs=1440 count=1

GRUB uses its own naming structure for drives and partitions in the form of (hdn,m), where n is the hard drive number and m is the partition number. The hard drive number starts from zero, but he partition number starts from one for normal partitions and five for extended partitions. Note that this is different form earlier versions where both numbers started from zero. For example, partition sda1 is (hd0,1) to GRUB and sdb3 is (hd1,3). In contrast to Linux, GRUB does not consider CD-ROM drives to be hard drives. For example, if using a CD on hdb and a second hard drive on hdc, that second hard drive would still be (hd1).

You can determine what GRUB thinks your disk devices are by running:

grub-mkdevicemap --device-map=device.map
cat device.map

The location of the boot partition is a choice of the user that affects the configuration. One recommendation is to have a separate small (suggested size is 100 MB) partition just for boot information. That way each build, whether LFS or some commercial distro, can access the same boot files and access can be made from any booted system. If you choose to do this, you will need to mount the separate partition, move all files in the current /boot directory (e.g. the linux kernel you just built in the previous section) to the new partition. You will then need to unmount the partition and remount it as /boot. If you do this, be sure to update /etc/fstab.

Using the current lfs partition will also work, but configuration for multiple systems is more difficult.

8.4.2. Setting Up the Configuration

Using the above information, determine the appropriate designator for the root partition (or boot partition, if a separate one is used). For the following example, it is assumed that the root (or separate boot) partition is sda2.

Install the GRUB files into /boot/grub:

grub-install --grub-setup=/bin/true /dev/sda

We use --grub-setup=/bin/true for now to prevent updating the Master Boot Record (MBR). In this way, we can test our installation before committing to a change that is hard to revert.

Generate /boot/grub/grub.cfg:

grub-mkconfig -o /boot/grub/grub.cfg

Here grub-mkconfig uses the files in /etc/grub.d/ to determine the contents of this file. The configuration file will look something like:

#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by /usr/sbin/grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
set default=0
set timeout=5
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/10_linux ###
menuentry "GNU/Linux, Linux 2.6.30.2-lfs65" {
        insmod ext2
        set root=(hd0,2)
        search --no-floppy --fs-uuid --set 915852a7-859e-45a6-9ff0-d3ebfdb5cea2
        linux   /boot/vmlinux-2.6.32.7-lfs-6.6-rc1 root=/dev/sda2 ro
}
menuentry "GNU/Linux, Linux 2.6.30.2-lfs65 (recovery mode)" {
        insmod ext2
        set root=(hd0,2)
        search --no-floppy --fs-uuid --set 915852a7-859e-45a6-9ff0-d3ebfdb5cea2
        linux   /boot/vmlinux-2.6.32.7-lfs-6.6-rc1 root=/dev/sda2 ro single
}
menuentry "GNU/Linux, Linux 2.6.28-11-server" {
        insmod ext2
        set root=(hd0,2)
        search --no-floppy --fs-uuid --set 6b4c0339-5501-4a85-8351-e398e5252be8
        linux   /boot/vmlinuz-2.6.28-11-server root=UUID=6b4c0339-5501-4a85-8351-e398e5252be8 ro
        initrd  /boot/initrd.img-2.6.28-11-server
}
menuentry "GNU/Linux, Linux 2.6.28-11-server (recovery mode)" {
        insmod ext2
        set root=(hd0,2)
        search --no-floppy --fs-uuid --set 6b4c0339-5501-4a85-8351-e398e5252be8
        linux   /boot/vmlinuz-2.6.28-11-server root=UUID=6b4c0339-5501-4a85-8351-e398e5252be8 ro single
        initrd  /boot/initrd.img-2.6.28-11-server
}
### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###

Note that even though there is a warning not to edit the file, you can do so as long as you do not re-run grub-mkconfig. The search lines are not meaningful for LFS systems as that command needs an initrd image for processing. If installing on a separate partition the linux and initrd lines will not have the /boot on the file names. In this example the kernel files for a Ubuntu installation are also found in /boot.

8.4.3. Testing the Configuration

The core image of GRUB is also a Multiboot kernel, so if you already have GRUB Legacy loaded you can load GRUB-1.97.2 through your old boot loader. To accomplish this, you will need to exit the chroot environment and re-enter it to finish the few remaining portions of the book.

/sbin/reboot
...
grub> root (hd0,1)
grub> kernel /boot/grub/core.img
grub> boot

Note that the GRUB commands above are assumed to be GRUB Legacy. At this point the GRUB prompt will appear (very similar to GRUB Legacy) and you can explore the interface or boot to one of the systems in the grub.cfg file.

8.4.4. Updating the Master Boot Record

If you tested the GRUB configuration as specified above, re-enter the chroot environment.

[Warning]

Warning

The following command will overwrite the current boot loader. Do not run the command if this is not desired, for example, if using a third party boot manager to manage the Master Boot Record (MBR).

Update the MBR with:

grub-setup '<DEVICE>'

Change the DEVICE above to your boot disk, normally '(hd0)' or /dev/sda. If using (hd0) be sure to escape the parentheses with backslashes or single quotes to prevent the shell from interpreting them as a sub-shell.

This program uses the following defaults and are correct if you did not deviate from the instructions above:

  • boot image - boot.img

  • core image - core.img

  • directory - /boot/grub

  • device map - device.map

  • default root setting - guessed

[Note]

Note

The root setting is the default value if a 'set root' instruction is not found in grub.cfg. This is the partition that is searched for the kernel and other supporting files. It is different from the 'root=' parameter on the 'linux' line in the configuration line. The latter is the partition the kernel mounts as '/'. In the example grub.cfg above, both values point to /dev/sda2, but if there is a separate boot partition, they will be different.