Emergent Design Emergent Design Atom Github

Switching to UEFI

12 July 2019

This describes a solution to a very specific problem that hopefully may be of use to someone in the future (and probably to us when we forget what we did).

Scenario

We installed an Ubuntu 18.04 server using the alternative installer and configured it as a RAID1 system.

The issue here is that the partitions were set up as ext4 boot partitions not as EFI and the installer happily went away and selected one of them to be mounted as /boot. This meant that the kernel and grub information only existed on that one partition with an MBR that is tied to it; if this disk happens to fail then the system will no longer boot.

Example

/dev/sda1 = 250MB boot partition mounted as /boot
/dev/sda2 = remaining space as RAID partition for root filesystem
/dev/sdb1 = 250MB boot partition
/dev/sdb2 = remaining space as RAID partition for root filesystem

What we actually wanted was a cloned EFI partition at the beginning of both disks and then /boot left alone on the root filesystem meaning that it exists on both disks as part of the mirrored RAID. The machine was already fully set up before we noticed and so to avoid having to reinstall everything we figured out a process to convert it to UEFI.

WARNING: The following is dangerous and you could easily break your system so please ensure that you’ve backed up before you start

Method

Install the grub EFI bits and bobs.

$ sudo apt install grub-efi-amd64

Unmount boot and mount to a temporary location.

$ sudo umount /boot
$ mkdir /tmp/boot
$ sudo mount /dev/sda1 /tmp/boot

Copy grub and kernel files to /boot and create a mountpoint for the EFI partition.

$ sudo cp -r /tmp/boot/* /boot/
$ sudo umount /tmp/boot
$ sudo mkdir /boot/efi

Edit /etc/fstab and remove the /boot mountpoint. Force a grub update so that the system is still able to boot although we’d probably recommend that you don’t attempt a reboot right now.

$ sudo update-grub2

Convert the boot partitions on both disks to EFI. The following example is for when the disk has a DOS partition table, if GPT is in use then you will need a different type identifier for EFI.

$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): t
Partition number (1,2, default 2): 1
Hex code (type L to list all codes): ef

Changed type of partition 'Linux' to 'EFI (FAT-12/16/32)'.

Command (m for help): w
The partition table has been altered.
Synching disks.

Repeat the above for /dev/sdb and then format the two EFI partitions to vfat.

$ sudo mkfs.fat -F32 /dev/sda1
$ sudo mkfs.fat -F32 /dev/sdb1

Inform the kernel of the partition modifications and then retrieve the UUID of /dev/sda1.

$ sudo partprobe /dev/sda
$ sudo partprobe /dev/sdb
$ blkid /dev/sda1
/dev/sda1: UUID="F42E-A8DD" TYPE="vfat" PARTUUID="6e964c87-01"

Since the EFI disk is going to be cloned they will both have the same UUID and it therefore doesn’t matter which is selected for boot. If sda is removed then /dev/sdb1 will be mounted to /boot/efi instead.

Add the mountpoint to fstab using the UUID retrieved in the previous step.

UUID=F42E-A8DD  /boot/efi       vfat    umask=0077      0       1

Mount the EFI partition, force grub to install the EFI related bits and then update grub.

$ sudo mount /boot/efi
$ sudo grub-install --target=x86_64-efi /dev/sda
Installing for x86_64-efi platform.
EFI variables are not supported on this system.
EFI variables are not supported on this system.
grub-install: error: efibootmgr failed to register the boot entry: No such file or directory.
$ sudo update-grub2

Ignore the failure since there should now be files in /boot/efi.

At this point the system should boot as UEFI although the MBR still exists. You can test if it works by disabling legacy boot in your BIOS settings.

When booted successfully via UEFI there should be files present in /sys/firmware/efi and the efibootmgr command should return valid information.

$ efibootmgr
BootCurrent: 0004
Timeout: 1 seconds
BootOrder: 0004,0002,0001,0003
Boot0001  Network Card
Boot0002* UEFI: Built-in EFI Shell
Boot0003  Hard Drive
Boot0004* ubuntu

The final step is to clone sda1 to sdb1.

$ sudo dd if=/dev/sda1 of=/dev/sdb1

Take care when using dd since it’s very easy to accidentally wipe out a partition or even entire disks!