In this post we will discuss the GRUB2 (GRand Unified Boot-loader) in Linux. recently i surfing the Linux base concepts then i came to know about grub2. here i ll share what is boot loader?, how grub2 boot loader is works? and how we can customize? and how we apply the themes?

What is the Boot Loader
            Boot loader is small code reside in MBR to load the kernel(core) of the operating system and give the control to OS. When PC is switched on the system controls goes to BIOS. Bios initialize the peripherals and check the booting order of the device then select the highest priority of the device execute the code stored in MBR of that device. Each Operating System shipped with boot loader

For Example :-
Microsoft Windows up to Windows XP comes with  NTLDR boot loader
From Windows Vista onward they use BOOTMGR as a boot loader.
These boot loaders are specifically designed to load Windows OS. If want use these boot loader to boot other OS (Linux, Mac OS X) then we need to tweak using 3rd party tools like easyBCD

For Linux Operating System uses many different boot loaders as distro choice like ISOLINUX, LILO, GRUB

Here we talk about GRUB2 boot loader.

What is Master Boot Record (MBR)?
          Master Boot Record (MBR) is one block with 512 bytes. Its located in first sector of HDD, CD, other device. Its holds the partition entries and boot code.
Following MBR block, the actual partition are begin.
This is helps if disconnect the HDD and connected to other machine, that machine understands ur partitions and file systems.

HDD Layout and MBR
We know Total size of MBR is 512 bytes. In this 446 bytes for Boot code (Boot loader code) and 4 Partition entry, each 16 bytes long (64 bytes) and 2 bytes MBR signature.

Here this is limitation that one hdd is maximum 4 primary partition or 3 primary partition and 1 logical partition. The reason is MBR can store maximum 4 entry in partition table.

If we use logical partition, its not actually  partition  its another partition entry tables. its called extended partition table, it contain many entries to logical volumes. (see the above diagram)

Here each volume (or) partition has separate boot sector its called volume boot record apart from MBR. its used for chain-loader of boot loader. The reason is grub boot loader can boot all Linux and some Unix also (freeBSD, netBSD, openBSD).
                      GRUB Boot loader --> Load Linux Kernel

But it can't load the windows OS because GRUB don't know about Windows start-up process. Only Windows Boot-loader know about this. Windows Boot-loader stored in volume boot sector. First Grub is loaded and show the menu. Is user select the windows then GRUB load the volume boot sector code and give the control.
GRUB Boot loader --> Windows Boot loader --> Load Windows Kernel


Here one limitation for some boot loaders  like Windows boot-loader. They can't boot the OS, if OS is installed in logical partition. that's why windows OS we must install in primary partition. because that boot code can't capable to access the logical volume.

The reason is if OS is installed primary partition then boot loaders easily can find the partition, because MBR contain primary partition entry.

But when u installed in logical partition then that partition entry is residing extended partition table. Boot loader get overhead to interpret the extended   partition table and get the entry then load the OS.


Why GRUB2?
     we already seen for Linux there are many boot loader. now let see why GRUB2 is more powerful than others.

  • GRUB2 is completely rewritten from original GRUB
  • Its modular based, so we can load any module when its needed
  • Its supports menu and themes
  • GRUB2 acts as a mini OS, we can mount many file-system
  • Its capable to boot ISO Linux directly. check here
  • Its capable to load the OS even though its installed logical volume.

GRUB2 comes as a default boot loader in many Linux like Ubuntu 9 onward's.
Normal GRUB Menu

How GRUB2 Works?
    In 446 bytes in MBR is not enough to put entire grub code. so MBR contain small code called boot.img and its capable to read first block of core.img file from any partition (logical volume too) before partition is mounted. Because that time they cant understand the file system. so they read directly one sector (block) where core.img file is located.

The first block of core.img is called diskboot.img for HDD or cdboot.img for cd-rom or pxeboot.img for network boot. This block contain many address of blocks , to iterate these address and load blocks then the complete core.img (around 32 KB) comes into memory. This is job for boot code in GRUB.

Actually core.img file is generated when GRUB is installed . This file contain one or more necessary module to mount the File system, where GRUB is installed. So core.img file is responsible for mount the File system and access the GRUB configuration file. Once file system is mounted then we can access the files normal like Linux. All other modules are located /boot/grub. Its loaded when its needed.

All grub related configuration (menu entries, graphics resolution, timeout, etc...) are stored grub.cfg file in /boot/grub/ location. No need to put this line in MBR. because once File system is mounted then we can access through path /boot/grub/grub.cfg


now we see 2 important command for GRUB

sudo update-grub    (or) sudo mk-config
   this command generate the grub.cfg file dynamically. Its not update into MBR or volume boot record. Its just generate configuration file.

sudo grup-install /dev/sda
   this command update the GRUB code in MBR. Its overwrite other boot loader code. (here sda is our HDD and dont mention any partition number)


sudo grup-install /dev/sda5
   this command update the GRUB code in Volume boot record. (here sda is our HDD and 5 is 5th partition)

If u want to install GRUB in pendrive
first mount the pendrive and use grup-install command with --root-directory option

sudo grup-install --root-directory=/media/pendrive /dev/sdb
   here /media/pendrive is mount point of pendrive and /dev/sdb is device file for pen drive , when want to know the device file run sudo fdisk -l


How grub.cfg file is generated?
    when i run the update-grub command then its generate the grub.cfg file. But how?

When install grub on our machine (its time of OS installation) they created some supporting files. they located in /etc/default/grub file and /etc/grub.d/ folder.

/etc/default/grub - contain environment variables of GRUB
/etc/grub.d/  - contain some helper scripts files like theme,OS probe,custom scripts file

When we run update-grub command then its called scripts files sequentially accenting number order. These scripts files read environment variables from  /etc/default/grub file and generate the  grub.cfg file

grub.cfg file is read only. so don't modify directly

Customize the GRUB menu
      grub.cfg file is read only. so we need to modify the environment file or script file for customization.

Very important Note : 
    If u modify the /etc/default/grub file or /etc/grub.d/ scripts  then its not reflected in grub.cfg file. So we must call the update-grub command to reflect these changes into grub.cfg.


1) Grub Time-Out
       When we start the system, grub menu appears and wait for some time, is u not select the menu they load the default menu.
if u want to modify the time out option then change GRUB_TIMEOUT=30 in /etc/default/grub file

2) Change Graphics Resolution
      Usually graphics menu appears in low graphics resolution like 640x480. If u know the resolution then change GRUB_GFXMODE environment variable
for ex:  GRUB_GFXMODE = 1024x768x32,640x480 here width height x bit-depth

2) Change GRUB Background
       Grub menu appears in normal black background. If u want to change the image of the background then create GRUB_BACKGROUND variable.
ex: GRUB_BACKGROUND=/home/ramki/Desktop/ram.jpg
Its accepts jpg,png,ttf format images
GRUB with background
If u want change the font colors and highlighting color.

Apply the Theme (see the video)
        Above customization are just change the color and background. They not change the menu position and we can't add new labels and progress bar.

We can build the theme with some components (boot_menu, progress_bar, image, vbox, hbox) arranged our own way position and size in theme definition file.

each component have left,top,width,height properties. we can mention in pixel value or in percentage vale. Percentage is recommended.
after theme is created then we need to specify in GRUB_THEME variable in /etc/default/grub file

ex:
GRUB_THEME = /boot/grub/theme/theme.txt

This is screenshot of simple theme i used
After Applying Theme
 Here we can use custom fonts also. but our fonts are not directly supported. we need to convert into pf2 format then we can use it.

sudo grup-mkfont --size=14 --range=0x0-0x7F --output=aerial.pf2 aerial.ttf
above line convert the aerial.ttf font to pf2 format with size is 14 and cover all character range.

If u want the above theme, u can get it from here   then u modify the background image and other details

Update 1 :

Why we need to install bootloader in Volume Boot Record (VBR)? 
                The reason is if MBR bootloader is not capable to boot Particular OS then that boot loader do the chain loading to load VBR boot loader and gives the control.

for example there are 5 OS is there, but only one OS boot loader can sit in MBR. If that MBR bootloader is not capable to load other OS then how to load other OS. the answer is chain loading.

so all 5 os install in different partition then bootloader is installed in VBR and MBR. but MBR is overrides based on which on recently installed. but VBR have one more copy of that OS specific boot loader. If MBR boot loader is capable to boot another OS then no need VBR boot loader. But If MBR bootloader is not capable to boot particular OS then MBR boot loader load the particular OS partition VBR and gives the control. now VBR boot loader can boot that OS.

simple, 2 OS is there windows in sda2 partition and Linux in sda3 partition. and each partition contain their respective boot loader code in VBR.  so sda2 VBR contain windows boot loader.

If GRUB bootloader is in MBR section, then it can load all Unix/Linux OS,but its can't load Windows OS. But windows boot loader is in sda2 VBR. so GRUB is load sda2 VBR to memory and gives control to it. Now Windows boot loader execute and load the Windows kernel.



Screen cast (Watch in HD)





comments are welcomed.

This entire post is based on my understanding of GRUB. If anything wrong or i misunderstood please let me know.

Resources: