Getting started with GRUB

Author Note: This was originally posted on linux.com in 2008. I am replicating it here for posterity.

GRUB, the GRand Unified Bootloader, is an integral part of many Linux systems. It is the first code that runs when the system starts up; it starts the Linux kernel. Here’s some background on GRUB, and some tips on installing and configuring the software.

GRUB was originally written by Erich Stefan Boleyn and is now part of the GNU project. The current production version is GRUB 0.9x, also known as GRUB Legacy. In 2002 the developers shifted their focus to GRUB 2 and stopped adding features to the legacy code, though it still receives regular patches and bug fixes. GRUB 2 remains a development version; most distributions still rely on GRUB Legacy.

GRUB is not dependent upon any operating system. It was written to conform with the Free Software Foundation’s Multiboot Specification, which allows it to boot almost any operating system. In fact, it can boot multiple operating systems on computers that have more than one installed on their hard drives. Among GRUB’s features:

  • Dynamic configuration. Users can change settings and parameters at boot time.
  • Support for multiple executable formats and hard drive filesystems.
  • Both a graphical and command-line interface to let users choose what operating system to boot.

One common task preformed by GRUB in the Linux world — besides booting the kernel — is to allow users to dual boot Linux and Microsoft Windows. The typical pattern for creating a dual boot system is to install Windows, if not already installed, then install Linux on another hard drive or on an empty partition. Most Linux distributions will detect the Windows installation and automatically install and configure an open source boot loader — such as GRUB or LILO — to boot both Linux and Windows. This is accomplished by completely replacing the Microsoft Widows bootloader. If your distribution does not do this by default you can manually install GRUB to do it all for you (continue reading for instructions). If you have a system that already uses GRUB to boot a different operating system, or another Linux distribution, most Linux distributions will just register their presence with GRUB when they are installed.

Installing GRUB

Boot From a USB drive using GRUB

Once upon a time GRUB was commonly used to make generic boot diskettes. A GRUB-enabled diskette would allow a user to boot directly into any operating system that GRUB recognized on the system. This was useful for recovering systems and fixing partitions that had bad master boot records (MBR).

Times have changed; many computers these days no longer have diskette drives, but they can boot from a USB drive. GRUB naturally adapted to USB disk drives. To get a USB disk drive to boot with GRUB, start with a USB thumb drive that is partitioned correctly and has a supported filesystem (BSD FFS, DOS FAT16 and FAT32, Minix fs, Linux ext2fs, ReiserFS, JFS, XFS, and VSTa fs). Then perform the following steps while logged in as root or a user that has sudo access.

First mount the USB drive. Under many distributions, USB storage devices are mounted automatically when the system recognizes them. If that’s not the case with your distribution, you will need to install the sg3-utils (a set of SCSI management tools) package. Running sg_scan -i will display a list of devices, find the one that matches the name of your device and make note of the /dev/sg* information. Now run sg_map to see how the devices is actually mapped on the system; match the /dev/sg* name to the /dev/sd* line to get the device name of the device. On my machine the USB drive is /dev/sda, so the command is:

# mkdir /mnt/usb
# mount /dev/sda1 /mnt/usb

Copy the boot stage files to your USB drive. Stage files are the executable files that comprise GRUB. You need to copy the correct stage file for your filesystem. In GNOME you can determine the file system by right clicking the icon for the drive and selecting Properties from the menu; the file system information in on the Volume tab. If you are not running GNOME you can determine the filesystem type by using fdisk. In all there should be three files to copy: stage1, stage2, and your filesystem stage file. (The example below uses the e2fs stage file, which corresponds to the ext2 filesystem.) Once the files are copied you need to unmount the device so the MBR can be created.

mkdir /mnt/usb/grub
cp /boot/grub/stage* /boot/grub/e2fs_stage1_5 /mnt/usb/grub/
umount /mnt/usb

The final step is to install a master boot record on the drive. An MBR is a small (512 bytes) hidden piece of the disk — found on sector 0 — that is reserved specifically for booting. To install the MBR, use the GRUB shell; the GRUB shell is a simple command shell that is used to preform GRUB related tasks.

grub
grub> device (hd0) /dev/sda
grub> root (hd0,0)
grub> setup (hd0)
grub> quit

  • device tells GRUB what disk device to use when processing commands.
  • root sets the default partition that GRUB will use to boot from. This is the partition where the stage files were copied to. GRUB starts counting at 0, not 1.
  • setup creates the MBR on the USB drive.
  • quit exits the GRUB command shell.

At this point GRUB is installed, and you should be able to boot the USB disk drive. When you do so, you should get dumped directly into the GRUB shell when the machine is booted. For information on what you can do with the command shell, review the GRUB command-line documentation.

If you want to go old school and use a diskette with GRUB, make sure the diskette is formatted as a FAT filesystem and replace /dev/sda1 with /dev/fd0 and hd0 with fd0.

Most Linux distributions that use GRUB come with it installed and ready to use. Many of the distributions that do not have GRUB installed by default have it available in their package systems; check there first before doing a manual installation.

If you have a different bootloader already installed there is really no reason to replace it. I would only recommend replacing your existing bootloader if there is a specific operating system that you are trying to use that is not supported by your installed bootloader. A prime example would be the Microsoft Windows bootloader; using it to boot Linux is possible but requires more work than just installing GRUB, or any other multiboot boot loader for that matter.

If something goes wrong during your attempted GRUB install you can cause your computer not to boot. The instructions that follow should not be applied to your primary hard drive unless you know what you are doing. Your primary hard drive should be /dev/hda or /dev/sda if you have a SCSI hard drive. I would highly suggest cutting your teeth on a USB disk or floppy drive first.

To do a manual installation you need to download GRUB from the GNU Web site; make sure you get the Legacy branch. Once you have the package, unpack, build, and install it with the commands:

tar -xzvf grub-0.9x.tar.gz
cd grub-0.9x
./configure
make
sudo make install

The last step needs to be performed by the root user, hence the sudo command. If all goes well, GRUB will be installed and ready to use. If you have issues building or installing, consult the GRUB FAQ or the GRUB wiki to get help.

Configuring the GRUB menu

Most users will want GRUB to automatically boot a specific operating system, or present them with a way to select what system they want from among all that are installed on the hard drive. That is the function of the GRUB menu — a configuration file called menu.lst that goes into the “grub” directory on the USB drive. If GRUB finds this file during boot it will automatically load the menu.

The file is just a plain text file with a set of directives and configuration parameters. The file below is an abbreviated version of the default menu.lst file from the Ubuntu 7.04 release.

default	   0
timeout    3
hiddenmenu

title      Ubuntu, kernel 2.6.20-15-generic
root       (hd0,0)
kernel     /boot/vmlinuz-2.6.20-15-... ro quiet splash
initrd     /boot/initrd.img-2.6.20-15-generic
  • default specifies which entry is the default. An entry comprises, at least, title, root, and kernel directives. Entry numbers start at 0 and increment upward.
  • timeout specifies how long, in seconds, the menu will be displayed before the default entry is executed.
  • hiddenmenu means that the menu is not displayed. This parameter is used to automate the boot process without asking the user what to do.
  • title is the text that is displayed in the menu for the entry that follows.
  • root tells GRUB on what device and partition it can find the kernel for this entry.
  • kernel specifies what kernel will be booted if this entry is selected. Options after this directive are passed to the kernel for processing. Here, ro means read-only (the device is not physically made read only it just tells the kernel not to attempt writing), quiet inidcates do not display debug information, and splash means show a splash screen while booting.
  • initrd tells GRUB what to run after the kernel has been loaded. When this directive is executed, GRUB passes off control of the system to the operating system.

You can set several other parameters here. It is possible to specify a color scheme, underlay a splash image, and even specify a boot-time password to protect the menu. Consult the GRUB documentation for a complete list of available commands.

If you need to have GRUB boot Windows you can use the following entries into the menu configuration file. The example provides GRUB with the instructions to boot a Windows 2000 installation on the primary hard drive.

title Windows 2000 
unhide (hd0,0)
hide (hd0,1)
hide (hd0,2)
rootnoverify (hd0,0)
chainloader +1

Conclusion

With GRUB you can boot a customized Linux distribution directly from a USB drive; boot embedded devices automatically; or simply build machines that allow users to select what distribution or operating system they want to use.