Recreate EFI partition with GRUB

Since the introduction of UEFI (the successor of the BIOS), when installing an operating system, a "hidden" partition is created and used for booting the operating system.

What happens if this partition (and only this partition!) gets corrupted or is deleted "by accident"?

You won’t be able to boot your Linux system again.

Even if the system is not bootable, it is possible to access all the files of the operating system and user.

Thus it is possible to make a backup of the data before reinstalling the operating system.

An even better solution is to recreate the UEFI partition and avoid reinstalling anything.

Boot with a live CD

As the system is not bootable, and assuming that taking the internal drive out and connecting it to another PC is not an option, one needs to boot a GNU/Linux distribution from a DVD or USB stick. If possible, the system on the USB stick should match the one installed on the machine, to avoid incompatibilities.

In case the EFI partition has been deleted and the PC is still running, it is possible to restore the partition without rebooting from a removable media.

Locate the (to be) EFI partition

First, it’s essential to locate the EFI partition, or what is going to be the EFI partition.

EFI partitions are relatively small (I’ve seen most sizes between 100MB and 500MB) and formatted in FAT32 with the boot flag.

With the program parted it is possible to see if such partition is available with parted --list

Model: ATA Samsung SSD 860 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number  Start   End    Size    File system     Name  Flags
 1      2097kB  317MB  315MB   fat32                 boot, esp
 2      317MB   463GB  463GB   ext4
 3      463GB   500GB  36.8GB  linux-swap(v1)        swap

On a system where the partition is missing, the first partition would not be there, but it is possible to query for free space with parted /dev/sda print free:

Model: ATA Samsung SSD 860 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number  Start   End    Size    File system     Name  Flags
        2097kB  317MB  315MB   Free Space
 2      317MB   463GB  463GB   ext4
 3      463GB   500GB  36.8GB  linux-swap(v1)        swap

It is also possible to use a graphical program, like GParted or KDE Partition Manager, depending on what is available, for searching for the EFI partition or where it should be located.

If missing, recreate the EFI partition

Create a FAT32 partition with your favorite tool (GParted and KDE Partition Manager included).

It is also possible to create a partition from the command line with parted, even if I find if less practical.

Install grub

Once we have a valid partition for EFI, we can install grub on it, and add an entry that will permit to boot to the installed operating system. As far as I know, there are no graphical tools for this task, so one has to resort to the command line.

In case GRUB is being restored from a live CD or USB stick, we need to mount some directories and use chroot:

sudo su;
 mkdir /tmp/hdd; # or another unused directory
 mount /dev/sdX2 /tmp/hdd; # /dev/sdX2 is the the partition where the operating system is located
 mount /dev/sdX1 /tmp/hdd/boot/efi; # /dev/sdX1 is the efi partition
 mount --bind /dev /tmp/hdd/dev;
 mount --type proc proc /tmp/hdd/proc;
 mount --type sysfs sys /tmp/hdd/sys;
 mount --type devpts pts /tmp/hdd/dev/pts;
 mount --bind /sys/firmware/efi/efivars /tmp/hdd/sys/firmware/efi/efivars;
 chroot /tmp/hdd;
  grub-install --efi-directory=/boot/efi --recheck;
  update-grub;
 exit;
 umount /tmp/hdd/sys/firmware/efi/efivars /tmp/hdd/dev/pts /tmp/hdd/sys /tmp/hdd/proc /tmp/hdd/dev /tmp/hdd/boot/efi /tmp/hdd

If GRUB is being restored from the system installed on the PC, it is sufficient to execute the following commands as root:

mount /dev/sdX1 /boot/efi; # /dev/sdX1 is the efi partition
grub-install --efi-directory=/boot/efi --recheck;
update-grub

If all commands are executed successfully, it should be possible to reboot without issues. During the execution of update-grub, the program should list the operating systems it found. In the case of a machine with multiple operating systems, if GRUB has been restored from a live CD/USB stick, it can happen that not all systems have been recognized.

This can be fixed by running update-grub as administrator after rebooting the computer.

Note 📝
update-grub might not be available on all GNU/Linux systems. For those systems where the command is not available, it should be equivalent to grub-mkconfig -o /boot/grub/grub.cfg.

If you have questions, comments, or found typos, the notes are not clear, or there are some errors; then just contact me.