Migrating an Ubuntu installation from BIOS to UEFI

For this article, I am assuming that you are comfortable getting around Linux in general and Ubuntu in particular, and are familar with the ins and outs of disk partitioning. I will be describing the steps that I took to make this happen, but not going into too much detail for each individual step. See the bottom of this post for some useful links if you’d like to read up on the topic first. Note that it is very possible to lose your partitions when doing this sort of work so proceed carefully and do not blame me if something goes awry.

As far as I know, you need the Linux 3.0 kernel or later to boot via UEFI, which means you’ll need to be using at least Ubuntu 11.10. All of the current 64-bit Ubuntu (11.10) ISO images seem to boot fine via UEFI, so I booted the desktop disc as a “live CD” for many of these operations.

If you have a standard Ubuntu installation, your disk has just two partitions: one for the file system and one for swap space. In my case, these were /dev/sda1 (ext4) and /dev/sda5 (swap). The disk itself is partitioned using the MBR method.

To switch to UEFI, you’ll have to convert the disk’s partition table from MBR to GPT. This can be done non-destructively using gdisk. You can install it from the “Universe” repository in Ubuntu (here’s the package page), if you’ve booted from the Ubuntu Lice CD, you’ll have to enable this repository. Once you launch gdisk from the terminal, you tell it which disk you’re interested in (/dev/sda in my case) and it will proceed with the conversion from MBR to GPT automatically — you’ll just need to tell it to write changes and quit. GPT actually stores two copies of the partition table on the disk, and gdisk will want to put one at the front and one at the end. For me, it complained that there was no room after the last partition on my disk to store the second partition table, so I had to open GParted and shrink my swap partition a little.

After the conversion to GPT, you’ll notice that there are some small gaps between your partitions. The reason for this is explained in more detail here, but, you can use GParted to move and resize the partitions to your liking after the conversion is done.

Now, UEFI systems need a special boot partition to hold the UEFI boot loader for your OS, called the EFI system partition, or ESP. (It can actually hold several different boot loaders, making multi-booting considerably more simple than before.) Create a 20 MB FAT16 partition at the start of the disk, and use the GParted flag management to enable the “boot” flag. I don’t think that this new partition necessarily has to be the first one on the disk, but I’m just copying the partition structure that you get from installing Ubuntu on a UEFI system from scratch. Make sure you use the “MiB” alignment option when messing with the partitions to keep them aligned properly if you have an SSD, “advanced format” 4K-sector drive, or RAID setup.

After the conversion to GPT, I had two partitions on the disk: /dev/sda1 (ext4) and /dev/sda2 (swap). When I added the FAT16 partition at the front, it was labeled /dev/sda3 even though it was the first partition on the disk.

Now, the conversion to GPT is done and the ESP has been set up. All that’s left is to reinstall grub so that it can boot your machine. First, install the grub-efi package. This will remove the grub-pc package, and allow us to install grub without having it freak out because your disk is not partitioned with MBR.

The following commands did it for me… you may have to adjust them if your partitions are labeled the same as mine.

sudo mount /dev/sda1 /mnt # Mount the root filesystem in /mnt
sudo mkdir /mnt/boot/efi # Create a directory to mount the ESP in
sudo mount /dev/sda3 /mnt/boot/efi # Mount the ESP
sudo grub-install --root-directory=/mnt /dev/sda # Run the grub install

When you’re done, you should be able to check the ESP (in /mnt/boot/efi) to see that the boot loader has been placed there — EFI/ubuntu/grubx64.efi.

Then, reboot and it boots! Depending on your motherboard’s UEFI implementation, you may have to point it to the Ubuntu boot loader the first time (EFI/ubuntu/grubx64.efi). Also, the first time I booted after the conversion, some errors popped up on the screen but the system still came up properly. Subsequent boots did not have any errors, so I’m not quite sure what that was about.

Be sure to install the grub-efi package on your main system too, so that if an update comes along that requires updating grub, it works properly.  Also, I actually ended up deleting and re-creating the swap partition during this process, so its UUID changed.  You may want to check your /etc/fstab file and make sure the UUIDs still match up with your partitions.

Resources:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>