47

I am running Ubuntu 20.04, I accidentally deleted the EFI partition (Damn GParted!) The system is still running. Can I create a new partition in the now UNUSED area, set the EFI flag and boot flag, and then just run update-grub? Will update-grub repopulate the EFI partition? Can I DD the EFI partition from another Ubuntu install?

Quick replies, Greatly Appreciated!

Dennis D
  • 819
  • 5
    Maybe if you're lucky, you just need to reset the partition start/end and the flags. update-grub does not copy in new bootloaders, you need grub-install for that. Do you have a Windows dual boot? The EFI bootloaders are just files on a FAT filesystem, you may copy them in, but the .../EFI/ubuntu/grub.cfg three line stub has a UUID you need to edit to be your root UUID. – ubfan1 Feb 24 '22 at 17:51
  • 1
    Thanks, but what files, copied from where. – Dennis D Feb 24 '22 at 18:42
  • 1
    Never mind, I copied files from another machine and then update the UUID is you indicated. Thanks. – Dennis D Feb 24 '22 at 19:18
  • 9
    If that worked for you, you may answer your own question, and accept it after a few days to mark it solved, helping others and gaining some reputation points for yourself. – ubfan1 Feb 24 '22 at 19:20
  • 1
    @ubfan1: Strictly-speaking, you only gain rep from answering your question if someone upvoted your answer - accepting your own answer doesn't give you the reputation boost that you accepting someone else's answer or someone else accepting your answer gives you. – Vikki Feb 27 '22 at 07:14
  • Don't reboot/poweroff your computer until you have fixed this... Won't boot AT ALL if you do. – Logan Mar 17 '22 at 16:30

4 Answers4

69

Thanks to ubfan1, I was able to repair and reboot. I followed these steps:

  1. Recreate a partition using GParted in the "unallocated" section created by deleting the original EFI partition.
  2. Select FAT32, manage flags and set the "boot" flag.
  3. Use blkid command to find the UUID of new partition.
  4. Edit /etc/fstab so that /boot/efi will be mounted using the new UUID.
  5. Mount /boot/efi.
  6. Copy all the files from /boot/efi of another Ubuntu machine to the new /boot/efi.
  7. Edit /boot/efi/EFI/ubuntu/grub.cfg to have UUID of root partition of machine being fixed.
  8. Reboot

Everything is fine now.

sotirov
  • 3,169
Dennis D
  • 819
  • 1
    I have an error on step 6: 6. install grub with: sudo grub-install /dev/sdX and sudo grub-install --efi-directory=/boot/efi My output is : grub-install: error: failed to get canonical path of/cow'`. Please help :/ – omerorkn Jul 09 '22 at 06:33
  • @omerorkn that's not step 6 in this answer. – Organic Marble Jul 09 '22 at 12:21
26

@Dennis D, just to complete your answer, not everybody will have another machine with *nix.. so

  1. recreate a partition using Gparted in the "unallocated" section created by deleting the original EFI partition.
  2. select FAT32, manage flags and set "boot" flag.
  3. use blkid to find the UUID of new partition.
  4. edit /etc/fstab so that /boot/efi will be mounted using the new UUID.
  5. mount /boot/efi
  6. install grub with: sudo grub-install /dev/sdX and sudo grub-install --efi-directory=/boot/efi
  7. run sudo update-grub

On step 6, just take care selecting the right device (/dev/sda, /dev/sdb, etc).

Hackz
  • 361
  • 1
    Thanks, I didn't know if those programs would reinstall those files. It sounds like you know that they do. Also, with all of my redundant backup systems, I had not backed up the /boot/efi directory. I do now! – Dennis D Feb 25 '22 at 23:45
  • This is a more useful answer as it doesn't require other instances of Linux in order to copy efi directory contents from and doesn't involve tweaking grub.cfg by hand. – oᴉɹǝɥɔ Jul 14 '23 at 17:33
3

I know that it is a bit late for resolving the problem, since you got the system working, but I provide two different solutions.

  1. Removing a partition does not remove the data. Think of the disk as a contiguous space of sectors (long 512, 4096 bytes or so). The information about disk partitioning is stored (for MBR a.k.a. DOS partition table) in the first sector or (for GPT) somewhere at the start and the end of the disk. The rest of disk is left for the partitions. Removing a partition only deletes the record about it in the are where the information about it is stored but not the actual partition data. The partition data will get overwritten only if you create another partition in the same area (or part of it) and write to it. So, when you create the same partition again, everything will work again. Just make sure that it has the same UUID (or change the UUID in GRUB config and fstab), type, position etc.

  2. Use the installation disk for repair. Debian installation CD, and if I remember correctly, also the Ubuntu installation disk has in its text-mode variant a tool exactly for these problems. It can reinstall GRUB or LILO or add it to moved installation. It can also reinstall core system packages etc.

andrew.46
  • 38,003
  • 27
  • 156
  • 232
jiwopene
  • 209
  • For the first solution you should indicate how to restore the partition without formatting it, e.g. with testdisk. – Didier L Feb 27 '22 at 23:38
  • @DidierL, testdisk is not needed, since you know where the original EFI partition was stored (since it usually is on the start or the end of the disk and other partitions follow, or it is between multiple partitions). Manually creating the partition (e.g. using GParted that is used by the asker) is enough. (testdisk will do just the same thing.) – jiwopene Mar 01 '22 at 16:30
  • Creating a new partition with GParted will allocate a new, empty FAT, so it won’t recover anything, right? In fact if it wasn’t the case it would be likely to create corrupted partitions when allocating over previously existing space. testdisk however can just resurrect the previously existing partition. – Didier L Mar 01 '22 at 16:37
  • I do not use GParted anymore, but if I remember it correctly, creating a FAT partition without formatting it (I am sure that GParted allows that), the FAT is not allocated (= formatted), but only a msdos partition table (“MBR”) entry or GPT partition record is created. When you set the same parameters (start and end sector, the type and with GPT also the UUID), you get your partition back in unchanged state. (I am doing this usually using fdisk, which I prefer.) testdisk tries to do the same thing, but only guesses the correct parameters. – jiwopene Mar 13 '22 at 12:44
0

A simple solution exists to rescue a removed partition with parted:

  • boot a linux setup usb key (like ubuntu live USB try-or-install)
  • run sudo parted in a terminal
  • print devices to list your devices
  • select the right device with the select command
  • print to show the hole where your deleted partition was
  • then use the rescue command giving the bounds printed above

The partition is restored and its UUID has not changed. You just have to reboot.

etham
  • 101