Starting a VirtualBox image on boot up

Recently with current hardware, it has become feasible to run windows 2003 machines from within VirtualBox PC emulators (opensource - http://virtualbox.org).

One strong point for using an emulated windows server is the relative ease to move this emulated machine onto another physical computer without needing to reinstall everything up.

VirtualBox is handy because it runs fairly well and is very easy to configure, but one of the missing features is the option to select a virtual machine to start when computer boots up.

The lack of this feature forces an administrator to manually start the virtual machine everytime a reboot is necessary.

Looking around the web I've stumbled on this solution: http://farfewertoes.com/stories/2008-03-09-start-virtualbox-virtual-machines-on-boot/

But I required something a bit simpler and decided to make a smaller version from the mentioned script that is easier for anyone else to customize as needed.

One thing I should mention is that this script will first attempt to send an ACPI shutdown to the virtual machine so that it can close by itself and if this machine is not closed within a specified time frame (12 seconds), the script will force the shutdown without further delay.

Enough talk for today, here's the code:
# Simple script to start virtual box on boot up
# Credits - http://farfewertoes.com/code/vboxcontrol

# How is this virtual box called?
emulatedbox="Windows XP"

# The script will send a soft reset when shutting down,
# how long should it wait before sending a power off? (0 to disable)
until_shutdown=12

## Start the program ##

if [ "$1" == "start" ]; then
echo "Starting VirtualBox Image.. ("$emulatedbox")";
VBoxManage startvm "$emulatedbox"
exit 0
fi


if [ "$1" == "stop" ]; then
echo "Stopping VirtualBox Image.. ("$emulatedbox")";
counter=0;
RUNNING_MACHINES=`VBoxManage list runningvms | wc -l`
if [ $RUNNING_MACHINES != 0 ]; then
VBoxManage controlvm "$emulatedbox" acpipowerbutton
if [ $until_shutdown != 0 ];
then sleep $until_shutdown; fi;
RUNNING_MACHINES=`VBoxManage list runningvms | wc -l`
if [ $RUNNING_MACHINES != 0 ]; then
VBoxManage controlvm "$emulatedbox" poweroff; exit 0; fi
fi
exit 0
fi

echo "No parameters specified, exiting."
exit 1
I hope you know how to use this script, if you don't then I'd suggest reading up the suggestions from the link I've suggested on the top of this blog post as it gives some instructions regarding how this should be applied on your server machine.


Good luck, hope this helps you.

:)