64

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.

1
  • 1
    @SergiyKolodyazhnyy This question is not a duplicate of that, by any means. May 5, 2017 at 8:56

14 Answers 14

66

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

8
  • how to start a particular virtual machine using startup script
    – Beginner
    Sep 26, 2014 at 7:36
  • 1
    My 14.04 claims that there's no such service as 'vboxautostart-service', have you tested this?
    – Arronical
    Jun 23, 2015 at 16:24
  • @Arronical The OP and the instructions from which the answer was sourced were both for Ubuntu 12.04. YMMV with versions after that.
    – kdmurray
    Jun 25, 2015 at 22:36
  • It turns out that it virtualbox 4.3.10 doesn't download the init scripts on 14.04, but ndasuser's answer below helped me fix that. +1s all round!
    – Arronical
    Jun 26, 2015 at 8:26
  • I was trying make this solution work on ubuntu 15.10 with no sucess.. I ended using this post freesoftwareservers.com/index.php/2015/10/24/… .. maybe not the correct approach, but it works
    – mozart_ar
    Apr 17, 2016 at 1:42
18

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.

2
  • 2
    In version 5.2 you have to edit vboxautostart-service and replace vboxdrv in the # Required Start / # Required Stop lines by virtualbox. It seems that they renamed the service, so the OS cannot detect start/stop priorities correctly. I think you don't need step 6c if you replaced those lines.
    – fishbone
    Oct 5, 2018 at 4:55
  • Thank you so much @fishbone your comment was the trick lacking to me Dec 16, 2018 at 3:05
11

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.

7

After many unhappy hours trying to get the procedures in from kdmurray above to work, without success I eventually found a method that worked simply.

First my Linux host (Mint 17) was set up at boot time to login automatically to my main account.

Second I used the facilities in VirtualBox GUI (v4.3.12) to create a shortcut for each Virtual Machine on the desktop. {right-click the VM in left column & select "Create Shortcut on Desktop"}

Next: I had already found that bringing up Menu->Control Centre -> Startup Applications -> Add and adding the desktop shortcuts gave files that did not work, I spotted from other answer on this page that the location of the startups is /home/USERNAME/.config/autostart so I did a right-click on each of the desktop VM shortcuts and then pasted them into that folder, overwriting the earlier crap files. Now they developed the 'pretty' VirtualBox icons.

And sure enough on restarting the host computer, the 3 VMs started automagically.

:-)

Bill Williams

PS: alas I still have something missing, because often the Virtual Machines will not start up on boot, instead they generate an error dialog box which says they are locked, even if I have told them to shutdown before the host was re-booted.

1
  • 2
    FYI there is no "answer 5". The number you see on the left of each answer is the total vote count. Having said that, yours is a good alternative GUI method. Thanks for the contribution.
    – hmayag
    Aug 24, 2014 at 20:42
5

In addition to the good description of the vboxautostart-service above, you can do

VBoxManage modifyvm server --autostop-type savestate

to automatically stop the guest at shutdown. For Ubuntu 14.04 I had to add a sleep 30 to the vboxautostart-service stop section, so that it waits until guest has been saved completely.

2

I found a simpler way under Ubuntu 16.04 running VBox 5.1.34.

While in VBox, right-click on the running or idle VM in the list, select 'Create Shortcut on Desktop". Once you see the icon on your desktop, open with any tool that let's you see the underlying command VBox created (on my system, I right click and select 'properties') Copy this to the clipboard.

Open the program to update 'startup applications' and create a new application that starts at boot time, pasting in the command you copied from the desktop link created by VirtualBox.

1

To start a VM on logging in to our session we may want to define a .desktop file:

When this file was copied or moved to our autostart directory it will then be executed after we log in:

Running Virtual Box in user space may help to avoid unwanted side effects of starting them during boot (e.g. the VM runs as root, boot time increases incredibly, ...).

1

This is how i start my Centos (name of my virtual machine) when my host machine reboots. I use crontab for that job. Here is my crontab entry:

@reboot VBoxHeadless --startvm "Centos" &
1

This can be done using VirtualBox Command Line Management Interface. Just run the following script at start up.

#!/bin/bash
VMUSER="vmuser"
VMNAME="RedHatVM" #also you can put UUID in this variable (VMNAME="4ec6acc1-a232-566d-a040-6bc4aadc19a6")

case "$1" in
    start)
        echo "===Starting VirtualBox VM==="
        sudo -H -u $VMUSER VBoxManage startvm "$VMNAME" --type headless
        ;;
    stop)
        echo "===Saving state of Virtualbox VM==="
    sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" savestate
    sleep 20
        ;;
    shutdown)
        echo "===Shutting down Virtualbox VM==="
        sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" acpipowerbutton
    sleep 20
        ;;
    reset)
        echo "===Resetting VirtualBox VM==="
         sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" reset
        ;;
    status)
         echo -n "VMNAME->";sudo -H -u $VMUSER VBoxManage showvminfo "$VMNAME" --machinereadable |grep "VMState="| cut -d "=" -f2
        ;;
        echo "Usage: /etc/init.d/VMscript {start|stop|shutdown|reset|status}"
        exit 1
        ;;
    esac

exit 0

There is a nice tutorial for more info.

1
  • Welcome .. Please Copy / paste important info from the link
    – storm
    May 27, 2016 at 19:55
1

Addon to @kdmurray answer.

If you get error on:

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

To solve this, I had to manually create files "username".start and "username".stop with:

sudo touch "username".start
sudo touch "username".stop

And change owner with:

sudo chown "username" "username".start
sudo chown "username" "username".stop

rerun:

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

then restart service:

sudo service vboxautostart-service stop
sudo service vboxautostart-service start

check is all is ok with:

top

and you should see "VBox..." process in a list, if virtual machine is running

1

On my Ubuntu 20 desktop machine:

Open crontab:

crontab -e

Add this line at the bottom:

@reboot sleep 10 && VBoxHeadless --startvm "MyVirtualMachineName" &

Reboot to confirm it works.

1
  • damn... this is good and simple Nov 22, 2023 at 14:18
0

You could also get the gnome session manager described here. Gnome Session Manager It is in the sofware center or you could just go to a terminal and type sudo apt-get install gnome-session-common

Next you can create a little script to run and use the gnome session manager to start it. Here is a little sample script to get you going...

#!/bin/sh

#vboxmanage startvm "my virtual machine" --type=headless|gui|sdl (one of those)
# So if your username is user, and virtualbox put your VMs in the default location, and you want headless...

vboxmanage startvm '/home/user/VirtualBox VMs/myvmname' --type=gui
0

For Ubuntu server 16.04.1 LTS I solved this by doing:

First activate the rc-local.service

sudo systemctl enable rc-local.service

Add the following line in /etc/rc.local

su virtual_box_user -c "VBoxHeadless -startvm 'virtual_machine_name' &"

Note that the virtual_box_user is the one that can see the virtual machine (that you are willing to start) executing the command:

VBoxManage list vms

Example:

$ VBoxManage list vms
"virtualbox1" {2eb3ba3b-bcb3-4515-95d9-d33bce214c28} 
"virtualbox2" {62365634-f2q4-40c0-b4ed-22f4ab84441a}

The modified /etc/rc.local should look like this

#!/bin/sh -e
# 
# rc.local 
# 
# This script is executed at the end of each multiuser runlevel. 
# Make sure that the script will "exit 0" on success or any other 
# value on error.
# 
# In order to enable or disable this script just change the execution
# bits. 
# 
# By default this script does nothing. 

su bob -c "VBoxManage startvm 'virtualbox1' --type headless &" 
su bob -c "VBoxManage startvm 'virtualbox2' --type headless &" 

exit 0 

If you don't choose the right owner of virtualbox, you should see the error typing the command:

sudo systemctl status rc-local.service

I hope this can help

0

1 - Create a file:

nano /etc/init.d/vm-start

Paste this:

#!/bin/bash
/bin/echo "* Starting VMS..."
/bin/sleep 1
/bin/su validuser -c "/usr/bin/vboxmanage startvm MACHINENAME--type headless"
/bin/sleep 1
  • Replace "validuser" with your username
  • Replace "MACHINENAME" with your virtual machine name
  • If you need to start more than 1 machine, repeat the line above with your virtual machine name:

/bin/su validuser -c "/usr/bin/vboxmanage startvm MACHINENAME2--type headless" /bin/sleep 1

2 - Make file vm-start executable:

chmod +x /etc/init.d/vm-start

3 - Edit rc.local

nano /etc/rc.local

Add this:

#!/bin/bash
/etc/init.d/vm-start
exit 0

4 - Reboot.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .