13

There are bazillions of grub2/EFI threads on the net, but I found it difficult to find out how grub2 finds its configuration file in EFI boot. We have been told that in grub2 the configuration file is /boot/grub/grub.cfg. But when there are boot problems grub2 might end up in a command prompt (aka emergency shell) without any error message. So before being able to solve any problems it would be nice to understand how grub2 exactly tries to locate its configuration file.

2 Answers 2

10

The first thing to understand that there is no fixed way.  grub2 can be configured differently during installation, and the name grub.cfg is not hard-coded.  The second learning is that there is no single configuration file, e.g., my Ubuntu system at the time of writing uses two different files in sequence, both actually named grub.cfg.

By default, grub2 does two essential things when starting:

  1. set the prefix variable to a value built-in during grub2 installation
  2. continue with configuration file $prefix/grub.cfg if it exists

(For more details, refer to the GNU GRUB Manual.)

The "if it exists" condition is the nasty one: If the file does not exist (e.g., because $prefix is not pointing to the right place), there is no error message what it tried to do, and you just end up in the command prompt.

If you end up in the emergency shell, the first things to check are the value of the prefix variable (using set command) and the contents of that directory (using ls and cat commands).

(The default behaviour could be overwritten by a built-in configuration file, but I don't think I have seen that used in practice.)

Ubuntu sets the prefix to the same place where the grubx64.efi and the shimx64.efi used for secure boot are located. So the first configuration file grub.cfg is loaded from the same directory where these EFI binaries reside. From Linux perspective, the path is /boot/efi/EFI/ubuntu and from grub2 perspective (hd0,gpt1)/efi/ubuntu (the drive and partition numbers might vary depending on where your ESP, i.e., EFI system partition, is located.  Filenames in grub2 seem to be case-insensitive, so the EFI shown by Linux is shown as efi by grub2).

The first grub.cfg file contains only 3 statements:

  1. locate the partition containing the root file system by UUID (in systems using a separate boot file system locate the boot file system instead)
  2. set prefix to a new value of $root/boot/grub (using the $root value determined in the previous step, for systems with a separate boot partition this should be $root/grub)
  3. execute the configfile from location $prefix/grub.cfg

The latter is the Linux location /boot/grub/grub.cfg, which we are usually told.

2
  • 1
    its pretty good information, but why do you put up w/ all that jazz? why not just uninstall it? Commented Dec 29, 2015 at 19:06
  • @mikeserv Because then, depending on the chosen replacement, you'll either have to learn a new way to specify kernel boot parameters at boot time, or altogether lose the ability to do that without rebuilding your boot configuration. That capability has helped a lot of people, so giving up that capability or making major changes to it for no immediate tangible reward is going to feel like a downgrade. Commented Apr 20 at 21:08
0

From my (ongoing) hassles with this issue here is my take on it: shimx64 (which is just a small signed version of grub) looks in a directory called 'ubuntu' for a 'mini' grub.cfg. This mini-grub searches for a partition that matches the install partition and then finds the 'real' grub.cfg as /boot/grub/grub.cfg From then on it is a normal grub bootup sequence. This skips over two relevant facts:

  1. Shim cannot be changed or patched by Ubuntu respins, or derivatives such as Mint because then its signature would no longer be valid and secure boot would fail.
  2. I have never seen it documented how shim knows where to find the 'ubuntu' directory where the mini-grub file is located. It is stated that it looks in the directory where the shim binary is run from, but I have never seen it documented how it knows that. Using efibootmgr I can create entries in the UEFI that will load shim from wherever I want on the ESP. But I don't think I can put the mini-grub file wherever I want. Would be nice to know that because it would help in MultiBoot cases.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.