How To Set Your VirtualBox 4.2 VM to Automatically Startup
Being system administrators and tweakers we always like to make things automatically take care of themselves, right? Well of course we do and that’s where any auto starts we find are likely to get put in place.
A new feature added in VirtualBox 4.2 is autostart, this feature is available in Linux, Solaris and MacOSX hosts only. Let’s say there is a VirtualBox VM you start everytime you startup your system, why not make it startup as soon as you start VirtualBox. I am using Fedora 17 for this example.
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:
# vi /etc/default/virtualbox
# virtualbox defaults file
VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/vbox.cfg
Update: It has been commented that the file named vbox.cfg didn’t work for some. They have been successful using autostart.cfg, if vbox.cfg doesn’t work for you, then trying naming the file autostart.cfg.
Now we need to create /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
}
If you are the only user you can just add the line default_policy = allow to the vbox.cfg file.
Set permissions on directory to the vboxuser group and make sure users can write to the directory as well as sticky bit.
# chgrp vboxusers /etc/vbox
# chmod 1775 /etc/vbox
Add each of the users to the vboxusers group. Make sure to check their group memberships so they do not loose any groups they currently have assigned to their id.
# groups myuserid
myuserid wheel
# usermod -G wheel, vboxusers myuserid
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
Now we are ready to set the VM’s we choose to start.
$ VBoxManage modifyvm --autostart-enabled on
This will create a myuserid.start file in /etc/vbox directory
Now restart the vboxautostart-service to read in the changes.
# service vboxautostart-service restart
Reboot your system and your VM should start
24 Responses to “How To Set Your VirtualBox 4.2 VM to Automatically Startup”
Trackbacks/Pingbacks
- virtualbox autostart – Gábor Mészáros - […] better source, source […]
- How to start Virtual box machines automatically when booting? - Stack All Flow - […] You can use the VirtualBox Auto-start service. A good tutorial describing how to do this is posted on the…
Thanks for post. It got me heading in the right direction, but I’ve found a few errors (at least for VirtualBox-4.2-4.2.0_80737_el6-1.x86_64)
VBOXAUTOSTART_CONFIG can’t be /etc/vbox/vbox.cfg because /etc/init.d/vboxautostart-service is a shell script and trys to source /etc/vbox/vbox.cfg for config values for vboxwebsvr. The syntax for the vboxautostart config is not valid shell syntax and causes errors when /etc/init.d/vboxautostart-service runs. Set VBOXAUTOSTART_CONFIG to anything but /etc/vbox/vbox.cfg and it will work. I suggest /etc/vbox/vboxautostart.cfg
Also, your VBoxManage command should be:
VBoxManage modifyvm –autostart-enabled on
Finally, vboxautostart-service doesn’t have a restart function; only start and stop.
clearly the site strips some characters. Possibly why your VBoxManage command didn’t display correctly. I was trying to write:
VBoxManage modifyvm name_of_vm –autostart-enabled on
I tried using autostart.cfg for my config file and it failed. /etc/vbox/vbox.cfg is what is listed in /etc/init.d/vboxautostart-service on Fedora 17 and that’s what worked for me. All the commands and settings I posted worked on Fedora 17 x64. the autostart enabled switch in post looks like a single dash but it is two dashes and WordPress can sometimes mash things together. rpm’s and distribution’s tend to change locations and settings from what the developer creates. It looks like you are using either CentOS or RHEL derivative and they are different than Fedora which is bleeding edge.
I will try the installs on CentOS 6 and see what I get with the package and config but I am betting it is what you saw.
Incase someone runs into the same issue. I have an Ubuntu 12.04 box I’m using to host my vms. When I ran the ‘VBoxManage modify name_of_vm -autostart-enabled on’ command I received this error:
VBoxManage: error: Adding machine ‘nb1’ to the autostart database failed with VERR_ACCESS_DENIED
VBoxManage: error: Details: code NS_ERROR_UNEXPECTED (0x8000ffff), component SessionMachine, interface IMachine, callee nsISupports
VBoxManage: error: Context: “COMSETTER(AutostartEnabled)(ValueUnion.f)” at line 2418 of file VBoxManageModifyVM.cpp
I triple checked my permissions on the /etc/vbox dir and confirmed it was correct. So I manually created the /etc/vbox/[userid].start file with touch. Re-ran the VBoxManage command and it completed without error.
ur access denied problem was caused by sticky bit of the /etc/vbox. so try setting sticky of this file and ur life will be as normal and simple as virtualbox wants it.
how did u create the userid.start file and what were the contents
stuck here pl help
As long as you have the permission set for /etc/vbox
# chgrp vboxusers /etc/vbox
# chmod 1775 /etc/vbox
And you are part of the vboxusers group it will create when you run the command
VBoxManage modifyvm –autostart-enabled on
It should create it in the /etc/vbox directory.
Thanks for the nice guide! There’s one problem I came across with it.
I am working with the 4.2.6-82870~Ubuntu~precise package and found one problem. The vboxautostart-service checks that the kernel modules are loaded and fails if they are not. But that package sets the vboxdrv service to start at the same time (S20 priority) so that the autostart service fails quietly every time the host boots.
I fixed it by renaming the startup symlinks to S98vboxautostart-service from S20vboxautostart-service in the rc[2-5].d directories.
Great article – i was able to get autostart up and running from this article no problem – worked like a charm
Configure the system as indicated and auto-start will work. However, auto-stop is not supported in the 4.2 release. In addition, the presence of auto-stop properties in vboxmanage is currently (05-2013) considered a “bug”.
So, to make auto-start/stop work properly on Ubuntu 12.04, I modified the /etc/init.d/vboxautostart-service script to properly handle both. This script should work on other distributions. VM’s that are started manually must still be turned off manually (unless you somehow stuff the VM’s uuid into the .stop file in /etc/vbox).
An optional configuration option can be put in /etc/default/virtualbox. The value VBOXAUTOSTART_STOP_DELAY should be set to however many seconds you want the script to wait between issuing the “acpipowerbutton” command and the “poweroff” command. This gives ACPI-aware guests a chance to cleanly shut down. 10 seconds is the default.
Here is the complete vboxautostart-service script that goes in /etc/init.d:
#!/bin/sh
#
# VirtualBox autostart service init script.
#
# Copyright (C) 2012 Oracle Corporation
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License (GPL) as published by the Free Software
# Foundation, in version 2 as it comes in the “COPYING” file of the
# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
#
# chkconfig: 35 35 65
# description: VirtualBox autostart service
#
### BEGIN INIT INFO
# Provides: vboxautostart-service
# Required-Start: vboxdrv
# Required-Stop: vboxdrv
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: VirtualBox autostart service
### END INIT INFO
PATH=$PATH:/bin:/sbin:/usr/sbin
DEBIAN=yes
NOLSB=
[ -f /lib/lsb/init-functions ] || NOLSB=yes
[ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
if [ -n “$INSTALL_DIR” ]; then
binary=”$INSTALL_DIR/VBoxAutostart”
else
binary=”/usr/lib/virtualbox/VBoxAutostart”
fi
# silently exit if the package was uninstalled but not purged,
# applies to Debian packages only
[ -z “$DEBIAN” -o -x $binary ] || exit 0
[ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
system=unknown
if [ -f /etc/redhat-release ]; then
system=redhat
elif [ -f /etc/SuSE-release ]; then
system=suse
elif [ -f /etc/debian_version ]; then
system=debian
elif [ -f /etc/gentoo-release ]; then
system=gentoo
elif [ -f /etc/arch-release ]; then
system=arch
elif [ -f /etc/slackware-version ]; then
system=slackware
elif [ -f /etc/lfs-release ]; then
system=lfs
else
system=other
fi
if [ -z “$NOLSB” ]; then
. /lib/lsb/init-functions
fail_msg() {
echo “”
log_failure_msg “$1″
}
succ_msg() {
log_success_msg ” done.”
}
begin_msg() {
log_daemon_msg “$@”
}
fi
if [ “$system” = “redhat” ]; then
. /etc/init.d/functions
if [ -n “$NOLSB” ]; then
start_daemon() {
usr=”$1″
shift
daemon –user $usr $@
}
fail_msg() {
echo_failure
echo
}
succ_msg() {
echo_success
echo
}
begin_msg() {
echo -n “$1”
}
fi
fi
if [ “$system” = “suse” ]; then
. /etc/rc.status
start_daemon() {
usr=”$1″
shift
su – $usr -c “$*”
}
if [ -n “$NOLSB” ]; then
fail_msg() {
rc_failed 1
rc_status -v
}
succ_msg() {
rc_reset
rc_status -v
}
begin_msg() {
echo -n “$1”
}
fi
fi
if [ “$system” = “debian” ]; then
start_daemon() {
usr=”$1″
shift
bin=”$1″
shift
start-stop-daemon –background –chuid $usr –start –exec $bin — $@
}
killproc() {
start-stop-daemon –stop –exec $@
}
if [ -n “$NOLSB” ]; then
fail_msg() {
echo ” …fail!”
}
succ_msg() {
echo ” …done.”
}
begin_msg() {
echo -n “$1”
}
fi
fi
if [ “$system” = “gentoo” ]; then
if [ -f /sbin/functions.sh ]; then
. /sbin/functions.sh
elif [ -f /etc/init.d/functions.sh ]; then
. /etc/init.d/functions.sh
fi
start_daemon() {
usr=”$1″
shift
bin=”$1″
shift
start-stop-daemon –background –chuid $usr –start –exec $bin — $@
}
killproc() {
start-stop-daemon –stop –exec $@
}
if [ -n “$NOLSB” ]; then
fail_msg() {
echo ” …fail!”
}
succ_msg() {
echo ” …done.”
}
begin_msg() {
echo -n “$1”
}
if [ “`which $0`” = “/sbin/rc” ]; then
shift
fi
fi
fi
if [ “$system” = “arch” ]; then
USECOLOR=yes
. /etc/rc.d/functions
start_daemon() {
usr=”$1″
shift
su – $usr -c “$*”
test $? -eq 0 && add_daemon rc.`basename $2`
}
killproc() {
killall $@
rm_daemon `basename $@`
}
if [ -n “$NOLSB” ]; then
fail_msg() {
stat_fail
}
succ_msg() {
stat_done
}
begin_msg() {
stat_busy “$1”
}
fi
fi
if [ “$system” = “slackware” ]; then
killproc() {
killall $1
rm -f $PIDFILE
}
if [ -n “$NOLSB” ]; then
fail_msg() {
echo ” …fail!”
}
succ_msg() {
echo ” …done.”
}
begin_msg() {
echo -n “$1″
}
fi
start_daemon() {
usr=”$1”
shift
su – $usr -c “$*”
}
fi
if [ “$system” = “lfs” ]; then
. /etc/rc.d/init.d/functions
if [ -n “$NOLSB” ]; then
fail_msg() {
echo_failure
}
succ_msg() {
echo_ok
}
begin_msg() {
echo $1
}
fi
start_daemon() {
usr=”$1″
shift
su – $usr -c “$*”
}
status() {
statusproc $1
}
fi
if [ “$system” = “other” ]; then
if [ -n “$NOLSB” ]; then
fail_msg() {
echo ” …fail!”
}
succ_msg() {
echo ” …done.”
}
begin_msg() {
echo -n “$1”
}
fi
fi
vboxdrvrunning() {
lsmod | grep -q “vboxdrv[^_-]”
}
start() {
#begin_msg “Starting VirtualBox VMs configured for autostart (DB:$VBOXAUTOSTART_DB CFG:$VBOXAUTOSTART_CONFIG) ” ]
[ -z “$VBOXAUTOSTART_DB” ] && exit 0
[ -z “$VBOXAUTOSTART_CONFIG” ] && exit 0
begin_msg “Starting VirtualBox VMs configured for autostart”;
vboxdrvrunning || {
fail_msg “VirtualBox kernel module not loaded!”
exit 0
}
PARAMS=”–background –start –config $VBOXAUTOSTART_CONFIG”
# prevent inheriting this setting to VBoxSVC
unset VBOX_RELEASE_LOG_DEST
for user in `ls $VBOXAUTOSTART_DB/*.start`
do
user_name=`basename $user | sed -ne ‘s/\(.*\).start/\1/p’`
echo -n > $VBOXAUTOSTART_DB/${user_name}.stop
vboxmanage list vms | awk ‘BEGIN { FS = “[{}]” } ; {print $2}’ | while read uuid
do
enabled=`vboxmanage showvminfo $uuid | grep -c “Autostart Enabled: on”`
if [ “$enabled” = “1” ]
then
echo $uuid >> $VBOXAUTOSTART_DB/${user_name}.stop
fi
done
start_daemon $user_name $binary $PARAMS > /dev/null 2>&1
done
return $RETVAL
}
stop() {
[ -z “$VBOXAUTOSTART_DB” ] && exit 0
[ -z “$VBOXAUTOSTART_CONFIG” ] && exit 0
if [ -z “$VBOXAUTOSTART_STOP_DELAY” ]
then
# default 10 seconds to shut down nicely
VBOXAUTOSTART_STOP_DELAY=10
fi
# Seriously?
#exit 0
begin_msg “Stopping VirtualBox VMs configured for autostop”;
vboxdrvrunning || {
fail_msg “VirtualBox kernel module not loaded!”
exit 0
}
PARAMS=”controlvm”
# prevent inheriting this setting to VBoxSVC
unset VBOX_RELEASE_LOG_DEST
binary=”/usr/bin/vboxmanage”
[ ! -x “$binary” ] && exit 0
for user in `ls $VBOXAUTOSTART_DB/*.stop`
do
user_name=`basename $user | sed -ne ‘s/\(.*\).stop/\1/p’`
echo ” User: $user_name”
for uuid in `cat $VBOXAUTOSTART_DB/${user_name}.stop`
do
PARAMS=”$PARAMS $uuid”
echo ” ACPI Power button: $uuid”
#start_daemon $user_name -c $binary $PARAMS > /dev/null 2>&1
su – $user_name -c “$binary $PARAMS acpipowerbutton” > /dev/null 2>&1
sleep $VBOXAUTOSTART_STOP_DELAY
echo ” Power off: $uuid”
#start_daemon $user_name $binary $PARAMS > /dev/null 2>&1
su – $user_name -c “$binary $PARAMS poweroff” > /dev/null 2>&1
done
done
return $RETVAL
}
case “$1” in
start)
start
;;
stop)
stop
;;
*)
echo “Usage: $0 {start|stop}”
exit 1
esac
exit $RETVAL
I’m using Ubuntu 13.04. FYI: When I install the Virtualbox package from the Software Centre (4.2.10), there is no /etc/init.d/vboxautostart-service
I had to do the same av edog1973
I manually created the /etc/vbox/[userid].start file with touch
Following these instructions I discovered a fatal flaw in autostart for those of us who are keeping the VM machine files on a NFS share common to multiple virtualization servers. If a given VM is registered on multiple machines but only intended to autostart on one of them (the others are there as a backup or for teleportation) you’re out of luck. The autostart config is stored in the machine definition .vbox file so it would try to autostart on every server which has it registered. A more correct implementation would have kept the autostart information in the ~/.VirtualBox config space.
Is there anywany to start the vm with gui?
i followed above procedure and it worked well but the vm is not starting with gui
anyone please help.
Siddharth,
autostart is for use with headless VMS – the idea is that they start in the background, and then you connect using RDP. For this to work you need to have installed the VirtualBox Extensions pack, and configured the specific VM to have the remote display (also known as VRDE) turned on for a specific port. You’ll have to read the manual to figure that out, but that’s how its supposed to work.
Toby
Hola!!!
Muchas gracias por el aporte, me sirvió.
ocupé un RHEL 6.5 para ver la virtualización con VirtualBox
Saludos!
Maybe worth considering using
usermod -aG vboxusers myusername
instead – it’s less of a headache for those with many, many user groups, which come out space-separated fromgroups
but need to be comma-separated forusermod
.I didn’t include that in the article as the system I was using only had a single user, but you are correct it makes it much easier to make changes for multiple users.
hello, interesting post, I have had no problems to make it works, actually I have my VM Host running perfectly and it starts just the machines I have configured.
But I have 2 questions:
What order does it follow to start machines?
Do it safely shutdown the machines when the Host is shutted down?
there is nothing that states what is started in a particular order and nothing about shutdown. One thing you could do is create an init.d script (CentOS / Fedora) to start and stop the VM’s. That way you can control start and stop
#!/bin/sh
#chkconfig: 2345 40 60
#description: VirtualBox
start()
{
echo -n “Starting VM01”
/usr/bin/VBoxManage startvm “VM01” –type headless
echo -n “Starting VM02”
/usr/bin/VBoxManage startvm “VM02” –type headless
}
stop()
{
echo -n “Saving VM VM01 State…”
/usr/bin/VBoxManage controlvm “VM01” savestate
echo -n “Saving VM VM02 State…”
/usr/bin/VBoxManage controlvm “VM02” savestate
}
case “$1″ in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
*)
echo $”Usage: $0 {start|stop|restart}”
exit 1
;;
esac
exit 0
Thank you, this helped me a lot.
### sudo usermod -a -G vboxusers USERNAME
on the above line, which USERNAME did you use..
the one on which your VM machine is configured or the VM machine’s user account name?
The username of the user that needs access to Virtualbox. Example, your login id.
Hey there ϳust wanted to give yoᥙ a quick heaԁs up.
The text in your article seem to be running off the scrreen іn Internet eҳplorer.
I’m not sure if this іs a formatting issue oг something to do with internet browser compatibility
buut I figured I’d post to let youu know. Tһe
desiցn look great though! Hoope yoou get the ргoblem resolvedd soon. Thanks