GRUB Tutorial for Beginners
GRUB, Linux July 20th, 2008
Linux newbies often get frightened by GRUB errors. This article will give you an insight of “What the hell is this GRUB ??” and may also help you solve you many GRUB errors.
What is GRUB ??
GRUB is a boot loader. Now, you *might* be asking what is boot loader (calm down! you would get to know as you read on.)
FYI: GRUB stands for GRand Unified Bootloader.
How does GRUB work ??
During the boot process, BIOS transfers its control to the booting device according to the priority set in the BIOS. For the sake of simplicity, let us assume that our first boot device is our hard disk.
The first sector of Hard Disk is called the Master Boot Record (MBR). This sector is only 512 bytes long. It contains the partition table (64 bytes) describing the primary and extended partitions and the primary boot loader (446 bytes). Now you can figure out, why there can be only 4 primary partitions or 3 primary partitions and 1 extended partition. (Size of partition table is fixed)
GRUB replaces default MBR with its own code. But GRUB is too big to fit in MBR, so it works in stages.
STAGE 1 : This stage either points directly to STAGE 2 or it points to STAGE 1.5 which loads STAGE 2. STAGE 1.5 is stored immediately after MBR. STAGE 2 can be located anywhere on the disk.
STAGE 2 : This stage contains all the configuration files which results is complex user interface and menus we normally see.
GRUB Naming Conventions
The naming convention used in GRUB is bit different from Linux and Windows.
Following examples will clarify the naming convention rules -
(fd0)
The device name must be enclosed in ‘(‘ and ‘)’ . The ‘fd’ part means floppy disk and similarly ‘hd’ means hard disk. The number 0 indicates the drive number which starts from 0. This expression means the grub will use the entire floppy disk.
(hd0,1)
hd means Hard Disk. The 0 here indicates the drive number that is the first hard disk. And the integer 1 here indicates the partition number. Here again partition number starts from 0. So, 1 indicates the 2nd partition. This expression means the second partition of the first hard disk. In this case GRUB uses only one partition of the hard disk.
(hd0,4)
This expression specifies the first extended partition of the first disk. Important point to note is that, the partition number for extended partitions are counted from 4, regardless of the actual number of primary partitions on the disk.
Another point to note is that GRUB does not differentiate between IDE and SCSI drives unlike Linux.
GRUB command line interface has TAB completion feature. For example :
grub> root (
If you press <Tab> at this point, GRUB will display the list of drives and partitions.
To specify a file we write like this:
(hd0,0)/vmlinuz
This expression means the file vmlinuz in the first partition of the first disk. Tab completion works with file names as well.
Now let use explore few configuration files of GRUB
How to change the GRUB menu ?
GRUB menu that we see when we boot a system with Linux is generated from the file
/boot/grub/menu.lst
Lines starting with a hash (#) is a comment. One can safely edit this file to customize the menu that we see at the start up. Let us quickly analyze this file.
Here’s a screenshot of my menu.lst file :
At the top you will find a line (this line is not visible in the above screenshot):
default 0
This sets the default entry, i.e. the the Operating System that will load by default if you do not press any key for a specified number of seconds. Here 0 signifies the first entry in the menu. As usual numbering starts from 0.
Another line you will se somewhere near the above line is
timeout 3
This sets the timeout in seconds, before automatically booting the default entry starts. Here 3 signifies three seconds. You can easily change 3 with any number according to your convenience.
Another optional line is
hiddenmenu
If this line is present, menu will not be displayed by default. This is by default set in Ubuntu and unset in Fedora.
We can have a colorful GRUB menu by setting this line :
color cyan/blue white/blue
Syntax of this line is
color normal [highlight]
The colour normal is used for most lines and the color highlight is used for the lines where the cursor points. Highlight color is optional. By default, the inverted colour of normal is used for highlighted line. The format of a color is foreground/background. The colors that can be specified here are limited and can be found in GRUB manual pages. We can even have different menu items in different colours. I will write about coloured GRUB menu in some other post. Stay Tuned !
Now lets come straight to the menu items. Consider an example:
title Ubuntu 8.04.1, kernel 2.6.24-19-generic
root (hd0,0)
kernel /boot/vmlinuz-2.6.24-19-generic root=UUID=ee3978bb-3563-4445-a6c4-6ba90675cbac ro quiet splash
initrd /boot/initrd.img-2.6.24-19-generic
- title: This one is simple. Simply edit it and get your own custom title for every menu entry.
- root (hd0,0) tells grub to look in the 1st partition of 1 disk.
- kernel specifies the location of kernel image
- initrd is the temporary file system image
There can be many blocks like the above one in your menu.lst file. To remove any entry from the GRUB menu, comment the entire block by placing a hash (#) at the beginning of every line.
And take a backup of menu.lst file before making any changes.
Booting from GRUB command line interface.
Sometimes when you delete a partition or resize a partition or … bla bla … and then restart you computer you see an error like
GRUB error 22 …..
or may be some other error and u see a grub prompt like this
grub>
To boot for the grub prompt follow the following steps:
You need to know which disk and which partition contains your kernel image.
grub> root (
press <tab> at this point and the all disk and partitions will be displayed. enter the correct partition at the prompt. Say for example linux kernel image is on the first partition of first disk.
grub> root (hd0,0)
grub> kernel /vmlinuz-.. root=/dev/sda1 ro
this loads the kernel. Here also you can use tab completion feature to find the exact filename. You can specify some other options like ro , vga etc. After this just enter boot :
grub> boot
And voilla ! you see your linux booting.
If you use initrd, execute the command initrd after kernel at grub prompt like this :
grub> initrd /initrd.img-…
Again Tab completion helps you in completing the file name.
Booting Windows from GRUB prompt:
Say for example windows is installed on 1st partition of first hard disk.
grub> rootnoverify (hd0,0)
grub> makeactive
grub> chainloader +1
grub> boot
Woow.. you see your Windows booting normally.
There’s a lot more about this GRUB. If you are interested in learning more read the GNU GRUB Manual . I also learnt from this manual only.
If you want me to explain any particular thing in this post drop down a comment.
And even if you find any mistakes please drop down a comment.
…………
