VirtualBox as service

Originally from this post: http://www.kernelhardware.org/virtualbox-auto-start-vm-centos-fedora-redhat/

However, in case you’re unable to view the page (404 error), here’s the steps:

Get list of virtual machines (VM) available: (run as user who owns the VMs)

VBoxManage list vms
"XP" {ea077a37-fe9c-40bd-a508-a4ea0be17e17}
"fedora13" {0cf0d60f-e482-407b-8c4f-b661692f6435}
"centos5" {4a10fb5f-37ba-4012-a41a-e94c6035fc15}

Create config file: (fill with only VM that you want to start on boot or when you run ‘service vboxcontrol start’)

vim /etc/virtualbox/machines_enabled
XP
fedora13
centos5

Create startup script:

vim /etc/init.d/vboxcontrol
#!/bin/sh
# vboxcontrol   Startup script for VirtualBox Virtual Machines
#
# chkconfig: 345 98 02
# description: Manages VirtualBox VMs
# processname: vboxcontrol
#
# pidfile: /var/run/vboxcontrol/vboxcontrol.pid
#
### BEGIN INIT INFO
#
### END INIT INFO
#
# Version 20100414 by azwan082
# Version 20090301 by Kevin Swanson <kswan.info> based on:
# Version 2008051100 by Jochem Kossen <jochem.kossen@gmail.com>
# http://farfewertoes.com
#
# Released in the public domain
#
# This file came with a README file containing the instructions on how
# to use this script
#
# Source function library.
if [ -f /etc/init.d/functions ] ; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 1
fi

########################################################################
# INITIAL CONFIGURATION
VBOXDIR="/etc/virtualbox"
VM_USER="username"
USE_NAT="no"

export PATH="${PATH:+$PATH:}/bin:/usr/bin:/usr/sbin:/sbin"
if [ -f $VBOXDIR/config ]; then
. $VBOXDIR/config
fi

SU="su $VM_USER -c"
VBOXMANAGE="VBoxManage -nologo"

########################################################################
# FUNCTIONS

# Determine if USE_NAT is set to "yes"
use_nat() {
if [ "$USE_NAT" = "yes" ]; then
return `true`
else
return `false`
fi
}

log_failure_msg() {
echo $1
}

log_action_msg() {
echo $1
}

# Check for running machines every few seconds; return when all machines
# are down
wait_for_closing_machines() {
RUNNING_MACHINES=`$SU "$VBOXMANAGE list runningvms" | wc -l`
if [ $RUNNING_MACHINES != 0 ]; then
sleep 5
wait_for_closing_machines
fi
}

########################################################################
# RUN
case "$1" in
start)
if [ -f /etc/virtualbox/machines_enabled ]; then
cat /etc/virtualbox/machines_enabled | while read VM; do
log_action_msg "Starting VM: $VM ..."
$SU "$VBOXMANAGE startvm "$VM" -type headless"
RETVAL=$?
done
touch /var/lock/subsys/vboxcontrol
fi
;;
stop)
# NOTE: this stops all running VM's. Not just the ones listed
# in the config
$SU "$VBOXMANAGE list runningvms" | egrep -o "^"[a-zA-Z0-9]*"" | while read VM; do
log_action_msg "Shutting down VM: $VM ..."
$SU "$VBOXMANAGE controlvm "$VM" acpipowerbutton"
done
rm -f /var/lock/subsys/vboxcontrol
wait_for_closing_machines
;;
start-vm)
log_action_msg "Starting VM: $2 ..."
$SU "$VBOXMANAGE startvm "$2" -type headless"
;;
stop-vm)
log_action_msg "Stopping VM: $2 ..."
$SU "$VBOXMANAGE controlvm "$2" acpipowerbutton"
;;
poweroff-vm)
log_action_msg "Powering off VM: $2 ..."
$SU "$VBOXMANAGE controlvm "$2" poweroff"
;;
status)
echo "The following virtual machines are currently running:"
$SU "$VBOXMANAGE list runningvms" | egrep -o "^"[a-zA-Z0-9]*"" | while read VM; do
echo -n "$VM ("
echo -n `$SU "VBoxManage showvminfo ${VM%% *}|grep Name:|sed -e 's/^Name:s*//g'"`
echo ')'
done
;;
*)
echo "Usage: $0 {start|stop|status|start-vm <VM name>|stop-vm <VM name>|poweroff-vm <VM name>}"
exit 3
esac
exit 0

Set script as executable

chmod 755 /etc/init.d/vboxcontrol

Add to service registry:

chkconfig --add vboxcontrol

Set it to start on boot:

chkconfig vboxcontrol on
Share this...
Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPin on Pinterest

4 thoughts on “VirtualBox as service”

  1. When you have fedora 15 and systemd so you change line SU=”su $VM_USER -c” on SU=”daemon –user $VM_USER” Because, systemd did not know that vitrual machines are running. The systemd did not call stop on vboxcontrol init script.

    Now: systemctl status vboxcontrol.service
    Active: active (running)
    Before the change:
    Active (exited)

Leave a Reply

Your email address will not be published. Required fields are marked *