VirtualBox fencing and Red Hat Enterprise Linux Cluster Suite

I know there is already a perl script to fence VirtualBox machines, but since I am not very familiar with Perl, a shell scripts seemed easier.

So; when you run VirtualBox and would like (virtual) cluster nodes to be able to fence, you may use this shell script on the virtual machines:

#!/bin/sh -x

# Script to stop and start a virtual machine.
# The only required argument is machinename.

eval $(cat -)

# I use Apple Mac OS X, but any OS may be used.
vboxmanage="/Applications/VirtualBox.app/Contents/MacOS/VBoxManage"

usage () {
/bin/echo "Usage: $0 -a NAME [-o ACTION]"
/bin/echo
/bin/echo " -a NAME"
/bin/echo "   The name of the virtual machine to be fenced."
/bin/echo "   In case it contains spaces, use double quotes."
/bin/echo " -o ACTION"
/bin/echo "   What to do; start|stop|reboot(default)."
/bin/echo
exit 1
}

while [ "$#" -gt 0 ] ; do
case "$1" in
  -a)
   if [ "$2" ] ; then
    vm="$2"
    shift ; shift
   else
    /bin/echo "Missing value for $1."
    /bin/echo
    usage
    shift
   fi
  ;;
  -o)
   if [ "$2" ] ; then
    action="$2"
    shift ; shift
   else
    /bin/echo "Missing value for $1."
    /bin/echo
    usage
    shift
   fi
  ;;
  *)
   /bin/echo "Not a know option, $1."
   usage
   shift
  ;;
esac
done

if [ ! "${action}" ] ; then
action=reboot
fi

if [ ! "${vm}" ] ; then
/bin/echo "Error, please specify a name."
usage
fi

check() {
ssh $host "$vboxmanage showvminfo ${vm}" > /dev/null 2>&1
if [ ${?} != 0 ] ; then
  /bin/echo "Error, VM ${vm} not found, choose one of these:"
  ssh $host "$vboxmanage list vms | sed 's/" .*/"/'"
  exit 1
fi
}


stop() {
ssh $host "$vboxmanage controlvm ${vm} poweroff > /dev/null 2>&1"
}

start() {
ssh $host "$vboxmanage startvm ${vm} > /dev/null 2>&1"
}

reboot() {
stop
sleep 3
start
}

case $action in
start)
  check
  start
;;
stop)
  check
  stop
;;
reboot)
  check
  reboot
;;
*)
  /bin/echo "Unknown action: $action"
  /bin/echo
  usage
;;
esac

To use this script, add this block to every "clusternode" in /etc/cluster/cluster.conf:

    <fence>
        <method name="1">
            <device host="robertdebock@clustermember2.example.com" name="vbox" vm="ClusterMember2"/>
        </method>
    </fence>

And create a shared fence device in /etc/cluster/cluster.conf:

    <fencedevices>
        <fencedevice agent="fence_vbox" name="vbox"/>
    </fencedevices>

The important variables are "host" and "vm". "host" is used to connect to the physical box running VirtualBox, "vm" is the actual name of the virtual machine, as displayed by VirtualBox.

Comments

Hi, Please clarify how

Hi,

Please clarify how exactly you provide script with correct $host value?

ssh $host "$vboxmanage controlvm ${vm} poweroff > /dev/null 2>&1"

and one more question:
how will cluster use that command. I receive the following error:
Apr 24 14:39:16 cl01 fenced[4026]: fencing node "cl02.jk.com"
Apr 24 14:39:16 cl01 fenced[4026]: agent "fence_vbox" reports: Error, please specify a name. Usage: /sbin/fence_psbm -a NAME [-o ACTION] -a NAME The name of the virtual machine to be fenced. In case it contains spaces, use double quotes. -o ACTION What to do; start|stop|reboot(default).
Apr 24 14:39:16 cl01 fenced[4026]: fence "cl02.jk.com" failed

Sorry, for spam. :) Issue was

Sorry, for spam. :) Issue was related with that I comment
eval $(cat -)
string.

I am cannot understand why that string is necessary. I did not understand your description below, May you provide me with more details?

What is the script name? As I

What is the script name? As I understood that script should be on the each cluster VM.

After that I add corect variables vm and host into cluster configuration file.

But still not clear how cluster should understand how to execute that script.

Cars and houses are expensive

Cars and houses are expensive and not everyone is able to buy it. But, loans was created to aid people in such hard situations.

Where do you store the

Where do you store the script? (directory/filename)

That's in /sbin You'll find

That's in /sbin

You'll find all kinds of fence_ scripts there. Regards, Robert.

What does the eval $(cat -)

What does the eval $(cat -) do?

It interprets any input

It interprets any input (STDIN) so variables can be passed.

May you describe it in more

May you describe it in more details? I did not understand it.

How about the ssh password?

How about the ssh password?

About Consultancy Articles Contact




References Red Hat Certified Architect By Robert de Bock Robert de Bock
Curriculum Vitae By Fred Clausen +31 6 14 39 58 72
By Nelson Manning robert@meinit.nl