There's always time to play

Sunday, April 4, 2010

Ubuntu Lucid on (X86) Mac Mini with EFI

I was gonna use my Mac Mini as replacement home server (uses less power than an idle Core 2 Duo + Geforce 8800GTS + 4 Harddisks), so I decided to put Ubuntu Lucid on it. Installation was really easy. I used the Lucid Lynx Beta 1 Server install CD, and installation went just fine. Press/hold c to boot from CD, be sure to create a seperate boot partition or keep some free space to create a seperate boot partition afterwards.

Just installing Ubuntu is no issue at all. You don't even need to create a seperate boot partition, because it'll boot just fine using the Mac's legacy booter. However if you're a Mac Mini owner and want to boot headless, there is only one solution (and a workaround that uses a 'dummy monitor dongle', which is not what I would want). This solution is making use of the EFI features of GRUB 2, which I'll detail in the rest of this post.

When the installation is done, install hfsplus and hfsprogs so you're able to create HFS+ volumes (you probably only need one of those, but it wasn't exactly clear for me which one to use and due to lack of time I haven't looked any further yet). Copy files from your boot partition to a temporary space, then unmount and format the boot partition as HFS+ (mine is /dev/sda2):
# mkfs.hfsplus -v boot /dev/sda2

Edit /etc/fstab to use the new boot partition as boot partition:
/dev/sda2 /boot hfsplus defaults 0 2

Unfortunately it seems UUID's can't be used for HFS+ volumes at the moment, so hardcode the device name in there.

After creating the volume mount it again and copy over all the files from your temporary space.

Now install grub-efi, this will automatically remove grub-pc. Generate a GRUB EFI executable using the following command:
# grub-mkimage -o /boot/grub/grub.efi -p /grub part_gpt hfsplus fat ext2 normal sh boot configfile linux

You actually don't even need the fat and ext2 modules and probably more can be stripped, but I haven't experimented with GRUB 2 a lot yet, that'll be for another day (and another post).

We're almost done, now it's on to getting the Mac to actually use our EFI enabled GRUB. First toggle the boot flag on your shiny HFS+ partition. Run parted (or any other GPT-aware partitioning tool) and type:
(parted) set 2 boot off

Parted will probably tell you the disk is in use and you need to reboot for the change to become effective, but it doesn't matter for us.

Now the final step is to tell the Mac (or actually, the filesystem) that our grub.efi is bootable so it'll show up in the Mac boot menu. There should be a utility call hfspbless, which allows you to do this from within Linux, however the first hit on Google doesn't seem to offer a quick guide, so I skipped this part. Instead, put in the Mac OS X install DVD. As soon as a menu bar shows up (I believe you have to click next at least once), fire up a terminal. In the terminal, enter the following:
# mkdir /Volumes/boot
# mount_hfs /dev/disk0s2 /Volumes/boot
# bless --folder=/Volumes/boot --file=/Volumes/boot/grub/grub.efi --label boot --setBoot

The bless command has now set some metadata on the HFS+ filesystem that the Mac uses to identify a native bootable image. I assumed label should set the label accordingly for the boot menu, however my entry showed up as 'EFI something' IIRC, but I can't care more or less since it's a server and I'll never see the menu anyway. Now reboot and enjoy!

This did the job for me, however there are a few issues I still have to take care of. The Mac created a fake MBR partition map for me, which I don't need and don't use. It now shows 'Windows' as option in the Mac boot menu, but luckily it starts Linux by default. Also, the MBR partition map is out of sync if you do stuff with the GPT partition table. I used rEFIt to resync the MBR table, but when I figure out how to remove the MBR that's what I'm going to do.

Also, there's a file called Volume Name Icon on my boot partition. I guess this is used for the Mac boot menu, so it probably can be changed easily too. However I have no clue what the format is, I'll have to look this up some day and change it for a genuine Tux!


Unknown said...

I followed this on a Mac Mini Core Due (2006) that was upgraded from Hardy (and others before that). All I was able to get was a grub2 prompt on boot, which I think means I was nearly there, but it didn't seem to recognize the grub configuration, although it did see my two partitions. It only said that it couldn't recognize the filesystems, however, and I couldn't get past that point.

The same machine boots fine from grub-pc installed in the MBR.

Any thoughts would be appreciated. Thanks for the article.

Michael Croes said...

It's easy I think... Check that you provide the correct filesystem module(s) for grub-mkimage, in my case I only needed ext2 and hfsplus (for the grub partition), but if you made your root something like reiserfs you'll need another module.

Hope this gets you a bit further, I'm also thinking you're close!


Unknown said...

My root partition is ext4, which I surmised was covered by the ext2 module.

Michael Croes said...

I'm not sure if ext2 covers ext4, but your comment gave me another idea... I'm using a hfsplus boot partition, Ubuntu installed all the GRUB stuff there and after that I made my own GRUB image. However, I actually don't even need ext support because my kernel and initrd are also on /boot... Also, could it be you're confusing MBR with GPT partition numbers?


Unknown said...

Right, okay let's see. Here was my setup:

/dev/sda (MBR/GPT)
/dev/sda1 ext4 /
/dev/sda2 hfsplus /boot

grub-efi successfully booted from the /dev/sda2 partition, so that was all good. Then I believe my grub2 setup was to load the kernel from /dev/sda1, which doesn't make sense ultimately, because /dev/sda2 will replace the kernel location. So, the kernel should be booted from /dev/sda2, which only needs hfsplus. grub2 will consider that to be (hd0,2), and the kernel path should be simply /vmlinuz-xxx, instead of /boot/vmlinuz-xxx?

Alternately, could /dev/sda2 not be listed in /etc/fstab at all, only contain the grub.efi executable, and have both hfsplus and ext2? Not sure how that would affect kernel and grub2 updates. I'd rather not manually copy over everything after updates.

This howto suggests calling the grub boot partition /efi and putting the binaries inside a directory called /efi/boot:

Am I following correctly? I wonder how this is really intended to be done.

Michael Croes said...

You can have a /boot that's hfsplus:

mycroes@Mini:~$ mount | grep /dev/sda2
/dev/sda2 on /boot type hfsplus (rw)

All the kernels are directly in /boot, so the path for grub is /vmlinuz. I didn't have to change anything to get this working...


Unknown said...

No matter what I do, I just cannot get this working. The furthest I can get is a grub flashing prompt with a black screen, but grub will not load the OS on /dev/sda1. I've tried with /dev/sda2 formatted as fat32 with an /efi/boot structure, I've tried your method with /dev/sda2 formatted as hfs+ and mounted as /boot. No dice no matter what I do.

I wonder if there is something fundamentally funky about the EFI implementation in the 2006 Core Duo Mini's that is causing a problem.

Oh well, I think for now I'm giving up and going back to the monitor dongle.

Michael Croes said...

Hi Mark,

If you get the GRUB prompt, it means you're almost there. Keep in mind configuration filenames have changed between the old and new GRUB. You can just type in the commands to load your kernel and see if it works, if so you probably only need to fix the loading of the config file.

Anyway, I hope I can make this work for you, I see no reason why it wouldn't work and I hate to see someone having trouble getting my solution to work...


Anonymous said...

Using this howto, I've been trying to get this working on my MacBook. I completely erased its disk, created a gpt partition table and created for partitions in it. The first is my boot partition, formatted in hfsplus. Using the modules in your post, I was not able to boot, I just got to a grub console. This console was missing commands as well (like root). I found out that the following modules got me to a working menu (with still some errors and warnings about missing commands):

part_gpt hfsplus fat ext2 normal sh chain boot configfile minicmd linux loadbios reboot appleldr halt search part_msdos part_apple

If you use the default Ubuntu generated config, you will also need:

test terminal gfxterm keystatus

This will still give you an error about terminal, I don't know why because the command is available now. Also, it seems there is some graphic mode problem (although after grub, kernel modesetting etc works the way it should). Doesn't really matter for me, my MacBook now boots a lot faster. Thanks for the hints in the right direction, it would be great to see my modules added to your post so other people can benefit from them.