If I use GRUB2 on a GPT-enabled partition how does loader "know" where to find its configuration file and other 2nd stage's files?
Note: I found some mentions about a config file which is located in the same folder as GRUB's EFI loader and contains chained load of "primary" configuration file from the specified partition, but that definitely is not true - there is only one "something.efi" file.
There are actual several ways this can happen:
grub-mkimage
(called by grub-install
) execution time.The latter is probably the functionality you are really asking for - and it's a combination of the default config file name (grub.cfg
), the prefix (default /boot/grub
, but can be explicitly specified to grub-mkimage
) and the grub partition name for the partition where the prefix is located.
If I run strings /boot/efi/EFI/debian/grubx64.efi | tail -1
on my current workstation, it prints out the stored value: (,gpt2)/boot/grub
, telling grubx64.efi
to look for its configuration file in /boot/grub on GPT partition 2. The bit before the comma (the GRUB disk device name) gets filled in at runtime based on which disk the grubx64.efi
image itself was loaded from.
Dynamically loaded modules will also be searched for under this location, but in an architecture/platform-specific directory - in this case /boot/grub/x86_64-efi
.
for EFI image, I found that grub-install
or grub-mkimage
will always embed an early config
into the result EFI binary, regardless of whether or not you have specified the --config FILE
option. If you do not specify the --config FILE
option, it will try to embed /boot/grub/x86-64_efi/load.cfg
,
This early config file looks like this:
search.fs_uuid 8ef704aa-041d-443c-8ce6-71ac7e7f30da root hd0,gpt1
set prefix=($root)'/boot/grub'
configfile $prefix/grub.cfg # this line seems can be omitted, because it seems to be the default next action
uuid
means uuid of file system, not of partition, you can use blkid to list it.hd0,gpt1
is just a hint.set root=hd0,gpt1
This default behavior of auto embedding is different as in BIOS mode, the latter by default only embed a prefix string like (,gpt3)/boot
without bothering search.uuid.
I also found that Ubuntu bionic EFI image embedded a early config like this https://source.puri.sm/pureos/core/grub2/blob/master/debian/build-efi-images#L64
if [ -z "\$prefix" -o ! -e "\$prefix" ]; then
if ! search --file --set=root /.disk/info; then
search --file --set=root /.disk/mini-info
fi
set prefix=(\$root)/boot/grub
fi
if [ -e \$prefix/$platform/grub.cfg ]; then
source \$prefix/$platform/grub.cfg
elif [ -e \$prefix/grub.cfg ]; then
source \$prefix/grub.cfg
else
source \$cmdpath/grub.cfg
fi
The cmdpath
is the DIR of efi binary, so it will fallback to the grub.cfg in the same dir of the efi binary, as you found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With