Start VBoxHeadless VM at startup

Solution 1:

THIS is what finally worked!

1) Create the startup script file

in /etc/init.d - sudo nano /etc/init.d/StartVM.

Copy Paste the following in the file and replace "My VM Name" for your vm name:

#! /bin/sh
# /etc/init.d/StartVM
#

#Edit these variables!
VMUSER=spode
VMNAME="My VM Name"

case "$1" in
  start)
    echo "Starting VirtualBox VM..."
    sudo -H -b -u $VMUSER /usr/bin/VBoxVRDP -s "$VMNAME"
    ;;
  stop)
    echo "Saving state of Virtualbox VM..."
    sudo -H -u  $VMUSER /usr/bin/VBoxManage controlvm "$VMNAME" savestate
    ;;
  *)
    echo "Usage: /etc/init.d/StartVM {start|stop}"
    exit 1
    ;;
esac

exit 0

2) Give the script executable permission

with sudo chmod +x /etc/init.d/StartVM.

3) Tell script to run at startup.

tell the script be the first to shutdown and the last to startup.

sudo update-rc.d StartVM defaults 99 01

Solution 2:

FYI, This works on Opensuse without nohup.

VBoxHeadless -s "OpenSuSE 11.4 64bit" &

Solution 3:

This is working ok with Ubuntu server 12.04 and VirtualBox 4.2.20.

#! /bin/sh
# /etc/init.d/StartVM
#
#Edit these variables!
VMUSER=username
case "$1" in
  start)
    echo "Starting VirtualBox VM SMARTHOST ..."
    sudo -u $VMUSER VBoxManage startvm SMARTHOST --type headless
    echo "Starting VirtualBox VM wxp-acceso ..."
    sudo -u $VMUSER VBoxManage startvm wxp-acceso --type headless
    echo "Starting VirtualBox VM wmmaq_edi ..."
    sudo -u $VMUSER VBoxManage startvm vmmaq_edi --type headless
    ;;
  stop)
    echo "Saving state of Virtualbox VM SMARTHOST ..."
    sudo -u $VMUSER VBoxManage controlvm SMARTHOST savestate
    echo "Saving state of Virtualbox VM wxp-acceso ..."
    sudo -u $VMUSER VBoxManage controlvm wxp-acceso savestate
    echo "Saving state of Virtualbox VM vmmaq_edi ..."
    sudo -u $VMUSER VBoxManage controlvm vmmaq_edi savestate
    ;;
  *)
    echo "Usage: /etc/init.d/StartVM {start|stop}"
    exit 1
    ;;
esac

exit 0

And

sudo chmod +x /etc/init.d/StartVM

and

sudo update-rc.d StartVM defaults 99 01

as indicated at a previous answer.

Solution 4:

On recent versions of Virtualbox (4.2.0 onwards) you don't need to roll your own script to autostart a VM but it does take some configuration. See section 9.24 of the Virtualbox Manual "Starting virtual machines during system boot"

Unfortunately the manual only gives outline instructions and hasn't been updated in ages. I found this post on the virtualbox forums with some extra detail.

You could just put a line in rc.local to start your server but if you want to do it the "offical" way read on..

Add these lines to /etc/default/virtualbox:

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

Edit /etc/vbox/vboxautostart.cfg (this example denies autostart permission for all users except user "Bob":

# Default policy is to deny starting a VM, the other option is "allow".
default_policy = deny

# Bob is allowed to start virtual machines but starting them
# will be delayed for 10 seconds
bob = {
    allow = true
    startup_delay = 10
}

# Alice is not allowed to start virtual machines, useful to exclude certain users
# if the default policy is set to allow.
alice = {
    allow = false
}

Add vboxusers group to /etc/vbox and sticky bit:

# chgrp vboxusers /etc/vbox
# chmod 1775 /etc/vbox

Add all users who will use virtualbox to the "vboxusers" group, e.g:

# adduser Bob vboxusers

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

Users can then set VMs to autostart and configure how they will stop (e.g savestate, acpishutdown) with:

$ VBoxManage modifyvm <vmname> --autostart-enabled on
$ VBoxManage modifyvm <vmname> --autostop-type acpishutdown

The above worked for me with Virtualbox 5, installed from the virtualbox.org repository.