Thursday, 26 October 2017

linux - How to reinstall GRUB2 EFI?


After successfully updating my bios, something went wrong and I ended up with a blinking cursor on the top left corner of a black screen. No errors, no nothing. The bios now only listed a SATA:  boot option in place of the usual UEFI ubuntu one. I'm using a GPT partitioning scheme.


I eventually found that the working solution was to properly reinstall grub-efi-amd64. So, how do I do this ?


PS: Actually, i succeeded to reinstall GRUB2 EFI on my own and will post my answer here as I was unable to find any complete how-to on this.



Answer





  • Boot your computer with a live-USB/CD in UEFI mode. I had two boot options and UEFI: , 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).


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...