Skip to main content
Engineering LibreTexts

07-B.5: GRUB2 - Installation

  • Page ID

    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[5]). It is predominantly used for Unix-like systems. The GNU operating system uses GNU GRUB as its boot loader, as do most Linux distributions and the Solaris operating system on x86 system

    On a disk drive - the first 446 bytes is the boot.img file. The next 32,256 bytes is the core.img file, there is some amount of empty space, then the /boot/grub file is installed and loaded from that location at boot time.
    Figure \(\PageIndex{1}\): GRUB2 on MBR-partitioned hard drive. ("GNU_GRUB_on_MBR_partitioned_hard_disk_drives.svg" by ScotXWWikimedia Commons is licensed under CC BY-SA 3.0)

    Startup on systems using BIOS firmware:

    • 1st stage:boot.img is written to the first 440 bytes of the Master Boot Record (MBR in sector 0), or optionally in a partition's boot sector (PBR / VBR). It addresses diskboot.img by a 64-bit LBA address, thus it can load from above the 2 GiB limit of the MBR. The actual sector number is written by grub-install.
    • 2nd stage: diskboot.img is the first sector of core.img (called stage 1.5 in GRUB Legacy) 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 (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 C/H/S addressing used by Bios before 1998, therefore core.img is designed to be smaller than 32 KiB.
    • On GPT partitioned disks: partitions are not limited to 4, thus core.img is written to its own tiny (1 MiB), filesystem-less BIOS boot partition.
    • 3rd stage: core.img enters 32-bit protected mode, uncompresses itself (the kernel of grub and filesystem modules to reach /boot/grub), then loads /boot/grub/<platform>/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, where the user “can” find and load normal.mod, or the linux kernel.
    • The /boot/grub directory can be located on any partition (GRUB can read many filesystems, including NTFS). Depending on how it was installed it's either in the root partition of the distribution, or a separate /boot partition.
    • 4th stage: normal.mod (equivalent of stage 2 in GRUB Legacy) parses /boot/grub/grub.cfg, optionally loads modules (eg. for graphical UI) and shows the menu.

    Startup on systems using UEFI firmware:

    • Common on motherboards since ca. 2012.
    • /efi/<distro>/grubx64.efi is installed as a file in the EFI System Partition, and booted by the firmware directly, without a boot.img in sector 0.
    • /boot/grub/ can be installed on the EFI System Partition as well.

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

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

    • By pressing 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 nvidia-current, one could append modprobe.blacklist=nvidia-current at the end of the kernel parameters.
    • By pressing c, 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.[10]

    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.

    The grub2-install Command

    grub2-install installs GRUB onto a device. This includes copying GRUB images into the target directory (generally /boot/grub), and on some platforms may also include installing GRUB onto a boot sector. If grub2 is being installed on a UEFI system make sure to use the --efi-directory option so the files get placed in the proper directory.


    grub-install [OPTIONS]... INSTALL_DEVICE

    Notice the command is grub-install even though it installs grub2.

    Command Options:

    Option Option Meaning
    --modules=MODULES Pre-load modules specified by MODULES.
    --install-modules=MODULES Install only MODULES and their dependencies. The default is to install all available modules.
    --themes=THEMES Install THEMES. The default is to install the starfield theme, if available.
    --fonts=FONTS Install FONTS. The default is to install the unicode font.
    --locales=LOCALES Install only locales listed in LOCALES. The default is to install all available locales.
    --compress=no,xz,gz,lzo Compress GRUB files using the specified compression algorithm.
    --directory=DIR Use images and modules in DIR.
    --grub-mkimage=FILE Use FILE as grub-mkimage. The default is /usr/bin/grub-mkimage.
    --boot-directory=DIR Use DIR as the boot directory. The default is /boot. GRUB will put its files in a subdirectory of this directory named grub.
    --target=TARGET Install GRUB for TARGET platform. The default is the platform grub-install is running on.
    --grub-setup=FILE Use FILE as grub-setup. The default is /usr/bin/grub-setup.
    --grub-mkrelpath=FILE Use FILE as grub-mkrelpath. The default is /usr/bin/grub-mkrelpath.
    --grub-probe=FILE Use FILE as grub-probe. The default is /usr/bin/grub-mkrelpath.
    --allow-floppy Make the device also bootable as a floppy. This option is the default for /dev/fdX devices. Some BIOSes will not boot images created with this option.
    --recheck Delete any existing device map and create a new one if necessary.
    --force Install even if problems are detected.
    --force-file-id Use identifier file even if UUID is available.
    --disk-module=MODULE Use MODULE for disk access. This allows you to manually specify either biosdisk or native disk access. This option is only available on the BIOS target platform.
    --removable Treat the target device as if it is removable. This option is only available on the EFI target platform.

    Use ID as the bootloader ID. This option is only available on the EFI target platform.

    --efi-directory=DIR Use DIR as the EFI System Partition root. This option is only available on the EFI target platform.
    INSTALL_DEVICE Install GRUB to the block device INSTALL_DEVICE.

    Adapted from:
    "GNU GRUB.svg" by Multiple ContributorsWikimedia Commons is licensed under CC BY-SA 3.0

    • Was this article helpful?