From Fedora Project Wiki

(gen imp)
 
(84 intermediate revisions by 30 users not shown)
Line 8: Line 8:
 
Starting with Fedora 16, GRUB 2 is the default bootloader on x86 BIOS systems. For upgrades of BIOS systems the default is also to install GRUB 2, but you can opt to skip bootloader configuration entirely.
 
Starting with Fedora 16, GRUB 2 is the default bootloader on x86 BIOS systems. For upgrades of BIOS systems the default is also to install GRUB 2, but you can opt to skip bootloader configuration entirely.
  
= Tasks / Common issues =
+
= Adding and removing kernel command-line parameters using grubby =
  
== Adding Other operating systems to the GRUB 2 menu ==
+
Grubby is a utility that updates the bootloader-specific configuration files. The utility is a recommended way for making routine changes to the kernel boot parameters and setting a default kernel.
  
If you have other operating systems installed and wish to boot them via GRUB 2:  
+
Following are some of the selected illustrations of <code>grubby</code> usage:
 +
 
 +
* To add one kernel parameter to a single boot entry:
 +
<pre># grubby --args=<NEW_PARAMETER> --update-kernel=/boot/vmlinuz-5.11.14-300.fc34.x86_64</pre>
 +
 
 +
* To add multiple kernel paramters to a single boot entry:
 +
<pre># grubby --args="<NEW_PARAMETER1> <NEW_PARAMETER2 <NEW_PARAMETER_n>" --update-kernel=/boot/vmlinuz-5.11.14-300.fc34.x86_64</pre>
 +
 
 +
* To add one kernel parameter to all currently existing and future boot entries:
 +
<pre># grubby --args=<NEW_PARAMETER> --update-kernel=ALL</pre>
 +
 
 +
* To remove one kernel parameter from all currently existing and future boot entries:
 +
<pre># grubby --remove-args=<PARAMETER_TO_REMOVE> --update-kernel=ALL</pre>
 +
 
 +
* To set the default kernel:
 +
<pre># grubby --set-default=/boot/vmlinuz-5.11.12-300.fc34.x86_64</pre>
 +
 
 +
 
 +
 
 +
= Updating and repairing the GRUB 2 main configuration file =
 +
 
 +
== Discovering what firmware the system is running ==
 +
The <code>/boot/grub2/grub.cfg</code> is the main GRUB 2 configuration file. It is a static file that you rarely modify. Except in cases of disk replacement or installation of another Linux distribution.
 +
 
 +
To discover what firmware your machine is using, run the following command:
 +
 
 +
* On UEFI systems:
 +
<pre># ls -ld /sys/firmware/efi</pre>
 +
 
 +
* On BIOS systems:
 +
<pre># ls -lrt /etc/grub2.cfg</pre>
 +
 
 +
A directory listing of either of these commands indicate that you are running the corresponding firmware.
 +
 
 +
The <code>grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg</code> command adds entries for other detected operating systems. That will be done based on the output of the <code>os-prober</code> tool.
 +
 
 +
{{admon/note|The above command for updating the GRUB 2 configuration file is only applicable for UEFI systems with Fedora 33 and earlier. Everybody else should use <code>grub2-mkconfig -o /boot/grub2/grub.cfg</code>. |}}
 +
 
 +
 
 +
 
 +
== Repairing GRUB 2 ==
 +
 
 +
If your machine is not working because of the broken GRUB 2 bootloader, you can boot into the rescue mode to repair an already installed operating system.
 +
 
 +
For more details see [https://docs.fedoraproject.org/en-US/fedora/rawhide/install-guide/advanced/Boot_Options/#sect-boot-options-rescue Booting Your Computer in Rescue Mode].
 +
 
 +
After completing steps specified in the previous link, run the following command to mount the root partition:
 +
 
 +
<pre># chroot /mnt/sysimage</pre>
  
<pre>
 
# yum install os-prober
 
  
# grub2-mkconfig -o /boot/grub2/grub.cfg
 
</pre>
 
  
{{admon/note|Note|As of the F16 Beta, os-prober should get pulled in automatically during install and used to auto-generate entries for your other installed operating systems.}}
+
== Reinstalling GRUB 2 ==
  
== Setting default entry ==
+
The GRUB 2 packages contain commands for installing a bootloader and for creating a bootloader configuration file.
  
Due to <code>grub2-mkconfig</code> (and os-prober) we cannot predict the order of the entries in /boot/grub2/grub.cfg, so we set the default by name/title instead. To do this, first, we set <pre>GRUB_DEFAULT=saved</pre> in <code>/etc/default/grub</code>, and run <pre>grub2-mkconfig -o /boot/grub2/grub.cfg</pre> to update grub.cfg. Then we call <pre>grub2-set-default <title or number></pre> with the title of the newly installed Fedora's entry. From that point on you can change the default by calling <pre>grub2-set-default <title or number></pre> or view it by running <pre>grub2-editenv list</pre>
+
The <code>grub2-install</code> command installs the GRUB 2 bootloader usually in the master boot record (MBR), in free and unpartioned space. The bootloader files are placed in the <code>/boot/</code> directory. You can install the GRUB 2 bootloader with:
  
To do that, you'll need the list of possible menu entries, which you can find with
+
<pre># grub2-install /dev/sda</pre>
<pre>grep menuentry /boot/grub2/grub.cfg</pre> (Surely there's a better way?)
 
  
{{admon/note|Note|There are other, simpler, ways of setting the default entry, but they are prone to error if/when grub2-mkconfig is re-run. These include directly setting the default in /boot/grub2/grub.cfg or setting GRUB_DEFAULT to either a number or an entry title in /etc/default/grub. Neither of these methods is recommended.}}
+
{{admon/warning| Do not use the <code>grub2-install</code> command on UEFI systems. On those systems, bootloaders are in the <code>shim</code> and <code>grub-efi</code> packages. By reinstalling those packages, the bootloaders are reinstalled to their proper location in <code>/boot/efi/</code> (the EFI system partition). }}
  
If you understand the risks involved and still want to directly modify /boot/grub2/grub.cfg, here's how you can do it:
+
To reinstall the GRUB 2 bootloader:
  
Edit /boot/grub2/grub.cfg, and change the line
+
* Learn what firmware your system is running. See [https://fedoraproject.org/wiki/GRUB_2#Discovering_what_firmware_my_system_is_running Discovering what firmware my system is running] section.
  
{{admon/caution|This is not the recommended method|This will not survive grub2-mkconfig. It might not even survive a kernel update.}}
+
* On UEFI systems run:
 +
<pre># dnf reinstall shim-* grub-efi-*</pre>
  
 +
* On BIOS systems:
 +
** Find the device node the <code>/boot/</code> directory is located on:
 
<pre>
 
<pre>
set default="0"  
+
# mount | grep "/boot "
 +
/dev/sda4 on /boot type ext4 (rw,relatime,seclabel)
 
</pre>
 
</pre>
 +
The device node is <code>/dev/sda4</code>.
 +
 +
** Reinstall the bootloader while specifying the device node without the number:
 +
<pre># grub2-install /dev/sda</pre>
 +
 +
 +
 +
= Appendix =
 +
 +
== Enabling serial console in GRUB 2 ==
 +
 +
To enable serial console for usage on virtual environments you need to run the following command:
  
to
 
 
<pre>
 
<pre>
set default="5"
+
# grubby --args="systemd.journald.forward_to_console=1 console=ttyS0,38400 console=tty1" --update-kernel=/boot/vmlinuz-5.11.16-300.fc34.x86_64
</pre>
+
# grubby --set-default=/boot/vmlinuz-5.11.16-300.fc34.x86_64
 +
</pre>
 +
 
 +
The first command specifies the baud rate, console forwarding for <code>systemd</code>, what console to use (<code>tty1</code>) and on what kernel such changes should be applied. The second command ensures the specified kernel is going to be loaded by default on next reboot.
 +
 
 +
For instructions on how to enable serial consol in GRUB 2 for baremetal machines, see [https://www.gnu.org/software/grub/manual/grub/html_node/Serial-terminal.html Using GRUB via a serial line]
 +
 
 +
 
 +
{{admon/note|Note|In UEFI boot environment, use <code>efi0</code> instead of <code>--unit=0</code>. If that doesn't work, check that your serial port is visible in your UEFI environment, e.g. by running <code>devtree</code> or <code>dh -p SerialIO</code> in EFI Shell. See https://lists.gnu.org/archive/html/help-grub/2017-01/msg00007.html for more information.}}
 +
 
 +
 
 +
 
 +
== Setting default entry ==
 +
 
 +
{{admon/warning|Look to (default) kernel sysconfig options. |if file <code>/etc/sysconfig/kernel</code> have <pre>UPDATEDEFAULT=yes</pre> in every kernel update the grub entry is update to last entry, if you don't want that set:  <pre>UPDATEDEFAULT=no</pre> (write "no" in lower case) |}}
 +
 
 +
{{admon/warning|Some parts of this section is wrong or outdated for F17 and later releases. | <s>Be also aware of [[https://bugzilla.redhat.com/show_bug.cgi?id=768106 Bug 768106 - grubby does not support grub2 set default="${saved_entry}" and replaces with "0"]].</s> version grubby-8.28-1.fc19 has fixed issues with "Default Menuentry" as noted in the linked bug-report|}}
  
== Updating GRUB 2 configuration ==
+
Due to <code>grub2-mkconfig</code> (and os-prober) we cannot predict the order of the entries in <code>/boot/grub2/grub.cfg</code>, so we set the default by name/title instead.
  
It is safe to directly edit /boot/grub2/grub.cfg in Fedora. Grubby in Fedora modifies the configuration when a kernel update is performed but does so in a safe manner. Other distributions, in particular Debian and Debian-derived distributions provide a software patch that adds an {{command|update-grub}} command which is neither included nor needed in Fedora.
+
Open <code>/etc/default/grub</code> and ensure this line exists:
  
== Encountering the dreaded GRUB 2 boot prompt ==
+
<pre>GRUB_DEFAULT=saved</pre>
If improperly configured, GRUB 2 may fail to load and subsequently drop to a boot prompt. To address this issue, proceed as follows:
 
  
1. List the drives which GRUB 2 sees:
+
and ensure this line not exists:
<pre>
 
grub2> ls
 
</pre>
 
  
2. The output for a dos partition table /dev/sda with three partitons will look something like this:
+
<pre>GRUB_SAVEDEFAULT=true</pre>  
<pre>
 
(hd0) (hd0,msdos3) (hd0,msdos2) (hd0,msdos1)
 
</pre>
 
  
3.  While the output for a gpt partition table /dev/sda with four partitions will look something like this:
+
or ensure this line exists:
<pre>
 
(hd0) (hd0,gpt4) (hd0,gpt3)  (hd0,gpt2) (hd0,gpt1)
 
</pre>
 
  
4. With this information you can now probe each partition of the drive and locate your vmlinuz and initramfs files:
+
<pre>GRUB_SAVEDEFAULT=false</pre>  
<pre>
 
ls (hd0,1)/
 
</pre>
 
Will list the files on /dev/sda1. If this partition contains /boot, the output will show the full name of vmlinuz and initramfs.
 
  
5. Armed with the location and full name of vmlinuz and initramfs you can now boot your system.
+
{{admon/note|Note|If GRUB_SAVEDEFAULT is set to true, then, when an entry is selected, save it as a new default entry for use by future runs of GRUB. So, maybe, you need be sure that GRUB_SAVEDEFAULT is not set to true. GRUB_SAVEDEFAULT is only useful if GRUB_DEFAULT is saved.}}
  
5a. Declare your root partition:
+
Apply the change to <code>grub.cfg</code> by running: <pre>grub2-mkconfig -o /boot/grub2/grub.cfg</pre>
<pre>
 
grub> set root=(hd0,3)
 
</pre>
 
  
5b. Declare the kernel you wish to use:
+
Now list all possible menu entries
<pre>
+
<pre>grep -P "submenu|^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2</pre>  
grub> linux (hd0,1)/vmlinuz-3.0.0-1.fc16.i686 root=/dev/sda3 rhgb quiet selinux=0
 
# NOTE : add other kernel args if you have need of them
 
# NOTE : change the numbers to match your system
 
</pre>
 
  
5c. Declare the initrd to use:
+
Now set the desired default menu entry <pre>grub2-set-default "<submenu title><menu entry title>"</pre>
<pre>
 
grub> initrd (hd0,1)/initramfs-3.0.0-1.fc16.i686.img
 
# NOTE : change the numbers to match your system
 
</pre>
 
  
5d. Instruct GRUB 2 to boot the chosen files:
+
Verify the default menu entry <pre>grub2-editenv list</pre>
<pre>
 
grub> boot
 
</pre>
 
  
6. After boot, open a terminal.
+
{{admon/note|Note|The above method fails to work on some F20 (and newer) systems due to a missing or improperly linked /boot/grub2/grubenv file. The /boot/grub2/grubenv is symbolic linked to /boot/efi/EFI/fedora/grubenv but /boot is not mounted at the time of booting. So grub2 does not have access to the environment variables. To fix this, change /boot/grub2/grubenv to point to ../efi/EFI/fedora/grubenv instead and your chosen default OS will boot without any problems.}}
  
7. Issue the grub2-mkconfig command to re-create the grub.cfg file grub2 needed to boot your system:
+
{{admon/note|Note|There are other, simpler, ways of setting the default entry, but they are prone to error if/when grub2-mkconfig is re-run. These include directly setting the default in /boot/grub2/grub.cfg or setting GRUB_DEFAULT to either a number or an entry title in /etc/default/grub. Neither of these methods is recommended.}}
<pre>
 
grub2-mkconfig -o /boot/grub2/grub.cfg
 
</pre>
 
  
8. Issue the grub2-install command to install grub2 to your hard drive and make use of your config:
+
If you understand the risks involved and still want to directly modify /boot/grub2/grub.cfg, here's how you can do it:
<pre>
 
grub2-install --boot-directory=/boot /dev/sda
 
# Note: your drive may have another device name. Check for it with mount command output.
 
</pre>
 
  
== Other GRUB 2 issues ==
+
Edit /boot/grub2/grub.cfg, and change the line
  
''' Absent Floppy Disk ''': It has been reported by some users that GRUB 2 may fail to install on  a partition's boot sector if the computer floppy controller is activated in BIOS without an actual floppy disk drive being present. A possible workaround is to run (post OS install) from rescue mode:
+
{{admon/caution|This is not the recommended method|This will not survive grub2-mkconfig. It might not even survive a kernel update.}}
  
 
<pre>
 
<pre>
grub2-install <target device> --no-floppy
+
set default="0"
 
</pre>
 
</pre>
  
= Further Reading =
+
to
 
+
<pre>
http://www.gnu.org/software/grub/manual/grub.html
+
set default="5"
 
+
</pre>
http://fedoraproject.org/wiki/Features/Grub2
 
  
http://fedoraproject.org/wiki/Anaconda/Features/Grub2Migration
+
* http://www.gnu.org/software/grub/manual/grub.html
 +
* [[Features/Grub2]]
 +
* [[Anaconda/Features/Grub2Migration]]

Latest revision as of 15:13, 6 May 2021

Introduction

GRUB 2 is the latest version of GNU GRUB, the GRand Unified Bootloader. A bootloader is the first software program that runs when a computer starts. It is responsible for loading and transferring control to the operating system kernel, (Linux, in the case of Fedora). The kernel, in turn, initializes the rest of the operating system.

GRUB 2 has replaced what was formerly known as GRUB (i.e. version 0.9x), which has, in turn, become GRUB Legacy.

Starting with Fedora 16, GRUB 2 is the default bootloader on x86 BIOS systems. For upgrades of BIOS systems the default is also to install GRUB 2, but you can opt to skip bootloader configuration entirely.

Adding and removing kernel command-line parameters using grubby

Grubby is a utility that updates the bootloader-specific configuration files. The utility is a recommended way for making routine changes to the kernel boot parameters and setting a default kernel.

Following are some of the selected illustrations of grubby usage:

  • To add one kernel parameter to a single boot entry:
# grubby --args=<NEW_PARAMETER> --update-kernel=/boot/vmlinuz-5.11.14-300.fc34.x86_64
  • To add multiple kernel paramters to a single boot entry:
# grubby --args="<NEW_PARAMETER1> <NEW_PARAMETER2 <NEW_PARAMETER_n>" --update-kernel=/boot/vmlinuz-5.11.14-300.fc34.x86_64
  • To add one kernel parameter to all currently existing and future boot entries:
# grubby --args=<NEW_PARAMETER> --update-kernel=ALL
  • To remove one kernel parameter from all currently existing and future boot entries:
# grubby --remove-args=<PARAMETER_TO_REMOVE> --update-kernel=ALL
  • To set the default kernel:
# grubby --set-default=/boot/vmlinuz-5.11.12-300.fc34.x86_64


Updating and repairing the GRUB 2 main configuration file

Discovering what firmware the system is running

The /boot/grub2/grub.cfg is the main GRUB 2 configuration file. It is a static file that you rarely modify. Except in cases of disk replacement or installation of another Linux distribution.

To discover what firmware your machine is using, run the following command:

  • On UEFI systems:
# ls -ld /sys/firmware/efi
  • On BIOS systems:
# ls -lrt /etc/grub2.cfg

A directory listing of either of these commands indicate that you are running the corresponding firmware.

The grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg command adds entries for other detected operating systems. That will be done based on the output of the os-prober tool.

Note.png
The above command for updating the GRUB 2 configuration file is only applicable for UEFI systems with Fedora 33 and earlier. Everybody else should use grub2-mkconfig -o /boot/grub2/grub.cfg.


Repairing GRUB 2

If your machine is not working because of the broken GRUB 2 bootloader, you can boot into the rescue mode to repair an already installed operating system.

For more details see Booting Your Computer in Rescue Mode.

After completing steps specified in the previous link, run the following command to mount the root partition:

# chroot /mnt/sysimage


Reinstalling GRUB 2

The GRUB 2 packages contain commands for installing a bootloader and for creating a bootloader configuration file.

The grub2-install command installs the GRUB 2 bootloader usually in the master boot record (MBR), in free and unpartioned space. The bootloader files are placed in the /boot/ directory. You can install the GRUB 2 bootloader with:

# grub2-install /dev/sda
Warning.png
Do not use the grub2-install command on UEFI systems. On those systems, bootloaders are in the shim and grub-efi packages. By reinstalling those packages, the bootloaders are reinstalled to their proper location in /boot/efi/ (the EFI system partition).

To reinstall the GRUB 2 bootloader:

  • On UEFI systems run:
# dnf reinstall shim-* grub-efi-*
  • On BIOS systems:
    • Find the device node the /boot/ directory is located on:
# mount | grep "/boot "
/dev/sda4 on /boot type ext4 (rw,relatime,seclabel)

The device node is /dev/sda4.

    • Reinstall the bootloader while specifying the device node without the number:
# grub2-install /dev/sda


Appendix

Enabling serial console in GRUB 2

To enable serial console for usage on virtual environments you need to run the following command:

# grubby --args="systemd.journald.forward_to_console=1 console=ttyS0,38400 console=tty1" --update-kernel=/boot/vmlinuz-5.11.16-300.fc34.x86_64
# grubby --set-default=/boot/vmlinuz-5.11.16-300.fc34.x86_64

The first command specifies the baud rate, console forwarding for systemd, what console to use (tty1) and on what kernel such changes should be applied. The second command ensures the specified kernel is going to be loaded by default on next reboot.

For instructions on how to enable serial consol in GRUB 2 for baremetal machines, see Using GRUB via a serial line


Note.png
Note


Setting default entry

Warning.png
Look to (default) kernel sysconfig options.
if file /etc/sysconfig/kernel have
UPDATEDEFAULT=yes
in every kernel update the grub entry is update to last entry, if you don't want that set:
UPDATEDEFAULT=no
(write "no" in lower case)
Warning.png
Some parts of this section is wrong or outdated for F17 and later releases.
Be also aware of [Bug 768106 - grubby does not support grub2 set default="${saved_entry}" and replaces with "0"]. version grubby-8.28-1.fc19 has fixed issues with "Default Menuentry" as noted in the linked bug-report

Due to grub2-mkconfig (and os-prober) we cannot predict the order of the entries in /boot/grub2/grub.cfg, so we set the default by name/title instead.

Open /etc/default/grub and ensure this line exists:

GRUB_DEFAULT=saved

and ensure this line not exists:

GRUB_SAVEDEFAULT=true

or ensure this line exists:

GRUB_SAVEDEFAULT=false
Note.png
Note
If GRUB_SAVEDEFAULT is set to true, then, when an entry is selected, save it as a new default entry for use by future runs of GRUB. So, maybe, you need be sure that GRUB_SAVEDEFAULT is not set to true. GRUB_SAVEDEFAULT is only useful if GRUB_DEFAULT is saved.

Apply the change to grub.cfg by running:

grub2-mkconfig -o /boot/grub2/grub.cfg

Now list all possible menu entries

grep -P "submenu|^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2

Now set the desired default menu entry

grub2-set-default "<submenu title><menu entry title>"

Verify the default menu entry

grub2-editenv list
Note.png
Note
The above method fails to work on some F20 (and newer) systems due to a missing or improperly linked /boot/grub2/grubenv file. The /boot/grub2/grubenv is symbolic linked to /boot/efi/EFI/fedora/grubenv but /boot is not mounted at the time of booting. So grub2 does not have access to the environment variables. To fix this, change /boot/grub2/grubenv to point to ../efi/EFI/fedora/grubenv instead and your chosen default OS will boot without any problems.
Note.png
Note
There are other, simpler, ways of setting the default entry, but they are prone to error if/when grub2-mkconfig is re-run. These include directly setting the default in /boot/grub2/grub.cfg or setting GRUB_DEFAULT to either a number or an entry title in /etc/default/grub. Neither of these methods is recommended.

If you understand the risks involved and still want to directly modify /boot/grub2/grub.cfg, here's how you can do it:

Edit /boot/grub2/grub.cfg, and change the line

Stop (medium size).png
This is not the recommended method
This will not survive grub2-mkconfig. It might not even survive a kernel update.
set default="0" 

to

set default="5"