EFI-Booting Ubuntu on a Mac

by Rod Smith, rodsmith@rodsbooks.com

Last Web page update: 5/17/2011

I'm a technical writer and consultant specializing in Linux technologies. This Web page is provided free of charge and with no annoying outside ads; however, I did take time to prepare it, and Web hosting does cost money. If you find this Web page useful, please consider making a small donation to help keep this site up and running. Thanks!

Donate $1.00 Donate $2.50 Donate $5.00 Donate $10.00

Note: I wrote this page when Ubuntu 10.10 was current. Since then, Ubuntu 11.04 has been released with improved support for installing directly to EFI-based computers. (Sadly, the 32-bit disc images I've seen lack this support, which leaves owners of older Intel-based Macs out in the cold.) This new EFI support has the potential to greatly simplify a straight-up GPT/EFI configuration. I've upgraded my 10.10 installation to 11.04 using the update procedure in the Ubuntu 10.10 system, so I haven't installed 11.04 directly on my Mac using the 11.04 installer. Unfortunately, I've seen one report of an EFI boot damaging a Mac's firmware. As of 5/17/2011, it's unclear to me what the cause of this problem is and what steps can be taken to minimize the risk.

The Problem

When installing Ubuntu Linux on an Intel-based Macintosh, most people follow any of several guides available on the Internet, such as this Ubuntu document. Most of these guides, however, rely on features of the Macintosh that are intended to enable it to boot Windows. In particular, these guides typically end up creating a configuration that boots Linux using BIOS emulation on the Mac. In this mode, the Mac sets up a Basic Input Output System (BIOS) layer that the OS can use, rather than rely on the Extensible Firmware Interface (EFI) that OS X uses to run on the computer. BIOS emulation is expedient, and it's the only way that Windows can boot directly on a Mac, but it's got several drawbacks:

As far as I can tell, BIOS emulation mode only works when a hybrid MBR is present on the hard disk or when a BIOS-bootable optical disk is inserted in the optical drive. Thus, you can't use BIOS emulation mode with a conventional GPT disk unless you plan to use a bootable optical disc to boot Linux.

Ubuntu can use the BIOS emulation and hybrid MBR that enable Windows to boot on Macs, but it doesn't need to! If you don't use these features, you won't see their problems. Hybrid MBRs, in particular, are a Bad Idea (with a capital B and a capital I). I've seen numerous problems reported at the Ubuntu Forums related to Ubuntu installations on Macs with hybrid MBRs, and more problems with similar configurations elsewhere. See my hybrid MBR Web page for a technical description of what they are and what can go wrong with them. In short, they can get out of sync, cause confusion about which partition is which, and become damaged in frustrating ways. Whenever I think I've heard of every possible problem with hybrid MBRs, I stumble across a new one. You're better off without one, if that's possible—and it is, if you dual-boot OS X and Linux but not Windows.

The trouble is that Ubuntu doesn't (yet) support installing to a Mac in EFI mode. Therefore, to rid your computer of the dangerous hybrid MBR, you'll need to jump through some hoops. (An alternative to all of this is to run Ubuntu in a virtual environment, such as VirtualBox. This can be a good solution in some cases, but it doesn't give Linux direct access to the hardware and you'll lose some speed.)

Some Caveats

Although my opinion is that Intel-based Macs are best booted using EFI and a GPT with a protective MBR rather than a hybrid MBR, there are drawbacks to doing it this way. These include:

If you're running Fedora, Mandriva, OpenSUSE, Gentoo, Debian, or most other Linux distributions, the procedure I'm about to describe will probably work; however, I've not tested it with any of these distributions and some details may differ. Most notably, you may need to track down a suitable version of GRUB 2 (with EFI support). On the other hand, it's possible that your preferred distribution already supports direct EFI installation, in which case the problem of installing to a Mac in EFI mode may not exist. Check your distribution's documentation to be sure.

I've tested this procedure only on my first-generation Intel-based Mac Mini. This computer uses a 32-bit CPU and 32-bit EFI, so I've tested only the 32-bit version of Ubuntu. It's possible that the 64-bit version has different requirements, or you may need to do things differently on newer 64-bit Macs. You can check which type of EFI you've got with the following command, typed in an OS X Terminal:

$ ioreg -l -p IODeviceTree | grep firmware-abi

The result should include the string EFI32 or EFI64; the meaning of each should be obvious.

Prerequisites

Before proceeding, you'll need to download a few items:

With these items in hand, you can proceed with installing Ubuntu, and then fix it up so that it doesn't use a hybrid MBR.

Installing Ubuntu

If you've already got a working dual-boot configuration with OS X and Ubuntu, you can skip this section and jump ahead to "Fixing the Installation." You may be able to skip some of the steps in that section, too, such as rEFIt installation.

To install Ubuntu Linux 10.10 on an Intel-based Mac, follow these directions:

  1. If necessary, install Mac OS X.
  2. Boot into Mac OS X.
  3. If necessary, using OS X's Disk Utility, adjust the sizes of your partitions, making room for Linux. Note that resizing partitions is inherently dangerous, so back up your data before beginning! You may also want to create a FAT or unjournaled HFS+ ("Mac OS Extended") shared-data partition. The result might look like the below (the Linux partition in this screen shot could as easily be blank space; I've created it here mainly to show you where Linux will reside).
    You must create adequate space for your Linux installation
    before you begin.
  4. If you haven't done so already, install the OS X version of GPT fdisk (gdisk).
  5. Open a Terminal program.
  6. In the Terminal program, type sudo gdisk /dev/disk0 (change /dev/disk0 to /dev/disk1 or above if your computer has multiple hard disks and the one on which you want to install Ubuntu isn't /dev/disk0).
  7. In gdisk, type p to display your partition table. Verify that it's the same one shown by Disk Utility. (Note that Disk Utility hides the EFI System Partition, which is normally the first partition on the disk.)
  8. If you created a Linux partition, type d in gdisk to delete it. Enter its number when prompted. (If you created multiple partitions for Linux with the intention of using them as you created them, you can skip this step.)
  9. In gdisk, type n to create a new partition. Give it a number of 99, hit the Enter key to use the default starting sector (the program may tell you it's changed the sector value, but you can ignore this message), +128M as the ending sector, and a hex code of ef02. This creates a BIOS Boot Partition of 128 MiB at the start of the largest block of free space on the disk. This partition will be used by the initial Ubuntu GRUB installation, but you can and should delete it once you've got native EFI booting working. 128 MiB is actually much bigger than is required for this purpose, but OS X likes to see gaps between partitions when it installs or upgrades the OS, so when you eventually delete this partition, it will leave the type of gap that OS X likes to see. (If you created all your Linux partitions in OS X's Disk Utility, you can put the BIOS Boot Partition in one of the gaps that Disk Utility leaves, but you may need to accept a default value or use +1M rather than enter +128M as the end value.) Type p again to see the results; they should look something like this (although I've got two OS X partitions):
    You can manage partitions, but not the filesystems they contain,
    using gdisk.
  10. In gdisk, type w to save your changes. When asked, type y to confirm this choice.
  11. Insert your Ubuntu installation CD or DVD in the computer's drive.
  12. Restart your Mac and hold down the Option key (Alt if you're using a PC keyboard) as it restarts to get the Mac's OS selection screen.
  13. Pick the Ubuntu install disc from the options. (It will be mis-labelled as "Windows.") The Ubuntu installer will start up.
  14. When the appropriate screen comes up, select Install Ubuntu and proceed with a normal Ubuntu installation, except as noted in the next few steps....
  15. When the Allocate Drive Space dialog appears, select "Specify partitions manually (advanced)."
    Be sure to tell the installer you want to partition the disk
    manually.
  16. You will initially see a partitioning screen with your original OS X partitions, a biosgrub partition (what Ubuntu's partitioner calls the BIOS Boot Partition), and free space at the end of the disk. Select the free space and click Add to begin adding partitions....
    You can verify partitions and check the amount of free space
    before you begin adding Linux partitions.
  17. Create partitions for your Ubuntu installation in the free space near the end of the disk. (If you did this in OS X's Disk Utility, you should modify each partition by selecting it and clicking Change, then clicking the Format option and setting an appropriate filesystem and mount point.) I recommend creating the following partitions: When you're done, you'll see your new partitions displayed....
    Your new partitions appear at the end of the disk.
  18. Be sure that the "Device for boot loader installation" option is set to /dev/sda (it will include a complete name for your hard disk—I used VirtualBox to collect the last few screen shots, but yours will show a real disk model number).
  19. Click Install Now. The installation will proceed as on a PC. You can find generic installation guides online, such as this one at the Ubuntu site.

When the installation finishes, the computer will reboot—straight into Mac OS X! (See the below note if it doesn't boot, though.) You must now proceed with fixing it up in various ways....

Fixing the Installation

Note: If by chance your computer won't boot, try booting the Ubuntu installer but click Try Ubuntu to run it in its live CD mode. Then install the Ubuntu version of GPT fdisk and run steps 1–7 below in the Ubuntu live CD. (Use /dev/sda rather than /dev/disk0; Linux and OS X use different disk identifiers.) With any luck, OS X will start up again, and you can proceed with step #8.

If you've followed the directions, your computer should now be booted into OS X, looking very much like it did before. Ubuntu is installed, however, and your disk has a hybrid MBR. You must now take steps to return the hybrid MBR to a safer protective MBR, as the GPT standard requires, and to set up a boot loader that enables you to select which OS to boot when the computer powers up. To do so, follow these steps:

  1. In OS X, launch a Terminal.
  2. Type sudo gdisk /dev/disk0 (changing the disk identifier as necessary). If at any point in the next few steps something seems wrong, type q to exit without saving your changes.
  3. In gdisk, type x. The command prompt will change to read Expert command (? for help):.
  4. In gdisk, type o. This command displays the contents of the hybrid MBR, which will probably consist of four partitions, one of which is of type 0xEE. The Ubuntu installer created a hybrid MBR (if one wasn't already present) in an attempt to be helpful. Sometimes it will be corrupt even by hybrid MBR standards: It will contain two 0xEE partitions, one of which overlaps other partitions!
  5. In gdisk, type n. The program won't seem to do anything; it will just show you another command prompt.
  6. In gdisk, type o again. The MBR contents should be different from before; there should just be one partition, of type 0xEE. This is a standards-compliant protective MBR.
    You can convert a hybrid MBR to a protective MBR with gdisk.
  7. In gdisk, type w to save your changes.
  8. Open the rEFIt image file you downloaded earlier.
  9. Double-click the rEFIt.mpkg file to install rEFIt. Follow the prompts to install rEFIt.
  10. Reboot. If the rEFIt menu doesn't appear, reboot again. If it still doesn't appear, you'll have to debug your rEFIt installation; consult its Web page for suggestions. If the rEFIt menu does appear, it may have a Linux item, but it won't work (at least, it didn't for me, but there's no reason you can't try it). A successful rEFIt boot looks something like this, although this screen shot is from a completed installation; you won't see the highlighted EFI item on the left of the top row at this point. Also, my system has two OS X installations; yours might have just one (or three, or some other number).
    rEFIt presents a graphical menu of OS choices each time you boot.
  11. Insert the Super GRUB 2 Disk into the computer's DVD drive.
  12. Restart your Mac by selecting the reboot item (the up arrow on the right of the bottom row of icons).
  13. When the rEFIt menu appears, you'll see two Linux items, one for a hard disk boot and the other for a CD boot. Select the latter. (If that doesn't work, reboot and try again with the disk option; they seem to sometimes get mixed up.)
  14. After a while, a GRUB 2 menu should appear. Pick the "Detect any GRUB 2 installation (even if MBR is overwritten)" option from the menu. Note: On my system, if I leave this menu up for more than two or three seconds, it hangs at this point. If yours is the same, you'll need to act quickly!
  15. A new menu should appear listing the GRUB 2 installations it could locate. On my system, this consisted of a grand total of one installation, so there's no ambiguity: Select it!
  16. At this point, a normal Ubuntu GRUB 2 menu should appear, enabling you to boot Ubuntu as you would on a PC. (The screen will go completely black for part of the boot process. Don't worry; this is normal.)
    The GRUB menu on a Mac typically includes Ubuntu, memory test, and
    OS X options.
  17. When the login screen appears, log into your Ubuntu installation.
  18. Select Applications -> Accessories -> Terminal from the GNOME menu.
  19. Type sudo apt-get install grub-efi. This replaces the BIOS version of GRUB that the Ubuntu installer installed with an EFI-based version of GRUB. In fact, there are two different EFI-enabled versions of GRUB: grub-efi-ia32 and grub-efi-amd64. Which is used depends on the architecture of the firmware, so if you're running a 32-bit version of Ubuntu on a recent 64-bit OS X firmware, you may need to explicitly install grub-efi-amd64.
  20. Type sudo grub-mkimage -O i386-efi -o grub.efi -p "(hd0,5)/grub" part_gpt hfsplus fat ext2 normal chain boot configfile linux multiboot xnu xnu_uuid jpeg png tga search loadenv font. Change (hd0,5)/grub to the correct path to your /boot/grub directory, in GRUB's nomenclature. This example corresponds to a /boot partition on /dev/sda5, so change the partition number accordingly. If you don't use a separate /boot partition, substitute (hd0,n)/boot/grub, where n is your Linux root partition's number. If your platform is AMD64, use -O x86_64-efi rather than -O i386-efi. Watch those underscores and dashes! If you used a Linux filesystem other than ext2/3/4 for the Linux /boot partition (or for the Linux root partition if you don't have a separate /boot partition), you'll need to add GRUB's support module for that filesystem to the long list at the end of the grub-mkimage command.
  21. Type sudo mount /dev/sda1 /mnt to mount the EFI System Partition at /mnt. (Change /dev/sda1 if your EFI System Partition has an unusual partition number.) You can use gdisk to check the EFI System Partition's number, if you like.
  22. Type sudo mkdir /mnt/EFI/BOOT/.
  23. Type sudo cp grub.efi /mnt/EFI/BOOT/.

At this point, if you did everything exactly correctly, you should be able to boot Ubuntu in EFI mode—but not quite in the obvious way. The Linux item in the rEFIt menu remains useless. Instead, you should see a new EFI item in the menu, which loads the grub.efi file you created in the preceding procedure. (See the earlier rEFIt screen shot.) This launches GRUB, from which you can launch Linux.

The OS X and memory test options in GRUB are useless, although they work from a BIOS boot—apparently they rely on GRUB modules that aren't available when booted in this way, and so far I've been unsuccessful in including them in the grub.efi file. This is no great loss, since you can boot OS X just fine from rEFIt.

If you want to boot more quickly into Linux, you can change the GRUB_TIMEOUT value in /etc/default/grub; however, reducing this value will make it harder to select alternate kernels or recovery options. If you make such a change, type update-grub afterwards to change the /boot/grub/grub.cfg file, which GRUB reads at boot time.

Once you're satisfied with your ability to boot and use both Linux and OS X, you should delete the BIOS Boot Partition from your hard disk. It's no longer needed, but OS X may want free space where it resides in the future. You can use GParted, parted, gdisk, or any other partitioning tool to delete this partition.

One caution: The rEFIt menu includes a "partitioning tool." As near as I can tell, this is nothing more than the gptsync utility, which is used to create a hybrid MBR. Don't use it! Although you'll still be able to boot afterwards, one of the big advantages of this boot method is that you eliminate the ugly and dangerous hybrid MBR, so why resurrect it? You can remove this option from the menu by editing the /efi/refit/refit.conf file in OS X and adding the following line:

hideui tools

Unfortunately, using this option also hides the icon to launch an EFI shell.

Going Further

It's possible to set the system up to use GRUB directly, without the intervention of rEFIt; however, I stumbled across this configuration early in my experimentation and now I can't seem to re-create it, so I can't tell you how I got it! Sorry! It's not a great loss, though, since this configuration was unstable—OS X kept resetting it, which is what led me to install rEFIt. Also, booting OS X from GRUB produced a black screen until OS X appears, which was a minor annoyance. (OS X did boot from this early configuration of mine—another detail that's lost.)

You can consult the UEFIBooting page to learn about more options for GRUB on a Mac. Some of that page's instructions didn't work for me, though; presumably they're either specific to particular Mac models or the page is dated.

I hope you've found this Web page informative. I realize the procedures outlined here are complex, but in my opinion, it's worth getting rid of the flaky and dangerous hybrid MBR even if doing so requires investing the effort described here.

References

The following sites have additional information that you might find useful:


If you have problems with or comments about this web page, please e-mail me at rodsmith@rodsbooks.com. Thanks.

Return to my main Web page.