GRUB 2

From FedoraProject

(Difference between revisions)
Jump to: navigation, search
(Enable Serial Console in Grub)
m (Further Reading: use wikilinks)
 
(14 intermediate revisions by 9 users not shown)
Line 10: Line 10:
 
= Tasks / Common issues =
 
= Tasks / Common issues =
  
== Updating GRUB 2 configuration ==
+
== Updating GRUB 2 configuration on BIOS systems ==
  
 
The grub2 packages contain commands for installing a bootloader and for creating a bootloader configuration file.
 
The grub2 packages contain commands for installing a bootloader and for creating a bootloader configuration file.
Line 26: Line 26:
 
The Fedora installer, anaconda, will run these grub2 commands and there is usually no reason to run them manually.
 
The Fedora installer, anaconda, will run these grub2 commands and there is usually no reason to run them manually.
  
It is generally safe to directly edit /boot/grub2/grub.cfg in Fedora. Grubby in Fedora patches the configuration when a kernel update is performed and will try to not make any other changes than what is necessary. (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.) Manual changes might however be overwritten with grub2-mkconfig next time the system is upgraded with anaconda. Some customizations can be placed in /etc/grubd/40_custom or /boot/grub2/custom.cfg and will survive running grub2-mkconfig.
+
It is generally safe to directly edit /boot/grub2/grub.cfg in Fedora. Grubby in Fedora patches the configuration when a kernel update is performed and will try to not make any other changes than what is necessary. (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.) Manual changes might however be overwritten with grub2-mkconfig next time the system is upgraded with anaconda. Some customizations can be placed in /etc/grub.d/40_custom or /boot/grub2/custom.cfg and will survive running grub2-mkconfig.
 +
 
 +
== Updating GRUB 2 configuration on UEFI systems ==
 +
 
 +
To install or fix GRUB 2 on a UEFI system on Fedora 18 or newer, you need to do four things:
 +
 
 +
=== Create an ESP ===
 +
 
 +
UEFI firmware, in general, likes to boot from an EFI System Partition on a disk with a GPT label.  In <code>gdisk</code>, it looks something like this:
 +
 
 +
<pre>
 +
Number  Start (sector)    End (sector)  Size      Code  Name
 +
  1            2048          264191  128.0 MiB  EF00  EFI System
 +
</pre>
 +
 
 +
That partition should be formatted as FAT.  If in doubt, FAT32 is a good dialect of FAT to choose.
 +
 
 +
Fedora expects this partition to be mounted at <code>/boot/efi</code>.
 +
 
 +
=== Install the bootloader files ===
 +
 
 +
If you don't already have the relevant packages installed, do:
 +
 
 +
<pre>yum install grub2-efi shim</pre>
 +
 
 +
If you do, then try:
 +
 
 +
<pre>yum reinstall grub2-efi shim</pre>
 +
 
 +
instead.
 +
 
 +
Make sure that /boot/efi is mounted when you do this.
 +
 
 +
This installs the signed shim and the GRUB 2 binary.
 +
 
 +
=== Create a GRUB 2 configuration ===
 +
 
 +
GRUB 2 looks for its configuration in <code>/boot/efi/EFI/fedora/grub.cfg</code>.  For newly installed kernels to work, <code>grubby</code> expects <code>/etc/grub2-efi.cfg</code> to be a symlink to the real grub.cfg (i.e. <code>/boot/efi/EFI/fedora/grub.cfg</code>).
 +
 
 +
If you already have a grub 2 EFI config file, you should be okay.  If not, grub2-mkconfig can help, but your mileage may vary.
 +
 
 +
=== Create a boot menu entry ===
 +
 
 +
TL;DR: This should happen automatically.  If it doesn't, read on.
 +
 
 +
When you power on your system, your firmware will look for EFI variables that tell it how to boot.  If you're already booted in EFI mode and EFI runtime services are working correctly, you can configure your boot menu with <code>efibootmgr</code>.  If not, you'll have to bootstrap the process.
 +
 
 +
Fortunately, <code>shim</code> can help you bootstrap.  The EFI program <code>/boot/efi/EFI/BOOT/fallback.efi</code> will look for files called <code>BOOT.CSV</code> in your ESP and will add boot entries corresponding to them, '''if such entries do not already appear to exist'''.  <code>shim</code> provides a <code>BOOT.CSV</code> file that will add an entry for <code>grub2-efi</code> for you.  So just using the EFI Shell to invoke <code>fallback.efi</code> should do the trick.  You can do this with commands like:
 +
 
 +
<pre>
 +
> fs0:
 +
> cd EFI\BOOT
 +
> fallback.efi
 +
</pre>
 +
 
 +
If you have no boot entries at all, then just booting off your disk in UEFI mode should automatically invoke <code>/boot/efi/EFI/BOOT/BOOTX64.EFI</code>, which will, in turn, invoke <code>fallback.efi</code>.
 +
 
 +
If you already have incorrect boot entries, you'll either need to delete them or to modify <code>BOOT.CSV</code> to create new entries with different names.
  
 
== Adding Other operating systems to the GRUB 2 menu ==
 
== Adding Other operating systems to the GRUB 2 menu ==
Line 32: Line 89:
 
grub2-mkconfig will add entries for other operating systems it can find. That will be done based on the output of the os-prober tool.
 
grub2-mkconfig will add entries for other operating systems it can find. That will be done based on the output of the os-prober tool.
  
That might however not work so well, especially not for booting other Linux operating systems. See http://www.gnu.org/software/grub/manual/grub.html#Multi_002dboot-manual-config .
+
That might however not work so well, especially not for booting other Linux operating systems, and especially not on UEFI systems. See http://www.gnu.org/software/grub/manual/grub.html#Multi_002dboot-manual-config .
  
 
== Setting default entry ==
 
== Setting default entry ==
  
{{admon/warning|Some parts of this section is wrong or outdated for f17. | 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"]]. | }}
+
{{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|}}
  
 
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.  
 
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.  
Line 47: Line 104:
  
 
Now list all possible menu entries
 
Now list all possible menu entries
<pre>grep ^menuentry /boot/grub2/grub.cfg | cut -d "'" -f2</pre>  
+
<pre>grep "submenu\|^\smenuentry" /boot/grub2/grub.cfg | cut -d "'" -f2</pre>  
  
Now set the desired default menu entry <pre>grub2-set-default <menu entry title></pre>
+
Now set the desired default menu entry <pre>grub2-set-default "<submenu title>><menu entry title>"</pre>
  
 
Verify the default menu entry <pre>grub2-editenv list</pre>
 
Verify the default menu entry <pre>grub2-editenv list</pre>
Line 142: Line 199:
 
== Setting a password for interactive edit mode ==
 
== Setting a password for interactive edit mode ==
  
If you would like to password protect interactive edit mode but allow password-less booting here is a way to do it (borrowed from the method provided by the anaconda installer):
+
If you wish to password-protect GRUB2's interactive edit mode '''but''' you do not want to require users to enter a password to do a plain, simple, ordinary boot, follow the instructions at [https://help.ubuntu.com/community/Grub2/Passwords Ubuntu Help: GRUB2 Passwords].
  
Create and edit /etc/grub.d/01_users and place the following inside it:
+
Starting from Fedora 17, the <code>--md5pass</code> kickstart option "breaks" GRUB2 password protection, rendering your system unbootable. (i.e., it will require a password to boot, and the password is NULL and thus you will never succeed in booting)
<pre>
+
set superusers="root"
+
password_pbkdf2 root GRUBPASSWORDHASH
+
export superusers
+
</pre>
+
  
Please note the existance of the grub2-mkpasswd-pbkdf2 command that is part of the grub2-tools package.  You run grub2-mkpasswd-pbkdf2 from the command line and it asks for a password twice and then outputs a password hash.  You take that output and substitute it for the word GRUBPASSWORDHASH in the above sample /etc/grub.d/01_users file.
+
A few bugs have already been filed:
  
"superusers" is magic, and from that point on everything should be password protected except booting the default entries.
+
* https://bugzilla.redhat.com/show_bug.cgi?id=840204
 +
* https://bugzilla.redhat.com/show_bug.cgi?id=840160
 +
* https://bugzilla.redhat.com/show_bug.cgi?id=882721
  
 
== Using old graphics modes in bootloader ==
 
== Using old graphics modes in bootloader ==
Line 164: Line 218:
 
</code>
 
</code>
  
The default in Fedora is gfxterm and to get the legacy graphics modes you need to set GRUB_TERMINAL to right variable from the description above in /etc/defaults/grub
+
The default in Fedora is gfxterm and to get the legacy graphics modes you need to set GRUB_TERMINAL to right variable from the description above in /etc/default/grub
  
 
== Enable Serial Console in Grub ==
 
== Enable Serial Console in Grub ==
Line 186: Line 240:
 
= Further Reading =
 
= Further Reading =
  
http://www.gnu.org/software/grub/manual/grub.html
+
* http://www.gnu.org/software/grub/manual/grub.html
 
+
* [[Features/Grub2]]
http://fedoraproject.org/wiki/Features/Grub2
+
* [[Anaconda/Features/Grub2Migration]]
 
+
http://fedoraproject.org/wiki/Anaconda/Features/Grub2Migration
+

Latest revision as of 06:45, 9 July 2014

Contents

[edit] 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.

[edit] Tasks / Common issues

[edit] Updating GRUB 2 configuration on BIOS systems

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

grub2-install will install the bootloader - usually in the MBR, in free unpartioned space, and as files in /boot. The bootloader is installed with something like:

grub2-install /dev/sda

grub2-mkconfig will create a new configuration based on the currently running system, what is found in /boot, what is set in /etc/default/grub, and the customizable scripts in /etc/grub.d/ . A new configuration file is created with:

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

The configuration format has evolved over time, and a new configuration file might be slightly incompatible with the old bootloader. It is thus often/always a good idea to run grub2-install before grub2-mkconfig for some reason is run.

The Fedora installer, anaconda, will run these grub2 commands and there is usually no reason to run them manually.

It is generally safe to directly edit /boot/grub2/grub.cfg in Fedora. Grubby in Fedora patches the configuration when a kernel update is performed and will try to not make any other changes than what is necessary. (Other distributions, in particular Debian and Debian-derived distributions provide a software patch that adds an update-grub command which is neither included nor needed in Fedora.) Manual changes might however be overwritten with grub2-mkconfig next time the system is upgraded with anaconda. Some customizations can be placed in /etc/grub.d/40_custom or /boot/grub2/custom.cfg and will survive running grub2-mkconfig.

[edit] Updating GRUB 2 configuration on UEFI systems

To install or fix GRUB 2 on a UEFI system on Fedora 18 or newer, you need to do four things:

[edit] Create an ESP

UEFI firmware, in general, likes to boot from an EFI System Partition on a disk with a GPT label. In gdisk, it looks something like this:

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          264191   128.0 MiB   EF00  EFI System

That partition should be formatted as FAT. If in doubt, FAT32 is a good dialect of FAT to choose.

Fedora expects this partition to be mounted at /boot/efi.

[edit] Install the bootloader files

If you don't already have the relevant packages installed, do:

yum install grub2-efi shim

If you do, then try:

yum reinstall grub2-efi shim

instead.

Make sure that /boot/efi is mounted when you do this.

This installs the signed shim and the GRUB 2 binary.

[edit] Create a GRUB 2 configuration

GRUB 2 looks for its configuration in /boot/efi/EFI/fedora/grub.cfg. For newly installed kernels to work, grubby expects /etc/grub2-efi.cfg to be a symlink to the real grub.cfg (i.e. /boot/efi/EFI/fedora/grub.cfg).

If you already have a grub 2 EFI config file, you should be okay. If not, grub2-mkconfig can help, but your mileage may vary.

[edit] Create a boot menu entry

TL;DR: This should happen automatically. If it doesn't, read on.

When you power on your system, your firmware will look for EFI variables that tell it how to boot. If you're already booted in EFI mode and EFI runtime services are working correctly, you can configure your boot menu with efibootmgr. If not, you'll have to bootstrap the process.

Fortunately, shim can help you bootstrap. The EFI program /boot/efi/EFI/BOOT/fallback.efi will look for files called BOOT.CSV in your ESP and will add boot entries corresponding to them, if such entries do not already appear to exist. shim provides a BOOT.CSV file that will add an entry for grub2-efi for you. So just using the EFI Shell to invoke fallback.efi should do the trick. You can do this with commands like:

> fs0:
> cd EFI\BOOT
> fallback.efi

If you have no boot entries at all, then just booting off your disk in UEFI mode should automatically invoke /boot/efi/EFI/BOOT/BOOTX64.EFI, which will, in turn, invoke fallback.efi.

If you already have incorrect boot entries, you'll either need to delete them or to modify BOOT.CSV to create new entries with different names.

[edit] Adding Other operating systems to the GRUB 2 menu

grub2-mkconfig will add entries for other operating systems it can find. That will be done based on the output of the os-prober tool.

That might however not work so well, especially not for booting other Linux operating systems, and especially not on UEFI systems. See http://www.gnu.org/software/grub/manual/grub.html#Multi_002dboot-manual-config .

[edit] Setting default entry

Warning (medium size).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
Apply the change to grub.cfg by running:
grub2-mkconfig -o /boot/grub2/grub.cfg

Now list all possible menu entries

grep "submenu\|^\smenuentry" /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
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"

[edit] Encountering the dreaded GRUB 2 boot prompt

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:

grub2> ls

2. The output for a dos partition table /dev/sda with three partitons will look something like this:

(hd0) (hd0,msdos3) (hd0,msdos2) (hd0,msdos1)

3. While the output for a gpt partition table /dev/sda with four partitions will look something like this:

(hd0) (hd0,gpt4) (hd0,gpt3)  (hd0,gpt2) (hd0,gpt1)

4. With this information you can now probe each partition of the drive and locate your vmlinuz and initramfs files:

ls (hd0,1)/ 

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.

5a. Declare your root partition:

grub> set root=(hd0,3)

5b. Declare the kernel you wish to use:

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

5c. Declare the initrd to use:

  
grub> initrd (hd0,1)/initramfs-3.0.0-1.fc16.i686.img
# NOTE : change the numbers to match your system

5d. Instruct GRUB 2 to boot the chosen files:

grub> boot

6. After boot, open a terminal.

7. Issue the grub2-mkconfig command to re-create the grub.cfg file grub2 needed to boot your system:

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

8. Issue the grub2-install command to install grub2 to your hard drive and make use of your config:

grub2-install --boot-directory=/boot /dev/sda
# Note: your drive may have another device name. Check for it with mount command output.

[edit] Other GRUB 2 issues

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:

grub2-install <target device> --no-floppy

[edit] Setting a password for interactive edit mode

If you wish to password-protect GRUB2's interactive edit mode but you do not want to require users to enter a password to do a plain, simple, ordinary boot, follow the instructions at Ubuntu Help: GRUB2 Passwords.

Starting from Fedora 17, the --md5pass kickstart option "breaks" GRUB2 password protection, rendering your system unbootable. (i.e., it will require a password to boot, and the password is NULL and thus you will never succeed in booting)

A few bugs have already been filed:

[edit] Using old graphics modes in bootloader

Terminal device is chosen with GRUB_TERMINAL; additional quote from http://www.gnu.org/software/grub/manual/grub.html#Simple-configuration

Valid terminal output names depend on the platform, but may include ‘console’ (PC BIOS and EFI consoles), ‘serial’ (serial terminal), ‘gfxterm’ (graphics-mode output), ‘ofconsole’ (Open Firmware console), or ‘vga_text’ (VGA text output, mainly useful with Coreboot).

The default is to use the platform's native terminal output.

The default in Fedora is gfxterm and to get the legacy graphics modes you need to set GRUB_TERMINAL to right variable from the description above in /etc/default/grub

[edit] Enable Serial Console in Grub

To enable Serial console in grub add the following entry's to /etc/default/grub

( Adjust baudrate/parity/bits/flow control to fit your environment and cables)

GRUB_CMDLINE_LINUX='console=tty0 console=ttyS0,115200n8'
GRUB_TERMINAL=serial
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"

And re-generate grub

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

[edit] Further Reading