Linux Boot loader – GRUB

The function of a boot loader is to locate the Linux kernel, and load it into memory before starting it. Boot loading usually occurs in at least two    stages – the BIOS loads the primary boot loader, which loads a secondary boot loader in order to load the kernel. The Grand Unified Boot Loader (GRUB) is currently the default Red Hat Linux boot loader.

When you use the Anaconda installer to install Red Hat, the GRUB boot loader is selected by default. If you don’t install GRUB when you install Red Hat, you can install it afterwards from the command line. There are three GRUB interfaces, including the menu interface, the menu entry editor interface, and the command-line interface.

 


1. Understanding the GRUB boot loader

On a Red Hat Linux system, the boot loader’s function is to locate the Linux kernel, and any other necessary files, and load them into memory. It then starts the kernel so that the kernel can run processes.

The boot loader also lets you control how a system is booted. If you dual boot, a boot loader enables you to choose between operating systems on startup.

When a computer boots, the basic input/output system (BIOS) in read-only memory usually loads the boot loader from the Master Boot Record (MBR) on the system’s primary hard drive.

However, some versions of Linux run directly from the flash memory, bypassing the BIOS and the boot loader. In fact, Linux was originally designed to run without an interactive boot loader – with the kernel located at particular sectors on the disk.

Using a boot loader offers several benefits including

  • the ability to boot multiple operating systems
  • the ability to pass parameters interactively to the kernel, which is useful for disabling certain features in order to solve hardware problems
  • the ability to load different kernels interactively, which is useful when you deploy a new kernel because it is easy to revert to the working kernel

Different architectures that support Red Hat Linux use different boot loaders.  The two boot loaders that Red Hat Linux provides for the x86 architecture are the Grand Unified Boot Loader (GRUB ) and the Linux Loader (LILO).

In the past, LILO has been the most popular boot loader for Linux and the default loader on most distributions.  However, the GNU project’s GRUB is now more commonly used. GRUB is currently the default Red Hat Linux boot loader. It is an improvement over LILO in that it enables you to

  • use password protection
  • try different boot parameters during the boot process without permanently changing the GRUB configuration file
  • boot Windows NT operating systems from the MBR area of a hard drive

GRUB also supports Logical Block Addressing (LBA) mode, which enables the computer to find the /boot files more easily, particularly if they are located beyond the 1024th cylinder of the hard drive.  Most boot loaders execute in two or more stages. GRUB loads itself into memory by

  • loading the stage 1 boot loader
  • loading the stage 1.5 boot loader
  • loading the stage 2 boot loader
  • loading the operating system or kernel

loading the stage 1 boot loader

First, the BIOS reads the stage 1 or primary boot loader into memory from the MBR.

The primary boot loader takes up less than 512 bytes of disk space in the MBR – too small a space to contain the instructions necessary to load a complex operating system. Instead, the primary boot loader performs the function of loading either the stage 1.5 or stage 2 boot loader.

loading the stage 1.5 boot loader

Some hardware requires an intermediate step between the stage 1 and stage 2 boot loaders. This can happen when the /boot partition is situated beyond the 1024 cylinder head of the hard drive, or when you are using LBA mode.

If necessary, the stage 1 boot loader reads the stage 1.5 boot loader into memory. The stage 1.5 boot loader is located either on the /boot partition or on a small section of the MBR and the /boot partition.

loading the stage 2 boot loader

The secondary boot loader is located somewhere on disk – on the boot sector of the first partition, for example. It displays the GRUB menu and command environment, which enables you to select which operating system or Linux kernel to boot. You can also use it to pass arguments to the kernel, or to examine system parameters.

loading the operating system or kernel

GRUB determines which operating system to start, and loads the operating system or kernel and initrd into memory. It then transfers control of the computer to the operating system.

The boot method for Red Hat Linux is called the direct loading method, because the boot loader loads the operating system directly, without using an intermediary stage between the boot loader and the kernel.

In the chain loading boot method, the MBR points to the first sector of the partition that contains the operating system. The MBR uses the files in this location to boot the operating system.

GRUB supports both direct and chain-loading boot methods, so it can boot almost any operating system.




2. Installing GRUB and GRUB interfaces

When you use the Anaconda installer to install Red Hat, the GRUB boot loader is selected by default. By default, GRUB is installed on the MBR of the first Integrated Device Electronics (IDE) hard drive (/dev/hda).

If you want to use a different boot loader, you can choose to change the boot loader during the installation process. If you don’t install GRUB during the installation of Red Hat Linux, you can install it afterwards. Once you’ve installed GRUB, it automatically becomes the default boot loader.

When you’ve done this, you type the command /sbin/grub-install followed by the location in which you want to install the GRUB stage 1 boot loader.

For example, to install GRUB in the MBR of the master IDE drive, you type this command.

[root@gurkulindia1 /]# /sbin/grub-install/dev/hda

The next time you boot the system, the GRUB boot loader menu appears before the kernel loads. The menu interface enables you to boot the Linux kernel, or another operating system if you choose.

The following three GRUB interfaces provide different levels of functionality:

  • menu interface
  • menu entry editor interface
  • command- line interface

menu interface

The menu interface is the default interface if you choose to configure GRUB automatically using the Red Hat Linux installation program.

The menu interface displays a list of kernels, with preconfigured boot commands. You can use the arrow keys to select an option other than the default option, and press Enter to boot it. If a specified timeout period is set, GRUB automatically loads the default option.

menu entry editor interface

To access the menu entry editor from the boot loader menu interface, you press E on the keyboard.

The GRUB commands for the selected menu entry display, enabling you to edit them before you boot the operating system. To edit a command line, you press E on the keyboard, and to delete one, you press D. You can also press O to insert a new line. To execute the commands and boot the operating system, you press the B key. Pressing Esc discards changes and reloads the standard menu interface.

command- line interface

You press C to load the command- line interface. Although this interface is the most basic GRUB interface, it provides you with the most control. You can type any GRUB command at the command prompt, and press Enter to execute it.

The interface also features advanced shell-like features, such as the ability to use keyboard strokes for navigation and auto-completion.

 


 

3. GRUB terminology and commands

If a system has more then one hard drive, GRUB refers to the first hard drive as (hd0). The first partition on that drive is called (hd0,0).

Note

You should remember that GRUB uses a numbering system for devices that starts with 0, rather than 1.

This is the general form of the naming convention that GRUB uses for file systems:(type_of_device BIOS_device_number, partition_number)GRUB’s file system naming convention can be broken down as follows:

  • type_of_device
  • BIOS_device_number
  • partition_number

type_of_device

The type_of_device is either a
hard disk (hd) or floppy disk (fd). This naming applies regardless of whether the system hard drives are IDE or Small Computer System Interface (SCSI) drives.

BIOS_device_number

The BIOS_device_number is the number of the device according to the system’s BIOS. The number of the primary IDE hard drive is 0, and the number of the secondary IDE hard drive is 1.

The sequence is equivalent to the device numbering sequence that the Linux kernel uses, only using numbers instead of letters.

partition_number

The partition_number refers to the number of a specific partition on the disk. The partition numbering starts at 0. Most partitions are specified using numbers, but Berkeley Software Distribution (BSD) partitions are represented by letters, such as a or b.

To specify an entire device – to configure the MBR for a particular disk, for example – you can omit the comma and the partition number.

When you need to refer to a file in a GRUB command, you include the file name immediately after specifying the device and partition.

For example, when enabling dual booting, you may want to specify the menu list to use as a file.

Usually, you specify a file using the directory path on the partition, followed by the file name.

(type_of_device BIOS_device_number, partition_number) /path_to_file/

Some GRUB commands that you can enter via the command- line interface are

  • displaymem
  • boot
  • root
  • kernel
  • initrd
  • rootnoverify
  • chainloader

displaymem The displaymem command uses BIOS information to display current memory use. You can use this command to find out how much RAM a system has before you boot it.

boot You use the boot command to boot the loaded operating system or chain loader.

root You use the root command followed by a device and partition, for example (hd0,0), to configure a GRUB root prtition – containing the /boot files. The command also mounts this partition so that the files can be read.

kernel You use the kernel command to indicate the location of the Linux kernel file to load from GRUB’s root file system when you are using direct loading to boot the operating system.You can specify options after the kernel command to pass them to the kernel when it is loaded.

initrd The initrd command, followed by a file name, specifies the location of the initial RAM disk to use when booting. The kernel may need certain modules to boot properly, for instance if the root partition is formatted with the ext3 file system. In this case, an initrd module is needed.

rootnoverify You use the rootnoverify command to configure a GRUB root partition. It performs the same function as the root command but without mounting the partition. You might need to use this command to boot a sensitive operating system.

chainloader   You use the chainloader command with a file name to load the specified file as a chain loader, and search it for /boot files. To load the file at the first sector of a specified partition, you use +1 instead of the file name.

 


4. The GRUB configuration file

GRUB uses a configuration file – found at /boot/grub/grub.conf – to create the list of operating systems to display in its menu interface.

The configuration file enables you to choose a group of commands that you want GRUB to execute.

You can use the same GRUB commands that you use in the command- line interface, as well as certain commands that are available for use only in the configuration file.

Commands that you can use only in the configuration file include

  • default – to specify the default entry title name or number that GRUB should load as the default
  • timeout – to set the time limit in seconds before GRUB starts the default operating system
  • splashimage – to set the default GRUB image
  • title – to group the commands used to boot a particular operating system under a particular title in the GRUB menu

default=0
timeout=0
splashimage=(hd0,0) /grub/splash/xpm.gz

# section to load linux
title Red Hat Linux (2.4.18-5.47)
root (hd0,0)
kernel /vmlinuz-2.4.18-5.47 ro root=/dev/sda2
initrd /initrd-2.4.18-5.47.img

# section to load Windows 2000
title windows
rootnoverify (hd0,0)
chainloader +1

Other configuration file commands that you can use are

  • password – to prevent unauthorized users from editing the menu option entries

color – to set the menu color scheme

  • hiddenmenu – to load the default entry without displaying the GRUB menu interface

In the GRUB configuration file, the commands that affect the menu interface are placed at the top.

These are followed by entries that affect the booting of each of the operating systems or kernels listed.

default=0
timeout=10
splashimage=(hd0,0)/grub/splash.xpm.gz

# section to load linux
title Red Hat Linux (2.4.18-5.47)
root (hd0,0)
kernel /vmlinuz-2.4.18-5.47 ro root=/dev/sda2
initrd /initrd-2.4.18-5.47.img

# section to load Windows 2000
title windows
rootnoverify (hd0,0)
chainloader +1



 

 

Ramdev

Ramdev

I have started unixadminschool.com ( aka gurkulindia.com) in 2009 as my own personal reference blog, and later sometime i have realized that my leanings might be helpful for other unixadmins if I manage my knowledge-base in more user friendly format. And the result is today's' unixadminschool.com. You can connect me at - https://www.linkedin.com/in/unixadminschool/

What is in your mind, about this post ? Leave a Reply

Close
  Our next learning article is ready, subscribe it in your email

What is your Learning Goal for Next Six Months ? Talk to us