Monday 24 June 2019

How to transfer install a legacy copy of Linux/Ubuntu onto a UEFI computer in legacy mode?

Nowadays, Ubuntu installation is no longer a problem. However, sometimes we might want to:
  • transfer a fully-functional copy of Ubuntu (i.e., with all software packages such as Wine, MS-Office, WeChat/QQ/WhatsApp installed) onto another computer
  • the fully-function copy of Ubuntu is in legacy mode, but the target computer requires booting in UEFI mode
  • during installation, the computer is booted in legacy mode (by default, installing UEFI Ubuntu requires booting Ubuntu install CD/thumb-drive/etc in UEFI mode)
This can be easily done for legacy-boot Ubuntu, but for UEFI, it is slightly more complicated.
The steps are as follows:
  1. Resize existing partitions and create new partitions for the new Ubuntu OS.
  2. Boot into {a copy of Ubuntu system} or {Ubuntu installer} in either legacy or UEFI mode.
  3. Copy the pre-installed Ubuntu system folders recursively onto the target partition (e.g., /target) (use "cp -rfa", exclude /dev, /proc, /sys, /mnt, /run, but create these folders on the target partition and use the mount command to bind them, also bind /dev/pts)
  4. Mount the EFI partition to /target/boot/efi
  5. chroot into /target, "sudo chroot /target", inside the fake root environment:
    • install grub-efi, "sudo apt-get install grub-efi"
    • link up EFI partition, "grub-install --target=x86_64-efi /dev/sdX", ignore any errors
  6. copy grubx64.efi (check /target/boot/EFI/ubuntu folder) into /target/boot/efi/EFI/BOOT/bootx64.efi and /target/boot/efi/EFI/ubuntu/grubx64.efi
  7. copy grub.cfg (check /target/boot/grub/grub.cfg) into /target/boot/efi/EFI/ubuntu/grub.cfg
  8. edit /target/etc/fstab and /target/boot/efi/EFI/ubuntu/grub.cfg to make sure all partitions are correct
(If Secure Boot is active, it gets more complex; you must copy shimx64.efi to EFI/BOOT/bootx64.efi and copy grubx64.efi to EFI/BOOT/grubx64.efi.)