Ubuntu – Which commands to convert a Ubuntu BIOS install to EFI/UEFI without boot-repair on single boot hardware

14.04boot-repairgrub2system-installationuefi

Accidental BIOS mode install

I have Intel 64-bit hardware with UEFI setup utility. Ubuntu 14.04.1 LTS was installed as the only operating system on the only drive attached. Accidentally Ubuntu was installed in BIOS/CSM/legacy mode.

Convert to UEFI

While later on learning about UEFI, the goal is to change this existing Ubuntu installation to (quicker) boot via EFI/UEFI. I do still want to have some kind of – 2 seconds displayed – boot menu that allows me to enter the UEFI setup utility. Therefore I think I do need use Grub (can't use an EFI boot stub) and GOP support requires Grub version 1.99 or higher. I have already re-partitioned the drive using a Live CD and inserted a 200 MiB EFI partition in the beginning of the drive and marked that fat16 formatted partition as type id 0xEF.

Before:

# fdisk -l /dev/sda
...
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   108478463    54238208   83  Linux
/dev/sda2       108480510   125044735     8282113    5  Extended
/dev/sda5       108480512   125044735     8282112   82  Linux swap / Solaris

After:

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *      411648   108478463    54033408   83  Linux
/dev/sda2       108480510   125044735     8282113    5  Extended
/dev/sda3            2048      411647      204800   ef  EFI (FAT-12/16/32)
/dev/sda5       108480512   125044735     8282112   82  Linux swap / Solaris

Partition table entries are not in disk order

No boot-repair please

The Ubuntu community wiki suggests to use boot-repair to Converting Ubuntu into EFI mode. I don't want to use a GUI, or install any extra packages, and I don't want any data being accidentally sent to pastebin.com and because I do want to know what will be changed exactly, I don't want to use Boot-repair.

Under the hood

Trying to figure out what boot-repair actually does, I found this snippet:

Boot-Repair will convert a BIOS install to UEFI by uninstalling grub-pc, and installing grub-efi, if gpt partitioned.

source: http://ubuntuforums.org/showthread.php?t=2147295&p=12657352#post12657352

UEFI mode install comparison

A clean Ubuntu 14.04.1 installation in UEFI mode creates a 512 MiB Fat32 formatted EFI partition. That partition contains one directory /EFI/ubuntu, containing 4 files: grub.cfg, grubx64.efi, MokManager.efi and shimx64.efi. The grub.cfg contains:

search.fs_uuid 7d843e47-3917-4114-8725-55dfa1fbe002 root hd0,gpt2
set prefix=($root)'/boot/grub'
configfile $prefix/grub.cfg

Search.fs_uuid points to the UUID of the Linux installation partition, in this comparison UEFI installation Linux is installed partition /dev/sda2 (versus sda1 in BIOS mode installation).

No potential Asrock firmware issues found

Having CSM still disabled in firmware and using the clean UEFI mode Ubuntu installation. Resizing the 512 MiB EFI partition to 200 MiB using a Live CD and gparted results in that its formatting changes from FAT32 to FAT16. The Asrock firmware P1.50 (incorrectly called BIOS by AMI in boot message "BIOS date") is still able to boot into UEFI mode Ubuntu: UEFI+FAT16 = ok.
Converting the partition table from GPT to MBR (msdos) using the same Live CD terminal command gdisk and its commands r g p w also results in a UEFI bootable Ubuntu on an MBR partitioned drive: UEFI+MBR = ok.

Question

Does this mean that the only commands I do need to execute – from the legacy Ubuntu install and in this order – are:

# apt-get install grub-efi
# apt-get remove grub-pc

? Or is more needed to be done?

Best Answer

Start a Ubuntu Linux (14.04) Live CD in UEFI mode. In case of a USB boot device, disable "Fast Boot" in UEFI.

Open a terminal window (Ctrl+Alt+T)

To verify that you are actually running in UEFI mode, use this bash command:

$ [ -d /sys/firmware/efi ] && echo UEFI || echo BIOS

The resulting output should be:

UEFI

In case it says BIOS, reboot into your firmware and correct the boot device preference.

To do the BIOS to EFI/UEFI conversion enter these commands:

$ sudo mount /dev/sda1 /mnt
$ sudo mkdir -p /mnt/boot/efi
$ sudo mount /dev/sda3 /mnt/boot/efi
$ sudo mount --bind /dev /mnt/dev
$ sudo mount --bind /proc /mnt/proc
$ sudo mount --bind /sys /mnt/sys
$ sudo mount --bind /run /mnt/run
$ modprobe efivars
$ sudo chroot /mnt
# apt-get install grub-efi-amd64

Apt-get networking failure?

# rm /etc/resolv.conf
# ln -s ../run/systemd/resolve/stub-resolv.conf resolv.conf
# apt-get install grub-efi-amd64

No apt-get failure

The following extra packages will be installed:
  efibootmgr grub-efi-amd64-bin
The following packages will be removed:
  grub-gfxpayload-lists grub-pc
...
After this operation, 2,399 kB of additional disk space will be used.

# grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ubuntu --recheck --no-floppy --debug

Despite ending in error message:

Fatal: Couldn't open either sysfs or procfs directories for accessing EFI variables.
Try 'modprobe efivars' as root.

the next reboot already shows "ubuntu" in the firmware its boot options menu, and boots to the console as before, except for now booting in efi mode:

$ dmesg | grep EFI
efi: EFI v2.31 by American Megatrends
fb0: EFI VGA frame buffer device
EFI Variables Facility v0.08 2004-May-17
fb: switching to inteldrmfb from EFI VGA

In case something goes wrong, https://superuser.com/questions/376470/how-to-reinstall-grub2-efi might help.