GRUB (also known as GNU GRUB or GNU Grand Unified Bootloader) is a bootloader and boot manager for Linux and other Unix-based OSes. GRUB starts after BIOS finishes the necessary hardware tests and loads it from the Master Boot Record (MBR). Once loaded, GRUB takes control of the system and loads the Linux kernel.
In this post, we will explore the following topics:
- When does GRUB come into picture in boot loading?
- What are the edit options and how to use them?
- Additional information
When does GRUB come into picture in boot loading?
As mentioned earlier, GRUB is the first program that runs after the BIOS. GRUB is responsible for finding and loading the kernel image and the initial RAM disk (initrd) image, which contain the drivers and modules needed by the kernel. GRUB also allows the user to choose between different operating systems, if there is more than one on the system.
GRUB consists of two stages: the first stage is stored in the boot sector, and the second stage is stored in a file system. The first stage of GRUB loads the second stage, which is a larger program that can access multiple file systems and display a graphical menu. The menu allows the user to select a kernel image to boot, or to edit the boot options. If the user does not select anything, GRUB loads the default kernel image after a timeout.
GRUB can also chain-load other boot loaders, such as Windows Boot Manager, by loading their boot sector instead of a kernel image. GRUB can also load an initial RAM disk (initrd) image, which is a temporary file system that contains drivers and modules needed by the kernel. GRUB then passes control to the kernel, which initializes the rest of the operating system.
What are the edit options and how to use them?
GRUB provides a command-line interface and a graphical menu interface for editing the boot options. The command-line interface can be accessed by pressing c
at the GRUB menu, and the graphical menu interface can be accessed by pressing e
at the GRUB menu. Both interfaces allow the user to modify the boot parameters, such as the kernel image, the initrd image, the root file system, and the kernel options.
The command-line interface has a prompt that looks like this:
grub>
The user can type commands to list the available devices, partitions, files, and kernels. For example, the command ls
lists the devices and partitions, and the command ls (hd0,1)/boot
lists the files in the /boot directory of the first partition of the first hard disk. The user can also use the set
and unset
commands to set and unset environment variables, such as root
, prefix
, and default
. The user can also use the linux
and initrd
commands to specify the kernel and initrd images to load. The user can also use the boot
command to boot the system with the specified parameters.
The graphical menu interface has a screen that looks like this:
GNU GRUB version 2.04
+-------------------------------------------------------------------+
|*Ubuntu |
| Advanced options for Ubuntu |
| Windows Boot Manager (on /dev/sda2) |
| System setup |
+-------------------------------------------------------------------+
Use the ↑ and ↓ keys to select which entry is highlighted.
Press enter to boot the selected OS, `e' to edit the commands
before booting or `c' for a command-line.
The user can use the arrow keys to select an entry, and press Enter
to boot it. The user can also press e
to edit the commands for the selected entry. The user can then see a screen that looks like this:
+-------------------------------------------------------------------+
|setparams 'Ubuntu' |
| |
|recordfail |
|load_video |
|gfxmode $linux_gfx_mode |
|insmod gzio |
|if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi |
|insmod part_gpt |
|insmod ext2 |
|set root='hd0,gpt1' |
|if [ x$feature_platform_search_hint = xy ]; then |
| search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt1 |
| --hint-efi=hd0,gpt1 --hint-baremetal=ahci0,gpt1 b58ee4f7-5b6a-4 |
|else |
| search --no-floppy --fs-uuid --set=root b58ee4f7-5b6a-40fb- |
|fi |
|linux /boot/vmlinuz-5.4.0-42-generic root=UUID=b58ee4f7-5b6a-40f |
|b-8d1c-9f2a8d8a8a8a ro quiet splash $vt_handoff |
|initrd /boot/initrd.img-5.4.0-42-generic |
+-------------------------------------------------------------------+
The selected entry will be started automatically in 10s.
Press Ctrl-x to start, Ctrl-c for a command prompt or Escape
to discard edits and return to the GRUB menu.
The user can use the arrow keys to move the cursor, and press Backspace
or Delete
to delete characters, and press Insert
or Type
to insert characters. The user can also press Ctrl-a
to move to the beginning of the line, and press Ctrl-e
to move to the end of the line. The user can also press Ctrl-x
to boot the system with the edited parameters, or press Esc
to discard the changes and return to the GRUB menu.
Additional information
GRUB is a powerful and flexible bootloader and boot manager for Linux systems. It supports various file systems, operating systems, and boot methods. It also provides a user-friendly interface for editing the boot options and customizing the boot process.
GRUB is configured by files that specify the boot entries, the menu appearance, and the default settings. The main configuration file is /boot/grub/grub.cfg, which is generated by the grub-mkconfig
command. The user should not edit this file directly, as it may be overwritten by system updates. Instead, the user should edit the files in /etc/grub.d and /etc/default/grub, which are the source files for generating the grub.cfg file. The user can also use a graphical tool, such as Grub Customizer, to modify the GRUB settings.
GRUB can be installed, reinstalled, or updated by using the grub-install
command. The user can also use the grub-update
command to update the grub.cfg file after changing the GRUB configuration files or installing a new kernel. The user can also use the grub-reboot
command to set a one-time boot entry for the next reboot, or use the grub-set-default
command to set a permanent default boot entry.
Conclusion
In this post, we have learned about GRUB in Linux, and how it works as a bootloader and a boot manager. We have also learned how to access, edit, and customize GRUB with a graphical tool or a terminal command. We hope that this post has given you a better understanding of how GRUB boots and manages your Linux system.