#!/bin/sh
# $Id$
#
# Script:	S20vxm
#
# Description:	RSF-1 service script for Veritas Volume Manager disk groups
#		Diskgroups must be listed in <RSFDIR>/etc/fs/diskgroups.<service>
#		(Use S25vfs to mount filesystems)
#
# Platform:	Solaris
#
# Author:	High-Availability.Com Ltd
#

. /opt/HAC/bin/rsf.sh

service=${RSF_SERVICE:-"servicename"}	# short service name
dgtab="${PRODUCT_ETC}/fs/diskgroups.${service}"		# Veritas disk group name

maxtries=3	# max. no of attempts at execution
script="`basename $0`"
state="$1"
attempts=${2:-'1'}

if [ ${attempts} -gt ${maxtries} ]; then
	echo "Too many retries, aborting!"
	exit ${COMPANY_ABORT}
fi

# ensure we can find commands
PATH=/usr/sbin:${PATH}
export PATH

if [ ! -f "${dgtab}" ]; then
	dated_echo "Couldn't find diskgroups list ${dgtab}, aborting"
	exit ${COMPANY_ABORT}
fi
# read diskgroup namss from file
diskgroups=`awk '!/^#/ { print $1 }' ${dgtab}`

case "${state}" in
'start')
	for dgroup in ${diskgroups}; do

		dated_echo "Importing ${dgroup} disk group ... \c"
		vxdg -t import ${dgroup} >/dev/null 2>&1
		case $? in
		0)
			echo "done"
			;;
		12)
			echo "already imported, proceeding"
			;;
		*)
			echo "failed!"
			dated_echo "Attempting forced import ... \c"
			vxout=`vxdg -tfC import ${dgroup} 2>&1`
			if [ $? -ne 0 ]; then
				echo "failed!"
				dated_echo "*** ERROR - Failed to import ${dgroup} disk group:"
				dated_echo ${vxout}
				dated_echo "Aborting ${state}"
				exit ${COMPANY_ABORT}
			else
				echo "done"
			fi
			;;
		esac
	
		dated_echo "Starting volumes in ${dgroup} ... \c"
		vxvol -g ${dgroup} -o bg startall 2>&1
		if [ $? -ne 0 ]; then
			echo "failed!"
			dated_echo "*** ERROR! Unable to start ${dgroup} volumes:"
			dated_echo "Restarting ${service}"
			exit ${COMPANY_RESTART}
		else
			echo "done"
			sleep 2	# give 'em chance
		fi

	done
	;;

'stop')
	abort=0	# flag
	for dgroup in ${diskgroups}; do

		dated_echo "Deporting ${dgroup} disk group ... \c"
		vxout=`vxdg deport ${dgroup} 2>&1`
		case $? in
		0)
			echo "done"
			;;
		11)
			echo "not imported, skipping"
			;;
		*)
			echo "failed!"
			dated_echo "*** ERROR - Failed to deport ${dgroup} disk group:"
			dated_echo ${vxout}
			dated_echo "Please investigate"
			abort=1
			;;
		esac

	done

	if [ ${abort} -ne 0 ]; then
		dated_echo "Problems deporting diskgroup(s), aborting"
		exit ${COMPANY_ABORT}
	fi
	;;

*)
	echo "Usage: ${script} <start|stop>"
	exit ${COMPANY_WARN}
	;;

esac

exit ${COMPANY_OK}
