Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
GNU GRUB
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Operation== {{Technical|section|date=August 2021}}[[File:GNU GRUB on MBR partitioned hard disk drives.svg|thumb|upright=1.5|GRUB2 on [[Master boot record|MBR]]-partitioned hard drive; stage 1 (<code>boot.img</code>) can alternatively be written into one of the [[Volume boot record|partition boot sectors]].<nowiki/>]] [[File:GNU GRUB on GPT partitioned hard disk drives.svg|thumb|upright=1.5|GRUB2 on a [[GUID Partition Table|GPT]]-partitioned hard drive, booting on [[BIOS|BIOS firmware]], or [[UEFI#CSM booting|UEFI compatibility mode (CSM)]]]] ==={{Anchor|MAP-FILE}}Booting=== {{See also|UEFI disk device compatibility}} When a computer is turned on, its [[BIOS]] finds the primary bootable device (usually the computer's hard disk) and runs the initial [[Bootstrapping (computing)|bootstrap]] program from the [[master boot record]] (MBR). The MBR is the first [[Disk sector|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|10|lc=y}}, such hard disks are still accessed in 512-byte sectors, using the [[Advanced Format#512e|512e emulation]].<ref>{{cite web |last = Smith |first = Ryan |title = Western Digital's Advanced Format: The 4K Sector Transition Begins |publisher = [[AnandTech]] |date = December 18, 2009 |url = http://www.anandtech.com/show/2888 |access-date = October 10, 2013 |archive-date = March 12, 2012 |archive-url = https://web.archive.org/web/20120312061308/http://www.anandtech.com/show/2888 |url-status = live }}</ref> The legacy [[MBR partition table]] supports a maximum of four partitions and occupies 64 bytes, combined. Together with the optional [[MBR disk signature|disk signature]] (four bytes) and [[MBR disk timestamp|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,<ref>{{cite web |url = http://mbldr.sourceforge.net/ |title = mbldr (Master Boot LoaDeR) |year = 2009 |access-date = October 10, 2013 |website = mbldr.sourceforge.net |archive-date = March 16, 2013 |archive-url = https://web.archive.org/web/20130316062331/http://mbldr.sourceforge.net/ |url-status = live }}</ref> it is not big enough to contain a boot loader supporting complex and multiple [[file system]]s, 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 (operating system)|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''{{snd}} 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.<ref name="boot-admin">{{cite web | url = http://www-uxsup.csx.cam.ac.uk/pub/doc/suse/suse9.0/adminguide-9.0/node8.html | title = Booting and Boot Managers | access-date = 2013-10-10 | publisher = SUSE | archive-date = September 17, 2013 | archive-url = https://web.archive.org/web/20130917130646/http://www-uxsup.csx.cam.ac.uk/pub/doc/suse/suse9.0/adminguide-9.0/node8.html | url-status = live }}</ref> Another approach is to make a boot loader aware of the underlying file systems, so kernel images are configured and accessed using their actual [[Path (computing)|file path]]s. 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.<ref name="boot-admin" /> 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)=== [[File:GRUB v1.5 Screenshot, Ubuntu 9.04.png|thumb|GRUB Legacy menu (running as part of [[Ubuntu (operating system)|Ubuntu]] 9.04 installation)]] 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 [[chain loading|chainloads]] GRUB ''stage 1'' from the active [[Partition Boot Sector|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, [[GUID Partition Table|GPT]] or [[Logical Volume Manager (Linux)|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 <code>/boot/grub</code>. ''Stage 2'' will then load the default configuration file and any other modules needed. ==={{anchor|GRUB 2}}Version 2 (GRUB 2)=== [[File:GNU GRUB components.svg|thumb|upright=1.5|GRUB 2 β MBR vs. GPT partitioning and boot sequence visualized (systems using BIOS firmware).]] ==== Startup on systems using [[BIOS]] firmware ==== * See illustration in last image on the right.<ref>{{Cite web|title=GNU GRUB Manual 2.04|url=https://www.gnu.org/software/grub/manual/grub/grub.html#Images|access-date=2020-11-10|website=www.gnu.org|archive-date=April 3, 2018|archive-url=https://web.archive.org/web/20180403023208/http://www.gnu.org/software/grub/manual/grub/grub.html#Images|url-status=live}}</ref> * <code>boot.img</code> ('''stage 1''') is written to the first 440 bytes of the [[Master boot record|Master Boot Record]] (MBR boot code in sector 0), or optionally in a [[partition boot sector]] (PBR). It addresses <code>diskboot.img</code> by a 64-bit [[Logical block addressing | LBA]] address. The actual sector number is written by <code>grub-install</code>. <code>diskboot.img</code> is the first sector of <code>core.img</code> with the sole purpose to load the rest of <code>core.img</code> identified by LBA sector numbers also written by <code>grub-install</code>. ** On MBR partitioned disks, <code>core.img</code> ('''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 of [[Cylinder-Head-Sector]] (C/H/S) addressing used by [[BIOS]] before 1996, therefore <code>core.img</code> is designed to be smaller than 32 KiB. **On GPT partitioned disks <code>core.img</code> is written to its own partition. It must be flagged "BIOS_grub", must not be [[file system|formatted]] and can be as tiny as 1 MiB. * '''stage 2:''' <code>core.img</code> loads <code>/boot/grub/i386-pc/normal.mod</code> from the partition configured by <code>grub-install</code>. If the partition index has changed, GRUB will be unable to find the <code>normal.mod</code>, and presents the user with the GRUB Rescue prompt. * Depending on how GRUB2 was installed, the <code>/boot/grub/</code> is either in the ''root'' partition of the Linux distribution, or in the separate ''/boot'' partition. * '''after normal.mod loaded:''' <code>normal.mod</code> parses <code>/boot/grub/grub.cfg</code>, optionally loads modules (eg. for graphical UI and file system support) and shows the menu. ==== Startup on systems using [[UEFI]] firmware ==== * <code>/efi/<distro>/grubx64.efi</code> (for [[x64]] UEFI systems) is installed as a file in the [[EFI system partition|EFI System Partition]], and booted by the firmware directly, without a <code>boot.img</code> in MBR sector 0. This file is like stage1 and stage1.5. * <code>/boot/grub/</code> can be installed on the EFI System Partition or the separate ''/boot'' partition, among others. * For x64 UEFI systems, stage2 are the <code>/boot/grub/x86_64-efi/normal.mod</code> file and other <code>/boot/grub/</code> files. ==== 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 {{Keypress|Shift}}, or in some modern GRUB versions loaded using UEFI, pressing {{Keypress|Esc}} rapidly while the computer is booting makes it possible to access the boot menu.<ref name="howtogeek.com">{{cite web|url=http://www.howtogeek.com/196655/how-to-configure-the-grub2-boot-loaders-settings/|title=How to Configure the GRUB2 Boot Loader's Settings|website=HowToGeek.com|first=Chris|last=Hoffman|date=September 22, 2014|access-date=April 25, 2015|archive-date=April 28, 2015|archive-url=https://web.archive.org/web/20150428233135/http://www.howtogeek.com/196655/how-to-configure-the-grub2-boot-loaders-settings/|url-status=live}}</ref> In the operating system selection menu GRUB accepts a couple of commands: * By pressing {{key press|e}}, 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 (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 module <code>nvidia-current</code>, one could append <code>modprobe.blacklist=nvidia-current</code> at the end of the kernel parameters. * By pressing {{key press|c}}, 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.<ref>{{cite web |url=https://www.gnu.org/software/grub/grub-documentation.html |title=GNU GRUB documentation |access-date=April 16, 2014 |archive-date=April 18, 2014 |archive-url=https://web.archive.org/web/20140418191412/http://www.gnu.org/software/grub/grub-documentation.html |url-status=live }}</ref> 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 [[Power-on self-test|POSTs]], the hard disk connected to a specific motherboard port ''could'' be assigned the same identifier, for example {{code|hd0, 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 entering {{code|ls}} into either the ''GRUB rescue console'' (available after loading <code>core.img</code> ) or the ''GRUB console'' (available after loading <code>normal.mod</code>) a list of all available hard disks and partitions can be obtained. For example, {{code|ls (hd0,5)/}}) will show numbers that can be assigned to actual hard disks and partitions. As it cannot be guaranteed that the "<code>hd0"</code>style numbering of hard disks via device numbers is consistent, GNU GRUB can use a [[Universally 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 the [[Superblock (file system)|superblock]]. All operations other than formatting should leave the UUID unaltered. It is possible to change the UUID or duplicate it by using [[dd (Unix)|dd]] to clone an entire partition. The file {{code|grub.cfg}} is used to configure GRUB. It contains commands to be executed during each start-up. Without an existing and valid {{code|grub.cfg}}, GRUB will present a prompt. An absolute minimal {{code|grub.cfg}} might contain only the following two commands (cf. [[initial ramdisk]]): <pre> 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 </pre> A fancier {{code|grub.cfg}} will describe a menu to be presented, use multiple colors, and may specify a background picture.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)