Virtualbox 4.2 VM autostart on Debian Squeeze & Wheezy

One new feature of VirtualBox 4.2 is that it has support for auto-starting vm’s on bootup of the host server (via init etc). This means I can remove my hackish ‘su – vbox -c “VBoxHeadless –startvm VMName &”‘ additions in /etc/rc.local, and the VM’s will also hopefully be terminated gracefully on shutdown.

The docs/guides online which I could find were a bit cryptic, or incomplete, so here’s what I ended up doing :

Edit /etc/default/virtualbox and add in the following two lines :

 

VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg

 

Then create /etc/vbox/autostart.cfg with :

 

# See e.g. https://www.virtualbox.org/manual/ch09.html#autostart
default_policy = deny
vbox = {
    allow = true
}

 

(My virtual machines run under a user called ‘vbox’).

Finally, set the VMs to autostart and so on – so as the user who the VMs should run as (in my case, vbox) run the following commands :

 

VBoxManage setproperty autostartdbpath /etc/vbox
VBoxManage modifyvm NameOfVirtualMachine --autostart-enabled on
VBoxManage modifyvm NameOfAnotherVirtualMachine --autostart-enabled on
....

 

Then, as root, run :

/etc/init.d/vboxautostart-service start

And you’ll then see your various VMs running.

14 thoughts on “Virtualbox 4.2 VM autostart on Debian Squeeze & Wheezy”

  1. Hello,

    i got a problem with your “howto” at my Debian Squeeze Server.

    I follow your instructions and at the point of setting the VM to autostart i got an error.

    What i do: e.g:

    my vm is named ns2:

    so i run the commend in the shell: “VBoxManage modifyvm ns2 –autostart-enabled on”

    But the following error appears:

    VBoxManage: error: Adding machine ‘ns2′ to the autostart database failed with VERR_ACCESS_DENIED
    VBoxManage: error: Details: code NS_ERROR_UNEXPECTED (0x8000ffff), component SessionMachine, interface IMachine, callee nsISupports
    VBoxManage: error: Context: “COMSETTER(AutostartEnabled)(ValueUnion.f)” at line 2422 of file VBoxManageModifyVM.cpp

    Hope you can help me dude.

    best regards phil

  2. You need to run the command as the same system user who is running the virtual machines.

    If you are then I suspect your file permissions are wrong somewhere.

    David

  3. Worked like a charm, I changed the user and location to my user, and had no issues at all.
    I was using the hackish VBoxManage command, placed in the rc.local.

  4. phil,
    I had the same problem and found the solution on another blog.
    assuming your virtual machine will be started by the vbox user and you did all the steps before
    vbox@machine $ VBoxManage modifyvm ns2 –autostart-enabled on
    run this
    rootorsudo@machine # chgrp vboxusers /etc/vbox
    rootorsudo@machine # chmod 1775 /etc/vbox

    then run your
    vbox@machine $ VBoxManage modifyvm ns2 –autostart-enabled on

    David, thanks for your article

  5. Hi… does this script make them auto shutdown as well during host shutdown or do we need to add something else?

    Cheers.

  6. Hi -
    It doesn’t seem to.

    The init script (/etc/init.d/vboxautostart-service) doesn’t (yet) support a ‘stop’ action. So I’m pretty sure my VMs are shutdown by the VBoxHeadless command being kill’ed by a generic init-script.

  7. Great recipe!
    With the solution from Ion, it worked like a charm and my customer is very very satisfied!
    And i’m proud of the end result (one debian 7 server, one autostarting vbox xp autostarting as FileMaker host of the databases and client server access solution with X2Go/Vbox/Win7).
    This is like the cherry on the pudding ;-)
    André

  8. Hi David. Thanks for posting this, it’s an excellent script. As you note, VirtualBox’s autostart service doesn’t cover the stop case, so VMs are killed in their tracks. Not good. So I’m doubly grateful for your script.

    I ran into an issue with one of my Windows VMs where it didn’t want to shut down. In the event of a server shutdown, I can’t have the script waiting forever for the VM. So I modified the script to take an adjustable timeout, after which the VM would be powered off.

    I’d like to contribute the changes back to you, but I don’t see a way to email you. You can email me if you’d like to get a copy. My email’s now in your blog database.

  9. When I enter command : VBoxManage modifyvm server –autostart-enabled on,
    I get this error : VBoxManage: error: Unknown option: –autostart-enabled

    Need help

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>