I cannot seem to get my VM to run at startup.

I tried the "startup applications" and update-rc.d with no luck.

sudo update-rc.d startvms defaults 99 10

This created all the appropriate symbolic links for the different run levels but the VM still does not start.

Here's my startvms script:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          startvms
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start my VMs at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO
case "$1" in
  start)
        echo "Starting"
        nohup VBoxHeadless --startvm "UbuntuServer" &
        ;;
  stop)
        echo "Stopping $DESC"
        VBoxManage controlvm "UbuntuServer" poweroff
        ;;

  restart|force-reload)
        echo "Restarting $DESC"
        VBoxManage controlvm "UbuntuServer" poweroff
        nohup VBoxHeadless --startvm "UbuntuServer" &
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

What am i doing wrong?

  • Did you upgrade Virtual Box to PUEL to include VRDP support (needed for headless mode)? – Takkat Aug 15 '11 at 19:02
  • @Takkat: yes, i should have mentioned: headless mode works great for me... i just can't get it to run on startup. – capdragon Aug 15 '11 at 19:04
  • 1
    What if you tried VBoxManage startvm --type headless "UbuntuServer" instead of VBoxHeadless command? – Takkat Aug 15 '11 at 19:15
  • You may also need to be root to get it running like su -c 'VBoxHeadless --startvm <MACHINE>'. – Takkat Aug 15 '11 at 19:18
  • @Takkat: no dice. But again, i can get VBoxHeadless working just fine with the command i'm using. It's ONLY not working when i do it in a startup script. – capdragon Aug 15 '11 at 19:36
up vote 27 down vote accepted

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

  • 2
    doesnt the VRDP add extra overhead, wouldnt VBoxManage startvm --type headless be better ? – mojzis Sep 7 '12 at 21:09
  • 3
    In my specific case, I wanted the VM to shutdown gracefully rather than saving state. replacing "savestate" with "acpipowerbutton" worked perfectly for this. – muzzamo May 23 '15 at 6:59

FYI, This works on Opensuse without nohup.

VBoxHeadless -s "OpenSuSE 11.4 64bit" &
  • So i use the same script i have except leave out the nohup? – capdragon Nov 7 '11 at 17:21
  • Got rid of hohup, still doesn't work. I must be doing something else wrong. Are you sure we're using the same script? Please edit your answer and include your script so i can try it. – capdragon Nov 7 '11 at 22:19

Since you mention that VBoxHeadless works fine if you type it from the command line of a terminal but does not start any VM when launched from a init script, my guess is that it is looking for some environment variable which is defined when you run a shell in your terminal but undefined in the init script environment (which is basically empty, except for parameters given on the kernel command line).

Can you try replacing the VBoxHeadless invocations in the script with this?

env USER=username HOME=/path/to/user/homedir VBoxHeadless ...same options as before...

Here "username" and the home directory path should be changed to match those of the user you use to start the VM.

If that works, you might want to use su -l to run VBoxHeadless instead of this env hack.

  • +1 for su -l - that's a good point! – Takkat Aug 15 '11 at 20:14
  • nope... still doesn't work. I decided to just use a cron job – capdragon Aug 15 '11 at 20:26

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.

  • This works great for me! Except with Vbox 5.0 Its sudo -u $VMUSER VBoxHeadless --startvm $VMNAME & so startvm has 2 "--" and you just sue VBoxHeadless instead of --type headless. Also worth noting is that while I am sure we are on a server, without the "&" at the end, the server does not boot to login prompt but all services work like SSH,RDP etc etc, just something I noticed. It leaves the server stuck at boot logon verbose information! – FreeSoftwareServers Oct 24 '15 at 2:23

one might also want to set directly the port when using vrdp .. comment to the top answer, which compartmentalizes well to use different scripts for different vms

VBoxVRDP -startvm "myVM" -vrdpport 3391 &

Wow.. I am seeing all these solutions, which seem a bit complex, but this is my third laptop on which I ended up simply writing:

VBoxHeadless -s "Ubuntu Server"

in my rc.local file in the /etc/ directory and that worked pretty well. Runs the virtual machine automatically on a reboot.

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.