Auto-start VirtualBox VMs (headless) after reboot on Mac OSX

Image of Mattias Geniar

Mattias Geniar, November 27, 2019

Follow me on Twitter as @mattiasgeniar

I’ve got a Mac Mini at home to act as a server & desktop. On it there are several Virtual Box VMs, among which a a pihole to block unwanted requests via DNS. It’s pretty vital these run at all time.

Every time I reboot this machine (for updates), I want the VMs to auto-start. Preferrably headless, so it’s purely a background service. You don’t need a GUI when you have SSH. 😉

Note: since disks are by default encrypted, you need to log in to your machine (to decrypt the disk) before the launch daemon can start your VMs. If your server reboots as a result of a power loss, it needs manual intervention for that initial login!

Auto-starting VirtualBox VMs on macOS Catalina

The steps are pretty straight-forward.

You can create a launch daemon that starts the VirtualBox VM in headless mode. You do so by creating a specific plist file. Here’s one example.

The path /Users/mattias should be changed to your users’ home directory. You can find your username by running whoami in the terminal.

$ cat /Users/mattias/Library/LaunchAgents/org.virtualbox.launch.ubuntu-1804-pihole.plist
<?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>org.virtualbox.launch.ubuntu-1804-pihole</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Applications/VirtualBox.app/Contents/MacOS/VBoxManage</string>
		<string>startvm</string>
		<string>ubuntu-1804-pihole</string>
		<string>--type</string>
		<string>headless</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>

A few things are important here: the filename contains the string “ubuntu-1804-pihole”. That’s the name of my VM.

That name is also present in the array of ProgramArguments and in the string-value of the Label key, as the name of the VM to start.

In other words: copy/paste your VM’s name in there. It should be changed in the filename & twice in the XML.

To find your VM name, run this command in the terminal:

$ VBoxManage list vms
"ubuntu-1804-mediaserver" {9b85bf53-7e9f-4120-b88a-277eec324cfe}
"ubuntu-1804-webserver" {342565be-abdd-46db-9274-919010bb5dbf}
"ubuntu-1804-pihole" {2538be02-0a41-426a-9876-69cfa30625fd}

The first quoted value is your VMs name.

To test that the config works, try to launch it manually first:

$ launchctl load /Users/mattias/Library/LaunchAgents/org.virtualbox.launch.ubuntu-1804-pihole.plist

As a result, the VM should be now booted in headless mode.

Now reboot your Mac to test if this config worked! On boot, Mac will look at all launch daemons in /Users/mattias/Library/LaunchAgents/ and execute them.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.