Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Auto-starting VirtualBox VMs on OS X

Auto-starting VirtualBox VMs on OS X

After finding a lot of other posts on the topic that didn't work out for me this one did the trick so I'm reposting for my own sense of self preservation.

Link to original article.

Copy the Virtualbox autostart plist template file to your system's LaunchDaemons folder.

sudo cp \
    /Applications/VirtualBox.app/Contents/MacOS/org.virtualbox.vboxautostart.plist \
    /Library/LaunchDaemons

Then edit /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist set Disabled to false, set KeepAlive to true, and confirm the last string entry in the command array is set to /etc/vbox/autostart.cfg. The file should look 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>Disabled</key>
<false/>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>org.virtualbox.vboxautostart</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/VirtualBox.app/Contents/MacOS/VBoxAutostartDarwin.sh</string>
<string>--start</string>
<string>/etc/vbox/autostart.cfg</string>
</array>
</dict>
</plist>

Make the directory /etc/vbox and create the file /etc/vbox/autostart.cfg with the following content:

default_policy = deny
osxusername = {
allow = true
}

Make sure to change osxusername to the username on your system that the VMs are under.

Next properly set permissions:

sudo chmod +x /Applications/VirtualBox.app/Contents/MacOS/VBoxAutostartDarwin.sh
sudo chown root:wheel /etc/vbox
sudo chown root:wheel /etc/vbox/autostart.cfg
sudo chown root:wheel /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist

Now, configure the VMs that should automatically start and set how they should be stopped:

VBoxManage modifyvm vmname --autostart-enabled on
VBoxManage modifyvm vmname --autostop-type acpishutdown

Finally, test the configuration by running:

sudo launchctl load /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist

After a reboot, the VMs that have been set with autostart enabled should be running!

References

https://www.virtualbox.org/manual/ch09.html#autostart-osx

https://forums.virtualbox.org/viewtopic.php?f=8&t=51593&start=15#p240724

https://forums.virtualbox.org/viewtopic.php?f=11&t=51529#p236492

@smradman

This comment has been minimized.

Copy link

commented Feb 25, 2014

Brilliant!

This worked like a charm.
With VirtualBox 4.3.6 r91406 (or before) there seem to be changes to the org.virtualbox.vboxautostart.plist and VBoxAutostartDarwin.sh though.
The --start argument is no longer required (and the default org.virtualbox.vboxautostart.plist has different defaults).
You might want to update the relevant part in your article.

I just love to have a Windows VM on my Mac that won't stop working ever ;-)

Thanks
Stefan

@cdezaz

This comment has been minimized.

Copy link

commented May 22, 2014

This is a great article and really helped. Thank you for putting it together.

I have installed VirtualBox 4.3.12 r93733 and found that the arguments to the VBoxAutostartDarwin.sh shell have been changed. You no longer need the "--start" option. You just need the config option for the shell command. Not sure which version of VirtualBox this changed in, but the above plist file fails if the --start option is present.

Thanks.
Craig

@andrew-dixon

This comment has been minimized.

Copy link

commented Jun 5, 2014

Followed this however it doesn't start on boot, only if I run the command:

sudo launchctl load /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist

I have checked everything and it all appears to be correct. I'm using 4.3.12 so I don't have "--start" as suggested by the other comments, however it still doesn't start on boot.

Any suggestions?

@droizenberg

This comment has been minimized.

Copy link

commented Jun 9, 2014

I still got the same problem. Nothing happened. Virtualbox 4.3.12 and MacOS 10.9.3.

Any things that i can check? I'm new to Mac.

@andrew-dixon

This comment has been minimized.

Copy link

commented Jun 10, 2014

I got it to work by changing the contents of the /etc/vbox/autostart.cfg to:

default_policy = allow
@thomasgruebler

This comment has been minimized.

Copy link

commented Feb 26, 2015

For me it's not working.
OSX 10.10.2 Yosemite Server

Vboxmanage lists the VM's as autolaunch, but they are not launching.

When I try out after reboot i says that it's running: /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist: Operation already in progress
But when Virtualbox GUI is not showing any running machine.
Also tried the "default_policy = allow".

@lhower

This comment has been minimized.

Copy link

commented Mar 12, 2015

ThomasGruebler(or others): I am having the same problem, since an upgrade to Yosemite. I am able to get autostart working, in a fashion, but don't know how to proceed towards a permanent fix. Do you have any ideas?

First, setting default_policy to allow does not seem to be any help, as you note. When the system boots, with the "stock" plist scripts installed and configured, I get the mac time-ticking icon, flashing through my desktop background, during mouse/screen interaction, and no autostart. That started me thinking it was a startup timing issue.

I placed a sleep 60 in the script called from the autostart plist: /Applications/VirtualBox.app/Contents/MacOS/VBoxAutostartDarwin.sh. What is interesting now is that if ANY users log in within 60 seconds of bootup, the vbox autostarts. If no user logins in, the vbox does not start... but I no longer have the mac time-tick icon spinning around.

Does any of this give you any ideas on how to fix the root problem?

--LHower

@jgentsch

This comment has been minimized.

Copy link

commented Mar 16, 2015

I adapted /Applications/VirtualBox.app/Contents/MacOS/VBoxAutostartDarwin.sh to include a check for the VirtualBox-Kernel-Extensions and now it works just fine for me:

... start of skript ommitted ...
function waitForKernelModules()
{
  VBOX_EXT=0
  CHECK_CNT=0

  until [[ $VBOX_EXT -eq 1 || $CHECK_CNT -gt 60 ]]; do
    echo Check for VBox-Kernelmodules - ${CHECK_CNT}

    sleep 1
    VBOX_EXT=1
    CHECK_CNT=$(( ${CHECK_CNT} + 1 ))

    if kextstat -lb org.virtualbox.kext.VBoxDrv 2>&1 | grep -q org.virtualbox.kext.VBoxDrv; then
      echo "Found org.virtualbox.kext.VBoxDrv. Good."
    else
      VBOX_EXT=0
    fi
    if kextstat -lb org.virtualbox.kext.VBoxUSB 2>&1 | grep -q org.virtualbox.kext.VBoxUSB; then
      echo "Found org.virtualbox.kext.VBoxUSB. Good."
    else
      VBOX_EXT=0
    fi
    if kextstat -lb org.virtualbox.kext.VBoxNetFlt 2>&1 | grep -q org.virtualbox.kext.VBoxNetFlt; then
      echo "Found org.virtualbox.kext.VBoxNetFLT. Good."
    else
      VBOX_EXT=0
    fi
    if kextstat -lb org.virtualbox.kext.VBoxNetAdp 2>&1 | grep -q org.virtualbox.kext.VBoxNetAdp; then
      echo "Found org.virtualbox.kext.VBoxAdp. Good."
    else
      VBOX_EXT=0
    fi
  done
}

CONFIG=${1}
waitForKernelModules
vboxStartStopAllUserVms "start"
trap vboxStopAllUserVms HUP KILL TERM
@lhower

This comment has been minimized.

Copy link

commented Mar 20, 2015

jgentsch, thanks, I tried your function addition in place of the sleep 60 I was using. Your patch did not help my case. I expect that your function completes its first try. In that case, it is fully completed at boot time. The script does work fine if I run it from a shell after I log in, and it starts the VMs.

So for my case, running VBoxAutostartDarwin.sh does not start any VMs unless some user, local or network, has logged in the newly booted system before it runs. My sleep 60 in VBoxAutostartDarwin.sh gives me window after boot, but before VBoxAutostartDarwin.sh does its real work, to accomplish that. If there is log in, the VMs start. If not, they don't.

Any ideas?

@rvendrame

This comment has been minimized.

Copy link

commented Jul 31, 2015

Thanks a lot, it worked perfectly!

@iainhouston

This comment has been minimized.

Copy link

commented Aug 10, 2015

@jgentsch solution works for me on Yosemite. Thanks.
If @jgentsch, you could spare a moment to explain how you diagnosed the problem I'd be extremely grateful. All I could see from the system log was that (org.virtualbox.vboxautostart): Service only ran for 5 seconds. Pushing respawn out by 5 seconds. with no clues as to why.

@yuntaek

This comment has been minimized.

Copy link

commented Jan 6, 2016

Thank you. It is really helpful for me. I've forked to translate it for Korean.

@mrJakez

This comment has been minimized.

Copy link

commented Jan 9, 2016

Thanks!

@schafdog

This comment has been minimized.

Copy link

commented Mar 21, 2016

Not working for me with VirtualBox 5 on El Capitan. It doesn't seem like /Applications/VirtualBox.app/Contents/MacOS/VBoxAutostart does anything if I run it manually as a user. I would think that this should trigger starting any VMs marked with autostart-enabled

@rjschie

This comment has been minimized.

Copy link

commented Mar 27, 2016

Yes, I'm getting that too @schafdog. It looks like the VBoxAutostart no longer works in either version 5 or El Capitan or a combination of both.

@excessivedemon

This comment has been minimized.

Copy link

commented Apr 8, 2016

@schafdog, @rjschie - this is fully working on VirtualBox 5.0.16 and OS X El Capitan 10.11.4. Just change /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist to look 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>Disabled</key>
  <false/>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>org.virtualbox.vboxautostart</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Applications/VirtualBox.app/Contents/MacOS/VBoxAutostartDarwin.sh</string>
    <string>/etc/vbox/autostart.cfg</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>LaunchOnlyOnce</key>
  <true/>
</dict>
</plist>
@ricky-wong

This comment has been minimized.

Copy link

commented Apr 18, 2016

@excessivedemon it looks like the only difference (compared to OP) is the inclusion of RunAtLoad and LaunchOnlyOnce? For me, those keys were already included in the source file (/Applications/VirtualBox.app/Contents/MacOS/org.virtualbox.vboxautostart.plist) after installing VirtualBox 5.0.16 r105871 on OS X 10.10.5 and OS X 10.11.4.

@kcoombs

This comment has been minimized.

Copy link

commented May 17, 2016

jgentsch, thanks. Your script modifications worked for me.

OS X El Capitan 10.11.4; Virtualbox 5.0.20.

@senamaud

This comment has been minimized.

Copy link

commented Jun 6, 2016

@andrew-dixon Thanks! Changing "default_policy" to allow was the solution for me!
Mac OS 10.10.5 with VirtualBox Version 5.0.18

@schafdog

This comment has been minimized.

Copy link

commented Jun 25, 2016

My problem was related to the user. The script looks at local users, and the user was an LDAPv3 user. Modifying the
dscl .
to
dscl /LDAPv3/127.0.0.1
fixed the problem

@antman2

This comment has been minimized.

Copy link

commented Jan 3, 2017

@excessivedemon: Thank you! Your modifications worked for me, but on my system the file was actually: /Library/LaunchDaemons/org.virtualbox.startup.plist

@rdeguzman

This comment has been minimized.

Copy link

commented Mar 14, 2017

Thanks, I confirm the script of @excessivedemon still works on El Capitan + VirtualBox5.1.16 r113841

@CommChap

This comment has been minimized.

Copy link

commented Jun 18, 2017

I followed the instructions to the T but it still is not working for me. I dont get any errors when i type any of the commands. All the files are saved in as .plist (/Library/LaunchDaemons/) or .cfg (/etc/vbox/) in the appropriate folders. However, when I run the sudo launchctrl command nothing happens. What am I doing wrong? I am running MacOS 10.12.15 and VirtualBox 5.1.22.

@carlos-alarcon

This comment has been minimized.

Copy link

commented Oct 31, 2017

Ok this seems to work properly on a Sierra + VirtualBox5.2.0 r118431 (machines start properly on startup)
but seems it did not stop VMs on Mac Mini shutdown
Actually if I run
sudo launchctl list
I cannot see anything like 'org.virtualbox.vboxautostart' which I thought it would be the 'service' started
Seems like it is configured just as a 1 time script to be run on startup

Probably I am missing many things. Any idea?

By the way if I execute:
/Applications/VirtualBox.app/Contents/MacOS/VBoxAutostart --quiet --stop --config /etc/vbox/autostart.cfg
The VM are properly shutdown.
From what I can understand from /Applications/VirtualBox.app/Contents/MacOS/VBoxAutostartDarwin.sh it will capture HUP, KILL or TERM signals to the process to run such command. the problem is the script will not receive any signal because it exists after doing the start.

So Launchd won't send any signal -> it won't stop anything.

@FaimMedia

This comment has been minimized.

Copy link

commented Apr 11, 2018

Why so difficult? Go to System Settings -> Users & Groups -> Login, drag and drop the .vbox file, check the 'hide' box. Done!

virtualbox

@alst-ovh

This comment has been minimized.

Copy link

commented May 5, 2018

Have done it for my Headless Mac Mini Server look at https://github.com/alst-ovh/mac_vboxautostart

macOS High Sierra 10.13.4 and Virtualbox 5.2.10

@deisi

This comment has been minimized.

Copy link

commented Oct 26, 2018

@FaimMedia
This works well for starting the VM but not for stopping. Also it keeps a window of the vm open on screen at all times.

Especially stopping is an Issue. If you try to shut down the host system, the vm will prevent the host from shutting down and instead endlessly wait for user input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.