Saturday, February 27, 2010

Three scripts for running VirtualBox in headless mode (Ubuntu)

I needed some init.d scripts for virtualbox, and I thought I should share them, as I found a lot of information out there that these scripts used ideas from:

Script 1 to start a virtualbox (vbox_start_vm)
Starts and sleeps x number of seconds, but doesn't start it if it's already running!

#!/bin/bash

if [ -z $1 ] ; then
        echo "SERVER Name is Missing... the number is 42."
        echo "usage vbox_single_start "
        exit 1
fi

if /usr/bin/VBoxManage -q showvminfo $1 | grep -i running ; then
        echo VM Guest $1 is already powered on!
        exit 1
else
        echo Starting $1
        /usr/bin/VBoxManage startvm $1 --type vrdp
        if [ $2 ] ; then
            sleep $2
        fi
fi
exit 0


Script 2 to stop a virtualbox (vbox_stop_vm)
This script tries to acpi-shutdown the system, and wait for 240 seconds before forcing it down if all fails.

#!/bin/bash
MAX_WAIT_TIME=300 # max seconds to wait before doing poweroff (force)

function wait_until_machine_shutdown_else_terminate {
    if /usr/bin/VBoxManage -q list runningvms | grep -i $1 ; then
        #echo "sleeping 10 seconds"
        sleep 10
        let MAX_WAIT_TIME=MAX_WAIT_TIME-10
        if [ $MAX_WAIT_TIME -lt 0 ] ; then
            echo "Had to force the power on the server... Sorry about that!"
            /usr/bin/VBoxManage controlvm $1 poweroff
            sleep 10
        else
            if [ $MAX_WAIT_TIME -lt 200 ] ; then
                    #try to do another acpipowerbutton
                /usr/bin/VBoxManage controlvm $1 acpipowerbutton
            fi
            wait_until_machine_shutdown_else_terminate $1
        fi
    fi
}

#ensure that the server name is entered
if [ -z $1 ] ; then
        echo "SERVER Name is Missing... the number is 42."
        echo "usage vbox_stop_vm "
        exit 1
fi

if /usr/bin/VBoxManage -q showvminfo $1 | grep -i running ; then
        echo VM Guest $1 is powered on! Will send ACPI shutdown.
        /usr/bin/VBoxManage controlvm $1 acpipowerbutton
        # if parameter for sleep is provided, do here
        if [ $2 ] ; then
            sleep $2
        fi
        # wait until it's been shutdown!
        wait_until_machine_shutdown_else_terminate $1
else
        echo Server $1 is not running!
        exit 1
fi
exit 0

Script 3 - the actual init.d script calling the other scripts (call it what you want on your system)

#! /bin/sh
# some errors with networking, so need to restart at end of init sequence
#NOT NEEDED BRIDGE WORKS?!?!?!?!

case "$1" in
   start)
        echo -n "Starting all the VirtualBox VMS"
        /etc/init.d/vbox_start_vm "Windows2008_DC" 60
        /etc/init.d/vbox_start_vm "Windows2008_Test" 60
        /etc/init.d/vbox_start_vm "Ubuntu_Web_Server" 15
        /etc/init.d/vbox_start_vm "Ubuntu_DB_Server" 15
        /etc/init.d/vbox_start_vm "Ubuntu_Testing_Server" 15
        echo "Done";
;;

stop)
        echo -n "Stopping all the VirtualBox VMS"
        /etc/init.d/vbox_stop_vm "Windows2008_DC" 
        /etc/init.d/vbox_stop_vm "Windows2008_Test" 
        /etc/init.d/vbox_stop_vm "Ubuntu_Web_Server" 
        /etc/init.d/vbox_stop_vm "Ubuntu_DB_Server" 
        /etc/init.d/vbox_stop_vm "Ubuntu_Testing_Server" 
        echo "Done";
;;

*)
echo "Usage: /etc/init.d/vbox_vm_startup {start|stop}"

exit 1
;;

esac
exit 0

I needed to implement these functions, as I now have 1 Ubuntu server with Virtualbox on top running all 5 servers at home. I wanted to have a full-blown test environment to play around in without having more than 1 server. 

The only problem so far is that the disk controller wasn't fast enough (Dell SAS 6/ir). To help this perform better I added an extra HD outside the SAS controller running the swap spaces and the pagefiles for the Windows servers. That made a huge difference, especially for the windows servers. If you are going to run your vm's on a mirrored raid, I won't recommend the SAS 6/ir if the virtual servers will be accessed by more than 20-25 users. It will feel a bit sluggish... Maybe not so much for the users, but if you do iostat as a admin, you will be in "shock" over the terrible write performance for the SAS 6/ir in mirror mode.