Ubuntu installation on USB stick with pure EFI boot (Mac compatible)

  • an USB key with Ubuntu 16.04 LTS installed on it,
  • with /home and swap encrypted,
  • that I could boot from any of my 2 Macs, by simply pressing and holding the “option” (or “alt”) key immediately after boot,
  • without messing around with their respective boot loaders.

Disclaimer

Even though the process should leave your Mac’s hard drive intact (this was my case), I strongly recommend to backup your data and have an OS X recovery usb ready, just in case something goes wrong… and things often go wrong. I don’t take any responsibility of any damage for lost data or unbootable Mac.

Equipment

  • Your Mac, obviously,
  • a USB stick with the Ubuntu installer on it (instructions here),
  • the USB stick where you are going to install Ubuntu on.

Ubuntu installation

Shut down your Mac, insert the installer USB stick and power on the machine while holding “option” or “alt” key until the boot menu appears. You should see something like this:

ubiquity --no-bootloader

The manual boot

While keeping both your USB sticks inserted, power on the machine while holding “option” or “alt” key until the boot menu appears, as you did before. You should see the Mac’s boot menu with the following options:

grub>
grub> ls
(memdisk) (hd0) (hd1) (hd1,gpt3) (hd1, gpt2) (hd1,gpt1) ...
grub> ls (hd2,gpt2)/home
mmiglier/
grub> ls (hd2,gpt1)/boot/grub
error: file `/boot/grub' not found.
grub> ls (hd2,gpt2)/boot/grub
unicode.pf2 ...
grub> set root=(hd2,gpt2)
grub> ls -l (hd2,gpt2)
Partition hd2,gpt2: Filesystem type ext* 〈...snip...〉 UUID e86c20b9-83e1-447d-a3be-d1ddaad6c4c6 - Partition start at [...]
grub> linux /boot/vmlinuz〈...tab here!...〉.efi.signed root=UUID=〈the UUID from above〉
grub> initrd /boot/initrd〈...tab here!...〉
grub> boot

Fixing the EFI partition

So why is the system unbootable? The problem is that the Mac bootloader expects the EFI partition to be formatted as HFS+, the typical Mac filesystem. It also expects certain files to be present. The Ubuntu installer actually formats it as VFAT and doesn’t create the files that are necessary for booting on a Mac.

$ sudo add-apt-repository ppa:detly/mactel-utils
[...]
Utilities for Intel based Macs (eg. recent MacBooks, Mac Minis). Includes a
fan control (macfanctld) and the HFS "bless" utility.
More info: https://launchpad.net/~detly/+archive/ubuntu/mactel-utils
Press [ENTER] to continue or ctrl-c to cancel adding it
$ sudo apt-get update
$ sudo apt-get install mactel-boot hfsprogs gdisk grub-efi-amd64
$ sudo fdisk -l
[...]
Disk /dev/sdd: 57,9 GiB, ...
[...]
$ sudo gdisk /dev/sdd
GPT fdisk (gdisk) version 1.0.1
Partition table scan:
MBR: hybrid
BSD: not present
APM: not present
GPT: present
Found valid GPT with hybrid MBR; using GPT.Command (? for help):
Command (? for help): p
Disk /dev/sdd: ...
[...]Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00 EFI System Partition
[...]
Command (? for help): d
Partition number (1-3): 1
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-121307102, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-1050623, default = 1050623) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): AF00
Changed type of partition to 'Apple HFS/HFS+'
Command (? for help): wFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdd.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
$ sudo mkfs.hfsplus /dev/sdd1 -v Ubuntu
Initialized /dev/sdd1 as a 512 MB HFS Plus volume
$ sudoedit /etc/fstab
# /boot/efi was on /dev/sda1 during installation
UUID=C59D-1B30 /boot/efi vfat defaults 0 1
$ mount | grep /boot/efi
/dev/sda1 on /boot/efi ...
$ sudo umount /dev/sda1
$ sudo bash -c 'echo UUID=$(blkid -o value -s UUID /dev/sdd1) /boot/efi auto defaults 0 0 >> /etc/fstab'
$ sudo mount /boot/efi
$ mount | grep /boot/efi
/dev/sdd1 on /boot/efi ...
$ sudo mkdir -p "/boot/efi/EFI/$(lsb_release -ds)/"
$ sudo bash -c 'echo "This file is required for booting" > "/boot/efi/EFI/$(lsb_release -ds)/mach_kernel"'
$ sudo bash -c 'echo "This file is required for booting" > /boot/efi/mach_kernel'
$ sudo grub-install --target x86_64-efi --boot-directory=/boot --efi-directory=/boot/efi --bootloader-id="$(lsb_release -ds)"
$ sudo hfs-bless "/boot/efi/EFI/$(lsb_release -ds)/System/Library/CoreServices/boot.efi"
$ sudo sed -i 's/GRUB_HIDDEN/#GRUB_HIDDEN/g' /etc/default/grub
$ sudo sed -i 's/GRUB_TIMEOUT=10/GRUB_TIMEOUT=0.1/' /etc/default/grub
$ sudo grub-mkconfig -o /boot/grub/grub.cfg
$ sudo apt-get install mactel-boot-logo
$ sudo cp /usr/share/mactel-boot-logo/ubuntu.icns /boot/efi/.VolumeIcon.icns
$ sudo mkdir -p /mnt/sda1
$ sudo mount /dev/sda1 /mnt/sda1
$ sudo rm -r /mnt/sda1/EFI

Final make up

If you are going to keep the USB always inserted, like me, you will notice that when starting OS X, the ESP partition will be automatically mounted. To prevent this, annotate the partition UUID by running from OS X:

$ diskutil info /Volumes/Ubuntu | grep "Volume UUID" | awk 'NF>1{print $NF}'
5F2578EF-237A-3AC9-BC32-0BD4D520A0A7
$ sudo vifs
UUID=5F2578EF-237A-3AC9-BC32-0BD4D520A0A7 none hfs rw,noauto

--

--

PhD candidate in Computer Science and Engineering. Technology and Software Enthusiast. Husband. Father²

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marco Miglierina

Marco Miglierina

PhD candidate in Computer Science and Engineering. Technology and Software Enthusiast. Husband. Father²