How To Create A Portable Linux Installation On External Drive Without Modifying Your Main Drive

June 9, 2024

If you've ever tried to install Linux on an external drive, you may have noticed that it boots fine on the machine you used for the installation but doesn't boot on other machines.

Additionally, even if you install everything, including the bootloader, on the external drive, your main drive with Windows and its bootloader may still be modified. This results in your machine automatically booting into GRUB, where you see two OS options.

This issue arises from a bug found in the installers of most Linux distributions. Common advice is to disconnect all other drives except the USB installer and the drive you want to install Linux on. While this prevents the Windows bootloader from being touched, it won't create a truly portable Linux installation. Here's why:

Requirements for Booting from an External Drive:

  1. ESP Partition: The drive must have an ESP partition formatted using FAT32.
  2. Bootable Flag: The ESP partition should be bootable, meaning it should have the bootable flag enabled.
  3. Bootloader: The ESP partition should contain a bootloader.

During Linux installation, you can choose where the ESP partition will be created, typically on the drive where Linux is being installed. However, due to a bug, the installer searches for an existing ESP partition on all connected drives. If it finds one, it installs the bootloader there instead of the selected location, fulfilling the first two criteria for the external drive but not the third.

To ensure all three criteria are met for your external drive, disconnect the drive with Windows during installation. However, you still won't be able to boot from this drive on different machines because the bootloader installed is named

grubx64.efi
and bios doesn’t recognise
grubx64.efi
and a bootloader instead it looks for
bootx64.efi
.

Why Your Machine Can Boot but Others Can't

The installer creates an entry in the motherboard's NVRAM, called a boot entry, so the BIOS knows about the bootloader named

grubx64.efi
on this drive. This is why you can only boot from the system used to install Linux.

Creating a Portable Linux Installation

To avoid modifying the Windows bootloader and BIOS boot entries and to create a portable Linux installation, you can use a virtual machine. Here's how:

  1. Load the installer in a virtual machine.
  2. Pass through the external drive to the VM.
  3. Go through the usual installation process.

This installs Linux on the external drive with the bootloader in it without modifying your boot entries.

You can follow the steps in this YouTube video.

Fixing the Bootloader

After following the tutorial, you may notice that even your machine can't boot from the drive because the BIOS boot entry isn't there, and the bootloader is named

grubx64.efi
instead of
bootx64.efi
.

To fix this, boot into your virtual machine with the live ISO again,

chroot
into the installed Linux on the external drive, and run the GRUB installer again with the
--removable
flag.

Here is what

--removable
flag does (from Stackoverflow):

It installs the EFI executable to the "fallback" path (e.g. 

EFI/boot/bootx64.efi
) to avoid having to register the executable to the UEFI firmware (NVRAM). The UEFI firmware will list it automatically (as a "disk" entry).

Follow these steps (adapted from this page):

  1. Mount all partitions from the external drive:

    # mount /dev/sdb2 /mnt/ # mount /dev/sdb1 /mnt/boot/efi
  2. Bind mount various virtual filesystems:

    # for i in /dev /dev/pts /proc /sys /sys/firmware/efi/efivars /run; do sudo mount -B $i /mnt$i; done
  3. Chroot into the system:

    # chroot /mnt
  4. Install GRUB with the

    --removable
    flag:

    # grub-install /dev/sdb --removable
  5. Generate the GRUB configuration file:

    # update-grub

Following these steps will ensure that your Linux installation on the external drive is truly portable and can boot on different machines without modifying the main drive's bootloader.