GNU GRUB


GNU GRUB is a boot loader package from the GNU Project. GRUB is the reference implementation of the Free Software Foundation's Multiboot Specification, which provides a user the choice to boot one of multiple operating systems installed on a computer set up for multi-booting or select a specific kernel configuration available on a particular operating system's partitions.
GNU GRUB was developed from a package called the Grand Unified Bootloader. It is predominantly used for Unix-like systems.

Operation

Booting

When an IBM PC compatible computer with a BIOS is turned on, the BIOS finds the primary bootable device and runs the initial bootstrap program from the master boot record. The MBR is the first sector of the hard disk. This bootstrap program must be small because it has to fit in a single sector. For a long time, the size of a sector has been 512 bytes. Since 2009 there are hard disks available with a sector size of 4096 bytes, called Advanced Format disks, but as of 2013, such hard disks are still accessed in 512-byte sectors, using the 512e emulation.
The legacy MBR partition table supports a maximum of four partitions and occupies 64 bytes, combined. Together with the optional disk signature and disk timestamp, this leaves between 434 and 446 bytes available for the machine code of a boot loader. Although such a small space can be sufficient for very simple boot loaders, it is not big enough to contain a boot loader supporting complex and multiple file systems, menu-driven selection of boot choices, etc. Boot loaders with bigger footprints are therefore split into pieces, where the smallest piece fits in the MBR, while one or more larger pieces are stored in other locations such as empty sectors between the MBR and the first partition. The code in the MBR then does little more than starting the second part.
The purpose of the remaining part of the boot loader is to actually boot an operating system by configuring it and starting the kernel. Kernels are in most cases stored as files residing on appropriate file systems, but the concept of a file system is unknown to the BIOS. Thus, in BIOS-based systems, the duty of a boot loader is to access the content of those files, so it can be loaded into the RAM and executed.
One possible approach for boot loaders is to load kernel images by directly accessing hard disk sectors without understanding the underlying file system. Usually, an additional level of indirection is required, in form of maps or map files auxiliary files that contain a list of physical sectors occupied by kernel images. Such maps need to be updated each time a kernel image changes its physical location on disk, due to installing new kernel images, file system defragmentation, etc. Also, in case of the maps changing their physical location, their locations need to be updated within the boot loader's MBR code, so the sectors indirection mechanism continues to work. This is not only cumbersome, but it also leaves the system in need of manual repairs in case something goes wrong during system updates.
Another approach is to make a boot loader aware of the underlying file systems, so kernel images are configured and accessed using their actual file paths. That requires a boot loader to contain a driver for each of the supported file systems, so they can be understood and accessed by the boot loader itself. This approach eliminates the need for hardcoded locations of hard disk sectors and existence of map files, and does not require MBR updates after kernel images are added or moved around. The configuration of a boot loader is stored in a regular file, which is also accessed in a file system-aware way to obtain boot configurations before the actual booting of any kernel images. Thus, fewer things can go wrong during system updates. As a downside, such boot loaders are larger and more complex.
GNU GRUB uses the second approach, by understanding the underlying file systems. The boot loader itself is split into multiple stages so that it fits in the MBR boot scheme.
Two major versions of GRUB are in common use: GRUB version 0, called GRUB legacy, is only prevalent in older releases of Linux distributions. GRUB 2 was written from scratch and intended to replace its predecessor, and is now used by a majority of Linux distributions.

Version 0 (GRUB Legacy)

GRUB 0.x follows a two-stage approach. The master boot record usually contains GRUB stage 1, or can contain a standard MBR implementation which chainloads GRUB stage 1 from the active partition's boot sector. Given the small size of a boot sector, stage 1 can do little more than load the next stage of GRUB by loading a few disk sectors from a fixed location near the start of the disk.
Stage 1 can load stage 2 directly, but it is normally set up to load the stage 1.5., located in the first 30 KiB of hard disk immediately following the MBR and before the first partition. In case this space is not available the install of stage 1.5 will fail. The stage 1.5 image contains file system drivers, enabling it to directly load stage 2 from any known location in the filesystem, for example from /boot/grub. Stage 2 will then load the default configuration file and any other modules needed.

Version 2 (GRUB 2)

Startup on systems using [BIOS] firmware

  • See illustration in last image on the right.
  • boot.img is written to the first 440 bytes of the Master Boot Record, or optionally in a partition boot sector. It addresses diskboot.img by a 64-bit LBA address. The actual sector number is written by grub-install. diskboot.img is the first sector of core.img with the sole purpose to load the rest of core.img identified by LBA sector numbers also written by grub-install.
  • * On MBR partitioned disks, core.img is stored in the empty sectors between the MBR and the first partition. Recent operating systems suggest a 1 MiB gap here for alignment. This gap used to be 62 sectors as a reminder of the sector number limit of Cylinder-Head-Sector addressing used by BIOS before 1996, therefore core.img is designed to be smaller than 32 KiB.
  • *On GPT partitioned disks core.img is written to its own partition which must be flagged "BIOS_grub", must not be formatted and can be as tiny as 1 MiB.
  • stage 2: core.img loads /boot/grub/i386-pc/normal.mod from the partition configured by grub-install. If the partition index has changed, GRUB will be unable to find the normal.mod, and presents the user with the GRUB Rescue prompt.
  • Depending on how GRUB2 was installed, the /boot/grub/ is either in the root partition of the Linux distribution, or in the separate /boot partition.
  • after normal.mod loaded: normal.mod parses /boot/grub/grub.cfg, optionally loads modules and shows the menu.

    Startup on systems using [UEFI] firmware

  • /efi//grubx64.efi is installed as a file in the EFI System Partition, and booted by the firmware directly, without a boot.img in MBR sector 0. This file is like stage1 and stage1.5.
  • /boot/grub/ can be installed on the EFI System Partition or the separate /boot partition, among others.
  • For x64 UEFI systems, stage2 are the /boot/grub/x86_64-efi/normal.mod file and other /boot/grub/ files.

    After startup

GRUB presents a menu where the user can choose from operating systems found by grub-install. GRUB can be configured to automatically load a specified OS after a user-defined timeout. If the timeout is set to zero seconds, pressing and holding, or in some modern GRUB versions loaded using UEFI, pressing rapidly while the computer is booting makes it possible to access the boot menu.
In the operating system selection menu GRUB accepts a couple of commands:
  • By pressing, it is possible to edit kernel parameters of the selected menu item before the operating system is started. The reason for doing this in GRUB can be an emergency case: the system has failed to boot. Using the kernel parameters line it is possible, among other things, to specify a module to be disabled for the kernel. This could be required if the specific kernel module is broken and thus prevents boot-up. For example, to blacklist the kernel module nvidia-current, one could append modprobe.blacklist=nvidia-current at the end of the kernel parameters.
  • By pressing, the user enters the GRUB command line. The GRUB command line resembles GNU Bash, but only implements a subset of the line editing functions and GRUB-specific commands.
Once boot options have been selected, GRUB loads the selected kernel into memory and passes control to the kernel. Alternatively, GRUB can pass control of the boot process to another boot loader, using chain loading. This is the method used to load operating systems that do not support the Multiboot Specification or are not supported directly by GRUB.

Identifying partitions (UUID workaround)

A computer can have multiple hard disks connected to it. These could be identified via their SATA port. Each time the computer POSTs, the hard disk connected to a specific motherboard port could be assigned the same identifier, for example. But what if such consistency cannot be guaranteed? What if the constellation of connected hard disks changed from one start up to another? What if a hard disk will be connected to another computer?
By entering into either the GRUB rescue console or the GRUB console will show numbers that can be assigned to actual hard disks and partitions.
As it cannot be guaranteed that the "hd0"style numbering of hard disks via device numbers is consistent, GNU GRUB can use a Universally Unique Identifier to identify partitions.
The file systems ext2, ext3, ext4 and xfs use a UUID to uniquely identify an instance. The UUID is created when a partition is formatted. The UUID is part of the file system and written to the superblock. All operations other than formatting should leave the UUID unaltered. It is possible to change the UUID or duplicate it by using dd to clone an entire partition.
The file is used to configure GRUB. It contains commands to be executed during each start-up. Without an existing and valid, GRUB will present a prompt.
An absolute minimal might contain only the following two commands :

linux /kernel/vmlinuz-3.20.1-4 ro # use the file named "vmlinuz-…" located in the directory /kernel on the first partition of the first hard disk as linux kernel image
initrd /boot/initrd.img-3.20.1-4 # use the file named "initrd.img–…" located in the directory /boot on the first partition of the first hard disk as initial ramdisk

A fancier will describe a menu to be presented, use multiple colors, and may specify a background picture.