VirtualBox init.d service autostart script


I’ve just installed started using VirtualBox on my Fedora 11 x86_64 and it works just perfectly. I’ve migrated from VMWare when it figured out that it is unable to work with latest kernel version. So I need to start some virtual machines in the background with system startup. I’ve made a lot of googling and found some dirty scripts that were not meeting my criterias. I’ve decided to write my own system startup service.

You need to create file named vbox in /etc/sysconfig/ to list the virtual machine names that you want to start with system. Also when shutting down this service will save state to all running VMs. Here is the script:

/etc/init.d/vbox

#!/bin/sh
#
# chkconfig: - 91 35
# description: Starts and stops vbox autostart VMs.

### BEGIN INIT INFO
# Provides: vbox
# Required-Start: $network $named $vboxdrv
# Required-Stop: $network $named
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: Autostart some Virtual Box VMs
# Description: Autostart some Virtual Box VMs that are mentioned in /etc/sysconfig/vbox file
### END INIT INFO

. /etc/rc.d/init.d/functions

MANAGE_CMD=VBoxManage

[ -r /etc/sysconfig/vbox ] && . /etc/sysconfig/vbox

prog=$"Virtual Box Machines"

start()
{
	echo -n $"Starting $prog: "
	RETVAL=0

	for vbox_name in ${VBOX_AUTOSTART}
	do
	    SERVS=1
	    echo -n "${vbox_name} "
	    daemon $MANAGE_CMD startvm "${vbox_name}" -type vrdp >/dev/null 2>&1
	    RETVAL=$?
	    [ "$RETVAL" -eq 0 ] || break
	done
	if [ -z "$SERVS" ]; then
	    echo -n "no virtual machines configured "
	    failure
	    RETVAL=6
	else
	    if [ "$RETVAL" -eq 0 ]; then
		success $"vbox startup"
		touch /var/lock/subsys/vbox
	    else
		failure $"vbox start"
	    fi
	fi
	echo
	return "$RETVAL"
}

stop()
{
	echo -n $"Shutting down $prog: "
	for vbox_name in ${VBOX_AUTOSTART}
	do
	    echo -n "${vbox_name} "
	    runuser root -c "$MANAGE_CMD -q controlvm "${vbox_name}" savestate" >/dev/null 2>&1
	done
	RETVAL=$?
	[ "$RETVAL" -eq 0 ] && success $"vbox shutdown" || \
	    failure $"vbox shutdown"
	echo
	[ "$RETVAL" -eq 0 ] && rm -f /var/lock/subsys/vbox
	return "$RETVAL"
}

status()
{
	for vbox_name in ${VBOX_AUTOSTART}
	do
	    echo -n "${vbox_name} "
	    $MANAGE_CMD showvminfo "${vbox_name}"|grep "^State:\s*.*$"
	done
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|force-reload)
	stop
	start
	;;
  status)
	status
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
	exit 3
	;;
esac

:

And here is configuration file:

/etc/sysconfig/vbox

# Virtual box machines to autostart
# Example to start 2 machines
#	VBOX_AUTOSTART = "MachineName1 MachineName2"

# VBOX_AUTOSTART=""

Thats it. If you have any questions do not hesitate to contact me.

Be Sociable, Share!

, , , ,

  1. #1 by Crasty at November 4th, 2009

    I read a few topics. I respect your work and added blog to favorites.

    • #2 by Pat b at April 20th, 2011

      I agree with what this. I’m a little behind on getting to this post, but I found while looking for tips on learning Linux. I’m a noob, but getting there!

  2. #3 by Alex Amiryan at November 6th, 2009

    Thank you very much.

  3. #5 by Pierre M at November 30th, 2009

    Excellent! Works great. Been looking for this for months.

    Notes:
    ================
    Watchout for word wrap when pasteing into Nano, I re-did it but used vi the second time.

    chmod 755 /etc/init.d/vbox
    chmod 644 /etc/sysconfig/vbox

    No Spaces!
    VBOX_AUTOSTART=”myvbox”

    chkconfig –add vbox
    chkconfig –level 3 vbox on
    chkconfig –list | grep vbox
    ================

    Thank you, thank you, thank you!
    Great job.

    • #6 by Bob at March 31st, 2010

      When I run the vbox script in a terminal I get
      “Usage: {start|stop|restart|force-reload|status}”
      and nothing happens.
      Any ideas?

      • #7 by Alex Amiryan at March 31st, 2010

        The thing is that you have to copy this script to /etc/init.d/vbox and you don’t have to run it from terminal. Make:
        chkconfig –add vbox
        chkconfig vbox on
        and then
        service vbox start

        Good luck!

  4. #8 by Nathan at January 23rd, 2010

    Awesome work thank you for sharing. I two have been a VMWare man until it looks like the anyany patches have stopped (plus a couple of years of can’t be ass’ed upgrading from fedora 8). Their fault for not making fedora a accepted host. VBox is looking good except for this startup problem.

    I had to make a symbolic link from my home directory to root. As I made my vms within the gui logged in as myself. This way VBoxManage could list them.

    For some reason there still not starting up but it’s a work in progress

    • #9 by Nathan at January 23rd, 2010

      Sorry forgot to post my code

      ln -s /home/{yourusername}/.VirtualBox/VirtualBox.xml /root/.VirtualBox/

  5. #10 by Troels Liebe Bentsen at February 4th, 2010

    I have made a init.d script for ubuntu that also creates a private network for the vms and gives that network access to the internet, feel free to use the code.

    https://www.it-kartellet.dk/dokuwiki/howto/virtualbox#initd_scripts_for_ubuntu

  6. #11 by Bob at April 3rd, 2010

    Here is what I get when I run “service vbox start”:
    Starting Virtual Box Machines: WindowsXPProfessional /etc/init.d/vbox: line 46: failure: command not found

    • #12 by Alex Amiryan at April 3rd, 2010

      If you get this message it means that you are using not Fedora linux. This script is for Fedora.

      • #13 by Max at October 23rd, 2010

        I would really like to use your script in ubuntu, but I receive a similar error (only on line 96 instead). How can is retrofitted to work in ubuntu?

  7. #14 by Emily N. at June 9th, 2010

    Hi, I’m very interested in Linux but Im a Super Newbie and I’m having trouble deciding on the right distribution for me (Havent you heard this a million times?) anyway here is my problem, I need a distribution that can switch between reading and writing in English and Japanese (Japanese Language Support) with out restarting the operating system.

  8. #15 by krodon at November 28th, 2010

    Hello Richard,

    I use your script, provided from Suse 11.3.
    I want to start the VM with non root account. If I try this, controlled by runlevel 3 it will not be started. (the user is in vbox)
    If I try to do this manually with vboxes start I will be asked for the password of this user. I guess that is the reason why it can not be controlled automaticly. Any Ideas how I can fix this?

    Regards

    krodon

  9. #16 by Sam Orchard at April 29th, 2011

    Max :
    I would really like to use your script in ubuntu, but I receive a similar error (only on line 96 instead). How can is retrofitted to work in ubuntu?

    I’ve tried myself, but had no luck, unfortunately.

  10. #17 by Paul Warren at September 1st, 2011

    Hi Alex,

    Thanks for the script! It seems to work well in SME Server 8b6 also.

    Is it possible to modify the script to work with VM names that have spaces? I’ve had a quick hack, but my sh scripting skills are a bit rusty, any ideas?

    Cheers
    Paul Warren

  11. #18 by Server Emule at September 25th, 2011

    Starting Virtual Box Machines: WindowsXPProfessional /etc/init.d/vbox: line 46: failure: command not found

(will not be published)

  1. No trackbacks yet.