What follows includes my thoughts and notes more than definitive knowledge. I did manage to run Ubuntu in VirtualBox, but the process how I got there may include incorrect conclusions. Here goes.
- VirtualBox 4.2.16 under OS X
- OS X 10.8.4
- Ubuntu 12.04 32-bit
- Windows 8 32-bit
- MacBook Unibody late 2009
(Yes, I presume you have the above setup; if you have something that's 64-bit, unfortunately, I can't help you there.)
Everything in this post is dangerous to your data, even if you are an expert. Think thrice before doing anything, back up everything, triple-check everything I suggest you do for hidden assumptions that may be related to my machine.
Some steps may have inadvertently been omitted. I did not note everything I did, and I don't feel like reproducing each and every of my steps.
I know this disclaimer will probably not dissuade you from trying this, but this includes particularly dangerous stuff and I don't want anyone misunderstanding: I am NOT advising you to do this. I am NOT advising you to follow my advice. I am only documenting this process for my own private future use.
Mac runs EFI instead of BIOS. This is quite similar to today's UEFI machines, but does present its problems.
The partition table format used is GPT. Again, this is something normal and expected on today's new UEFI machines, but something people may be unfamiliar with.
Somewhat specific to Mac, if one installs Windows, then the resulting partition table format includes both MBR and GPT. This is a result of the fact that 32-bit Windows don't boot from GPT and don't like EFI, and even more so, a result of the support for Windows on Macs being produced before -Longhorn- Vista was released. So the lowest common denominator is, in this case, 32-bit Windows XP; so, "BootCamp" produces a GPT+MBR hybrid partition table. (Experts seem to dislike hybrid MBRs.)
Under (U)EFI, the equivalent of BIOS does very little. It has support for FAT (usually FAT32), for loading additional drivers from the FAT partition, and for running .EFI binaries (mostly boot loaders, although in some cases these may be utility programs or shells).
On PCs, we will be mostly interested in 32-bit and 64-bit Intel .EFI binaries (i386 and x86_64 binaries). There are even universal binaries. Running an appropriate .EFI binary is how we boot an operating system. Since 3.3, Linux kernels seem to include an EFI stub; this means you can take a "vmlinux" binary and feed it directly to the EFI subsystem of an (U)EFI machine, without use of ELILO, GRUB2, or some other boot loader.
To expose the binary to the EFI system of the machine, you need to mount the FAT-formatted partition of type "EFI System Partition", change to the \EFI directory on this partition, create a "vendor directory" (that's the name of your OS or your OS vendor), and put "boot.efi" binary in that directory. Some EFI systems may expose other binaries as well, but this is what one is supposed to do.
Alternatively, systems may include a "Compatibility Support Module". Macs do. This is a BIOS emulation layer and allows booting "legacy" operating systems. Microsoft says this must be turned off by default. Note that it is not a requirement; there is no need to include CSM. (VirtualBox does not.)
Please read around to familiarize yourself with EFI and GPT, and how they differ from BIOS and MBR. Trying to describe them here would be either a lot of work or would result in even more incorrect article than it's already bound to be.
Easy parts and troublesome parts
VirtualBox supports physical partitions in the VMDK format. You need to use the command line to create the VMDK disk image, but it's easily doable. It's essentially a one-liner under normal, tested conditions.
Even when the conditions are not as normal, I've successfully ran BootCamp'd Windows XP previously. (Search my blog if you're interested in hacks I had to do.)
That was easy: Windows XP deals with MBR, and MBR is trivial.
The problematic part with supporting Ubuntu is that its boot loader, GRUB2, actually appears to be too smart for its own good. Having installed the 32-bit version of Ubuntu 12.04, it has (by design) not deployed an EFI boot loader. Instead of deploying
grub-efi, it has deployed
grub-pc. This is great; the rEFIt boot loader that I use when booting the physical machine has picked up on this and allowed me to pick the Linux system. It does, unfortunately, depend on turning off the "bootable" flag in MBR with
fdisk. This appears to be a limitation (or a feature?) of the Apple-provided CSM.
That's all great. But when I tried to load this system with VirtualBox using default options, I got nothing. Zip. Nada. A black screen and that's it.
Why? I have no idea. But I presume that GRUB2 picked up on GPT partition table and then got very confused by this on a non-EFI system. Why? Again, no idea. Switching to EFI got the setup to work — but only after installing
grub-efi and deploying it to the appropriate place.
Creating virtual disk
You want to have a virtual disk that includes all relevant GPT information.
As I was originally playing with my previous MBR-based method, and the EFI+GPT method came from that, so does my script include some
dding of GPT data: the first 40 sectors of the disk, and the last 40 sectors of the disk.
IMPORTANT: Always check device names (GPT partition IDs) with
sudo diskutil list /dev/disk0. Always check MBR partition IDs with
sudo frisk /dev/disk0. My setup includes Windows 8 and is very weird.
IMPORTANT: This script is NOT intended to be run as-is! Read it to learn what's going on, triple check every single number, ensure you understand every single line, customize it for your machine and only then think twice before running anything below. Playing with partitions, with disk devices etc is dangerous. Ensure you have backups of everything that's even remotely significant.
I personally have a Time Machine backup of my important data under OS X, every important project is stored on online code hosting, and everything else under physically installed Ubuntu and Windows 8 is not important. What's your situation? Can you afford to lose data?
NOTE NOTE NOTE: As of OS X 10.9 Mavericks, Detection of disk size in blocks is broken in the script below. I haven't updated it; when I needed to run it, I read it from a manually-ran fdisk and put it in appropriate place. (n.b. this could possibly be doable from native code: ioctl(fd, DKIOCGETPHYSICALBLOCKSIZE, &block_size); see http://stackoverflow.com/a/15947809/39974)
# ALWAYS check devices with: # sudo diskutil list /dev/disk0 # ALWAYS check MBR partition IDs with: # sudo fdisk /dev/disk0 EFISYSTEMPARTITIONOSXDEVICEID=1 RECOVERYPARTITIONOSXDEVICEID=3 LINUXPARTITIONOSXDEVICEID=4 LINUXPARTITIONMBRDEVICEID=3 sudo chmod 777 /dev/disk0s$EFISYSTEMPARTITIONSOXDEVICEID sudo chmod 777 /dev/disk0s$RECOVERYPARTITIONSOXDEVICEID sudo chmod 777 /dev/disk0s$LINUXPARTITIONSOXDEVICEID sudo VBoxManage internalcommands createrawvmdk -filename PhysicalDisk.vmdk -rawdisk /dev/disk0 -partitions $EFISYSTEMPARTITIONOSXDEVICEID,$RECOVERYPARTITIONOSXDEVICEID,$LINUXPARTITIONOSXDEVICEID # (primary mbr=1, primary gpt header=1, primary gpt table = 32, extra = 6) * 512 PRIMARY=40 sudo dd bs=512 count=$PRIMARY if=/dev/disk0 of=PhysicalDisk-pt.vmdk # secondary gpt table=32 + secondary gpt table = 1 # see http://7bits.nl/blog/2012/01/02/mac-gpt-partition-table-recovery SECONDARY=33 DISKSIZE=`diskutil info disk0 | grep "Total Size: .*exactly .* 512-Byte-Blocks"|sed 's/.*Total Size:.*exactly \(.*\) 512-Byte-Blocks)/\1/ '` OFFSET=`calc $DISKSIZE-$SECONDARY` OFFSET=`echo $OFFSET|sed 's/^ *//'` # ltrim sudo dd bs=512 count=$SECONDARY iseek=$OFFSET oseek=$PRIMARY if=/dev/disk0 of=PhysicalDisk-pt.vmdk conv=notrunc
First, note the
chmods. These will be required after each reboot. OS X only allows
root to access the disks (for very important security reasons); VirtualBox does not run with
root privileges. I don't do this lightly; be VERY mindful that this is actually creating a local security hole, allowing user processes to read and even write to the disk.
Next, if you take a look at
PhysicalDisk.vmdk, it's a text file. You can see how various virtual disk sectors are mapped to various physical disk sectors, to "zero", or to
PhysicalDisk-pt.vmdk. (Please do check that you can find a section that matches this; if not, something went wrong in
VBoxManage, and you should delete both .vmdk files.)
dd might not be necessary; but I ensure that whatever's in GPT is not "accidentally" changed by VirtualBox's
Could one map even these 40 initial and 33 trailing sectors to the actual physical disk? Sure. But, why risk anything?
I decided to reuse
EFI System Partition. I could have just as easily used the system partition; Apple ships a HFS+ driver, so the EFI subsystem can boot directly from the system partition.
The thing is, Ubuntu can't write to the HFS partition, so it's slightly easier to reuse the
EFI System Partition.
What am I risking? Well, Apple might wipe this partition clean in an OS update. I hope they won't.
IMPORTANT: The following can mess up GRUB. I can still boot using the "BIOS" GRUB2, but your mileage may vary.
What follows is inspired by Rod Smith's EFI-Booting Ubuntu on a Mac.
- Boot physical Ubuntu.
sudo apt-get install grub-efi– This removed
grub-pcon my machine, although I still seem to have the ability to boot using BIOS. (Anything else would be… troublesome.)
sudo mkdir /boot/efi– This is the place where we'll mount the
EFI System Partition.
sudo mount /dev/sda1 /boot/efi
sudo mkdir -p /boot/efi/EFI/Ubuntu– Apple doesn't ship an
\EFIfolder. We'll create it, along with the "vendor" directory for Ubuntu.
sudo grub-install /dev/sda1– This should install
ls -al /boot/efi/EFI/Ubuntu– You should see two files from Ubuntu here.
It's important to understand: 32-bit Ubuntu installs 32-bit GRUB2. This will not be bootable on a 64-bit capable Mac. This is solely useful for VirtualBox.
So, ensure that you can still use the BIOS GRUB2, or have an alternative boot method, or else you're now converting your physical installation into a VirtualBox-only installation!
Creating virtual machine
I don't have a script for this one. Go back to OS X, go to VirtualBox GUI and create an Ubuntu-type virtual machine. Don't pick the 64-bit version; this changes the type of EFI that the virtual machine will use!
Pick the previously created
PhysicalDisk.vmdk while creating the machine.
Now edit the settings. Right click on machine name, pick "Settings", and change the machine to be an EFI machine on the System tab. So: right click [machine name]->Settings->System->Motherboard->Enable EFI (special OSes only).
Don't boot yet! Did you
chmod the disk devices? Remember, you rebooted. Please
sudo chmod 777 all partition devices in
/dev (and be mindful that this is a security hole you're creating, which you might somehow avoid with UNIX user groups, but meh).
After this point, do not recreate the
PhysicalDisk.vmdk without keeping in mind that this file includes disk image IDs in several places. VirtualBox keeps track of the disk images, and will NOT be happy if the ID changes.
So, done now? Great. Boot.
You'll be shown the EFI shell. Hoorah!
Now, let's change to the
EFI System Partition's filesystem and boot GRUB2.
fs0: cd EFI\Ubuntu boot.efi
This should show you your physical machine's GRUB menu and the booting should move on. Observe the disk light on the bottom of VirtualBox's window; if it stops flickering for longer than 15 seconds, and Ubuntu does not boot, you can presume you have some sort of an issue.
Note that virtual machine does have different hardware than your physical machine; for example, NVIDIA graphics driver does not work for me. I get the console, but not X11. It would be trivial to fix (replace the selected driver with
vesa or something similar in
Xorg.conf) but I don't care: I need to SSH into the machine and tunnel X11 to XQuartz on OS X. I don't need Unity: I need the ability to work on my code and display the X windows.
So, this works for me. Huzzah!
Add this to
/etc/fstab (based on Rod Smith's post, too):
/dev/sda1 /boot/efi vfat ro,fmask=133 0 0
Alternatively, change that
rw to get the partition to mount read-write; this may be important for grub updates.
Ubuntu 12.04 also ships with
sudo mount /boot/efi -o remount,rw # if not already mounted read-write sudo apt-get install grub-efi-amd64
Don't forget to change machine type to "Ubuntu (64-bit)" to update the EFI type.
grub-efi-amd64 conflicts with
grub-efi, so you'll end up losing the 32-bit version of the boot loader. This may or may not conflict with ability to boot from BIOS/CSM – I didn't test this yet.