Linux – Accidentally deleted /boot partition

bootfedoragrublinux

this may sound stupid and appear to have a bunch of duplicates but I have spent almost 3 days looking for a solution without look, see, I accidentally issued a mkfs.xfs command on /dev/sda2 instead of /dev/sdd2 and completely wiped my /boot partition (along with all vmlinuz and initrd files), and so I tried the standard recovery method from live media (with a few changes since my system was installed with LVM partitions and in EFI mode):

mkdir /mnt/fedsys
mount /dev/fedora/root /mnt/fedsys
mount /dev/sda2 /mnt/fedsys/boot
mkdir /mnt/fedsys/boot/efi (I had to create a new efi dir since it was lost)
mount /dev/sda1 /mnt/fedsys/boot/efi
mount --bind /proc/ /mnt/fedsys/proc
mount --bind /sys/ /mnt/fedsys/sys
mount --bind /dev/ /mnt/fedsys/dev
chroot /mnt/fedsys

It worked fine up until here, then I tried regenerating the grub.cfg file:

grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg

However it fails with the following messages:

WARNING: Failed to connect to lvmetad. Falling back to device scanning.
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
device-mapper: reload ioctl on osprober-linux-sdd1 failed: Invalid argument
Command failed
done

I then ignore the errors and proceed to install grub:

grub2-install /dev/sda

And it gives the following output:

Installing for x86_64-efi platform.
EFI variables are not supported on this system.
EFI variables are not supported on this system.
Installation finished. No error reported.

And when I reboot I am greeted with the grub prompt. Now, I think the problem lies in the fact that either vmlinuz nor initrd are anywhere to be found (of course, because I nuked /dev/sda2) but I can't find a way to either rebuild them or make the system boot.

What can I do? Is there a way to rebuild those files from live media? The system I'm trying to rescue was running Fedora 25 64 bits on EFI mode and on LVM.

Best Answer

Here is a generic method you can try to fix your system. Package names might vary between distributions so you might need to google some specifics.

  1. Boot into the live media
  2. Mount your hdd root partition somewhere (for example /mnt).
  3. Mount all system-necessary partitions inside this root - your /boot and any other you might have.
  4. Mount with -o bind folders /dev, /proc and /sys from the live media root into your hdd root in /mnt
  5. You can now safely chroot into your /mnt. It should give you a fully functional system.
  6. Reinstall grub package and either run mkinitramfs or better yet reinstall linux kernel as well.
  7. Generate your grub configuration and install grub into MBR (if you use it), you should have all that is necessary now.
  8. Exit the chroot back into live media and umount all in reverse order.
  9. Try to reboot into your original system.

Edit: By chrooting you enter environment of your local write-able disk installation. All libraries and executables are used from this environment with the exception of already running processes and kernel - so you will be able to use your installed package manager and package database. Mounting /dev, /proc and /sys is necessary to give you access to the hardware and control over processes, you will need it in order to generate new linux image in /boot, configure grub properly and access the network to download the packages. Chrooting is especially useful if you had to use the live media from another distribution.

As for reinstalling grub - package names could differ between distros, reinstalling grub* can't do any harm though. Don't forget that just installing the packages might not be enough. You might also need to run grub-install after you setup your configuration.

Related Question