Skip to content

Instantly share code, notes, and snippets.

@sitecode
Created December 21, 2023 15:32
Show Gist options
  • Save sitecode/636a54d15d815e9d883d8302557179f3 to your computer and use it in GitHub Desktop.
Save sitecode/636a54d15d815e9d883d8302557179f3 to your computer and use it in GitHub Desktop.
Convert Ubuntu 22.04 FDE hard drive from MBR to GPT (and UEFI optionally)

Convert Ubuntu 22.04 FDE hard drive from MBR to GPT (and UEFI optionally)

With zero data loss a Ubuntu 22.04 FDE (LUKS) drive can be converted to boot in a UEFI (non Legacy Boot) system. The LVM partition is left complete untouched. The boot partition is resized to give room for the EFI partition.

Make backup

Make a full backup of the drive. The partition table is getting totally rewritten which means data could be lost.

At a minimum backup the partition table of the drive:

sudo sh -c "sfdisk -d /dev/sda > sda.mbr.backup.sfdisk" #backup partition table
sudo sh -c "sfdisk /dev/sda <  sda.mbr.backup.sfdisk" #restore partition table

sudo sgdisk --backup=sda.gpt.backup /dev/sda #backup gpt
sudo sgdisk --load-backup=sda.gpt.backup /dev/sda #restore gpt

sudo dd if=/dev/sda of=sda.mbr.446 bs=446 count=1 #backup mbr
sudo dd if=sda.mbr.446 of=/dev/sda bs=446 count=1 #restore  mbr

Convert to GPT

Install

Useful things to have installed to have success.

sudo apt install gdisk

Create BIOS Boot Partition

The BIOS boot partition can often fit hopefully in the empty space on the drive physically right before the first partition even though it will have a higher partition number due to being created later.

sudo gdisk /dev/sda
p #print current partitions
n #new partition
2 #use default partition number
34 #start of 34
2047 #end of 2047
ef02 #specify partition type of BIOS boot partition
w #write changes
y #yes to proceed and write GPT data

Verify with sudo grub-install /dev/sda should show line GPT: present.

Update grub

Now update grub with the latest.

sudo partprobe #load latest partition changes
sudo grub-install /dev/sda

Verify

Reboot to verify that GPT conversion was successful.

Add UEFI

Install

Useful things to have installed to have success.

sudo apt install gparted gdisk grub-efi-amd64 grub-efi-amd64-signed efibootmgr

Create EFI Partition

Make room

In gparted free up extra unallocated space after the boot partition by resizing it to 601MB. If the boot partition was 731MB then 130MB would remain after the boot partition.

  • Applications > gparted > select boot drive (sda1) > unmount > resize > 601MB > apply
Work around (optional)

Trying to resize the boot partition when booted from the same drive might give an error in gparted causing it to fail when applying the desired operation. The error looks something like this:

/dev/sda1 is in use.
e2fsck: Cannot continue, aborting.

If so then boot from an Ubuntu setup media and using gparted in the Try Ubuntu desktop, resize, then restart from the original drive again.

Make partition

sudo gdisk /dev/sda
p #print current partitions
n #new partition
3 #use default partition number of 3 
1232896 #use default start of the empty space
+130M #use default end of unallocated space or specify size
ef00 #specify partition type of EFI system partition
c #change new partition name
3 #use the new partition number
EFI #new name without spaces for new partition
p #print, optional to review before saving
w #write changes
y #yes to proceed

Verify new partition label with sudo partprobe;ls -l /dev/disk/by-partlabel/EFI - should show a line including text something like /dev/disk/by-partlabel/EFI -> ../../sda3.

Make Writable and Reachable

First format new partition, make it auto mountable in proper location, then mount it.

sudo partprobe #load latest partition changes

sudo mkfs -t vfat -v /dev/disk/by-partlabel/EFI #format partition to vfat to make it writable

sudo sh -c “echo '/dev/disk/by-partlabel/EFI /boot/efi vfat defaults 0 2' >> /etc/fstab”
sudo mkdir /boot/efi
sudo mount /boot/efi

Verify mount with mount | grep efi - should show a line like /dev/sda3 on /boot/efi type vfat

Update grub

Now update grub with the latest while also creating the necessary EFI partition files.

sudo grub-install --target=x86_64-efi

Verify grub efi install with ls /boot/efi/ should show EFI directory now.

Verify

Booting successfully on the original or new machine, that has Legacy boot disabled, is the real test. You can also run this command efibootmgr after rebooting successfully which will now have meaningful output.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment