2

I've just installed Ubuntu 18.04 LTS on a UEFI laptop also running Windows 10 and, no matter what I try, every time I boot I get the Grub bash-like command line (grub>). Running set shows me that, each time I boot, root and prefix are set to (hd0,gpt1) and (hd0,gpt1)/boot/grub when they should both be on (hd0,gpt8). So I set these to the correct values, run normal, and I get to the Grub bootloader from which I can boot to Ubuntu or Windows - all fine.

BUT having got to Ubuntu, update-grub and grub-install don't fix the problem permanently. The root and prefix revert to (hd0,gpt1) on the next reboot. I have so far tried:

  • update-grub, grub-install, initramfs -v -u
  • Running boot-repair (trying purge, nodmraid)
  • Running Super Grub2 Disk on USB (which finds the .cfg files)
  • Reinstalling Ubuntu (twice)
  • Running Super Grub2 Disk by copying the .EFI into the boot partition and updating the BCD store with bcdedit in PowerShell

Interestingly when I tried to run Super Grub2 Disk from the EFI it ran into the same problem and only showed the Grub2 bash command line. I'd just like to be able to get to the grub menu without having to go through setting the prefix and root each time. I'd be really grateful for any help because I've been bashing my head against the wall for about two days!

3 Answers 3

1

For those who have a similar problem, please specify which partition you are trying to boot. The set command outputs prefix and root based on the partition being loaded. In my case, it was (hd0,gpt1) when it was necessary to load (hd0,gpt3). So changing the boot priorities of the first partition to the third in my case solved the problem.

1
  • this was my issue, wrong booting priority set on bios. Thanks man!
    – Carlos
    Commented Sep 22, 2023 at 17:57
1

It's possible that it is being set from /boot/efi/EFI/ubuntu/grub.cfg

It's a text file which you can only access with root privilege. But once you are in it's just a matter of changing some text.

On my Kubuntu 22.04 system, the contents of that file look like:

search.fs_uuid 73ffa900-dca2-47f2-9e0b-f8c1942ef918 root hd0,gpt2
set prefix=($root)'/@/boot/grub' ## Change this line
configfile $prefix/grub.cfg

On some distros it may be hard coded in a binary file in a sub-directory of the EFI directory; which will be significantly harder to change.

0

I found a solution how we can change the prefix permanently. However, it has the following caveats: We have to change it directly inside the grubx64.efi binary file, and we have to keep the file the same length. And how much space you are given may depend on your distribution. Read along for further explanation.

Disclaimers

  • If you want to do this you will have to turn off secure boot as the hash will have changed and the binary will be rejected. Not sure if this can be fixed by installing your own hashes (search for MOK).
  • Always make backups of the files you are changing. If you do as I did, you will be able to reverse your changes with a bootable USB stick. If you do not have another system at hand, then please create a bootable USB now and test if you can boot into it.

First the Problem:

Basically, this whole issue is a limitation of Secure Boot. For Secure Boot to accept the grubx64.efi file, it must be signed by an accepted authority. Therefore, this grub executable is signed and prebaked. The current standard to set this prefix seems to be /EFI/$(lsb_release -i -s). This of course also means that - no matter which loader path (for example efibootmgr--loader \\EFI\\other\\SSHIMX64.efi) - you specify when creating a new boot option, it will have no effect on the prefix variable in the grub bootloader.

The solution:

Let's assume our distro is ubuntu and we would like to rename that to longubuntu for some reason, we can do so following these steps:

# List contents of EFI directory
find /boot/efi/EFI;
# Rename the directory
mv /boot/efi/EFI/ubuntu /boot/efi/EFI/longubuntu;
# List contents of EFI/longubuntu for easy access
find /boot/efi/EFI/longubuntu;
# Always make a copy of the original
cp /boot/efi/EFI/longubuntu/BOOTX64.CSV /boot/efi/EFI/longubuntu/BOOTX64.CSV.bak
# Inside the .csv-file change 'ubuntu' to 'longubuntu'
nano /boot/efi/EFI/longubuntu/BOOTX64.CSV

Before proceeding:

Check that there are enough nul-characters available in the binary file. It is important to keep the overall length the same. If you don't do that, it will throw an error - if this happens to you, then copy back the original grubx64.efi and try again. When inspecting my grubx64.efi-binary with VS Code I do have enough available to change the prefix to my hearts content:
Many nul-characters directly behind /EFI/ubuntu

Now to the fun and critical part:

As I already said, we need to make sure that we keep the binary file the same length. We can so by either padding the new prefix with nul-chars (\0) or by padding the text to replace with nul-chars.

So if you want to specify a longer prefix (as in my case), you can do so with:

# Parameter -pi.bak will create a backup for you
perl -pi.perlbak -e 's/EFI\/ubuntu\0\0\0\0/EFI\/longubuntu/g' /boot/efi/EFI/longubuntu/grubx64.efi

If you choose a shorter prefix (for example bent), then pad the new value with \0:

# Parameter -pi.bak will create a backup for you
perl -pi.perlbak -e 's/EFI\/ubuntu/EFI\/bent\0\0/g' /boot/efi/EFI/longubuntu/grubx64.efi

You can check the results of your operation with, which will print the line containing the prefix.

$~: grep -a 'EFI\/longubuntu' /boot/efi/EFI/longubuntu/grubx64.efi

Which prints:
Grep will print the new prefix.

Do not forget to add a new boot option!

I will use efibootmgr. With efibootmgr we can delete the old boot option and add a new one.

# Print current boot options
:~# efibootmgr -v
BootCurrent: 0000
Timeout: 1 seconds
BootOrder: 0000
Boot0000* ubuntu        HD(1,GPT,28bd5547-5802-4f9c-97da-22ddd968dea6,0x800,0x100000)/File(\EFI\UBUNTU\SHIMX64.EFI)
# Delete current
:~# efibootmgr -b 0 -B
# List disks
:~# lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
[...]
nvme0n1     259:0    0 238.5G  0 disk
├─nvme0n1p1 259:1    0   512M  0 part /boot/efi
└─nvme0n1p2 259:2    0   238G  0 part /
# Create new boot option
:~# efibootmgr --create --disk /dev/nvme0n1 --part 1 --label "Long Ubuntu Name" --loader \\EFI\\longubuntu\\shimx64.efi

Enjoy:

When you reboot now. You should still boot into your distribution as before.
If not, then boot into the USB stick and mount the EFI-partition. Then undo the changes or copy back the original file. On my device, I mount the EFI-partition like so:

:~# lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
[...]
nvme0n1     259:0    0 238.5G  0 disk
├─nvme0n1p1 259:1    0   512M  0 part /boot/efi
└─nvme0n1p2 259:2    0   238G  0 part /
:~# mkdir -p /media/efi; mount /dev/nvme0n1p1 /media/efi
2
  • 1
    Why all these bold characters? They make the post unreadable !
    – Toto
    Commented Nov 8, 2023 at 19:35
  • For me, it was easier to read because the bold text would highlight the most important bits. But if other people think otherwise I am not against it. I removed most bold characters now. Commented Nov 8, 2023 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .