Mikkel Høgh

Coding the web since 1999

23 Dec 2010

Run VirtualBox virtual machines on boot in Mac OS X

To celebrate the launch of VirtualBox 4.0, I’d like to share a simple trick for making your virtual machines start automatically when your computer boots.

I have a Mac mini server that has a couple of virtual machines running. Until recently, I used VMWare Fusion with a wonky setup with a user that autologins and has VMWare Fusion as a login item, but that has proven to be cumbersome and error-prone, and since both of those virtual machines are servers, there’s really no reason they should be run graphically.

Thus, I switched to VirtualBox and set it up to run automatically on boot. In this example, revealit.dk is my company, virtual is the user account on the server that owns/runs the virtual machines and Chestnut is the name of the virtual machine in question.
The virtual server config resides in /Users/virtual/VirtualBox VMs/chestnut

  1. Set up your virtual machine as you please using the VirtualBox Manager GUI app (or whatever tool you prefer).
  2. Shut down the virtual machine (ie. close the window with its screen content). To be completely safe, turn it off completely.
  3. Create and edit /Library/LaunchDaemons/dk.revealit.ChestnutVirtualbox.plist setting it up something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>dk.revealit.ChestnutVirtualbox</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/bin/VBoxHeadless</string>
    <string>-s</string>
    <string>chestnut</string>
  </array>
  <key>UserName</key>
  <string>virtual</string>
  <key>WorkingDirectory</key>
  <string>/Users/virtual</string>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>
  1. Edit the details to match your system. Make sure that the label matches the filename.
  2. Run sudo launchctl load -w /Library/LaunchDaemons/dk.revealit.ChestnutVirtualbox.plist to load the launchd service. This will cause launchd to keep the virtual machine running indefinitely.

And that’s it. Be aware that you must now use launchctl stop dk.revealit.ChestnutVirtualbox should you want to stop the virtual machine. Stopping it in any other way will just cause launchd to restart it.

Tags