102.2. Install a boot manager
102.2 Install a boot manager
Weight: 2
Description: Candidates should be able to select, install and configure a boot manager.
Key Knowledge Areas:
Providing alternative boot locations and backup boot options
Install and configure a boot loader such as GRUB Legacy
Perform basic configuration changes for GRUB 2
Interact with the boot loader
The following is a partial list of the used files, terms and utilities:
menu.lst, grub.cfg and grub.conf
grub-install
grub-mkconfig
MBR
We have talked about linux boot process and boot loaders. We got introduced to LILO as a very old boot loader which was replaced by GRUB in late 1990s. In this course we take a closer look at GRUB and GRUB2 boot loaders .
GRUB


GRUB (GRand Unified Bootloader) is a boot loader package developed to support multiple operating systems and allow the user to select among them during boot-up.
GRUB versions
GRUB was created by Erich Stefan Boleyn and has been further developed under the GNU project as GNU GRUB. The original package is still available for download but no longer being developed.
GRUB2 has replaced what was formerly known as GRUB (i.e. version 0.9x), which has, in turn, become GRUB Legacy. Enhancements to GRUB2 are still being made, but the current released versions are quite usable for normal operation.
How does GRUB work?
When a computer boots, the BIOS transfers control to the first boot device, which can be a hard disk, a floppy disk, a CD-ROM, or any other BIOS-recognized device.
MBR
The first sector on a hard is called the Master Boot Record (MBR). This sector is only 512 bytes long and contains a small piece of code (446 bytes) called the primary boot loader and the partition table (64 bytes) describing the primary and extended partitions.

GRUB replaces the default MBR with its own code:

By default, MBR code looks for the partition marked as active and once such a partition is found, it loads its boot sector into memory and passes control to it.
Furthermore, GRUB works in stages.
Stage 1 is located in the MBR and mainly points to Stage 2, since the MBR is too small to contain all of the needed data.
Stage 2 points to its configuration file, which contains all of the complex user interface and options we are normally familiar with when talking about GRUB. Stage 2 can be located anywhere on the disk. If Stage 2 cannot find its configuration table, GRUB will cease the boot sequence and present the user with a command line for manual configuration.
The Stage architecture allows GRUB to be large (~20-30K) and therefore fairly complex and highly configurable, compared to most bootloaders, which are sparse and simple to fit within the limitations of the Partition Table.
GRUB Legacy vs GRUB2
Lets draw a big picture:

/boot/grub/menu.lst in GRUB Legacy has been replaced by /boot/grub/grub.cfg in GRUB2.
Which version of linux use which grub ?

Demonstration
In order to show differences between GRUB Legacy and GRUB2, lets change timeout parameter in Two systems (CentsOS5 and Centos7) :
menu.lst (GRUB Legacy)
just make a change in menu.lst and save it and that is all.
grub.cfg (GRUB2)
grub.cfg is overwritten by certain Grub 2 package updates, whenever a kernel is added or removed, or when the user runs update-grub.Do not edit grub.cfg directly!!
In order to make any changes in grub.cfg two steps are required.
1-/etc/default/grub
first edit /etc/default/grub:
save chanages in /etc/default/grub. The configurations are written to /boot/grub2/grub.cfg using grub-mkconfig command
2- grub-mkconfig
grub-mkconfig Generate a GRUB configuration file.
It reads main grub script files from /etc/grub.d and setting from /etc/default/grub and print out generated configuration.
grub2-mkconfig options:
For saving generated configuration we use -o option (it is recommanded to take a backup from /boot/grub2/grub.cfg file, how ever this is not touching Master Boot Record and just re touches GRUB menu):
We where able to use grub2-mkconfig > /boot/grub2/grub.cfg command too,and GRUB2 configuration steps finish here.
grub-update
When you run the update-grub command, GRUB automatically combines the settings from the /etc/default/grub file, the scripts from the /etc/grub.d/ directory, and everything else, creating a /boot/grub/grub.cfg file that’s read at boot.
GRUB Interfaces
There are three interfaces in GRUB which all provide different levels of functionality. The Linux kernel can be booted by the users with the help of these interfaces. Details about the interfaces are:
1-Menu Interface
The GRUB is configured by the installation program in the menu interface. It is the default interface available. It contains a list of the operating systems or kernels which is ordered by name. A specific operating system or kernel can be selected using the arrow keys and it can be booted using the enter key.
GRUB Legacy

GRUB2

Missing 'a' hah!
2-Menu Entry Editor Interface
The e key (a key GRUB Legacy) in the boot loader menu is used to access the menu entry editor. All the GRUB commands for the particular menu entry are displayed there and these commands may be altered before loading the operating system.
GRUB Legacy

GRUB2

3-Command Line Interface
This interface is the most basic GRUB interface but it grants the most control to the user. Using the command line interface, any command can be executed by typing it and then pressing enter. This interface also features some advanced shell features.
GRUB Legacy

It is possible to have GRUB Legacy command prompt after rebooting the system and gain information form there.
GRUB2

GRUB Installation
Normally when we setup a system GRUB is installed for us but there are some cases which we might need to install GRUB ourselves. There are different ways to install GRUB.
If we are on old system with GRUB legacy we can run grub-install command or using setup command in GRUB shell.
If we are on GRUB2 we can use one of GRUB2 utilities.
grub2-install
grub-install installs GRUB onto a device. This includes copying GRUB images into the target directory (generally /boot/grub), and on some platforms may also include installing GRUB onto a boot sector.
as an example grub2-install /dev/sdb will install grub on sdb device, to the master boot record of my hard disk.
How ever instaling GRUB a from running system seems cool, but most of the time there is a problem during boot process and we can not get into our system any more, so we need to reinstall GRUB from GRUB shell. Unfortunately the setup command has been removed from GRUB2 shell and it would need more efforts. We need to bring up the system in rescue mode using a live cd and then install GRUB on our hard disk using current temporary root system file:
it is just an example and do not run it because you would mess up your current system!
grub2-install --boot-directory=/tmp/root/boot /dev/sda
.
.
.
Sources:
https://en.wikipedia.org/wiki/GNU_GRUB
https://whatis.techtarget.com/definition/GRUB-GRand-Unified-Bootloader
https://www.gnu.org/software/grub/
https://www.dedoimedo.com/computers/grub.html
https://www.tutorialspoint.com/what-is-grub-in-linux
https://help.ubuntu.com/community/Grub2
https://www.mankier.com/8/grub2-install
.
Last updated
Was this helpful?