GRUB version 2

GRUB version 2 is the default boot loader for Fedora and Ubuntu. The GRUB 2 configuration file is located at /boot/grub/grub.cfg (some distributions place this file in /boot/grub2 to enable both GRUB Legacy and GRUB 2).

grub.cfg is generated automatically from the content of the /etc/default/grub file and the /etc/grub.d directory. You should modify or add to those files to configure GRUB 2. After making changes, you must explicitly rebuild the grub.cfg file by typing the update-grub command.

Here is an example /etc/default/grub file:

linux /etc/default/grub file

As you can see from the picture above, this file contains the GRUB 2 menu settings. You can select which OS will be loaded by default, background image, timeout, etc.

The main GRUB 2 scripts are located inside the /etc/grub.d directory. Here is the content of this directory in Ubuntu:

linux /etc/grub.d directory

Here is a brief description of each script:

  • 00_header – the script that loads GRUB settings from the /etc/default/grub file.
  • 05_debian_theme – defines the background, colors and themes.
  • 10_linux – loads the menu entries for the installed distribution.
  • 20_linux_xen – loads the Xen hypervisor
  • 20_memtest86+ – loads the memtest utility.
  • 30_os-prober – scans the hard disks for other operating systems and adds them to the boot menu.
  • 40_custom – a template that you can use to create additional entries to be added to the boot menu.

 

In GRUB 2, partitions are numbered starting from 1 rather than from 0. The devices are still numbered from 0. This means that sda1 is hd0,1 and NOT hd0,0, as with GRUB Legacy.

 

Adding a new GRUB script

To add a new boot option, you need to create a new file that has the XX_ prefix in the name (where XX is a sequence of numbers) in the /etc/grub.d directory. If you want the new entry to be placed above others, use lower numbers, if you want it to be placed below others, use higher numbers.

For example, 12_NAME will be placed after the default entries by the operating system, whereas 07_NAME will be placed before the 10_linux entries.

Here’s an example script:

linux /etc/grub.d custom script

Here is a brief description of each line in the script:

#!/bin/sh -e – use the bash shell to execute the script.
echo “String” – sets the string that you will see when running update-grub.
cat << EOF – defines the start of the actual boot entry.
menuentry “OS Name” – sets the name of the menu entry.
set root=(hdX,Y) – sets the root device.
linux /boot/vmlinuz – specifies a file that contains the Linux kernel.
initrd /boot/initrd.img – specifies an initial RAM disk.
EOF – ends the GRUB entry.

 

Unlike GRUB Legacy, GRUB 2 is designed to work with both BIOS and EFI-based computers.
Geek University 2022