Mac OS X running as Virtualbox guest – inside Opensolaris container.

With the release of Virtualbox 3.2 beta Mac OS X “guests” are now supported. 

The following guide will show you how to get OS X running inside virtualbox using the command line interface.  It applies to any host operating system, but in this case we’re adding an additional virtualization layer on top of opensolaris.

Officially, so as not to run afoul of the Apple EULA, if you want to run OS X as a Virtualbox Guest you need to be running Virtualbox on Apple hardware.

So, let’s imagine that instead of a nice, 64 core Intel nehalem server I actually have a Mac Pro on which I am – for the sake of argument and somewhat inexplicably – running Opensolaris.

Today we’re not only going to show you how to get Mac OS X running in Virtualbox, but also how to run it on Virtualbox running inside an Opensolaris container, or zone.  Just in case you had, for example, a need to add resource controls, or clone multiple versions of your resulting OS X virtual machine, teleport running VMs to another server…

You will need a copy of a modified version of the OS X install DVD.  If you search around you might locate something called “iDeneb_v1.6_1058_Lite_Edition.iso” which is what I used for this example.

If you don’t care about the Opensolaris container (or indeed the opernsolaris host OS) part of this tutorial, you can use the same sequence to run Mac OSX as a Virtualbox guest on any OS, provided you’ve installed the 3.2 beta version (or later).  You just need to ensure that whatever machine you’re using has a CPU that supports VMX.  Otherwise the OS X Install CD won’t load the Operating system properly.  (Other guests will be fine)

[Grab the beta here]

Click here to skip to the Mac OS X guest configuration section.

Step 1:  Create the opensolaris container

At the time of writing, I was using Opensolaris b133.  The bits I typed are in bold.

1.a.  Define a new zone.

root@nehalem:/# zonecfg -z vbox
vbox: No such zone configured
Use 'create' to begin configuring a new zone.
zonecfg:vbox> create
zonecfg:vbox> set autoboot=true

1.b.  Deciide where it will live.  In this case I chose /zones/vbox.

zonecfg:vbox> set zonepath=/zones/vbox

1.c Add some networking so that you can talk to the world. In this case I have ipmp set up for redundancy. Change to suit your actual network interface. Here, mine is ipmp0, and I’ve given it the address 1.2.3.4.

zonecfg:vbox> add net
zonecfg:vbox:net> set physical=ipmp0
zonecfg:vbox:net> set address=1.2.3.4
zonecfg:vbox:net> end

1.d.  This is the important part.  Add the two virtualbox drivers from the global zone. You can’t install devices in a container, so you need to make sure that the ones that the global zone uses are available to your container.

zonecfg:vbox> add device
zonecfg:vbox:device> set match=/dev/vboxdrv
zonecfg:vbox:device> end
zonecfg:vbox> add device
zonecfg:vbox:device> set match=/dev/vboxusbmon
zonecfg:vbox:device> end

Then all you need to do is create the zone.  This will take a few minutes.

zonecfg:vbox> commit
zonecfg:vbox> exit
root@nehalem:/# zoneadm -z vbox install
A ZFS file system has been created for this zone.
 Publisher: Using opensolaris.org (http://pkg.opensolaris.org/dev/ ).
 Publisher: Using contrib.opensolaris.org (http://pkg.opensolaris.org/contrib/).
 Image: Preparing at /zones/vbox/root.
[...]

Step 2:  Install the Virtualbox package.

This needs to be done in the global zone as well as your newly-created container.  So while our new container is installing, go ahead and grab the latest beta, and install it in the global zone:

root@nehalem:/var/tmp# pkgadd -d VirtualBox-3.2.0_BETA1-SunOS-r60785.pkg
The following packages are available:
 1  SUNWvbox     Oracle VM VirtualBox
 (i386) 3.2.0_BETA1,REV=2010.04.27.22.46.60785
Select package(s) you wish to process (or 'all' to process
all packages). (default: all) [?,??,q]:

Once that’s done and your new container is ready, boot it and you’ll need to install the exact same Virtualbox package in the container, as well as some other packages.

inside the new container:

root@vbox:/# pkg install SUNWuiu8 SUNWgccruntime
[.. output omitted ..]
root@nehalem:/var/tmp# pkgadd -d VirtualBox-3.2.0_BETA1-SunOS-r60785.pkg

At this point, we’re ready to create our Guest.  We’re going to do this using the command line interface, so this will work on any operating system.  It isn’t Solaris specific.

If you’re following along from the top, make sure you’ve logged into the Opensolaris container (use “zlogin <container>”).

You don’t need to be root.  Cut and paste the following lines, being sure to change the green parts as appropriate for your system.

[Note: Since this post was written creating a Snow Leopard capable VM is now a lot simpler.  If you have compatible hardware, you might be interested to check out this post instead. ]

VBoxManage createvm -name OSX -ostype MacOS_64 -basefolder /vbox -register
VBoxManage createhd ---filename /vbox/OSX/OSX.vmdk --size 10240
VBoxManage modifyvm OSX --cpus 8
VBoxManage modifyvm OSX --memory 8192
VBoxManage modifyvm OSX --usb on --usbehci on
VBoxManage modifyvm OSX --acpi on --ioapic on
VBoxManage storagectl OSX --name "SATA Controller" --add sata \
  --controller IntelAHCI
VBoxManage storageattach OSX --storagectl "SATA Controller" \
  --type hdd --port 0 --device 0 --medium /vbox/OSX/OSX.vmdk
VBoxManage storagectl OSX --name "IDE Controller" --add ide \
  --controller PIIX4
VBoxManage storageattach OSX --storagectl "IDE Controller" --port 0 \
 --device 1 --type dvddrive --medium /misc/iDeneb_v1.6_1058_Lite_Edition.iso

Now a well-known issue with earlier versions of Virtualbox was that if you managed to install OS X you might have the guest os complaining that no keyboard was connected.  Indeed, this is what happened to me.  In order to get a Mac OS X guest to run with a keyboard and mouse inside Virtualbox, you need to add the following lines to your configuration:

VBoxManage modifyvm OSX --mouse usb
VBoxManage modifyvm OSX --keyboard usb

Now you can go ahead and boot it up, and install Mac OS X.  You don’t need to change any package selections for the iDeneb CD.  Once you’re done just, reboot and voila!

VBoxManage startvm OSX --type vrdp

To access your OSX console, use VRDP.  Point your RDP client at the IP address you gave your container (or whatever IP address is running Virtualbox as the host if you skipped the Opensolaris part).

[ One thing worth noting - sometimes Mac OS X struggles if you give it more than 1 virtual CPU.  If this happens, set the number of CPUs to 1, and reboot and it should be fine.]

VirtualBox USB from the command line

If you’re one of those fancy-pants GUI users, this post probably isn’t for you.

On the other hand, if you can see the potential that awaits once you learn to control VirtualBox from the command line, you may be interested to know how to add and remove USB devices to your running VM.

Here’s the quick and dirty version:

  1. Ensure you actually have USB support for your target VM:
    # VBoxManage showvminfo "somevm" | grep USB
    USB:             enabled
  2. If it’s not set to “enabled” you’ll have to add USB support to your VM.  You’ll need to power off the VM to do this:

    # VBoxManage modifyvm "somevm" --usb on --usbehci on
  3. To attach a device that’s plugged into the same system as your VM (in my case, a Sony USB memory stick), grab its UID as follows:
    # VBoxManage list usbhost
    Sun VirtualBox Command Line Management Interface Version 3.1.4
    (C) 2005-2010 Sun Microsystems, Inc.
    All rights reserved.
    
    Host USB Devices:
    [...]
    UUID:               2a2c7255-3b90-448e-aa7a-b1c5710ddd79
    VendorId:           0x054c (054C)
    ProductId:          0x0243 (0243)
    Revision:           1.0 (0100)
    Manufacturer:       Sony
    Product:            Storage Media
    SerialNumber:       6A08102832911
    Address:            0x54c:0x243:256:/pci@0,0/pci108e,5347@2,1
    Current State:      Busy
    
  4. Create a usb filter which will tell VirtualBox to provide the USB device to your virtual machine when it’s detected as plugged in on the host:
    # VBoxManage usbfilter add 0 --target "somevm" --name usbstick \
                   --vendorid 054C --productid 0243
  5. Go ahead and power on your Virtual Machine.  You’ll notice that the USB device (if it’s currently plugged in) immediately becomes unavailable on the host.  You can confirm that it’s attached and that you didn’t make a typo with the vendor and/or product IDs:
    # VBoxManage showvminfo "somevm"
    [...]
    Currently Attached USB Devices:
    
    UUID:               582313d4-1d51-41ea-a053-ba5ac552d2e5
    VendorId:           0x054c (054C)
    ProductId:          0x0243 (0243)
    Revision:           1.0 (0100)
    Manufacturer:       Sony
    Product:            Storage Media
    SerialNumber:       6A08102832911
    Address:            0x54c:0x243:256:/pci@0,0/pci108e,5347@2,1

That’s it.  You can mount and unmount this device now inside your VM.

You can do some neat stuff with USB and Virtualbox, but that’s a subject for another post.