Is it still possible to install Ubuntu to an external harddrive with UEFI?

16,889

It actually is possible.

Before I continue, these instructions are meant for blank EFI System Partitions (ESP) and will probably overwrite existing files, or not work as expected. At least make backups!

Adding required partitions for different platforms

Platforms: Legacy PC, UEFI-based Windows computer, Apple computer

Add an ESP to an existing installation with MBR

You need to follow these instructions from another installation or live media.

  1. Install the grub-efi-amd64-bin package. This will only install the resources needed. It will not switch your existing MBR style installation to UEFI and turn things upside down.

  2. Shrink your Ubuntu partition in GParted to make space for an ESP. Recommended sizes for ESPs range from 100 MB to 500 MB, but the files created by following these instructions here will not require more than 2 to 5 megabytes.

    Note: It shouldn't matter where your ESP is located on the disk and shrinking your Ubuntu partition by a few megabytes from the end should be quick and safe. Shrinking at the beginning is not safe. One caveat though, your ESP should be a primary partition and not be part of an extended partition or a logical volume, partition numbers from 1 to 4 are fine, numbers above indicate an extended partition on MBR partition tables.

    Choose FAT32 as filesystem and set the boot flag.

  3. Mount the ESP you just created and the root filesystem you shrunk:

    mkdir -p /mnt/esp
    mount $esp_device /mnt/esp
    mkdir -p /mnt/rootfs
    mount $root_device /mnt/rootfs
    

    Note: Replace $esp_device and $root_device with the appropriate devices e.g. /dev/sdb2 and /dev/sdb1. The mountpoints /mnt/esp and /mnt/rootfs are just examples chosen to work with the remaining part of this answer.

    Install GRUB's EFI image and a minimal configuration file with:

    grub-install --efi-directory /mnt/esp --boot-directory /mnt/rootfs/boot --target x86_64-efi --removable $device
    

    Note that $device is the whole device e.g. /dev/sdb, not a partition.

  4. That's it, we are already done, but let me try to explain a few things.

    The parameter --target x86_64-efi will ensure that UEFI images and modules will be installed to the given paths. --removable will install the UEFI image to the hardcoded path \EFI\BOOT\BOOT{arch}.EFI for removable media, instead of a distribution specific path. Your grub.cfg in /boot/grub/ should work with both boot methods and a new folder named x86_64-efi should now exist next to i386-pc.

    For completeness, this is the command for non-removable media which writes and relies on NVRAM entries in the platform:

    grub-install --efi-directory /mnt/esp --boot-directory /mnt/rootfs/boot --target x86_64-efi --bootloader-id "Ubuntu" $device
    

    Proper UEFI installations include a line similar to the one below in /etc/fstab, but the setup is functional without and the UEFI image (similar to the MBR bootloader) is usually only written/updated during OS installation.

    UUID=1234-567F /boot/efi vfat defaults 0 1
    

Adding an ESP or GRUB-BBP to an installation with GPT

Adding an ESP to a legacy installation on a GPT partitioned drive is very similar to the above:

  1. Install the grub-efi-amd64-bin package.
  2. Create a partition with a FAT32 filesystem similar to the instructions above by resizing the root partition and set the boot flag.
  3. Mount the partitions and run the grub-install command from above that includes the --removable parameter.

This also works if you just want to boot your exisitng UEFI installation on another computer.

Creating a BIOS Bootable Partition (BBP) for GRUB is a bit different:

  1. Install the grub-pc-bin package.

  2. Create a partition similar to the instructions above by resizing the root partition and set the bios_grub flag. Choose no filesystem and leave it unformatted.

  3. Edit the configuration file /etc/default/grub with administrative rights and add GRUB_DEVICE=/dev/sdb6 to the end. Replace /dev/sdb6 with the actual device name of your BBP. You can use the commandline editor nano to keep thing a simple.

    Install the GRUB MBR image to the BBP with:

    grub-install --boot-directory /mnt/test/boot/ --target i386-pc $device
    

    You might want to remove or comment out the line in /etc/default/grub and add it to the actual installation.

Bonus: Add a partition for Macs on an installation with GPT

I found that the very new Mac (Mac Mini A1347 EMC 2840) I was testing with could boot from a standard FAT32 ESP, even from MBR! Anyways, here we go:

  1. Install the grub-efi-amd64-bin, hfsprogs, mactel-boot and mactel-boot-logo (optional) packages. You need to enable the Universe repositories (How do I enable the "Universe" repository from the command line?) and add the current Mactel support PPA (see Mactel Support Community team for more details) to install all of them.

  2. Create a partition with an HFS+ filesystem similar to the instructions above by resizing the root partition. This time we need a few megabytes more space, so double the size of the ESP if you had only 5 megabytes or less before. No flag is required.

  3. Mount the partitions:

    mkdir -p /mnt/esp-mac
    mount $esp-mac_device /mnt/esp-mac
    mkdir -p /mnt/rootfs
    mount $root_device /mnt/rootfs
    

    Note: I chose $esp-mac_device and /mnt/esp-macto avoid conflicts.

    Run the grub-install command that includes the --removable parameter:

    grub-install --efi-directory /mnt/esp-mac --boot-directory /mnt/rootfs/boot --target x86_64-efi --removable $device
    

    Finally setup up the partition the way a Mac expects it to be:

    sudo hfs-bless /mnt/esp-mac/EFI/BOOT/bootx64.efi
    sudo cp -v /mnt/esp-mac/EFI/BOOT/bootx64.efi /mnt/esp-mac/System/Library/CoreServices/boot.efi
    sudo cp -v /mnt/esp-mac/EFI/BOOT/grub.cfg /mnt/esp-mac/System/Library/CoreServices/grub.cfg
    sudo cp -v /usr/share/mactel-boot/SystemVersion.plist /mnt/esp-mac/System/Library/CoreServices/SystemVersion.plist
    sudo cp -v /usr/share/mactel-boot-logo/ubuntu.icns /mnt/esp-mac/.VolumeIcon.icns
    echo "Dummy kernel for booting" | sudo tee /mnt/esp-mac/mach_kernel
    

    To boot Ubuntu from external media on a Mac you would now just need to press the option key or Alt on Windows keyboards and select the right disk.

    Mac Startup Manager showing volumes and Ubuntu logo

About partition flags, type codes and GUIDs

To be as accurate as possible, the boot flag isn't important to GRUB, by default GRUB searches for UUIDs to find the right filesystem to boot from. Setting the boot flag or the bios_grub flag in GParted usually sets a type (MBR) or GUID (GPT) for the partition.

The benefit of setting the correct type or GUID, besides having a properly working setup, is that these partitions will be hidden in the filemanager. The easiest way to check and change types and GUIDs is via Disks (gnome-disks).

screenshot showing drives and partitions in gnome-disks

screenshot showing edit partition dialog in gnome-disks

Alternatively you could use parted or gdisk:

  • 0xef is the type code for ESP in MBR for use with fdisk
  • ef00 is the abbreviated type code for ESP in GPT for use with gdisk
  • ef02 is the abbreviated type code for BBP in GPT for use with gdisk

Wikipedia has a big list of GUIDs for GPT.

Commandline equivalents in parted according to the manual are parted $device set partition esp on and parted $device set partition bios_grub on.

Share:
16,889

Related videos on Youtube

Csabi Vidó
Author by

Csabi Vidó

This is my AskUbuntu profile and most of my stuff here on StackExchange is tech-oriented. However I do have other hobbies and interests, you may find more about these in my other profiles on the network. I'm lwbt on Reddit and Telegram. I started with Ubuntu and Linux somewhere between 2006 and 2007. With the release of 08.04 I completely migrated away from Windows to Ubuntu and while this has been a few years ago I still consider myself fairly new to this topic and I know I can still discover something new and exciting everyday. The following listing is not to brag about hardware, I'm rather trying to reproduce issues and share experience. Custom built desktop computers with Intel HD graphics (dualscreen setup), Lenovo T530 & T560 (touch) without discrete graphics (also run Windows 10), Chromebooks Lenovo N22 & ASUS Flip C302 (with GalliumOS) Behringer FCA610 USB (PulseAudio multichannel configuration), JDS Labs C5D & The Element, Bluetooth dongles and BT audio speakers and headphones Wacom Intuos 4 PTK-540WL HP 3005pr Port Replicator (works with proprietary DisplayLink driver) Aten CS1794 HDMI KVMP Switch Logitech TK820 and some unifying receiver mice …and some APC UPS

Updated on September 18, 2022

Comments

  • Csabi Vidó
    Csabi Vidó almost 2 years

    It looks like installing Ubuntu to an external harddrive isn't possible with UEFI, where as with MBR there wasn't a big difference between external and internal. Is that true or is it still possible?

    Is it even possible to combine both boot methods?

  • oldfred
    oldfred over 9 years
    I did not document details. But was able to just partition my flash drive to gpt, create an efi partition and copy efi boot files into efi partition. The grub-mkimage creates a grubx64.efi that expects certain paths for files, so I just copied file structure from my install into a /boot/grub and added a grub.cfg with a configfile entry to another grub.cfg in my /iso folder to loopmount boot my ISOs. Probably more correct to create your own grubx64.efi with the make image, but copy worked.
  • Mark E. Haase
    Mark E. Haase about 7 years
    I searched high and low trying to figure out how to do the "Add an ESP to an existing installation with MBR" part. I was nervous about it, but it worked on the first try! Great answer!
  • user68186
    user68186 almost 7 years
    I am trying to add EFI boot capability to a full BIOS/MRR install of Ubuntu on USB using the Add an ESP to an existing installation with MBR part of the answer. I get a "Error: File /boot/ not found". The grub.cfg in the ESP partition points to the right UUID. Any ideas? I would be happy to post a new question.
  • BRabbit27
    BRabbit27 over 6 years
    @user68186 were you able to do it?
  • BRabbit27
    BRabbit27 over 6 years
    I was able to install Ubuntu in my external drive from a Mac. When connected to the Mac it does boot correctly into Ubuntu. However, when connected to a Windows10 machine (xps 15 9560) in the boot devices the external hard drive does not appear. My hunch is that is due to the UEFI. Can someone confirm if my hunch is right and also if this thread is what I might need to be able to boot from a windows machine?