Linux Multiboot with BTRFS, LUKS and EFI (Part 2)

This is Part 2 of the article and contains steps for setting up the system described in Part 1.

Boot from a Ubuntu Live CD/USB and open a terminal window. Follow the steps in below sections one by one.

WARNING: You will lose all data on the laptop. I’m assuming you have taken backups, and have sufficient time for completing the setup and resolving any issues that may come up. This is best done on a weekend when you have plenty of time.

Disk Partitioning

We will use GParted for partitioning as it is very easy to use. Start GParted from the Applications menu.

  • Format the entire disk with a GPT partition table. This will delete all existing data and partitions on the disk.

GParted > Main Menu > Device > Create Partition Table…

  • EFI System Partition — Create a 100 MB partition formatted as FAT16. Right-click and select Manage Flags. Set the esp and boot flags.
  • Boot Partitions — Create 400 MB boot partitions formatted as EXT4 for each distribution that you want to install. It’s better to create 5–10 boot partitions at the beginning, so that you don’t need to create additional partitions in future. Each 400 MB partition can hold upto 5 Linux kernels. If you need to have many kernels installed, then increase the size of each partition. If you have the habit of uninstalling older kernels periodically then 400 MB is more than enough.
  • System Partition — Create one big partition to occupy remaining disk space. Leave it as unformatted. We will format it in the next section.
  • Labelling — Label partitions as shown in screenshot. Set both LABEL and PARTLABEL to same value. This makes it easy to identify the partitions and we will be using it later to create scripts for some common tasks. Keep a one-word lowercase name for each distribution that you wish to install (like “xenial”, “ubuntu”, “mint”, etc) and use it everywhere while labelling partitions. This will make it very easy to manage the systems once we are done installing them.
Note: PARTLABEL is displayed as Name in GParted
  • Swap Partition — You can create a swap partition if you wish to. You usually don’t need one if you have atleast 8 GB of RAM. You may see some benefit from having a swap partition if you run applications that use a lot of RAM. I have never used a swap partition, and never faced an issue from not having one.

LUKS Setup

Run the following commands to format system partition. Copy-paste the entire block of commands in a terminal window and hit Enter.

Enter the encryption passphrase when prompted. Enter it once again to unlock it. The partition will be unlocked and formatted with BTRFS.

Installing Ubuntu 16.04 LTS

  • Boot from the Ubuntu 16.04 LTS Live CD/USB
  • Open a terminal and copy-paste the following commands:

This will unlock the LUKS partition and map it to /dev/mapper/xps_system

  • Start the Ubuntu installer and select custom partitioning. Select /dev/mapper/xps_system as the root device (/) and /dev/sda2 as the boot device (/boot).
Note: Make sure that the partitions are NOT marked for formatting. Ignore any warnings displayed by installer for formatting the partitions.
Note: If the installer prompts for unmounting partitions, allow it to unmount. This does not lock the LUKS device that we unlocked in previous step.
Note: The partition selected for mounting /boot will depend on the distribution you are installing. I had labelled the /dev/sda2 partition as xps_boot_xenial so I will use the same one while installing Ubuntu 16.04.
  • Select /dev/sda for installing the GRUB bootloader.
Note: We need GRUB on /dev/sda only for booting the newly installed system for the first time. Once we have booted into the newly installed system, we will install rEFInd and generate a GRUB image file, so that we never need to depend on GRUB again.
  • Once the installation completes, do not reboot. The installed system does not know that it is installed on an encrypted partition, and will fail to boot as a result. We need to make some changes to the installed system to make it boot successfully.
  • Mount system partition under /mnt/btrfs
  • Rename subvolumes — Rename \@ and \@home to \@xenial and \@xenial_home
  • Mount system devices Mount the devices for /, /home, /boot and /boot/efi under /mnt/xenial.
Note: Do not mount devices to folders under path /mnt/btrfs/@xenial. If you do, the chrooted system will not be able to detect the root filesystem, and you will face errors while updating GRUB. Mount all devices explicitly under /mnt/${distname} using commands given above.
  • Chroot to the installed system. I recommend using groot. It hides much of the complexity of preparing for chroot. Download and install the 64-bit version of groot and chroot to the installed system.
Note: Running sudo groot starts a chrooted session. Any commands you type after this will act on the installed system, instead of the system that you are currently working from.
  • Update Initramfs Settings — Run the following block of commands. It will update the initramfs settings for booting from an encrypted root.
WARNING: Run the above commands in the chrooted terminal so that changes are made to the installed system, and not to the system that you are currently running. This applies to following steps as well, till we reach the step where we exit the chroot session.
  • Update GRUB Settings — Open the grub configuration file and modify it. If you started the session using groot then you will be able to run GUI editors such as gedit, mousepad, etc instead of terminal-based tools like nano, vi, etc.

Change the line for GRUB_CMDLINE_LINUX to the following:

  • Update fstab file— Edit /etc/fstab file and change the subvolume name.
Note: I replaced the UUID device names with /dev/disk/by-partlabel/<partlabel> syntax. It’s easier to remember and you are less likely to make mistakes.
Note: Columns in fstab file can be separated by spaces, tabs or a combination of both. Line up the columns so that they are easier to read.
  • Create crypttab file—Create a new /etc/crypttab file as you are not likely to have one.
  • Rebuild Initramfs — We need to rebuild the initramfs file for changes we have done above. Run the following commands:
Note: This will regenerate the initramfs, update the grub menu, and re-install GRUB to /dev/sda
  • Exit the chroot session by typing exit in the terminal
  • Reboot the laptop and boot into the new system. You will be prompted to unlock the system partition when it boots. This indicates that the previous steps were successful.

Install the Boot Manager

You can stop at this point if you don’t wish to install multiple distros. However, if you wish to continue then we need to install a boot manager.

  • Install the rEFInd boot manager from PPA or by downloading the DEB file.

rEFInd installs itself to folder /boot/EFI/refined/ and updates the EFI firmware settings to make itself the default boot manager.

  • Generate a grub image file on boot partition. This generates a file named core.efi in /boot. This will be detected by rEFInd and used for booting the system. Once this file has been created, we no longer need the GRUB version installed on /dev/sda for booting the system.
Note: Change the GPT partition number --prefix=(hd0,gpt2) to match the boot partition number. In this case, the boot partition is /dev/sda2 which is GPT partition 2.
Note: This file must be generated after booting to the installed system using GRUB. If you generate this from a chrooted session or from another system, then you will get an error “error: symbol table not found” while booting the system. This error is harmless and you can continue booting by pressing Enter.

Configure the Boot Manager

  • Reboot the system.
  • You will be greeted by the rEFInd boot manager.
  • rEFInd displays boot icons for the core.efi files we created above.
  • It also displays boot icons for every kernel it finds on the boot partitions, and also for the grubx64.efi files that Ubuntu installs to /boot/efi/efi/ubuntu. We need to hide these extra entries. Select the icons one by one and hit Delete. We need to do this every time a new kernel is installed to the boot partition while installing system updates.

Installing Additional Distributions

To install another Ubuntu-based distro, repeat the steps above. Replace distname=xenial with distname=mint for example, if you install Linux mint. Substitute xenial with mint in all the files in steps above.

WARNING: Be very careful NOT to reformat the LUKS partition or the BTRFS device (/dev/mapper/xps_system) while installing additional distributions.
Note: Installing non-Ubuntu distributions like Fedora, Arch Linux, etc requires different steps as they don’t install to /@ and /@home subvolumes. We need to create subvolumes like /@arch and /@arch_home and install the system using a bootstrapper script like arch-bootstrap.

Continued in Part 3… (coming soon)


Donate

If you find this artcle useful and wish to say thanks, you can buy me a coffee by making a donation with Paypal.