#!/bin/ksh 
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/utils/cl_setprkey.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2000,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)45	1.4 src/43haes/usr/sbin/cluster/events/utils/cl_setprkey.sh, hacmp.events, 61haes_r714 3/26/07 17:52:19

###############################################################################
#
#
#   COMPONENT_NAME: EVENTUTILS
#
#   FUNCTIONS: Set persistent reserve key in ODM
#
#		arguments:	none
#
#		returns:	0 - persistent reserve key set up
#					? - failure
#
#	ENVIRONMENT:	PATH, LOCALNODENAME
#
#   ORIGINS: 27
#
###############################################################################
PROGNAME=$(basename ${0})
[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
[[ "$VERBOSE_LOGGING" = "high" ]] && version='1.4'
set -u

###############################################################################
#
#	Name:		newkey
#
#	Function:	Derive the persistent reserve key used by this node
#
#	Returns:	Persistent reserve key written to stdout
#
#	Input:		None
#
#	Environment:	LOCALNODENAME set by cluster manager
#					cluster id in HACMPcluster ODM stanza
#					node id in HACMPnode stanza
#
###############################################################################
newkey() {

	typeset PS4_FUNC="newkey"
	[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
	typeset clusterid planarid nodeid

	#	Take 2 bytes of cluster id - note the use of <tab> below - be careful
	#	when editing.,
	clusterid=$(odmget HACMPcluster | grep '	id =' | cut -f2 -d'=')
	typeset -Z2 clusterid

	#	Take 4 bytes of hardware planar id
	planarid=$(/usr/bin/uname -m | cut -c3-8)
	typeset -Z4 planarid

	#	Take 2 byte of cluster node id - note the use of <tab> below - be
	#	careful when editing.
	nodeid=$(odmget -q "name = $LOCALNODENAME" HACMPnode | grep '	node_id =' \
		| uniq | cut -f2 -d'=')
	typeset -Z2 nodeid

	#
	#	construct the persistent reserve key this node should use
	#
	#	The value returned is:
	#
	#		xxyyzzzz
	#
	#			where:	zzzz - low order 4 digits of planar ID            
	#					yy	 - low order 2 digits of HACMP node ID
	#					xx   - low order 2 digits of cluster ID
	#
	print ${clusterid}${nodeid}${planarid}

	return 0
}

###############################################################################
#
#	Main routine
#
#	Function:	Set up persistent reserve key
#
###############################################################################

#
#	If there are any vpath devices on this node, we need to make sure that
#	they use the proper persistent reserve key
#
if vpath_list=$(ls /dev/vpath* 2>/dev/null) ; then 

	#	construct the persistent reserve key this node should use
	new_prkey=$(newkey)

	#
	#	This defines the ODM stanza that holds the persistent reserve key:
	#
	#	CuAt:
	#		name = "ioaccess"
	#		attribute = "preservekey"
	#		value = "xxxxxxxx"
	#		type = "R"
	#		generic = ""
	#		rep = "s"
	#		nls_index = 0
	#
	key_classname="CuAt"
	key_line="preservekey"
	key_criteria="ioaccess"
	current_prkey=$(odmget -q "name = $key_criteria and attribute = $key_line" \
		$key_classname | grep '	value =' | cut -f2 -d'"')
	#
	#	If there's no persistent reserve key attribute in ODM, or if
	#	what's there doesn't match the new one we just constucted, update
	#	the ODM.
	#
	if [[ -z $current_prkey || $current_prkey != $new_prkey ]] ; then
		#
		#	Build a new ODM stanza in a temporary file
		#
		echo "$key_classname:" > /tmp/cl_setprkey.$$
		echo "	name = $key_criteria" >> /tmp/cl_setprkey.$$
		echo "	attribute = $key_line" >> /tmp/cl_setprkey.$$
		echo "	value = $new_prkey" >> /tmp/cl_setprkey.$$
		echo "	type = R" >> /tmp/cl_setprkey.$$
		echo "	rep = s" >> /tmp/cl_setprkey.$$

		#
		#	update the PR key in ODM, both in the current CuAt and in the
		#	saved one in the RAM file system used during boot/config
		#
		if [[ -n $current_prkey ]] ; then
			#	Update any existing key
			odmchange -q "name = $key_criteria and attribute = $key_line" \
				-o $key_classname /tmp/cl_setprkey.$$
			odmrc=$?
		else
			#	Add a stanza for a key, if its not there already
			odmadd /tmp/cl_setprkey.$$
			odmrc=$?
		fi
		if (( 0 == $odmrc )) ; then
			#	push the change out to the ODM on the boot device
			savebase

			#	Clean up the temporary file
			rm /tmp/cl_setprkey.$$

			#
			#	Finally, force a reconfiguration of all the vpath devices on this
			#	node, so that they'll pick up the new key
			#
			[[ "$VERBOSE_LOGGING" = "high" ]] && vflag='-v' || vflag=''
			parentlist=""
			for vpathdev in $vpath_list ; do
				#	find the parent of the given vpath device
				#	Note that each parent need be done only once
				parent=$(lsdev -Cc disk -l ${vpathdev##/dev/} -F parent)
				if ! echo $parentlist | grep -wq $parent ; then
					#	and reconfigure it and its descendents,
					#	to cause it to pick up the new key
					cfgmgr -l $parent $vflag
					#	remember which ones we've done so far
					parentlist="$parentlist $parent"
				fi
			done
		else
			return $odmrc
		fi
	fi
fi

return 0
