VirtualBox Server (headless with init.d scripts)

Discussions related to using VirtualBox on Linux hosts.
Post Reply
cthompson
Posts: 2
Joined: 13. Jul 2010, 23:51
Primary OS: Ubuntu other
VBox Version: PUEL
Guest OSses: Debian, Windows XP

VirtualBox Server (headless with init.d scripts)

Post by cthompson »

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
firebladenz
Posts: 1
Joined: 2. Oct 2010, 02:22
Primary OS: Ubuntu 8.10
VBox Version: OSE Debian
Guest OSses: Linux

Re: VirtualBox Server (headless with init.d scripts)

Post by firebladenz »

FYI The script didn't like VirtualBox servers with spaces in the name, also I found the stop & force-stop didn't work correctly (due to the test condition '= 0' when should have been 'not = 0' is the word count was a positive interger indicating a running VM was detected) so I've adjusted the script as below. The start, stop, force-stop tested fine.

Code: Select all

SERVICENAME=VirtualBox
VIRTUALMACHINE="Linux Server"
#echo "VirtualBox Virtual Machine details"
#echo "=================================="
#echo "VIRTUALMACHINE=$VIRTUALMACHINE"
VMSTATUS=`VBoxManage list runningvms | grep "$VIRTUALMACHINE" | wc -l`
PIDFILE="/var/run/$SERVICENAME-$VIRTUALMACHINE.pid"

# Check for missing binaries
VIRTUALBOX_BIN=/usr/bin/VBoxHeadless
VIRTUALBOX_CONTROL_BIN=/usr/bin/VBoxManage
test -x $VIRTUALBOX_BIN || -x $VIRTUALBOX_CONTROL_BIN || { echo "$VIRTUALBOX_BIN or $VIRTUALBOX_CONTROL_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"
#echo "VIRTUALMACHINE_CONFIG=$VIRTUALMACHINE_CONFIG"
#echo " "
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 -- -s "$VIRTUALMACHINE"
      ;;
   stop)
      echo "ACPI power button depression sent to Virtual Machine."
      echo -n "Shutting down $SERVICENAME: $VIRTUALMACHINE "
      if [ $VMSTATUS = "0" ]; then echo "NOT RUNNING"; exit 2; else echo ""; fi;
      VBoxManage controlvm "$VIRTUALMACHINE" acpipowerbutton
      echo -n "Please wait for shutdown to complete: "
      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;
      while [ `$VIRTUALBOX_CONTROL_BIN list runningvms | grep "$VIRTUALMACHINE" | wc -l` != "0" ]
      do
        echo -n "."
        result=`$VIRTUALBOX_CONTROL_BIN controlvm "$VIRTUALMACHINE" poweroff`
        sleep 1
      done
      echo "Done"
      ;;
   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}"
      echo " "
      exit 1
      ;;
esac
:
echo " "
larrykemp
Posts: 4
Joined: 28. Sep 2010, 22:51
Primary OS: Linux other
VBox Version: OSE other
Guest OSses: Cent OS, Windows

Re: VirtualBox Server (headless with init.d scripts)

Post by larrykemp »

This is a great post. I am looking for a method to do the same thing for a CentOS host system.

I tried the method described here http://www.kernelhardware.org/virtualbo ... ra-redhat/ and it did not work for me.

My VM's did not boot when the physical host server booted. I had to manually start them headless at the CLI.
My VM's did not shutdown either. The host system just hung, waiting for the VM's to shutdown so it could reboot.

If I cannot figure this out it looks like VMWare will have their way with us because they have this automatic start and graceful shutdown functionality built into their platform for hosting VM's.

Can anyone help me?
larrykemp
Posts: 4
Joined: 28. Sep 2010, 22:51
Primary OS: Linux other
VBox Version: OSE other
Guest OSses: Cent OS, Windows

Re: VirtualBox Server (headless with init.d scripts)

Post by larrykemp »

This is on a CentOS host btw.

Okay I got my VM's to auto start when my host booted.

- Created files in /etc called "vboxstartup" and "vboxshutdown".

- Created symbolic links from /etc/init.d to the files in /etc

- Made the links executable:
cd /etc/init.d
chmod -x vboxstartup
chmod -x vboxshutdown

- Edited /etc/vboxstartup to look like this:
#! /bash/sh
# For CentOS:
# chkconfig: 2345 95 20
# description: Starting VirtualBox VM's at system boot.
# processname: vboxstartup
### BEGIN INIT INFO
# Provides: vboxstartup
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Should-Start:
# Short-Description: Starts VirtualBox VM's.
# Description: Daemon executes the command "VBoxManage startvm NAMEOFYOURVM --type headless".
### END INIT INFO
VBoxManage startvm VMSERVER1 --type headless
VBoxManage startvm VMSERVER2 --type headless
end

- Then I performed:
cd /etc
chkconfig --level 2345 vboxstartup on

My question is now...how do I get my vboxshutdown to execute when the system is going down for poweroff or reboot?
My file /etc/vboxshutdown simply will execute the commands:
VBoxManage controlvm NAMOFVMSERVER1 poweroff
VBoxManage controlvm NAMOFVMSERVER2 poweroff

The idea is to gracefully shutdown the VM's. Now if the system simply loses power then it will not matter because the VM's like the host will simply hard power-off.

Can anyone help?
cthompson
Posts: 2
Joined: 13. Jul 2010, 23:51
Primary OS: Ubuntu other
VBox Version: PUEL
Guest OSses: Debian, Windows XP

Re: VirtualBox Server (headless with init.d scripts)

Post by cthompson »

Sounds like you might just need some help with writing init scripts. This is a reasonable guide: http://www.novell.com/coolsolutions/feature/15380.html. Basically you should be combining the start and stop scripts into a single file. You might also have to link the scripts into /etc/rc.d/ - I'm not entirely sure about how CentOS handles that part, but Debian asks you to link start and kill links with a priority (IE: /etc/rc.d/rc1.d/(S|K)(\d\d)example_scriptname).
spe05081
Posts: 5
Joined: 17. Apr 2009, 04:13
Primary OS: MS Windows XP
VBox Version: PUEL
Guest OSses: Ubuntu 9.04

Re: VirtualBox Server (headless with init.d scripts)

Post by spe05081 »

I'm following this post to enable my virtualbox to run at srartup. I'm using Fedora 14, but I get stuck at:
use rcconf or sysv-rc-conf to set the init.d script to start during boot
I used instead:

Code: Select all

chkconfig VirtualBox-win7 on
But I get this:

Code: Select all

service VirtualBox-win7 does not support chkconfig
Doe anyone know how to initialize this script in Fedora?
spe05081
Posts: 5
Joined: 17. Apr 2009, 04:13
Primary OS: MS Windows XP
VBox Version: PUEL
Guest OSses: Ubuntu 9.04

Re: VirtualBox Server (headless with init.d scripts)

Post by spe05081 »

spe05081 wrote:I'm following this post to enable my virtualbox to run at srartup. I'm using Fedora 14, but I get stuck at: [...]
Doe anyone know how to initialize this script in Fedora?
I figured that part out. Now though I get stuck at the init line:

Code: Select all

start-stop-daemon --background --make-pidfile --pidfile "$PIDFILE" --start --exec

Code: Select all

line 42: start-stop-daemon: command not found
which is only Debian. Googling led me to suggestions of daemon and relative options in place of start-stop-daemon
But this time I really don;t know what to replace this with... :(
kenyee
Posts: 33
Joined: 31. Dec 2007, 05:19

Re: VirtualBox Server (headless with init.d scripts)

Post by kenyee »

It's easier if you use vboxtool:
http://vboxtool.sourceforge.net/

Though I did have to modify a bunch of stuff to get it to work w/ virtualbox 4.0.x
Post Reply