What is GRUB and how to use it?


When you power on the system, the bootloader is the first program that runs and also It is responsible for loading and transferring the control to the operating system Kernel.Grub is the default bootloader for many Linux distributions
Bootloader(GRUB)-Grand Unified Boot Loader it resides inside the MBR program, In MBR partition scheme the first sector of your hard disk contains the bootloader and the partition table information.
Total size of the MBR is 512 bytes, in that 440 bytes are used for bootloader and partition table

Grub can understand filesystem formats, so you can load an arbitrary operating system the way you like, without recording the physical position of your kernel on the disk.Thus you can load the kernel by mentioning its file name and drive and the partition where the kernel resides.
When booting the os with GRUB either you can use CLI method(COMMAND LINE INTERFACE).The command line interface provides a prompt and after it, an editable text area looks like a command line in Unix or DOS. OR mENU BASED METHOD by using the arrow keys, The menu is based on the configuration file.While in the menu, you could switch to CLI interface and also you can edit the menu entries before you use them.

Features of GRUB


1.It has the backward compatibility for booting Linux, FreeBSD, NetBSD and other proprietary kernels such as DOS, windows.All these are supported via a chain-loading function.
Chain loading function: Is nothing but a bootloader starts another bootloader to begin the process. (In simple, calling the bootloader from the Linux bootloader to start the windows os.) if you have only Linux systems then the Grub could boot all of them without the chain-loading function If you are using dual boot(Linux and windows) and Grub being installed on the master boot record then it should load the windows bootloader to boot the windows with the help of the chain loader function.
2.It supports automatic decompression, it can decompress the files which are compressed by the gzip, this is transparent to the users, this greatly reduces the file size and loading time.
3.It supports the network booting, We all know GRUB is a disk-based bootloader and it has network support.Means you could load Operating system image from a network by using the TFTP protocol.
TFTP: Is nothing but used to read and write the files by using the remote server, TFTP it uses UDP to perform the action which is not a secure way.
4.One of the most important features in GRUB is it can provide remote terminal support, which means you can control the GRUB from the remote server.
Note: At present, it allows us only the serial terminal support.

 

GRUB Naming Convention:


As a system administrator knowing the device syntax in GRUB is very important, then only you can assign the specific drive or partition.
Ex:1 Look at the below-naming convention
(sd0)
An important concept you need to know is, GRUB requires the device name should be enclosed it with “( and )”.Here in our example sd part means it is a hard disk,“0” indicates the drive number, which is counted from 0.Here the GRUB will use the whole disk
Ex:2  (hd0,1)
Here the “hd” indicates the hard disk,”0″ indicates the drive number(i.e first hard drive)and “1” indicates the partition number.
Note: Partition number are counted from 0 not from 1, In our example, it shows the second partition from the first hard drive and grub uses only one partition from the disk instead of using the whole disk.
Ex:3  (hd0,4)
Here in this example, the drive number as “0” (first hard drive) and “4” indicates the first extended partition of the first hard drive.
Note: The partition number for the extended partitions  are counted from 4.(As all we know in MBR partition scheme it allows us to create only 4 primary partitions) To know more about the Linux disk management here is the link MBR AND GPT Partition schemes

How to install the GRUB as your bootloader?


To install GRUB as your bootloader first you need to install the GRUB package in your system and then by using the grub-install command you can install the GRUB on your hard drive.
Note: if you are installing GRUB on UNIX like operating system, make sure you have a copy of backup boot disk ready with you, in case if anything goes wrong then your hard disk becomes unbootable.
You must have to provide one argument to the program grub-install(i.e mention where to install the bootloader), The argument you can mention a device file or you can also give the partition number specified in GRUB syntax number

#grub-install  /dev/sda

The above screenshot shows the GRUB is installed in to the MBR of the first SCSI/SATA drive.
Note: if it is an IDE drive then it will look like /dev/hd0
The key point to remember: In reality, most of the operating system it will not say GRUB how to map the BIOS drives to the Operating system devices properly.But GRUB can guess the mapping, but this will not succeed in most cases.So that GRUB provides you one map file known as “device map”.so when you mention device map, the grub shell generates the device map file automatically(if it is not there).The location for the map file is /boot/grub/device-map, so if the device map file is there, the Grub shell examine it to map BIOS drives to OS devices, from the device map file you will find the lines like this,
device  file

ex: (hd0,1)

device =Drive specified in the GRUB

file=its a device file or OS file

Why does the GRUB shell give you a device map file?


In some environment it couldn’t guess the map between the BIOS drives and OS devices properly, for example, if you change the boot sequence between IDE and SCSI in your BIOS, it will get the wrong order,so editing the file in the grub shell makes a mistake ,you can add “#” in the first character then the grub shell will assume the line is just a comment.

Boot the OS from the GRUB command line method:


It’s very easy to boot the OS from the GRUB all you need to do is, you have to specify the device map details and the path to the kernel and initrd images and then after that give the boot command to start booting the OS

Step:1  After power on the server skip the booting process by pressing the down or up arrow key from your keyboard

After you press the keys you will get the grub menu screen where you can specify the commands

 

Step:2 Now to use the command line editing from the grub menu type “c”, after that you will land on the grub console window

Note: This is the grub console it accepts only the grub commands, it will not accept the Linux console commands.

Step:3 Setting root partition and hard disk

We have a grub console command called “root” by using this command we can get the name of the  kernel ,no need to remember the full path name.we can use the TAB key to get the full name path .after typing the “root” command press TAB key it will display all the hard disk and partition, after choosing this you have to give the kernel name(you can use the TAB to get that since grub can understand the kernel file formats, it will check that from the /boot directory).

Step:4 Now type the name of the kernel and its path

Type kernel  /boot/vm and press TAB key it will give you the full path location where it resides
In the beginning, I have explained that the kernel could understand kernel file formats, so by default, it will look inside the /boot directory.

Note: vmlinuz is the default kernel package name

Step:5  Now Start the OS

That’s it😀…The OS will start running after this👍

Note: You can use this method to boot the server with corrupted grub configuration.

 

Let us Have a lookt at GRUB configuration file


#cat  /boot/grub/grub.conf

default=0 says the default OS to boot, from the above screenshot “0” means the first entry which means it is Red Hat Linux server, default=1 means it takes the second title entry as the default operating system.
timeout=5 says the default timeout seconds before the option is selected to boot the grub automatically
hidden menu: This will boot  the OS without showing the grub menu
splash image: It shows  the Location behind the menu
The path for the grub configuration file is /boot/ grub/grub.conf and also it is symbolic linked to the file /etc/grub.conf

I hope now you have understood the concept of grub and how it works.

If you found this article useful, Kindly Subscribe here👉🏿👉🏿 Click this link to Subscribe