VirtualBox Server (headless with init.d scripts)
Posted: 14. Jul 2010, 00:37
I've worked on using the headless mode as a full vmware server replacement and have developed an init.d script for debian that will allow machines to be started and stopped automatically. I thought this might be useful to someone else as well so I've decided to post it. The below commands are all done as root.
- Install ubuntu or debian on a server.
- Follow the procedure listed on the download page to add an apt source and install virtualbox.
- Upload the debian netinst ISO to /var/ISOs/
- Configure a test machine using the following commands [code] VBoxManage createvm --name "example-vmname" --ostype Debian --register VBoxManage modifyvm "example-vmname" --memory 512 --acpi on --boot1 dvd --nic1 bridged --bridgeadapter1 eth0 --vrdpport 3389 VBoxManage createhd --filename "example-vmname.vdi" --size 10000 --remember VBoxManage storagectl "example-vmname" --name "SATA Controller" --add sata --controller IntelAhci VBoxManage storageattach "example-vmname" --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium "example-vmname.vdi" VBoxManage openmedium dvd /var/ISOs/debian-504-i386-netinst.iso VBoxManage storagectl "example-vmname" --name "IDE Controller" --add ide --controller PIIX4 VBoxManage storageattach "example-vmname" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium /var/ISOs/debian-504-i386-netinst.iso [/code]
- Create a folder for machine configurations in /etc and link in the machine config [code] mkdir /etc/virtualbox cd /etc/virtualbox ln -s /root/.VirtualBox/Machines/example-vmname/example-vmname.xml [/code]
- Copy the below code to /etc/init.d/VirtualBox-example-vmname. [code] #! /bin/sh # # Copyright (c) 2010 Christopher Thompson # All rights reserved. # # Author: Christopher Thompson, 2010 # # /etc/init.d/virtualbox # # Description: The virtualbox headless service allows a specified virtualbox # VM to be run with just RDP access. # SERVICENAME=VirtualBox VIRTUALMACHINE=example-vmname VMSTATUS=`VBoxManage list runningvms | grep $VIRTUALMACHINE | wc -l` PIDFILE=/var/run/$SERVICENAME-$VIRTUALMACHINE.pid # Check for missing binaries VIRTUALBOX_BIN=/usr/bin/VBoxHeadless test -x $VIRTUALBOX_BIN || { echo "$VIRTUALBOX_BIN not installed"; if [ "$1" = "stop" ]; then exit 0; else exit 5; fi; } # Check for existence of needed config file and read it VIRTUALMACHINE_CONFIG=/etc/virtualbox/$VIRTUALMACHINE.xml test -r $VIRTUALMACHINE_CONFIG || { echo "$VIRTUALMACHINE_CONFIG does not exist"; if [ "$1" = "stop" ]; then exit 0; else exit 6; fi; } case "$1" in start) echo -n "Starting $SERVICENAME: $VIRTUALMACHINE " if [ $VMSTATUS = "1" ]; then echo "ALREADY RUNNING"; exit 2; else echo ""; fi; ## Start daemon with start-stop-daemon. If this fails ## the return value is set appropriately by start-stop-daemon. start-stop-daemon --background --make-pidfile --pidfile $PIDFILE --start --exec $VIRTUALBOX_BIN -- --startvm "$VIRTUALMACHINE" ;; stop) echo "ACPI power button pressed." echo -n "Shutting down $SERVICENAME: $VIRTUALMACHINE " if [ $VMSTATUS = "0" ]; then echo "NOT RUNNING"; exit 2; else echo ""; fi; VBoxManage controlvm $VIRTUALMACHINE acpipowerbutton echo -n "Waiting for shutdown: " while [ `VBoxManage list runningvms | grep $VIRTUALMACHINE | wc -l` = "0" ] do echo -n "." sleep 2 done echo "Done" rm -f $PIDFILE ;; force-stop) echo "This is a very harsh way of managing the machines, please shut them down using SSH or RDP" echo -n "Shutting down $SERVICENAME: $VIRTUALMACHINE " if [ $VMSTATUS = "0" ]; then echo "NOT RUNNING"; exit 2; else echo ""; fi; start-stop-daemon --pidfile $PIDFILE --stop $VIRTUALBOX_BIN -- --startvm "$VIRTUALMACHINE" rm -f $PIDFILE ;; restart) ## Stop the service and regardless of whether it was ## running or not, start it again. $0 stop $0 start ;; status) echo -n "Checking for service $SERVICENAME : " if [ $VMSTATUS = "1" ]; then echo "RUNNING"; else echo "NOT RUNNING"; fi; #VBoxManage showvminfo "$VIRTUALMACHINE" ;; *) ## If no parameters are given, print which are avaiable. echo "Usage: $0 {start|stop|force-stop|status|restart}" exit 1 ;; esac : [/code]
- Set executable permission on the script [code]chmod u+x /etc/init.d/VirtualBox-example-vmname[/code]
- use rcconf or sysv-rc-conf to set the init.d script to start during boot
- Start the machine [code]/etc/init.d/VirtualBox-example-vmname start[/code]
- Connect to the machine using RDP from another computer [code]rdesktop xxx.xxx.xxx.xxx:3389[/code]
- Install the OS and wait for the automatic reboot to the login prompt
- Eject the ISO from the VM [code]VBoxManage storageattach "example-vmname" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium emptydrive[/code]
- Make separate init scripts for each VM and alter the RDP port when creating new VMs