#!/bin/sh
#############################################################################
# Script to enable and start, or disable and stop, vhostmd with an associated
# SR, VDI and PBD.
# Note that enabling it involves restarting xapi.
# 
# Usage:
#   xe-vhostmd <option>
#
# where <option> is either:
#   enable - Enable and start vhostmd, and create an SR and VDI for its data.
#               CAUTION: Note that this involves restarting xapi.
#   disable - Disable vhostmd, and remove associated SR etc. if not in use.
#
#############################################################################

function restart_xapi {
	service xapi restart
    # ...and wait for it to be fully started
	echo 'Waiting for xapi...'
	XAPI_STARTUP_COOKIE=/var/run/xapi_startup.cookie
	XAPI_INIT_COMPLETE_COOKIE=/var/run/xapi_init_complete.cookie
	until [ -e $XAPI_STARTUP_COOKIE -a -e $XAPI_INIT_COMPLETE_COOKIE ]; do
		sleep 1
	done
}

function usage {
	echo "This script can be used to enable and start, or disable and stop, vhostmd."
	echo "When enabling, it also creates an associated SR, PBD and VDI."
	echo "When disabling, it deletes these and any associated VBDs."
	echo
	echo "Usage:"
	echo "  `basename ${0}` <option>"
	echo
	echo "where <option> is either:"
	echo "enable - Enable and start vhostmd, and create an SR and VDI for its data."
	echo "            CAUTION: Note that this involves restarting xapi."
	echo
	echo "disable - Disable vhostmd, and remove associated SR etc. if not in use."
}

set -e

if [ $# -ne 1 ] ; then
        usage
        exit 1
fi

action=$1
case "${action}" in
    'enable') ;;
    'disable') ;;
    *)
        usage
        exit 1
esac

if [ "${action}" = enable ] ; then
    if chkconfig --level 3 vhostmd && [ -e /opt/xensource/sm/SHMSR ]; then
        echo "vhostmd appears to be enabled already."
		exit 0
    fi

	chkconfig vhostmd reset
	if [ ! -h /opt/xensource/sm/SHMSR ]; then
		echo 'Enabling SRs of type shm in xapi.'
		ln -sf SHMSR.py /opt/xensource/sm/SHMSR
		# Need to restart xapi to notice the link we just created.
		restart_xapi
	fi
	service vhostmd start
	srUuid=`xe sr-create type=shm name-label='SAP Metrics' \
		device-config:location=/dev/shm/vhostmd0 shared=true`
	echo "Created metrics SR with uuid $srUuid"

	for (( i=10 ; i-- ; i > 0 )); do
		sleep 1
		vdiUuid=`xe sr-param-get param-name=VDIs uuid=$srUuid`
		[ -n "${vdiUuid}" ] && break
	done
	if [ -z "${vdiUuid}" ]; then
		echo 'Error: failed to create VDI.'
		exit 1
	fi

	echo "The associated VDI has uuid $vdiUuid"
	echo
	echo "Now you can make this VDI available in your choice of guest VM"
	echo "by using XenCenter to attach the SAP Metrics /dev/shm/vhostmd0 disk"
	echo "or alternatively by using the following sequence of two commands:"
	echo
	echo "xe vbd-create mode=RO vdi-uuid=$vdiUuid \\"
	echo "    device=<N> vm-uuid=<uuid of your chosen VM>"
	echo
	echo "xe vbd-plug uuid=<uuid of the VBD you just created>"
	exit 0

else # disable...

	chkconfig vhostmd off
	[ -n "`service vhostmd status | grep 'stopped$'`" ] || service vhostmd stop
	/bin/rm -f /dev/shm/vhostmd0
	echo "Disabled the vhostmd service."

	for srUuid in `xe sr-list params=uuid type=shm | sed 's/.*: //'`; do
		for vdiUuid in `xe sr-param-get param-name=VDIs uuid=$srUuid | sed 's/;/ /g'`; do
			for vbdUuid in `xe vdi-param-get \
				param-name=vbd-uuids uuid=$vdiUuid | sed 's/;/ /g'`; do
				vmUuid=`xe vbd-param-get param-name=vm-uuid uuid=$vbdUuid`
				if [ 'running' = "`xe vm-param-get param-name=power-state uuid=$vmUuid`" -a \
					'true' = "`xe vbd-param-get param-name=currently-attached uuid=$vbdUuid`" ]; then
					echo "About to unplug  vbd $vbdUuid"
					xe vbd-unplug timeout=10 uuid=$vbdUuid
				fi
				echo "About to destroy vbd $vbdUuid"
				xe vbd-destroy uuid=$vbdUuid
			done
			echo "About to forget vdi $vdiUuid"
			xe vdi-forget uuid=$vdiUuid
		done
		for pbdUuid in `xe sr-param-get param-name=PBDs uuid=$srUuid | sed 's/;/ /g'`; do
			echo "About to unplug  pbd $pbdUuid"
			xe pbd-unplug uuid=$pbdUuid
			echo "About to destroy pbd $pbdUuid"
			xe pbd-destroy uuid=$pbdUuid
		done
		echo "About to forget sr $srUuid"
		xe sr-forget uuid=$srUuid
	done
	/bin/rm -f /opt/xensource/sm/SHMSR
	echo 'xapi will not handle the shm SR type after its next restart.'
	exit 0
fi
