Recreate EFI partition with GRUB


3 - 4 minutes to read, 779 words
Categories: backup debian linux
Keywords: GRUB UEFI backup chroot data recovery debian linux

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

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

The system won’t be able to boot again.

But even if the system is not able to boot anymore, it is possible to access all other files of the operating system and user.

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

Even better, it is possible to recreate the partition, and avoiding to to reinstall everything.

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, in order 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

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 it is generally 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;
 chroot /tmp/hdd;
  grub-install --efi-directory=/boot/efi --recheck;
  update-grub;
 exit;
 umount /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, we just need to execute following commands as administrator

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 might be that the other systems are missing from grub.

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.

Do you want to share your opinion? Or is there an error, some parts that are not clear enough?

You can contact me here.