Wednesday, June 3, 2015

VirtualBox autostart on boot

I run a number of VMs inside VirtualBox on a server - while I've got VMware, I'm more familiar with VirtualBox from working with it for Windows VMs on my mac, as well as development using Vagrant and so forth... so I use VirtualBox for some small test systems. Also, I'm told VMware has specific hardware requirements, and although I've got a stack of enterprise-level servers, they're noisy and generate a surprising amount of heat. In order to avoid having to set up the air conditioning in my computer room, I run my VMs on a decent desktop-class system which is quiet and generates much less heat. Decent trade-off.

After rebooting I discovered my VMs weren't running, so I took a look online to figure out how to do this, and many places pointed to the same blog post:

http://lifeofageekadmin.com/how-to-set-your-virtualbox-vm-to-automatically-startup/

While the procedure worked, it wasn't perfect, so I'm posting my modified version of the procedure here, mostly for my own reference. (run these commands as the user who owns the VBox VMs which will be autostarted.)
  1. Create the file /etc/default/virtualbox with the following contents:

    sudo bash -c 'cat << EOF > /etc/default/virtualbox
    # virtualbox defaults file
    VBOXAUTOSTART_DB=/etc/vbox
    VBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg
    EOF'

    *NOTE:
    Don't use vbox.cfg as found in the above link. Apparently some VirtualBox scripts look for that file for other purposes.

  2. Make sure your user is a member of the vboxusers group:

    if ! groups | grep -w nobody; then sudo usermod -aG nobody $LOGNAME; echo Log out; fi


    If you see "Log out" as the output of the above command, log out and back in so your group membership is updated. If you're okay, you should see the output of the groups command.

  3. Create the /etc/vbox directory and the autostart.cfg file:

    sudo mkdir -p -m 1775 /etc/vbox
    sudo chgrp vboxusers /etc/vbox
    sudo bash -c "cat << EOF > /etc/vbox/autostart.cfg
    # Default policy is to deny starting a VM, the other option is "allow".
    default_policy = deny
    # Create an entry for each user allowed to run autostart
    $LOGNAME = {
      allow = true
    }
    EOF"


    You set the sticky bit (the "1" in the "1775" to keep users in the vboxusers group from deleting the files aside from their own. This is how the /tmp directory works - 1777 and users can only delete their own files.

  4. Set the autostart property via VBoxManage

    vboxmanage setproperty autostartdbpath /etc/vbox


  5. Modify the appropriate VM with autostart enabled:

    vboxmanage modifyvm Graphite --autostart-enabled=on


  6. Restart the vboxautostart-service"

    systemctl restart vboxautostart-service

  7. Restart your system and confirm that the VMs have started:

    vboxmanage list runningvms

No comments: