sections in this module City College of San Francisco - CS260A
Unix/Linux System Administration

Module: StartupShutdown II
module list

grub

Note: as of this writing, Redhat is still using grub version 0.97. There are significant changes in newer versions of grub, allowing it to boot from more varied devices and making it more like a mini-kernel but the essential concepts are the same.

The grand unified bootloader is a second-level boot program responsible for booting the operating system. It is installed in the master boot record (MBR) of a disk. When the BIOS boots from that disk, grub is invoked. 

The sequence of events that occur from the transfer of control of the BIOS to grub's booting of linux is roughly as follows:

NOTE: There is some discrepancy in the documentation as to the name of grub's configuration file. The actual file is grub.conf; however, grub's documentation refers to the file menu.lst. My suspicion is that menu.lst was the old name. To work around this issue, grub configures itself to use grub.conf as the configuration file, and a symbolic link is created to it. The symbolic link is named menu.lst

If either of these restrictions does not hold, grub requires a filesystem-specific intermediate stage for additional preparation before invoking stage2. The main purpose of the intermediate stage is to decipher the filesystem that stage2 resides on in case stage2 has moved. (This is apparently also required if stage2 is not "near enough".) This intermediate stage is called stage1_5. There are many stage1_5 files, one per filesystem.  When grub is installed, the stage1_5 file corresponding to the filesystem that stage2 resides on is stored in an additional sector on the disk reserved for booting. Then when stage1 (in the MBR) is booted, it jumps to stage1_5 instead of stage2. The final goal in either case is to invoke stage2. Note that the use of stage1_5 means that stage2 can be found using normal filesystem operations rather than an absolute address.

Many of the intricacies of this process are beyond the scope of this course. However, a working knowledge of grub's operation and syntax is required in order to alter its configuration if required and intercede if problems develop. One of the most unfortunate and frustrating situations to encounter is to have a usable system with valuable data that you just cannot boot!

(FYI, these parts of grub, called stages, are actually files. A listing is below. Notice the filesystem-specific versions of stage1_5)

# cd /boot/grub
# ls -F
device.map     grub.conf         minix_stage1_5     stage2
e2fs_stage1_5  iso9660_stage1_5  reiserfs_stage1_5  ufs2_stage1_5
fat_stage1_5   jfs_stage1_5      splash.xpm.gz      vstafs_stage1_5
ffs_stage1_5   menu.lst          stage1             xfs_stage1_5

#

grub disks and partitions

Even though we have not covered filesystems, the readers know the concepts of disks and of partitions. A disk is a physically separate device. A partition is a self-contained set of data residing on a portion of a disk. (As far as grub is concerned, logical volumes do not exist. The bootable portion of a linux system should always be stored on a physical filesystem.)

grub has its own syntax for referring to disks and partitions. The disk is indicated by a two character abbreviation to indicate the the disk type, followed by a single digit to indicate the number. The number corresponds to the BIOS drive number, zero-based.

fd0 - the first floppy disk

hd4 - the fifth [hard] disk

The partition is then indicated by a zero-based number. The pair is indicated as a tuple:

(hd0,1)  - the second partition of the first disk

(hd1,4) - the fifth partition of the second disk.

grub filepath is then indicated as an absolute path relative to the start of the indicated disk. For example, if the current disk is set to (hd0,1), and this partition is normally mounted at /usr, then the path to /usr/bin/xxx relative to the start of the partition is /bin/xxx, or, in grub-speak, (hd0,1)/bin/xxx

The boot area

The typical location for boot information on a linux system is /boot. It is common, although not necessary, to configure /boot as a separate small partition which is the first partition for this system. There are several reasons for this:

The boot area contains kernel files (/boot) and grub information (/boot/grub)

The grub configuration file grub.conf

grub's configuration file is kept at /boot/grub/grub.conf  It is created at installation and contains three types of items

The boot configurations are implicitly numbered, with the first configuration number 0. 

Each new kernel that is installed creates a new configuration #0 and renumbers the existing configurations. Thus the previous configuration #0 becomes configuration #1.

grub directives

The directives that you will most often see are

hiddenmenu - do not display the menu of bootable configurations unless interrupted

timeout=N  - after N seconds, go ahead and boot the default configuration

default=N  - set N as the default configuration. Without a default directive, the default is 0

spashimage=x  - use path x as an image file to use as a background to the menu

password  - set the grub password. If a password is set, it must be entered in order to modify a configuration. If you use a password, you should encrypt it using your choice of hash algorithms (md5, sha256, sha512) with grub-crypt and use the --encrypted option on the password directive. This is discussed in the section on Protecting the Boot Process

A boot configuration

A boot configuration begins with a title directive and continues until the next title directive or the end of the file (at least I believe this is the entire rule). Normally it contains three commands

root (hd0,1)
kernel /vmlinuz-2.6.32-131.6.1.el6.i686 ro root=UUID=83780f8f-178f-436d-8eb6-00ecb7a9d889 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto crashkernel=auto rhgb quiet

This kernel command shows two commonly-used kernel options

Kernel command-lines on RH6.5 are documented in the man page for dracut(1). This is not obvious, and is well worth memorizing!
initrd /initramfs-2.6.32-131.6.1.el6.i686.img

If you'd like to see what is in an initrd file, find one and try the following command (we use the name initrd here. You should substitute the full name like /boot/initramfs-2.6.32-131.6.1.el6.i686.img ) (dont execute this command as root in case you make a mistake)

gunzip < initrd | cpio -it

A sample grub.conf file

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda3
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --encrypted $1$S6oWcMTQ$t0j2Vnemd24steF4RlJT./
title Red Hat Enterprise Linux Server (2.6.32-131.6.1.el6.i686)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-131.6.1.el6.i686 ro root=UUID=83780f8f-178f-436d-8eb6-00ecb7a9d889 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto crashkernel=auto rhgb quiet
    initrd /initramfs-2.6.32-131.6.1.el6.i686.img
title Red Hat Enterprise Linux Server (2.6.32-131.4.1.el6.i686)
    root (hd0,0)
...

A few things should be pointed out from the above file

title Other
    rootnoverify (hd0,1)
    chainloader +1

(Note that this is not from the same grub.conf file.) Here the other OS has its own bootloader on the second partition and grub chain-loads to it (+1 means load one sector from the current partition (indicated by the rootnoverify directive)) rootnoverify says that grub should set the current root but should not attempt to mount it. 

Taking a look at the first bootable linux system's configuration:

title Red Hat Enterprise Linux Server (2.6.32-131.6.1.el6.i686)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-131.6.1.el6.i686 ro root=UUID=83780f8f-178f-436d-8eb6-00ecb7a9d889 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto crashkernel=auto rhgb quiet

# cd /boot
# ls -F
config-2.6.32-131.0.15.el6.i686           lost+found/
config-2.6.32-131.4.1.el6.i686            symvers-2.6.32-131.0.15.el6.i686.gz
config-2.6.32-131.6.1.el6.i686            symvers-2.6.32-131.4.1.el6.i686.gz
efi/                                      symvers-2.6.32-131.6.1.el6.i686.gz
grub/                                     System.map-2.6.32-131.0.15.el6.i686
initramfs-2.6.32-131.0.15.el6.i686.img    System.map-2.6.32-131.4.1.el6.i686
initramfs-2.6.32-131.4.1.el6.i686.img     System.map-2.6.32-131.6.1.el6.i686
initramfs-2.6.32-131.6.1.el6.i686.img     vmlinuz-2.6.32-131.0.15.el6.i686*
initrd-2.6.32-131.0.15.el6.i686kdump.img  vmlinuz-2.6.32-131.4.1.el6.i686*
initrd-2.6.32-131.4.1.el6.i686kdump.img   vmlinuz-2.6.32-131.6.1.el6.i686*

#
The installation program leaves the older versions of the kernel in place so that you can back out to it if you need to. In the listing above we see multiple versions of the kernel/initrd pair
(initial part of file omitted)
title Red Hat Enterprise Linux Server (2.6.32-131.4.1.el6.i686)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-131.4.1.el6.i686 ro root=UUID=83780f8f-178f-436d-8eb6-00ecb7a9d889 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto crashkernel=auto rhgb quiet
    initrd /initramfs-2.6.32-131.4.1.el6.i686.img

To boot the older kernel, you would only need to change the grub default line to 1 instead of 0, or to interrupt grub during the boot process and select the alternate configuration to boot, as we shall see.

Preview question: A linux-savvy person can easily take over your system if they can reboot it. Protecting your system even if physical access is allowed is the next topic.

Prev This page was made entirely with free software on linux:  
the Mozilla Project
and Openoffice.org      
Next

Copyright 2012 Greg Boyd - All Rights Reserved.