Creating an OS X Mountain Lion base box in VMware Fusion 5

Before We Begin

Some precautionary notes to be aware of:

A virtual machine with OS X is going to be large. An OS X base box will be approximately 5.65GB. This exceeds the recommended box size Vagrant states for portability. This is unavoidable. Bare it in mind and ensure you have the disk space, in addition to the patience when spawning boxes. Larger boxes take longer to download and clone. As I am doing this for development and administration purposes, the spawn times are acceptable for the sake of repeatable testing. To reduce pull times you might want to host the box on a local HTTP server inside your network rather than on the Cloud, but this is optional.

Your version of Vagrant MUST be 1.3.0 or higher. Support for OS X didn’t exist without work arounds and hacks prior to 1.3.0.

Sanity Check

This article has been tested with the following:

Apple OS X Mountain Lion (Host & Guest)

VMware Fusion 5.0.3

Vagrant 1.3.5

Vagrant’s plugin vagrant-vmware-fusion 2.0.8

Note: An OS X Mavericks Host & Guest will follow the same steps but will require VMware Fusion 6.x at minimum.

Creating an OS X Virtual Machine in VMware Fusion

Create a new virtual machine in VMware Fusion.

If you are unfamiliar with creating a OS X VMware Fusion VM leave requests for a tutorial. Otherwise I am going to make assumptions you know how to do this at present. Things to note when creating the virtual machine in VMware Fusion:

When saving the VM, change the name of the file to mountain64-1_vmware.vmwarevm.

Change the virtual machine hard disk settings not to split the hard drive into 2GB files.

On the first boot of the system, the initial user fields Full Name, Username, and Password are populated with vagrant.

Configuring the Guest OS

All steps should be considered within the Guest OS unless otherwise stated.

Install VMware Tools

As the first step we’ll install VMware Tools to allow for copy/pasting between the Host and Guest.

Setting up password-less sudo

The vagrant user requires password-less sudo permission, run the following in Terminal:

$ sudo visudo

Add or modify the following lines as necessary:

Defaults env_keep += 'SSH_AUTH_SOCK'
Defaults:vagrant !requiretty
# Add the vagrant user at the end of the file
vagrant ALL=NOPASSWD: ALL

Exit and save.

Optimizing the Guest

If we looked at the size of our VMware file at this point we’ll see it at around 16.26GB or 14.11GB, running or shutdown respectively. Shutting down the VM and shrinking it at this point will barely put a dent in reducing the VM size; it would only be decreased the VM by only 0.02GB.

However if we zero out the free space on the guest disk and shrink it we can reduce the VM to approximately 7.48GB.

Open a Terminal and type:

$ diskutil secureErase freespace 0 Macintosh HD
$ sudo halt

Finalizing the Box

From this point on everything done will be on the Host.

Open a Terminal and change directory to the location the Virtual Machine was saved. Typically this is ~/Documents/Virtual Machines/.

Packaging the Virtual Machine

Using tar and gzip to package the virtual machine reduces the box size. After boxed the VM should be around 5.63GB.

Run the following in Terminal:

$ tar -cvzf mountain64-1_vmware.box ./*

After compression has completed. Move the .box file to a more suitable project working directory or a web server. For this example we’ll use ~/workspace/vagrant-example. This folder will likely not exist on your system so create it before trying to move it obviously.

Start Up the Vagrant OS X Base Box

In Terminal change directory to our project workspace, ~/workspace/vagrant-example. Run the following: