How to reinstall GRUB2 EFI?

  • Boot your computer with a live-USB/CD in UEFI mode. I had two boot options <flash_drive> and UEFI: <flash_drive>, the second is needed to expose the efi variables in /sys/firmware/efi/ so that efibootmgr don't fail later on. Booting with the first option gives me the following error:

    Fatal: Couldn't open either sysfs or procfs directories for accessing EFI variables.
    Try 'modprobe efivars' as root.
    

    modprobe efivars did'nt work for me.

  • chroot into the broken system (similar to the ubuntu grub2 help but with efi specificities):

    sudo mount /dev/sda2 /mnt #sda2 is the root partition
    sudo mount /dev/sda1 /mnt/boot/efi #sda1 is the efi partition
    for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt$i; done
    sudo cp /etc/resolv.conf /mnt/etc/ #makes the network available after chrooting
    modprobe efivars # make sure this is loaded
    sudo chroot /mnt
    
  • Depending on your linux distribution, you now do different things.

    • For Ubuntu/Debian:

      apt-get install --reinstall grub-efi-amd64
      

      or alternatively:

      apt-get install --reinstall grub-efi
      update-grub
      

      should the above give you a grub, but not a bootable one

    • For Fedora (up to 16, may work for others):

      yum reinstall grub-efi
      

      In the following command, you have to replace sdX with the device which has the EFI partition you want to boot from. In --part Y you have to replace the Y with the number of the EFI partition (as in /dev/sdXY).

      efibootmgr -c --disk /dev/sdX --part Y
      efibootmgr -v # verify a new record called Linux is there
      
  • Now type Ctrl+D to exit chroot, unmount everything and reboot:

    for i in /sys /proc /dev/pts /dev; do sudo umount /mnt$i; done
    sudo umount /mnt/boot/efi #please do this. Corrupted efi partitions are not nice
    sudo umount /mnt
    sudo reboot
    

You may need to adapt this to your needs (different partition table, separate /boot partition, etc.) and it may not be the only option but this worked just fine for me.

A suitable live-system for fixing things is grml. There is also an extensive guide on how to setup a bootable USB device, of which the Mac section is the most useful actually (just create a FAT32 partition, copy the files, reboot, done).


As a potential simplification of the first method, it is possible to directly boot into the system on the hard disk, only using grub of the live CD. Tested on xubuntu 13.10 with the xubuntu 13.10 live CD.

Make sure that Secure Boot is disabled in your BIOS. Insert the live CD and boot it via UEFI. The CD's GRUB menu will show. the Press "c" to get to the command line.

configfile (hd0,gpt1)/EFI/ubuntu/grub.cfg

Adapt the grub command above if you have a different EFI system partition.

After your system has booted from the hard disk, it should be enough to re-install grub on the EFI system partition and to register it with the firmware via grub-install.

sudo grub-install

As with Maxine, I found my UEFI settings in BIOS to get damaged and my machine wouldn't boot.

In my case, it's a Lenovo ThinkServer RD430 with Linux Mint Debian and it seemed anything I'd do about update-grub or changing any hard drives in the server would cause it to not boot. OS in my case is linuxmint-201403-mate-dvd-64bit installed via USB. (see below for a complete description of the events that would cause UEFI to not work)

Going through exactly the same steps on a ThinkServer TS140 did not result in UEFI losing its mind even once. Looked at RD430 driver page and my bios is two versions old. I have never had to update bios on a motherboard before, so I'm not one to automatically update when there are new versions available. After updating the bios, Maxine's answer above worked, only with a twist...

# efibootmgr -c --disk /dev/sdX --part Y
# efibootmgr -v
BootCurrent: 0004
Timeout: 1 seconds
BootOrder: 0002,0000,0003,0001,0004
Boot0000* linuxmint HD(1,800,1f4000,829f6cc9-5b17-479c-b3ea-61e43faecbf7)File(\EFI\linuxmint\grubx64.efi)
Boot0001* LMDE Linux Mint Debian    HD(1,800,15d505800,934c598c-fe3c-fd43-84a1-fa38e4f72552)File(\EFI\linuxmint\grubx64.efi)
Boot0002* Linux HD(1,800,1f4000,829f6cc9-5b17-479c-b3ea-61e43faecbf7)File(\elilo.efi)
Boot0003* UEFI: Built-in EFI Shell  Vendor(5023b95c-db26-429b-a648-bd47664c8012,)AMBO
Boot0004* UEFI: VerbatimSTORE N GO 1.00 ACPI(a0341d0,0)PCI(1a,0)USB(1,0)USB(4,0)HD(1,80,1d70780,00000000)AMBO
mint / # 

The efibootmgr -c command added two entries 0000 and 0002!
The Boot0002* Linux HD entry first in boot order is not correct.
The 0000 entry is correct.

To test this, I tried booting without any interruption, which is the 0002 entry. As expected, it didn't work. So I restarted the server, hit F12, and chose linuxmint. As hoped for, it did boot to my LMDE installation.

The way to remove unwanted entries via efibootmgr is:

# efibootmgr -b 2 -B

I used this command to remove entries 0001 and 0002. Option 0001 was from the last of my many attempts to recover the OS.


UEFI notes

If you're reading this and as frustrated with UEFI as I am/was, here are some notes and resources:
» Booting to UEFI Shell is akin to using a DOS shell.
» Intel made a PDF reference manual for efi shell commands.
» Lenovo's UEFI_on_TS430 document is the only resource I have seen explaining usage of efi shell.
» Another uefi shell reference from nPartition Administrator's Guide.
» You can try booting to a partition from the efi shell by navigating to the loader and executing it.
» UEFI wants the disk to have a GPT partition table, not msdos part table.
» UEFI wants the first partition on your disk to be formatted fat32 or vfat.
» For a "generic" boot there must be a /EFI/boot directory at the root with bootx64.efi in it.
» Some people copy their grubx64.efi from where it was installed to /EFI/boot/bootx64.efi and this cheat worked for them.
» Anytime you make grub changes, use efibootmgr -v before and after to ensure your reboot is ok.


My RD430 experience

I have resinstalled the OS 10+ times in the past week trying to sort this out and set up the server. My configuration is a SSD on this RAID controller in the PCIe 2.0 slot with LMDE installed on it. AOC-S3008L-L8i RAID controller (reflashed to IT mode) in the 2nd PCIe 3.0 slot with 6x 3TB drives. RAM: 12GB ECC (3x 4GB).

Here are changes I would make that caused my system to not boot:
» Change S3008L-L8i pci slots (leaving the SSD+card alone).
» Disable the LSi software raid bios prompt for onboard controller.
» Add my old HighPoint RocketRaid card to an open PCIe slot.
» Make a change to /etc/default/grub and then run update-grub.
(maybe grub-install needs to be run as well?)