Articles‎ > ‎

Grub Explained

Grub stands for Grand Unified Boot loader.

Grub consists of two stages. First stage is located in MBR or or boot sector of a disk. First stage of Grub is used only to load Second stage of Grub.

Grub has the capability to access a number of file systems including Ext2, Ext3 & DOS FAT.

Grub allows to change boot parameters at the time of boot. Thus, we can login to a system which has got a corrupted grub.conf.

1. Boot Menu


Grub will display a graphical menu at the time of booting. This menu is controlled by a file /boot/grub/menu.lst which is usually soft-linked to /boot/grub/grub.conf.

=====================================================================================
root@server [~]# ll /boot/grub/menu.lst
lrwxrwxrwx  1 root root 11 Mar 21  2007 /boot/grub/menu.lst -> ./grub.conf
=====================================================================================

/etc/grub.conf will also be soft-linked to /boot/grub/grub.conf.

============================================================================================
root@server [~]# ll /etc/grub.conf
lrwxrwxrwx  1 root root 22 Mar 21  2007 /etc/grub.conf -> ../boot/grub/grub.conf
============================================================================================

2. Sample grub.conf

An example of grub.conf is shown below.

==========================================================================================
root@s269 [~]# cat /etc/grub.conf
# 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,0)
#          kernel /boot/vmlinuz-version ro root=/dev/sda1
#          initrd /boot/initrd-version.img
#boot=/dev/sda3
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.9-89.0.9.EL)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.9-89.0.9.EL ro root=LABEL=/
        initrd /boot/initrd-2.6.9-89.0.9.EL.img
==========================================================================================

3. grub.conf directives

Important grub directives used are explained below.
  • default
    • It is the default title number that will load when the menu interface times out.
  • fallback
    • It is the default title number that will load when the first boot attempt fails.
  • timeout
    • Specifies the time, in seconds, that GRUB waits before loading the default title.
  • splashimage
    • Specifies the path of the splash screen image to be used when GRUB boots.
  • hiddenmenu
    • This prevents the GRUB menu interface from being displayed. This will load the default title when the timeout period is expired.
  • title
    • Specifies name of Operating System name in the boot menu.
  • root
    • This helps to simplify specification of kernel and initrd files.
    • Device or partition should be specified in grub format as argument to root command.
  • kernel
    • Specifies path of the kernel file.
    • The root partition is specified as a argument along with kernel directive.
    • This is is specified in conventional Linux format since this information is interpreted by Linux kernel.
    • ro tells the kernel to mount root file system as read-only.
  • initrd
    • Specifies path of initrd file
  • password
    • Specifies grub password

4. Hard disk naming convention used in grub
  • The first hard disk is named as /dev/hd0.
  • Four primary partitions are allowed per disk. They are numbered from 0 to 3.
  • Logical partitions are counted from number 4.
  • (hd0,0)   The first primary partition on the first hard disk.
  • (hd0,1)   The second primary partition on the first hard disk.
  • (hd0,2)   The third primary partition on the first hard disk.
  • (hd0,3)   The fourth primary partition (It will be extended partition, most probably).
  • (hd0,4)   The first logical partition on the first hard disk.
  • (hd0,5)   The second logical partition on the first hard disk.
5. Set Grub Password

Setting a grub password will be a good idea to safe guard your system. This will prevent unauthorized persons from editing grub.

1. Login as root
2. Run the command "grub"
3. Create an encrypted password using "md5crypt" command.

==============================================
grub> md5crypt
Password: ********
Encrypted: 0f402367dd3d6f43765b18e982f6df60
==============================================

4. Now, paste the encrypted string in /boot/grub/grub.conf

=========================================================
root@server [~]# cat /boot/grub/grub.conf
# 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,0)
#          kernel /boot/vmlinuz-version ro root=/dev/sda1
#          initrd /boot/initrd-version.img
#boot=/dev/sda3
default=0
timeout=5
password  --md5 0f402367dd3d6f43765b18e982f6df60
===========================================================


Comments