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.)

Tuesday 22 January 2019

How to use Intel GPU for desktop display and dedicate Nvidia GPU (nvidia-415 and above) for CUDA in Ubuntu?

Nvidia has updated their graphics driver, so in the latest nvidia-415, my previous solution (2017.9 "How to use Intel GPU for desktop display and dedicate Nvidia GPU for CUDA in Ubuntu?") no longer works.

Here is the updated solution to deal with the new driver:
METHOD A:
1. You still select Nvidia by "sudo prime-select nvidia"
2. add the following in /etc/X11/xorg.conf

Section "Device"
    Identifier      "intel"
    Driver          "intel"
    BusId           "PCI:0:2:0"
EndSection

Section "Screen"
    Identifier      "intel"
    Device          "intel"
EndSection
where the BusId is the PCI address of your integrated graphics card (run lspci).


METHOD B:
1. To use Intel GPU to display desktop environment, you first need to switch to Intel GPU using "sudo prime-select intel" and reboot.

2a. Every time after entering desktop, you need to run the following command to load the kernel driver:
sudo modprobe ipmi_msghandler
sudo insmod /lib/modules/$(uname -r)/updates/dkms/nvidia.ko

Take note that as Nvidia keeps updating their driver:
- the filename and location of nvidia.ko might change, you need to know these every time you install the Nvidia driver.
- its dependency kernel module, ipmi_msghandler, might also change. You can use modinfo to check Nvidia kernel driver's dependency

2b. Alternatively (instead of 2a), you can simply run the following command to load Nvidia driver:
sudo prime-select nvidia
sudo modprobe nvidia
sudo prime-select intel