Jump to content

GNU GRUB

From Wikipedia, the free encyclopedia

GNU GRUB
Original author(s)Erich Boleyn
Developer(s)GNU Project
Initial release1995;29 years ago(1995)
Stable release
2.12[1]Edit this on Wikidata / 20 December 2023
Preview release
2.12-rc1[2]Edit this on Wikidata / 10 July 2023
Repository
Written inAssembly,C[3]
Operating systemLinux,GNU/Hurd,macOS,BSD,(Solaris/illumos(x86 port)), andWindows(through chainloading)
PlatformIA-32,x86-64,IA-64,ARM,PowerPC,s390x,MIPS,RISC-VandSPARC
Available inEnglishand others
TypeBootloader
License2007:GPL-3.0-or-later[a][5]
1999:GPL-2.0-or-later[b]
Websitewww.gnu.org/software/grub

GNU GRUB(short forGNU GRand Unified Bootloader,commonly referred to asGRUB) is aboot loaderpackage from theGNU Project.GRUB is thereference implementationof theFree Software Foundation'sMultiboot Specification,which provides a user the choice to boot one of multipleoperating systemsinstalled on a computer or select a specifickernelconfiguration available on a particular operating system's partitions.

GNU GRUB was developed from a package called theGrand Unified Bootloader(a play onGrand Unified Theory[6]). It is predominantly used forUnix-likesystems.

Operation

[edit]
GRUB2 onMBR-partitioned hard drive; stage 1 (boot.img) can alternatively be written into one of thepartition boot sectors.
GRUB2 on aGPT-partitioned hard drive, booting onBIOS firmware,orUEFI compatibility mode (CSM)

Booting

[edit]

When a computer is turned on, itsBIOSfinds the primary bootable device (usually the computer's hard disk) and runs the initialbootstrapprogram from themaster boot record(MBR). The MBR is the firstsectorof 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, calledAdvanced Formatdisks, but as of October 2013,such hard disks are still accessed in 512-byte sectors, using the512e emulation.[7] The legacyMBR partition tablesupports a maximum of four partitions and occupies 64 bytes, combined. Together with the optionaldisk signature(four bytes) anddisk timestamp(six bytes), this leaves between 434 and 446 bytes available for themachine codeof a boot loader. Although such a small space can be sufficient for very simple boot loaders,[8]it is not big enough to contain a boot loader supporting complex and multiplefile 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 thekernel.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 theRAMand 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 ofindirectionis required, in form ofmapsormap 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.[9]

Another approach is to make a boot loader aware of the underlying file systems, so kernel images are configured and accessed using their actualfile 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.[9]

GNU GRUB uses the second approach, by understanding the underlying file systems. The boot loader itself is split into multiplestagesso that it fits in the MBR boot scheme.

Two major versions of GRUB are in common use: GRUB version 0, calledGRUB legacy,is only prevalent in older releases of Linux distributions.GRUB 2was written from scratch and intended to replace its predecessor, and is now used by a majority of Linux distributions.

Version 0 (GRUB Legacy)

[edit]
GRUB v1 menu (running as part ofUbuntu8.04 installation)

GRUB 0.x follows a two-stage approach. The master boot record (MBR) usually contains GRUBstage 1,or can contain a standard MBR implementation whichchainloadsGRUBstage 1from the activepartition's boot sector.Given the small size of a boot sector (512 bytes),stage 1can 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 1can loadstage 2directly, but it is normally set up to load thestage 1.5.,located in the first 30KiBof hard disk immediately following the MBR and before the first partition. In case this space is not available (unusual partition table, special disk drivers,GPTorLVMdisk) the install ofstage 1.5will fail. Thestage 1.5image contains file system drivers, enabling it to directly loadstage 2from any known location in the filesystem, for example from/boot/grub.Stage 2will then load the default configuration file and any other modules needed.

Version 2 (GRUB 2)

[edit]
GRUB 2 – MBR vs. GPT partitioning and boot sequence visualized (systems using BIOS firmware).

Startup on systems usingBIOSfirmware

[edit]
  • See illustration in last image on the right.[10]
  • boot.img(stage 1) is written to the first 440 bytes of theMaster Boot Record(MBR boot code in sector 0), or optionally in apartition boot sector(PBR). It addressesdiskboot.imgby a 64-bitLBAaddress. The actual sector number is written bygrub-install.diskboot.imgis the first sector ofcore.imgwith the sole purpose to load the rest ofcore.imgidentified by LBA sector numbers also written bygrub-install.
    • On MBR partitioned disks,core.img(stage 1.5) is stored in the empty sectors (if available) between the MBR and the first partition. Recent operating systems suggest a 1 MiB gap here for alignment (2047 512-byte, or 255 4KiB, sectors). This gap used to be 62 sectors (31 KiB) as a reminder of the sector number limit ofCylinder-Head-Sector(C/H/S) addressing used byBIOSbefore 1996, thereforecore.imgis designed to be smaller than 32 KiB.
    • On GPT partitioned diskscore.imgis written to its own partition. It must be flagged "BIOS_grub", must not beformattedand can be as tiny as 1 MiB.
  • stage 2:core.imgloads/boot/grub/i386-pc/normal.modfrom the partition configured bygrub-install.If the partition index has changed, GRUB will be unable to find thenormal.mod,and presents the user with the GRUB Rescue prompt.
  • Depending on how GRUB2 was installed, the/boot/grub/is either in therootpartition of the Linux distribution, or in the separate/bootpartition.
  • after normal.mod loaded:normal.modparses/boot/grub/grub.cfg,optionally loads modules (eg. for graphical UI and file system support) and shows the menu.

Startup on systems usingUEFIfirmware

[edit]
  • /efi/<distro>/grubx64.efi(forx64UEFI systems) is installed as a file in theEFI System Partition,and booted by the firmware directly, without aboot.imgin MBR sector 0. This file is like stage1 and stage1.5.
  • /boot/grub/can be installed on the EFI System Partition or the separate/bootpartition.
  • For x64 UEFI systems, stage2 are the/boot/grub/x86_64-efi/normal.modfile and other/boot/grub/files.

After startup

[edit]

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⇧ Shiftwhile the computer is booting makes it possible to access the boot menu.[11]

In the operating system selection menu GRUB accepts a couple of commands:

  • By pressinge,it is possible to edit kernel parameters of the selected menu itembefore the operating system is started.The reason for doing this in GRUB (i.e. not editing the parameters in an already booted system) 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 (blacklisted) 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 modulenvidia-current,one could appendmodprobe.blacklist=nvidia-currentat the end of the kernel parameters.
  • By pressingc,the user enters the GRUB command line. The GRUB command line is not a regular Linux shell, like e.g.bash,and accepts only certain GRUB-specific commands, documented by various Linux distributions.[12]

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, usingchain loading.This is the method used to load operating systems that do not support theMultiboot Specificationor are not supported directly by GRUB.

Identifying partitions (UUID workaround)

[edit]

A computer can have multiple hard disks connected to it. These could be identified via their SATA port. Each time the computerPOSTs,the hard disk connected to a specific motherboard portcouldbe assigned the same identifier, for examplehd0, hd1,….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 enteringlsinto either theGRUB rescue console(available after loadingcore.img) or theGRUB console(available after loadingnormal.mod) a list of all available hard disks and partitions can be obtained. For example,ls (hd0,5)/) 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 aUniversally Unique Identifier(UUID) to identify partitions (actually file system instances).

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 thesuperblock.All operations other than formatting should leave the UUID unaltered. It is possible to change the UUID or duplicate it by usingddto clone an entire partition.

The filegrub.cfgis used to configure GRUB. It contains commands to be executed during each start-up. Without an existing and validgrub.cfg,GRUB will present a prompt.

An absolute minimalgrub.cfgmight 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

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

History

[edit]

GRUB was initially developed by Erich Boleyn as part of work on booting theoperating systemGNU/Hurd,developed by theFree Software Foundation.[13]In 1999, Gordon Matzigkeit and Yoshinori K. Okuji made GRUB an official software package of theGNU Projectand opened thedevelopment processto the public.[13]As of 2014,the majority of Linux distributions have adopted GNU GRUB 2, as well as other systems such as Sony'sPlayStation 4.[14]

Development

[edit]

GRUB version 0 (also known as "GRUB Legacy" ) is no longer under development and is being phased out.[15]The GNU GRUB developers have switched their focus to GRUB 2,[16]acomplete rewritewith goals including making GNU GRUB cleaner, more robust, more portable and more powerful. GRUB 2 started under the namePUPA.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-x86platforms,internationalization and localization,non-ASCII characters, dynamic modules,memory management,a scriptingmini-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.[17][18]

Three of the most widely usedLinux distributionsuse GRUB 2 as their mainstream boot loader.[19][20][21]Ubuntuadopted it as the default boot loader in its 9.10 version of October 2009.[22]Fedorafollowed suit with Fedora 16 released in November 2011.[23]OpenSUSEadopted GRUB 2 as the default boot loader with its 12.2 release of September 2012.[24]Solarisalso adopted GRUB 2 on the x86 platform in the Solaris 11.1 release.[25]Buildrootalso uses GNU GRUB forx86andx86_64targets.

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

Variants

[edit]

GNU GRUB isfree software,so several variants have been created. Some notable ones, which have not been merged into GRUB mainline:

  • OpenSolarisincludes a modified GRUB Legacy that supports Solaris VTOC slices, automatic 64-bit kernel selection, and booting fromZFS(withcompressionand multiple boot environments).[28][29]
  • Google Summer of Code2008 had a project to support GRUB legacy to boot fromext4formatted partitions.[30]
  • TheSyllableproject made a modified version of GRUB to load the system from itsAtheOS File System.[31]
  • TrustedGRUBextends GRUB by implementing verification of the system integrity and boot process security, using theTrusted Platform Module(TPM).[32]
  • The Intel BIOS Implementation Test Suite (BITS) provides a GRUB environment for testing BIOSes and in particular their initialization of Intel processors, hardware, and technologies. BITS supports scripting via Python, and includes Python APIs to access various low-level functionality of the hardware platform, including ACPI, CPU and chipset registers, PCI, and PCI Express.[33]
  • GRUB4DOS is a GRUB legacy fork that improves the installation experience on DOS andMicrosoft Windowsby putting everything besides the GRLDR config in one image file. It can be loaded directly from DOS, or byNTLDRorWindows Boot Manager.[34][35]GRUB4DOS is under active development and as of 2021 supports UEFI.[36]

Utilities

[edit]

GRUB configuration tools

[edit]
StartUp-Manager, a program used to configure GRUB

The setup tools in use by various distributions often include modules to set up GRUB. For example,YaST2onSUSE LinuxandopenSUSEdistributions andAnacondaonFedora/RHELdistributions. 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.[37]GRUB Customizer is also available for Arch-based distributions.

For GRUB 2 there are KDE Control Modules.[38][39]

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

Boot repair utilities

[edit]

Boot-Repairis a simple graphical tool for recovering from frequent boot-related problems with GRUB andMicrosoft Windowsbootloader. This application is available underGNU GPL license.Boot-Repair can repair GRUB on multiple Linux distributions including, but not limited to, Debian, Ubuntu,Mint,Fedora, openSUSE, andArch Linux.

GRUB Customizer

Installer for Windows

[edit]

Grub2Winis 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 underGNU GPL LicenseatSourceForge.

Alternative boot managers

[edit]

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:

  • rEFInd– Macintosh-style graphical boot manager, only for UEFI-based computers (BIOS not supported).
  • CloverEFI– Macintosh-style graphical boot manager for BIOS and UEFI-based computers. It emulates UEFI with a heavily modifiedDUETfrom the TianoCore project and requires a FAT formatted partition even on BIOS systems. As a benefit, it has a basic filesystem driver in the partition boot sector, avoiding the brittleness of a second or third stage and the infamous GRUB Rescue prompt. The user interface looks similar to rEFInd: both inherit from the abandoned boot managerrEFIt.

Non-graphical alternatives:

  • systemd-boot– Light, UEFI-only boot manager with text-based OS selector menu.
[edit]

How-Tos and troubleshooting

[edit]

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

Documentation

[edit]

Introductory articles

[edit]

Technicalities

[edit]

See also

[edit]

Notes

[edit]
  1. ^GPL-3.0-or-later since 2007-07-21.[4]
  2. ^GPL-2.0-or-later from 1999 until 2007-07-02.

References

[edit]
  1. ^Daniel Kiper (December 20, 2023)."GRUB 2.12 released".RetrievedDecember 20,2023.
  2. ^"GRUB 2.12 release candidate 1".July 10, 2023.
  3. ^"Ohloh Analysis Summary – GNU GRUB".Ohloh.Archivedfrom the original on February 4, 2009.RetrievedMay 12,2010.
  4. ^"Migrate to GNU General Public License Version 3".July 21, 2007.Archivedfrom the original on October 27, 2021.RetrievedOctober 27,2021.
  5. ^"GNU GRUB license".Archived fromthe originalon September 11, 2013.
  6. ^EnterpriseLinux.com DefinitionsArchivedDecember 3, 2010, at theWayback MachineDefinition of GRand Unified Bootloader
  7. ^Smith, Ryan (December 18, 2009)."Western Digital's Advanced Format: The 4K Sector Transition Begins".AnandTech.Archivedfrom the original on March 12, 2012.RetrievedOctober 10,2013.
  8. ^"mbldr (Master Boot LoaDeR)".mbldr.sourceforge.net.2009.Archivedfrom the original on March 16, 2013.RetrievedOctober 10,2013.
  9. ^ab"Booting and Boot Managers".SUSE.Archivedfrom the original on September 17, 2013.RetrievedOctober 10,2013.
  10. ^"GNU GRUB Manual 2.04".www.gnu.org.Archivedfrom the original on April 3, 2018.RetrievedNovember 10,2020.
  11. ^Hoffman, Chris (September 22, 2014)."How to Configure the GRUB2 Boot Loader's Settings".HowToGeek.com.Archivedfrom the original on April 28, 2015.RetrievedApril 25,2015.
  12. ^"GNU GRUB documentation".Archivedfrom the original on April 18, 2014.RetrievedApril 16,2014.
  13. ^abGRUB Manual – 1.2 Grub HistoryArchivedApril 16, 2014, at theWayback Machine.Gnu.org (2012-06-23). Retrieved on 2012-12-01.
  14. ^"PS4 runs Orbis OS, a modified version of FreeBSD that's similar to Linux".extremetech.com.June 24, 2013.Archivedfrom the original on November 18, 2020.RetrievedOctober 23,2013.
  15. ^GNU GRUB – GRUB LegacyArchivedApril 20, 2014, at theWayback Machine.Gnu.org. Retrieved on 2012-12-01.
  16. ^"GNU GRUB – GRUB 2".Archived from the original on June 7, 2008.RetrievedApril 18,2014.{{cite web}}:CS1 maint: bot: original URL status unknown (link).
  17. ^Serbinenko, Vladimir (June 28, 2012)."GRUB 2.00 released".grub-devel(Mailing list).Archivedfrom the original on January 13, 2013.RetrievedDecember 1,2012.
  18. ^Larabel, Michael."GRUB 2.00 Boot-Loader Officially Released".Phoronix.com.Archivedfrom the original on September 13, 2016.RetrievedJune 28,2012.
  19. ^Haddon, Tom (January 26, 2012)."An Introduction to Ubuntu".WebJunction. Archived fromthe originalon October 28, 2012.RetrievedSeptember 21,2012.
  20. ^Janssen, Cory."What is Red Hat Enterprise Linux (RHEL)?".Technopedia.Archivedfrom the original on October 3, 2012.RetrievedSeptember 21,2012.
  21. ^Varghese, Sam (September 20, 2012)."SUSE chief lists progress since privatisation".Archivedfrom the original on September 22, 2012.RetrievedSeptember 21,2012.
  22. ^"9.10 Karmic GRUB version".Distrowatch.com.Archivedfrom the original on May 9, 2012.RetrievedJuly 8,2012.
  23. ^GRUB 2ArchivedAugust 5, 2012, at theWayback Machine.FedoraProject. Retrieved on 2012-12-01.
  24. ^openSUSE:Upcoming features – openSUSEArchivedSeptember 20, 2012, at theWayback Machine.En.opensuse.org. Retrieved on 2012-12-01.
  25. ^Solaris 11.1ArchivedJune 19, 2018, at theWayback Machine.Oracle Retrieved 2015-03-19.
  26. ^Khandelwal, Swati (December 16, 2015)."You can Hack into a Linux Computer just by pressing 'Backspace' 28 times".thehackernews.com.Archivedfrom the original on April 30, 2017.RetrievedMarch 13,2017.
  27. ^Marco and, Hector; Ripoll, Ismael (December 2015)."Back to 28: Grub2 Authentication 0-Day".Archivedfrom the original on May 17, 2017.RetrievedMarch 13,2017.
  28. ^x86: Modifying Boot Behavior by Editing the GRUB Menu at Boot TimeArchivedJuly 19, 2011, at theWayback Machine,Modifying Solaris Boot Behavior on x86 Based Systems (Task Map) – System Administration Guide: Basic Administration
  29. ^x86: Supported GRUB ImplementationsArchivedOctober 21, 2009, at theWayback Machine,(System Administration Guide: Basic Administration) – Sun Microsystems
  30. ^Peng, Tao."Grub4ext4".Archivedfrom the original on December 31, 2016.RetrievedJune 13,2017.
  31. ^2.3 Why does Syllable have its own version of GRUB?ArchivedJanuary 7, 2011, at theWayback Machine,Syllable Documentation
  32. ^"TrustedGRUB project".sourceforge.net.June 5, 2013.Archivedfrom the original on November 29, 2014.RetrievedNovember 18,2014.
  33. ^BIOS Implementation Test SuiteArchivedOctober 19, 2016, at theWayback Machine,Official BITS website
  34. ^"grub4dos".Google Site.Archivedfrom the original on April 7, 2019.RetrievedApril 7,2019.
  35. ^"GRUB for DOS Introduction".grub4dos.sourceforge.net.2007. Archived fromthe originalon April 7, 2019.RetrievedApril 7,2019.
  36. ^grub4dosonGitHub
  37. ^"StartUp-Manager is dead: StartUp-Manager".launchpad.net.May 6, 2011.Archivedfrom the original on August 1, 2020.RetrievedApril 9,2017.
  38. ^GRUB2 Bootloader EditorArchivedSeptember 7, 2015, at theWayback Machine.Kde-apps.org (2012-06-18). Retrieved on 2012-12-01.
  39. ^"Grub2 KCM".KDE-Apps.org.Archivedfrom the original on September 6, 2015.RetrievedJanuary 27,2011.
  40. ^"Grub4dos tutorial".Narod.ru.Archivedfrom the original on May 3, 2015.RetrievedApril 25,2015.