Skip to content

Instantly share code, notes, and snippets.

@reidransom
Created July 19, 2013 20:13
Show Gist options
  • Save reidransom/6042016 to your computer and use it in GitHub Desktop.
Save reidransom/6042016 to your computer and use it in GitHub Desktop.
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

@schafdog
Copy link

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
Copy link

antman2 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
Copy link

rdeguzman commented Mar 14, 2017

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

@CommChap
Copy link

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
Copy link

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
Copy link

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

virtualbox

@alst-ovh
Copy link

alst-ovh 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
Copy link

deisi 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.

@graycreate
Copy link

<?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>

This still could work, thanks for all.

@rraymondgh
Copy link

For those that like to automate to setup, following ansible tasks will configure MacOS host

ansible
    - name: vm autostart config exists
      ansible.builtin.file:
        path: /etc/vbox
        state: directory
        recurse: true
      become: true

    - name: vm autostart config
      ansible.builtin.copy:
        dest: /etc/vbox/autostart.cfg
        content: |
          default_policy = deny
          {{ ansible_user_id }} = {
          allow = true
          }
      become: true

    - name: get vm autostart plist
      community.general.xml:
        xmlstring: "{{ lookup('ansible.builtin.file', '/Applications/VirtualBox.app/Contents/MacOS/org.virtualbox.vboxautostart.plist') }}"
        xpath: /plist/dict/key[.='Disabled']/following-sibling::*[1]
        state: absent
      register: autostart_xml

    - name: vm autostart plist, enable
      community.general.xml:
        xmlstring: "{{ autostart_xml.xmlstring }}"
        xpath: /plist/dict/key[.='Disabled']
        insertafter: true
        add_children:
          - "false"
      register: autostart_xml

    - name: vm autostart plist
      ansible.builtin.copy:
        dest: /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist
        content: "{{ autostart_xml.xmlstring }}"
      become: true

I'm using vagrant to create my vm. Following excerpt will set vm properties

Vagrant.configure("2") do |config|
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--autostart-enabled", "on"]
    vb.customize ["modifyvm", :id, "--autostop-type", "poweroff"]
  end
end

This works with VirtualBox 6.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment