How to start Virtual box machines automatically when booting?

I have many Virtual box machines in my Ubuntu12.04, each time i shutdown or reboot i have to start them one by one.

I wanna a help in writing script for automatically autostart the VBox machines when booting.


Solution 1:

You can use the VirtualBox Auto-start service. A good tutorial describing how to do this is posted on the "Life of a Geek Admin" blog.

The following steps are adapted from the linked blog post:

  1. First you need to create the file /etc/default/virtualbox and add a few variables.

    VBOXAUTOSTART_DB which contains an absolute path to the autostart database directory and
    VBOXAUTOSTART_CONFIG which contains the location of the autostart config settings. The file should look similar to this:

    # virtualbox defaults file
    VBOXAUTOSTART_DB=/etc/vbox
    VBOXAUTOSTART_CONFIG=/etc/vbox/vbox.cfg
    
  2. Now we need to create the /etc/vbox/vbox.cfg file and add

    # 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
    myuserid = {
    allow = true
    }
    

    Note: If the filename vbox.cfg doesn't work above, try naming it autostart.cfg.

    If you are the only user you can just add the line default_policy = allow to the vbox.cfg file.

  3. Set permissions on directory to the vboxuser group and make sure users can write to the directory as well as sticky bit.

    sudo chgrp vboxusers /etc/vbox
    sudo chmod 1775 /etc/vbox
    
  4. Add each of the users to the vboxusers group.

    sudo usermod -a -G vboxusers USERNAME
    

    (replace USERNAME with the username)

NOTE: If you have changed group permissions for the current user, log out and back in again to refresh the permissions. (credit @kR105)

  1. Every user who wants to enable autostart for individual machines has to set the path to the autostart database directory with

    VBoxManage setproperty autostartdbpath /etc/vbox
    

    and enable autostart for an individual VM with

    VBoxManage modifyvm <uuid|vmname> --autostart-enabled on
    

    This will create a myuserid.start file in /etc/vbox directory

  2. Now restart the vboxautostart-service to read in the changes.

    sudo service vboxautostart-service restart
    
  3. Reboot your system and your VM should start

Solution 2:

I had similar unhappy incidents trying this operation on the vanilla LTS.

~$ cat /etc/os-release 
NAME="Ubuntu" 
VERSION="14.04.1 LTS, Trusty Tahr" 

On this version, the key file /etc/init.d/vboxautostart-service was not installed.

As far as I know all the VitualBox and requirements were put in by apt-get, so I cannot say why the 'vboxautostart-service' file was not also provided. But to get over this here are my update to kdmurray's post.

1) /etc/default/virtualbox file existed for me. So must add vars:

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

2) Must create /etc/vbox/autostart.cfg as indicated by OP.

6b) Need to get a vboxautostart-service script and make it executable.

cd /etc/init.d/
sudo wget http://www.virtualbox.org/browser/vbox/trunk/src/VBox/Installer/linux/vboxautostart-service.sh?format=raw -O vboxautostart-service
sudo chmod +x vboxautostart-service

6c) Alert the rc.d controller, but I used 24 as the start time. Putting just 20 and it did not start up. Perhaps it ran even before virtualbox was working.

sudo update-rc.d vboxautostart-service defaults 24 24

Then rebooting launched the VM correctly.

Solution 3:

You can use vboxmanage startvm "my virtual machine" --type=headless|gui|sdl (one of those). Use "headless" if they're servers that you connect to by other means than using the gui.

To actually run these commands at the right time during boot, you'll want to read up on Upstart.