Important note: If you are new to UEFI please read this 30min article to understand what’s happening: UEFI boot: how does that actually work, then?
The goal of this note is to fix the UEFI Boot Manager (located in the NVRAM) for a Debian installation, by using a Debian Live image to mount a broken system via chroot and then reinstall grub-efi. This will recreate the boot loader for grub2-efi in the EFI System Partition (as /boot/efi) and add an entry for it in the boot manager. A possible scenario to execute this workaround is when your current Linux installation dissapeared from the UEFI Boot Manager. The instructions here were executed on a Debian Jessie box.
Steps summary:
- Download Debian Live
- Burn it to an USB drive
- Fix UEFI on the USB drive
- Boot Debian Live
- Verify Debian Live was loaded with UEFI
- Review devices location and current configuration
- Mount broken system (via chroot)
- Reinstall grub-efi
- Verify configuration
- Logout from chroot and reboot
Download Debian Live
Download it from Debian Live.
Important: At this point, UEFI support exists only in Debian’s installation images. The Live images do not have support for UEFI boot.
Burn it to an USB drive
As stated in the initial recommended reading, UEFI works best with FAT32 and GPT partition table. However, Debian Live does not support it yet so any method could be chosen. However, be sure to format the USB drive as FAT32. Recommended tools:
- For Debian Linux, Ether. Note that in the instructions use the retired site pgp.mit.edu, therefore use hkp://pool.sks-keyservers.net instead.
- For Windows, Rufus. It has the advantage of clearly allow to select the Partition scheme and system type.
Fix UEFI on the USB drive
- Dowload Shell.efi from UEFI Shell.
- On the USB drive create a EFI folder and a BOOT sub folder, then copy Shell.efi to EFI\BOOT\ and rename it to Bootx64.efi.
- Using a text editor, create a file called liveboot.nsh on the root of the USB drive and paste the following into the file:
live\vmlinuz initrd=live\initrd.img append boot=live components
Boot Debian Live
Reboot the computer and hit F8 or F12 to bring the UEFI Boot Menu, and select your USB Drive. If it is shown more than one time, ensure to select the one that mentions UEFI. After UEFI Shell loads, type liveboot.nsh and press Enter.
Verify Debian Live was loaded with UEFI
When Debian Live finishes loading, verify it it was loaded with UEFI with two basic methods:
- Checking dmesg output you should see many lines related to EFI or UEFI.
$ dmesg | grep -i efi
- Check the EFI vars are correctly loaded
$ ls -l /sys/firmware/efi | grep vars
Review devices location and current configuration
- Identify the root, boot and EFI partitions from the broken system. Use fdisk -l to survey the current partition. EFI normally goes into /dev/sdX1. Mount one by one trying to identify which is the correct one. As a reference, /boot would normally look like:
$ ls -l /boot
total 21581
-rw-r--r-- 1 root root 157721 Oct 19 03:45 config-3.16.0-4-amd64
drwxr-xr-x 5 root root 1024 Apr 5 15:26 grub
-rw-r--r-- 1 root root 16023698 Dec 5 09:50 initrd.img-3.16.0-4-amd64
-rw-r--r-- 1 root root 2679264 Oct 19 03:45 System.map-3.16.0-4-amd64
-rw-r--r-- 1 root root 3126448 Oct 19 03:41 vmlinuz-3.16.0-4-amd64
Note: In the case of lvm volumes, you might need to download the lvm2 package and expose the volumes before mounting:
# apt-get install lvm2
# vgscan
# vgchange -a -y
# mount /dev/volumegroup/logicalvolume /mnt
- Download efibootmgr and review current UEFI Boot Manager configuration
# apt-get install efibootmgr
# efibootmgr -v
Mount broken system (via chroot)
Mounting another system via chroot is the usual procedure to recover broken system’s. Once the chroot comand is issues, Debian Live will treat the broken system’s “/” (root) as its own. Commands run in a chroot environment will affect the broken systems filesystems and not those of the Debian Live.
- Mount root partition (for example an lvm)
# mount /dev/volumegroup/logicalvolume /mnt
- Mount boot partition (F.e. in drive sda)
# mount /dev/sda2 /mnt/boot
- Mount the EFI System Partition (usually in /dev/sda1)
# mount /dev/sda1 /mnt/boot/efi
- Mount the critical virtual filesystems with the following single command:
# for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done
- Chroot to your normal (and broken) system device
# chroot /mnt
Important: From now on your changes will affect your normal system.
Note for Raspberry Pi OS (previously called Raspbian)
Nathan sent below comment from his experience running these notes, thanks!
For Raspbian LVM (Logical Volumes) are not used. There is no need to create directories for mounting a standard install. Once the partition called Linux Filesystem is mounted in /mnt, you have the /boot/efi folders, but efi is more like a placeholder where the EFI volume is mounted. Thus:
# sudo mount /dev/mmcblk0p# /mnt (# = partition number with Linux filesystem) # sudo mount /dev/mmcblk0p# /mnt/boot/efi (# = partition number with EFI System)
Reinstall grub-efi
Download and Install grub-efi package, set the debian bootloader and create a grub config file based on your disk partitioning schema
- Install grub-efi package
# apt-get install --reinstall grub-efi
- Put the debian bootloader in /boot/efi and create an appropiate entry in the computer’s UEFI Boot Manager
# grub-install /dev/sda
- Re create a grub config file based on your disk partitioning schema
# update-grub
Verify configuration
- Bootloader exists
# file /boot/efi/EFI/debian/grubx64.efi
/boot/efi/EFI/debian/grubx64.efi: PE32+ executable (EFI application) x86-64
(stripped to external PDB), for MS Windows
- NVRAM entry was properly created
# efibootmgr -v | grep debian
Logout from chroot and reboot
Type ctrl+d or exit to quit the chroot session, and reboot.