Converting Ubuntu from bios to UEFI

The simple way to convert Ubuntu VM from bios to UEFI boot

Converting Ubuntu from bios to UEFI

Status: 25 May 2020 - writing notes following doing this across a number of VM

Notes on slightly frustrating process to move an Ubuntu VM on QEMU / KVM from bios to UEFI boot.

This is actually pretty straight forward, but if you search for how to do this you will get some very very complicated instructions. Here is a proven and straight forward process...

Update Process

The process documented here is for a VM, which has some aspects that make this simpler (ie copy of disk rather than in place update)

  1. Do apt update to ensure you have lastest SW install
  2. Stop VM and use: virsh edit <Domain> to change machine to UEFI
  3. Copy qemu disk and resize copy +1G to ensure you have space for UEFI partition
  4. Attach desktop Ubuntu CD iso (must be same version as your VM)
  5. Attach the second (copied) disk to VM
  6. Reboot VM from Live CD
  7. Use gpartd (accepting resize fix on startup) and remove all partitions from 2nd (copied) disk
  8. Using gparted - add 512MB fat32 partition to copied disk
  9. Using gparted - copy orginal disk root partition to new 2nd disk and optionally any other partition (may need to do swapoff if you have swap partition that is in use)
  10. Using gparted - accept all updates and mark new UEFI partition as: esp/boot
  11. Shutdown VM and remove the original disk and reboot VM from Live CD
  12. Now do: install grub-efi-amd46 package, mount UEFI partition and root partition. Do grub install using "grub-install" and edit fstab to include efi partition mount. Reboot.
--- Using Live CD boot your machine, boot using "Try Ubuntu" option
--- Depending on your machines disk type, the disk could be on any of:
---   sdX - sata
---   vdX - virtio
-- Now ensure you have grub-efi installed on your Live environment
$ sudo apt install grub-efi
--- Create directories to mount disks and mount them
$ cd /media
$ sudo mkdir ROOT
$ sudo mkdir EFI
$ sudo mount /dev/vda2 /media/ROOT
$ sudo mount /dev/vda1 /media/EFI
--- Now install grub
$ sudo grub-install --target=x86_64-efi /dev/vda --efi-directory=/media/EFI --boot-directory=/media/ROOT/boot
Installing for x86_64-efi platform
Installation finished. No error reported.
--- cd into ROOT/etc and edit fstab to include mounting of efi
--- Here is sample after editing (see /boot/efi entry)
$ cd /media/ROOT/etc
$ cat fstab
# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
UUID=83199e8d-6d80-4db9-8dc1-8e7eed326d4e /               ext4    errors=remount-ro 0       1
UUID=214A-5446	/boot/efi vfat defaults 0 0
UUID=4006ff1d-8a08-4dac-8a18-c3a8a3d542a3 none            swap    sw              0       0
--- Now reboot from hd

You should be up and running again as UEFI / OVMF machine

References & Links:

"Ask Ubuntu" very complicated instructions - Based on physical machine, but final post provides simple way

"Ask Ubuntu" even more complicated instructions requiring chroot... - you really need to understand what you are doing to follow these ...