Installing Grub completly into the ESP


linux grub

How to install all Grub files from /boot/grub into the EFI System Partition.

Backgroud

Installing Linux Mint 21.3 offers the option to install almost everything into LVM – including the root filesystem. Only the EFI System Partition (ESP) is an extra partition. That’s quite nice because I like the flexibility offered by LVM when I grow and shrink my filesystems, shoveling them between the fast NVME and the slower harddisk in the background and much more.

The installation works but there are three little annoying things:

It seems that all three things are linked to the same core problem – one of them by accident, the other two legitimately. The core problem is that Grub can only read LVM volumes but it cannot write to them. And it wants to write to /boot/grub/grubenv at least two things:

So the last two of the three points from above are directly related to that. But the first one, the “accident”?

Well, the templates generating grub.cfg contain this code in /etc/grub.d/00_header (lines 411 ff.):

if [ "$recordfail_broken" = 1 ]; then
  cat << EOF
if [ \$grub_platform = efi ]; then
  set timeout=${GRUB_RECORDFAIL_TIMEOUT:-30}
  if [ x\$feature_timeout_style = xy ] ; then
    set timeout_style=menu
  fi
fi
EOF
fi

$recordfail_broken is set to 1 if /boot/grub is on a readonly filesystem or in a logical volume as in this case. So the lines between cat << EOF and EOF are written into grub.cfg. At Grub runtime the check $grub_platform = efi is always true and hence timeout is set unconditionally to 30 seconds because GRUB_RECORDFAIL_TIMEOUT is not defined by default. That’s interesting because this assignment is executed even when the boot did not fail at all! Even better: This code is not in the official Grub sources, so it must have been added either by Debian, Ubuntu or Mint. And even more funny: The code just above that insertion seems to deal with failed boots and timeouts already. So it might be an outdated customization patch.

In summary: That bad handling of timeout is really an accident.

Solution

Googling the error message leads to a Ask Ubuntu question and a Launchpad bug. These threads they seem to focus on disabling writes in the LVM case and hence suppressing the error.

A better solution to the core problem: Move the complete directory /boot/grub to a filesystem which Grub can write to. Since this directory is about 6 MB and does not change often I decided to move it into the ESP where other parts of Grub are already installed.

Just moving and symlinking the directory will not work – Grub will not find the main config file /boot/grub/grub.cfg any more and hence booting will fail. Welcome to the Grub command line!

The right procedure ist this (as root, of course):

cd /boot

# make backups
mv grub grub-bak
mv efi/EFI/ubuntu efi/EFI/ubuntu-bak

# prepare location
mkdir -p efi/EFI/ubuntu/grub
ln -s efi/EFI/ubuntu/grub

# install grub & regenerate config
grub-install
update-grub

Some notes about the documentation:

Considerations

Some notes about what to move:

Some notes about where to move:

Summary

Much digging, many thoughs but at least a nice solution.