GNU GRUB explained

GNU GRUB
Logo Size:120px
Logo Caption:GNU GRUB logo
Author:Erich Boleyn
Developer:GNU Project
Ver Layout:simple
Programming Language:Assembly, C[1]
Operating System:Linux, GNU/Hurd, macOS, BSD, (Solaris/ illumos (x86 port)), and Windows (through chainloading)
Platform:IA-32, x86-64, IA-64, ARM, PowerPC, s390x, MIPS, RISC-V and SPARC
Language:English and others
Genre:Bootloader
License:2007: GPL-3.0-or-later[2]
1999: GPL-2.0-or-later

GNU GRUB (short for GNU GRand Unified Bootloader, commonly referred to as 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 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 (a play on Grand Unified Theory[3]). It is predominantly used for Unix-like systems.

Operation

Booting

When a computer is turned on, its BIOS finds the primary bootable device (usually the computer's hard disk) and runs the initial bootstrap program from the master boot record (MBR). 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, such hard disks are still accessed in 512-byte sectors, using the 512e emulation.[4] The legacy MBR partition table supports a maximum of four partitions and occupies 64 bytes, combined. Together with the optional disk signature (four bytes) and disk timestamp (six bytes), 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,[5] 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(s) 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.[6]

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 1, 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 (MBR) 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 (512 bytes), 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 (within its first 1024 cylinders).

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 (unusual partition table, special disk drivers, GPT or LVM disk) 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

Startup on systems using UEFI firmware

After startup

GRUB presents a menu where the user can choose from operating systems (OS) 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 while the computer is booting makes it possible to access the boot menu.[8]

In the operating system selection menu GRUB accepts a couple of 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 port 1 (of the motherboard), could be assigned the same identifier, e.g. . 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 shall be connected to another computer?

By entering into either the in GRUB rescue console (available after loading core.img) or the GRUB console (available after loading normal.mod) a list of all available hard disks and partitions can be obtained. Either by guessing or by further investigation (e.g.) can these numbers be assigned to actual hard disks and partitions.

As it cannot be guaranteed that the numbering of hard disks via dumb numbers is consistent, GNU GRUB can use the UUID to identify partitions (actually file system instances) – that have this feature – instead.

The file systems ext2, ext3, ext4 and xfs use an Universally Unique Identifier (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 but a formatting should leave the UUID unaltered, but it is possible to change it or duplicate it (by using dd to clone an entire partition).

The file is used to configure GRUB. It is the file where the commands which shall be executed during each start up are stored. Without an existing and valid, GRUB will present a prompt.

An absolute minimal might contain only the following two commands (cf. initial ramdisk):

linux (hd0,1)/kernel/vmlinuz-3.20.1-4 ro  # use the file name "vmlinuz-…" located in the directory /kernel on the first partition of the first hard disk as linux kernel image
initrd (hd0,1)/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

More fancy will describe a menu to be presented, will use multiple colors, and maybe a background picture.

History

GRUB was initially developed by Erich Boleyn as part of work on booting the operating system GNU/Hurd, developed by the Free Software Foundation.[10] In 1999, Gordon Matzigkeit and Yoshinori K. Okuji made GRUB an official software package of the GNU Project and opened the development process to the public.[10], the majority of Linux distributions have adopted GNU GRUB 2, as well as other systems such as Sony's PlayStation 4.[11]

Development

GRUB version 1 (also known as "GRUB Legacy") is no longer under development and is being phased out.[12] The GNU GRUB developers have switched their focus to GRUB 2,[13] a complete rewrite with goals including making GNU GRUB cleaner, more robust, more portable and more powerful. GRUB 2 started under the name PUPA. PUPA was supported by the Information-technology Promotion Agency (IPA) in Japan. PUPA was integrated into GRUB 2 development around 2002, when GRUB version 0.9x was renamed GRUB Legacy.

Some of the goals of the GRUB 2 project include support for non-x86 platforms, internationalization and localization, non-ASCII characters, dynamic modules, memory management, a scripting mini-language, migrating platform specific (x86) code to platform specific modules, and an object-oriented framework. GNU GRUB version 2.00 was officially released on June 26, 2012.[14] [15]

Three of the most widely used Linux distributions use GRUB 2 as their mainstream boot loader.[16] [17] [18] Ubuntu adopted it as the default boot loader in its 9.10 version of October 2009.[19] Fedora followed suit with Fedora 16 released in November 2011.[20] OpenSUSE adopted GRUB 2 as the default boot loader with its 12.2 release of September 2012.[21] Solaris also adopted GRUB 2 on the x86 platform in the Solaris 11.1 release.[22] Buildroot also uses GNU GRUB for x86 and x86_64 targets.

In late 2015, the exploit of pressing backspace 28 times to bypass the login password was found and quickly fixed.[23] [24]

Variants

GNU GRUB is free and open-source software, so several variants have been created. Some notable ones, which have not been merged into GRUB mainline:

Utilities

GRUB configuration tools

The setup tools in use by various distributions often include modules to set up GRUB. For example, YaST2 on SUSE Linux and openSUSE distributions and Anaconda on Fedora/RHEL distributions. StartUp-Manager and GRUB Customizer are graphical configuration editors for Debian-based distributions. The development of StartUp-Manager stopped on 6 May 2011 after the lead developer cited personal reasons for not actively developing the program.[33] GRUB Customizer is also available for Arch-based distributions.

For GRUB 2 there are KDE Control Modules.[34] [35]

GRLDR ICE is a tiny tool for modifying the default configuration of grldr file for GRUB4DOS.[36]

Boot repair utilities

Boot-Repair is a simple graphical tool for recovering from frequent boot-related problems with GRUB and Microsoft Windows bootloader. This application is available under GNU GPL license. Boot-Repair can repair GRUB on multiple Linux distributions including, but not limited to, Debian, Ubuntu, Mint, Fedora, openSUSE, and Arch Linux.

Installer for Windows

Grub2Win is a Windows open-source software package. It allows GNU GRUB to boot from a Windows directory. The setup program installs GNU GRUB version 2.12 to an NTFS partition. A Windows GUI application is then used to customize the GRUB boot menu, themes, UEFI boot order, scripts etc. All GNU GRUB scripts and commands are supported for both UEFI and legacy systems. Grub2Win can configure GRUB for multiboot of Windows, Ubuntu, openSuse, Fedora and many other Linux distributions. It is freely available under GNU GPL License at SourceForge.

Alternative boot managers

See also: Comparison of bootloaders.

The strength of GRUB is the wide range of supported platforms, file systems, and operating systems, making it the default choice for distributions and embedded systems.

However, there are boot managers targeted at the end user that give more friendly user experience, graphical OS selector and simpler configuration:

Non-graphical alternatives:

External links

How-Tos and troubleshooting

Distribution wikis have many solutions for common issues and custom setups that might help you:

Documentation

Introductory articles

Technicalities

See also

Notes and References

  1. Web site: Ohloh Analysis Summary – GNU GRUB . . 2010-05-12 . February 4, 2009 . https://web.archive.org/web/20090204221010/http://www.ohloh.net/p/grub . live .
  2. Web site: GNU GRUB license. https://archive.today/20130911174027/http://bzr.savannah.gnu.org/lh/grub/trunk/grub/annotate/head:/COPYING . dead . 2013-09-11 .
  3. http://searchenterpriselinux.techtarget.com/definition/GRUB-GRand-Unified-Bootloader EnterpriseLinux.com Definitions
  4. Web site: Smith . Ryan . Western Digital's Advanced Format: The 4K Sector Transition Begins . . December 18, 2009 . October 10, 2013 . March 12, 2012 . https://web.archive.org/web/20120312061308/http://www.anandtech.com/show/2888 . live.
  5. Web site: mbldr (Master Boot LoaDeR) . 2009 . October 10, 2013 . mbldr.sourceforge.net . March 16, 2013 . https://web.archive.org/web/20130316062331/http://mbldr.sourceforge.net/ . live.
  6. Web site: Booting and Boot Managers . 2013-10-10 . SUSE . September 17, 2013 . https://web.archive.org/web/20130917130646/http://www-uxsup.csx.cam.ac.uk/pub/doc/suse/suse9.0/adminguide-9.0/node8.html . live .
  7. Web site: GNU GRUB Manual 2.04. 2020-11-10. www.gnu.org. April 3, 2018. https://web.archive.org/web/20180403023208/http://www.gnu.org/software/grub/manual/grub/grub.html#Images. live.
  8. Web site: How to Configure the GRUB2 Boot Loader's Settings. HowToGeek.com. Chris. Hoffman. September 22, 2014. April 25, 2015. April 28, 2015. https://web.archive.org/web/20150428233135/http://www.howtogeek.com/196655/how-to-configure-the-grub2-boot-loaders-settings/. live.
  9. Web site: GNU GRUB documentation . April 16, 2014 . April 18, 2014 . https://web.archive.org/web/20140418191412/http://www.gnu.org/software/grub/grub-documentation.html . live .
  10. https://www.gnu.org/software/grub/manual/html_node/History.html GRUB Manual – 1.2 Grub History
  11. Web site: PS4 runs Orbis OS, a modified version of FreeBSD that's similar to Linux . 2013-06-24 . 2013-10-23 . extremetech.com . November 18, 2020 . https://web.archive.org/web/20201118102620/http://www.extremetech.com/gaming/159476-ps4-runs-orbis-os-a-modified-version-of-freebsd-thats-similar-to-linux . live.
  12. https://www.gnu.org/software/grub/grub-legacy.html GNU GRUB – GRUB Legacy
  13. Web site: GNU GRUB – GRUB 2 . 2014-04-18 . bot: unknown . https://web.archive.org/web/20080607115918/https://www.gnu.org/software/grub/grub-2.en.html . June 7, 2008 . mdy-all. .
  14. GRUB 2.00 released . June 28, 2012 . December 1, 2012 . grub-devel . Vladimir . Serbinenko . January 13, 2013 . https://web.archive.org/web/20130113150325/http://lists.gnu.org/archive/html/grub-devel/2012-06/msg00093.html . live .
  15. Web site: Larabel. Michael. GRUB 2.00 Boot-Loader Officially Released. Phoronix.com. 28 June 2012. September 13, 2016. https://web.archive.org/web/20160913072241/http://www.phoronix.com/scan.php?page=news_item&px=MTEyODc. live.
  16. Web site: An Introduction to Ubuntu . Haddon . Tom . 26 January 2012 . WebJunction . 21 September 2012 . dead . https://web.archive.org/web/20121028160701/http://www.webjunction.org/documents/webjunction/An_Introduction_to_Ubuntu.html . October 28, 2012 . mdy-all.
  17. Web site: What is Red Hat Enterprise Linux (RHEL)? . Janssen . Cory . Technopedia . 21 September 2012 . October 3, 2012 . https://web.archive.org/web/20121003043553/http://www.techopedia.com/definition/15777/red-hat-enterprise-linux-rhel . live .
  18. Web site: SUSE chief lists progress since privatisation . Varghese . Sam . 2012-09-20 . 21 September 2012 . September 22, 2012 . https://web.archive.org/web/20120922122233/http://www.itwire.com/business-it-news/open-source/56686-suse-chief-lists-progress-since-privatisation . live .
  19. Web site: 9.10 Karmic GRUB version. Distrowatch.com. 8 July 2012. May 9, 2012. https://web.archive.org/web/20120509124455/http://distrowatch.com/table.php?distribution=ubuntu. live.
  20. http://fedoraproject.org/wiki/GRUB_2 GRUB 2
  21. http://en.opensuse.org/openSUSE:Upcoming_features openSUSE:Upcoming features – openSUSE
  22. http://www.oracle.com/technetwork/server-storage/solaris11/overview/solaris11-1-1845817.html Solaris 11.1
  23. Web site: You can Hack into a Linux Computer just by pressing 'Backspace' 28 times . thehackernews.com . December 16, 2015 . Swati . Khandelwal . March 13, 2017 . April 30, 2017 . https://web.archive.org/web/20170430230510/http://thehackernews.com/2015/12/hack-linux-grub-password.html . live .
  24. Web site: Back to 28: Grub2 Authentication 0-Day . Hector . Marco and . Ismael . Ripoll . December 2015 . March 13, 2017 . May 17, 2017 . https://web.archive.org/web/20170517200459/http://hmarco.org/bugs/CVE-2015-8370-Grub2-authentication-bypass.html#exploit . live .
  25. http://dlc.sun.com/osol/docs/content/SYSADV1/getov.html#fwbqs x86: Modifying Boot Behavior by Editing the GRUB Menu at Boot Time
  26. http://docs.sun.com/app/docs/doc/819-2379/getnk?a=view x86: Supported GRUB Implementations
  27. Web site: Peng . Tao . Grub4ext4 . June 13, 2017 . December 31, 2016 . https://web.archive.org/web/20161231162436/https://code.google.com/archive/p/grub4ext4/ . live .
  28. http://web.syllable.org/documentation/FAQ.html#2_3 2.3 Why does Syllable have its own version of GRUB?
  29. Web site: TrustedGRUB project . November 18, 2014 . sourceforge.net . June 5, 2013 . November 29, 2014 . https://web.archive.org/web/20141129024847/http://sourceforge.net/projects/trustedgrub/ . live .
  30. https://biosbits.org: BIOS Implementation Test Suite
  31. Web site: grub4dos . Google Site . 7 April 2019 . April 7, 2019 . https://web.archive.org/web/20190407043143/https://sites.google.com/site/grubdos/ . live .
  32. Web site: GRUB for DOS Introduction . grub4dos.sourceforge.net . 7 April 2019 . https://web.archive.org/web/20190407042958/http://microsaint.narod.ru/_Info_Grub4Dos/Grub4dos_tutorial.html . 7 April 2019 . 2007.
  33. Web site: StartUp-Manager is dead : StartUp-Manager. launchpad.net. May 6, 2011 . en. 2017-04-09. August 1, 2020. https://web.archive.org/web/20200801025445/https://launchpad.net/startup-manager/+announcement/8300. live.
  34. http://kde-apps.org/content/show.php/GRUB2+Bootloader+Editor?content=139643 GRUB2 Bootloader Editor
  35. Web site: Grub2 KCM. KDE-Apps.org. 2011-01-27. September 6, 2015. https://web.archive.org/web/20150906234436/http://kde-apps.org/content/show.php/Grub2+Kcm?content=137886. live.
  36. Web site: Grub4dos tutorial. Narod.ru. April 25, 2015. May 3, 2015. https://web.archive.org/web/20150503014019/http://microsaint.narod.ru/_Info_Grub4Dos/Grub4dos_tutorial.html. live.