An Introduction to GRUB
LinuxIf you use Linux, then there’s a good chance you use GRUB. Either that or LILO. LILO and GRUB are boot loaders. A boot loader is a small piece of software that allows the user to choose which operating system will boot. It’s commonly found on dual-boot systems, such as when Windows and Linux are used together. LILO and GRUB are the market leaders for *NIX systems, with NTLDR booting all Windows machines. Which one you use depends on your preference and\or distribution.
This document focuses on GRUB, since it is the most feature packed, and is very easy to use.
About Boot Loaders
To fully understand how GRUB works, it helps to have a little background knowledge of how modern PC’s boot. It all starts with the BIOS. When a computer is powered up, the BIOS is in charge. It checks all the vital components such as CPU, memory and video card, and checks which devices are connected. After that, it checks it’s boot sequence to see which device gets to boot. Typically, the first hard disk in the machine is the primary boot device, so when the BIOS hands control over to the disk, it only reads the first 512 bytes of the disk for the ‘boot loader’ program. Remember at this stage we are still operating system independent.
Since the first 512 bytes of the hard disk are reserved for boot loaders, they are not big programs. All they contain are instructions on where to find the Operating system, and it’s kernel. If you use Windows, the boot loader is called NTLDR, if you use Linux, it’s either GRUB, LILO, SYSLINUX or a range of other lesser-known programs.
GRUB stores it’s ‘stage1’ program in the first 512 bytes. ‘Stage1’ does little more then load ‘stage2’, which is located else where on the disk so can contain a bit more functionality – such as a menu. The user is then presented with either a command-line, or a menu of Operating systems available. Once one has been selected, GRUB locates the kernel, and passes control over to it.
Creating a GRUB boot disk
If your going to mess with a system running GRUB, you need a GRUB boot disk. The boot disk will boot into the GRUB command-line, and allow you to boot any operating system installed despite any other boot loaders currently installed. It’s also great if your GRUB installation becomes corrupted or if Windows performs a repair and completely removes GRUB altogether.
To create the disk, you need a Linux machine with the GRUB packages installed. Usually in the /usr/lib/grub or /usr/share/grub folders, the different boot stages of GRUB can be found in the files ‘stage1’ and ‘stage2’. You can get the exact location by typing whereis grub, and looking for the directory within either /usr/lib or /usr/share.
Before I could build the diskette, I needed to format it to ext2 (I’ve omitted the output of most commands).
[jonny@linux ~]$ umount /dev/fd0
[jonny@linux ~]$ /sbin/mke2fs /dev/fd0
[jonny@linux ~]$ whereis grub
grub: /sbin/grub /etc/grub.conf /usr/share/grub /usr/share/man/man8/grub.8.gz
[jonny@linux ~]$ cd /usr/share/grub/i386-redhat
(bla bla bla format floppy here...)
[jonny@linux i386-redhat]$ dd if=stage1 of=/dev/fd0 bs=512 count=1
[jonny@linux i386-redhat]$ dd if=stage2 of=/dev/fd0 bs=512 seek=1
The floppy disk will now be a GRUB boot disk. You can boot any system off this disk, and access the GRUB command line.
Ways to install GRUB
GRUB can be installed onto a system in one of two ways, however for GRUB to be present, at least one of the operating system’s installed must be a UNIX like OS. You cannot use GRUB to boot just Windows operating systems. GRUB is usually installed when Linux is installed.
GRUB can be installed via a floppy, which is definitely the safest way. If you followed the steps above, you’ll have a bootable GRUB install disk. Boot of it, and hopefully you will be greeted with a ‘grub>’ prompt. I’m not going to go into the specifics of Installing GRUB in this document, as the GRUB manual has an excellent wealth of information. It can be located at http://www.gnu.org/software/grub/manual/grub.html.
GRUB can also be installed while in Linux, using the grub-install program. This is slightly more risky, as GRUB has to ‘guess’ your disk layout, since the BIOS may not provide the correct results. Where possible, it’s always best to install GRUB via floppy (natively), and install it to the Master Boot Record, rather then just the start of a partition.
The two installation types are known as native and direct install. Native as the added benefit of being more stable and reliable, whereas direct means you don’t need to bother with floppies. All you need is a working Linux installation. However the majority of times, Linux installs GRUB automatically, removing the need for either install.
grub-install is the way to install directly. As root, simply run grub-install from the command line.
Configuring GRUB
Once you’ve got GRUB installed there are loads of options available to you. You can configure a menu with a background, add extra parameters to you kernel calls, loads of things! Again, the manual is the best source of information for all the options GRUB let’s you do, however I’ll cover the basics here.
Note
I've managed to confuse myself a little with the naming of configuration files. From reading the manual, it seems the file grub.conf is the main configuration file, and when the file menu.lst is present, a menu is presented. I've managed to install GRUB, and then remove the menu.lst file, only to find a menu is still presented, and I've also removed the grub.conf file, leaving a menu.lst file, with the unexpected result of still having a working GRUB install with a menu!The typical grub.conf looks like this:
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You do not have a /boot partition. This means that
# all kernel and initrd paths are relative to /, eg.
# root (hd0,1)
# kernel /boot/vmlinuz-version ro root=/dev/hda2
# initrd /boot/initrd-version.img
#boot=/dev/hda
default=0
timeout=5
splashimage=(hd0,1)/boot/grub/splash.xpm.gz.redhat
#hiddenmenu
password --md5
title Redhat Enterprise Linux (2.6.9-11.EL)
root (hd0,1)
kernel /boot/vmlinuz-2.6.9-11.EL ro root=LABEL=/ rhgb exec-shield=0 selinux=0 \
capability.disable=1 rhgb quiet resume=/dev/hda3
initrd /boot/initrd-2.6.9-11.EL.img
title Microsoft Windows XP Professional SP2
rootnoverify (hd0,0)
chainloader +1
Note the kernel command has been split onto two lines for clarify, the full kernel command should be:
kernel /boot/vmlinuz-2.6.9-11.EL ro root=LABEL=/ rhgb exec-shield=0 selinux=0 capability.disable=1 rhgb quiet resume=/dev/hda3
The layout of the file is fairly simple. The # indicates a comment – a line ignored by GRUB, and then some global options are declared, such as default, and timeout, which are pretty self explanatory. Then each option in the menu is separated into sections starting with title, which displays the Operating System name. Finally the options associated with each choice are listed. GRUB automatically adds the last boot command after all other options have been successfully loaded.
My Experiences of GRUB
Since most of this document doesn’t really cover the exact installing and maintaining of GRUB, I thought I’d give you some information regarding my experiences with it, and common problems I have found. The following machines have been set up to test:
- IBM ThinkPad T41 with Windows XP Service Pack 2, and Redhat Enterprise Linux dual booting using GRUB 0.95
- IBM ThinkPad T23 with Windows XP Service Pack 2, and Debian (base package only) dual booting using GRUB 0.95
Obviously the T41 was easier to work with, as Redhat Enterprise Linux came with Gnome, as opposed to Debian’s rather intimidating command line boot, however it was very useful to see GRUB work with a menu, and one without.
GRUB installed OK on both machines at boot time, and successfully detected the Windows NTFS partitions OK. Both defaulted to a menu system, and applied the correct kernel options allowing each OS to boot correctly.
Conclusion
GRUB is certainly a robust, feature-packed system that allows the user to control every aspect of their system, before the OS’s get a chance to take control. It’s compliant, and performs it’s tasks very well. For more information, check out the box at the top right of the page.
