For the longest time, I was scared of using UEFI on my PC. With the exception of my laptops, which force me to use UEFI though I disabled Secure Boot because it's a pain to deal with, I have been using CSM mode (compatibility mode) that simulates BIOS. Lately with a new hard drive to test various Linux OSes on, I've decided to reinstall Windows 10 inside the PC on UEFI mode, so on the new Linux hard drive I can install multiple OSes without having their bootloaders stepping on each other like in the BIOS age.
Howto
I learned this from this article in StackExchange. These steps apply too, I guess, Ubuntu, Fedora, Arch, or anything using GRUB as bootloader. Tldr, we chroot into the root directory of the OS you want to convert, run some commands and GRUB is restored.
First, I checked to see whether the machine is on UEFI mode using this command:
[ -d /sys/firmware/efi ] && echo UEFI || echo BIOS
Proceed if it says UEFI, otherwise go into the BIOS menu and change it.
Afterward, I mapped some partitions into the root directory of the OS that is on the way to be converted (supposed that sda1 is the OS partition, sda2 is the EFI system partition, /mnt is where you want to chroot to, could be anywhere else):
sudo mount /dev/sda1 /mnt
sudo mount /dev/sda2 /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
Later on, I loaded a module called efivarfs (UEFI Runtime Variables Support) using sudo modprobe efivarfs. Then I went in using sudo chroot /mnt.
Now, depend on the OS, say Ubuntu or Fedora, you use their respective package manager. For my case, that was apt. So:
apt update && apt list grub*
Check for package grub-efi and grub-efi-amd64 (grub-efi-amd64-signed if Secure Boot was enabled, I didn't try it). And then I went:
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ubuntu --recheck --no-floppy --debug
Alternatively, this article suggests reinstalling grub-efi. This is how I recover GRUB on Fedora, since grub-install didn't work.
References: