#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                     
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2022.  All rights reserved.         	
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                       
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/events/utils/cl_scsipr_event_functions.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2015 
# 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 
# @(#)  e60e57c 43haes/usr/sbin/cluster/events/utils/cl_scsipr_event_functions.sh, 61aha_r726, 2205A_aha726, Apr 01 2022 02:05 PM

export PATH=$(/usr/es/sbin/cluster/utilities/cl_get_path all)

###########################################################################
#	Function: node_up_scsipr_init
#	called from: node_up
#	Description: This function sets the prkey and policy(PR_shared)
#	of all the VGs which are HACMP resource
#	returns: 0, on success
#	         1, otherwise
#
##########################################################################


function node_up_scsipr_init {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset VGs="" #Volume Groups defined on this node
	typeset VG=""
	typeset PR_Key=""	# Persistent Reserver Key value
	typeset -i RC=0		# Return variable
	typeset -i RC1=0	# Return variable
	typeset -i flag=0

	typeset LOCALNODENAME=$(get_local_nodename)

        if (( $# > 0 )); then
           VGs="$@"
        fi

	PR_Key=$(clpr_obtain_prkey $LOCALNODENAME)
	if (( $? != 0 ))
	then

		#
		: Failed generating the PR Key of the node $LOCALNODENAME.
		#
		return 1
	fi


        if [[ -z $VGs ]]; then
           VGs=$(print $(lsvg -L 2> /var/hacmp/log/${PROGNAME}.LSVG.ERR | egrep -vw 'rootvg|caavg_private|altinst_rootvg|old_rootvg'))
        fi

	for VG in $VGs
	do
		typeset resgrp=$(clodmget -q "name like '*VOLUME_GROUP' and value = $VG" -f group -n HACMPresource)
		if [[ -n $resgrp ]]
		then

			#
			:   Volume group $VG is an HACMP resource
			#
			clpr_verifycap_vg $VG
			RC=$?
			if (( $RC == 0 ))
			then

				#Check if the policy is already set to PR_shared 

				typeset disklist=$(lspv -L | grep -w $VG | awk '{print $1}')
				if [[ -z $disklist ]]
				then

					#
					: Unable to get the list of hdisks.
					#
					return 1
				fi
				typeset hdisk=""
				for hdisk in $disklist
				do
					typeset policy=$(clpr_Get_policy $hdisk)
					if (( $? != 0 ));then
						return 1
					elif [[ $policy == PR_shared ]];then
						typeset pr_key=$(clpr_Get_prkey $hdisk)
						if (( $? != 0 ));then
							return 1
						elif [[ $pr_key == $PR_Key ]];then
							flag=1
							continue
						else
							flag=0
							break
						fi
					else
						flag=0
						break
					fi
				done
				
				# Set the PR policy to the PR_shared and set the PR Key 
				if (( $flag == 0 ));then
					clpr_Set_policy_prkey_vg $VG PR_shared $PR_Key
					if (( $? != 0 ))
					then

						#
						: Failed to set either policy or PR Key on the VG $VG.
						#
						# Continue processing the next Volume Group
						continue
					fi
				fi

				# Register and reserve the disks of Volume Group
				clpr_reg_res_vg $VG $PR_Key
				if (( $? != 0 ))
				then
					
					#
					: Failed to register and reserve the VG $VG from node $LOCALNODENAME.
					: Unregistering from the VG in case some of the disk got reserved.
					: VG $VG will not come online and corresponding RG will go to error state.
					#
					/usr/bin/errlogger "$PROGNAME : Failed to register and reserve $VG."
					clpr_removeReg_vg $VG 

					# Continue processing the next Volume Group
					continue
				fi

				#
				: Checking if reservation succeeded
				#
				clpr_verifyKey_vg $VG $PR_Key
				RC1=$?
				if (( $RC1 == 1 ))
				then
					
					#
					: No reservation exists on the VG $VG for node $LOCALNODENAME.
					#
				elif (( $RC1 == 0 ))
				then

					# 
					: Reservation success
					#
				else
		
					#
					: Failed to verify if the PR_Key of node $LOCALNODENAME got registered.
					#
				fi	

			elif (( $RC == 1 ))
			then

				#
				: VG $VG is not SCSIPR Type 7 capable.
				: $VG will not be varied on and corresponding RG will go to error state.
				: There are one or more disks in the VG $VG which are not SCSIPR capable.
				: Select the disks which are SCSIPR Type 7 capable or disable Disk Fencing
				: in order bring this Volume Group online.
				#
			else

				#
				: Failed to verify SCSIPR capability of the VG $VG.
				: VG $VG will not be varied on and corresponding RG will go to error state.
				#
			fi
		fi
	done

	return 0
}

#######################################################################################
#	Function: node_down_scsipr_term
#	called from: node_down_complete
#	Description: This function clears the reservation on a Volume Group which is a
#	HACMP resource, when last node is going down.
#	retrun: 0 on success, gives a warning on failure
#######################################################################################

function node_down_scsipr_term {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset VGs=""	# List of Volume Groups   
	typeset VG=""

	VGs=$(print $(lsvg -L 2> /var/hacmp/log/${PROGNAME}.LSVG.ERR | egrep -vw 'rootvg|caavg_private'))

	for VG in $VGs
	do
		if [[ -z $(clodmget -q "name like '*VOLUME_GROUP' and value = $VG" -f value -n HACMPresource) ]]
		then

			#
			: VG is not the part of HACMPresource.
			: Hence no SCSIPR action will be taken on VG $VG.
			#
		else

			#
			: Clear registration ond reservation from the Volume Group, $VG
			#
			clpr_clear_vg $VG 
			if (( $? != 0 ))
			then

				#
				: Failed to clear the reservation and registration from the VG $VG.
				: Registration and reservation needed to be cleared in order to
				: use the disks which are part of the VG $VG.
				: Use clmgr clpr_clear hdiskname to clear the reservation from the disks.
				#
			fi
		fi
	done

	return 0
}

######################################################################################
#	Function: cl_confirm_scsipr_res
#	called from: cl_pvo, clvaryonvg, cl_mode3
#	Description: This function checks if the reservation and registration are present
#	on the VG, which is to be varied on.
#	Arguements: Name of the VG
#	returns: 0, on success
#	         1, otherwise 
#
######################################################################################

function cl_confirm_scsipr_res {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset VG=$1
	typeset PR_Key=""	# Persistent Reserve Key value
	typeset -i RC=0 	# return variable
	typeset -i RC=0 	# return variable

	typeset LOCALNODENAME=$(get_local_nodename)

	PR_Key=$(clpr_obtain_prkey $LOCALNODENAME)
	if (( $? != 0 )) 
	then

		#
		: Failed generating the PR Key of the node $LOCALNODENAME.
		#
		return 1
	fi

	# Verifying if the Volume Group $VG is SCSIPR Type 7 capable

	clpr_verifycap_vg $VG
	RC=$?
	if (( $RC == 0 )) 
	then

		# 
		:  Volume Group $VG is SCSIPR Type 7 capable.
		:  Verifying if the PR Key of the $LOCALNODENAME is registered.
		#
		clpr_verifyKey_vg $VG $PR_Key
		RC1=$?
		if (( $RC1 == 0 ))
		then

			#
			: PR Key of $LOCALNODENAME is present on the Volume Group, $VG.
			#
			return 0
		elif (( $RC1 == 1 ))
		then

			#
			: No reservation exists on Volume Group $VG.
			: Volume Group $VG will not be varied on and corresponding RG will go to error state.
			#
		else
		
			#
			: Failed reading reservation on VG $VG from $LOCALNODENAME.
			: Volume Group $VG will not be varied on and corresponding RG will go to error state.
			#
		fi
	elif (( $RC == 1 ))
	then
		#
		: Volume Group $VG is not SCSIPR Type 7 capable.
		: Volume Group $VG will not be varied on and corresponding RG will go to error state.
		#
	else

		#
		: Failed to verify SCSIPR capability of the VG $VG.
		: Volume Group $VG will not be varied on and corresponding RG will go to error state.
		#
	fi

	return 1
}

#######################################################################################
#	Function: disk_replace_scsipr
#	called from: cldiskreplace
#	Description: This function clears the registration and reservation on the source
#			disk and register and reserve the new disk.
#	returns: 0, on success
#	         1, otherwise 
#
########################################################################################

function disk_replace_scsipr {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset source_hdisk=$1
	typeset replacement_hdisk=$2
	typeset VG_name=$3
	typeset PR_Key="" 	# Persistent Reserve Key value
	typeset -i RC=0         # return variable

	typeset LOCALNODENAME=$(get_local_nodename)

	#
	:   Find out whether cluster services are active on this node
	#
	if  LC_ALL=C lssrc -ls clstrmgrES 2>&1 | grep "Current state:" | egrep -q -v "ST_INIT|NOT_CONFIGURED"
	then

		#
		:    Checking if $VG_name is SCSIPR Type 7 capable.
		:    If it is so, then clear the reservation from $source_hdisk.
		#
		clpr_verifycap_vg $VG_name
		if (( $? == 0 ))
		then

			#
			:    Registering and Reserving the $replacement_hdisk if
			:    it is SCSIPR Type 7 capable.
			#
			clpr_verifycap $replacement_hdisk
			RC=$?
			if (( $RC == 0 ))
			then
				#  Generate the PR Key for the node.
				PR_Key=$(clpr_obtain_prkey $LOCALNODENAME)
				if (( $? != 0 ))
				then

					#
					: Failed generating the PR Key of the node $LOCALNODENAME.
					: 
					return 1
				fi

				# Set PR Key and policy(PR_shared) on replacement_hdisk.
				clpr_Set_policy_prkey $replacement_hdisk PR_shared $PR_Key
				if (( $? == 0 )) 
				then
					# Register and reserve replacement_hdisk.
					clpr_reg_res $replacement_hdisk $PR_Key
					if (( $? != 0 ))
					then

						#
						: Failed to register and reserve the hdisk $replacement_hdisk for node $LOCALNODENAME.
						#
						/usr/bin/errlogger "$PROGNAME : Failed to register and reserve $replacement_hdisk."
						return 1
					fi
				else

					#
					: Failed setting the Policy or PR Key.
					#
					return 1
				fi
			elif (( $RC == 1 ))
			then

				#
				: Disk $replacement_hdisk is not SCSIPR Type 7 capable.
				: This disk can not be replaced. Disable Disk Fencing in order to use this disk.
				#
				return 1
			else

				#
				: Failed to verify SCSIPR capability of the Disk $replacement_hdisk.
				#
				return 1
			fi
		fi
	fi

	#
	:   Clear the registration and reservation from $source_hdisk.
	#
	clpr_clear $source_hdisk
	if (( $? != 0 ))
	then

		#
		: Failed to clear the reservations on disk $source_hdisk.
		: Please use clmgr clpr_clear hdiskname to clear the reservation on the disk before using it again.
		#
	fi

	return 0
}


##################################################################################
#	Function: cl_scsipr_dare_Reg_Res
#	called from: reconfig_resource_acquire, cl_mkvg, cl_mkvg4vp
#	Description: This function register and reserve new VG being created.
#	returns: 0, on success
#	         1, otherwise
#
##################################################################################

function cl_scsipr_dare_Reg_Res {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
	
	typeset VG=$1
	typeset PR_Key="" 	# Persistent Reserve Key value
	typeset -i RC=0 	# return variable
	typeset -i RC1=0     	# return variable
	typeset -i flag=0   # If 1 policy and PR Key are already set.
						# if 0 policy and PR Key are to be set. 
	typeset LocalNode=$(get_local_nodename)

	PR_Key=$(clpr_obtain_prkey $LocalNode)
	if (( $? != 0 ))
	then

		#
		: Failed generating the PR Key of the node $LocalNode.
		#
		return 1
	fi

	#  Verifying if the Volume Group is SCSIPR Type 7 capable.
	clpr_verifycap_vg $VG
	RC=$?
	if (( $RC == 0 ))
	then

		#Check if the policy is already set to PR_shared

		typeset disklist=$(lspv -L | grep -w $VG | awk '{print $1}')
		if [[ -z $disklist ]]
		then

			#
			: Unable to get the list of hdisks.
			#
			return 1
		fi
		typeset hdisk=""
		for hdisk in $disklist
		do
			typeset policy=$(clpr_Get_policy $hdisk)
			if (( $? != 0 ));then
				return 1
			elif [[ $policy == PR_shared ]];then
				typeset pr_key=$(clpr_Get_prkey $hdisk)
				if (( $? != 0 ));then
					return 1
				elif [[ $pr_key == $PR_Key ]];then
					flag=1
					continue
				else
					flag=0
					break
				fi
			else
				flag=0
				break
			fi
		done

		# Set the PR policy to the PR_shared and set the PR Key
		if (( $flag == 0 ));then
			clpr_Set_policy_prkey_vg $VG PR_shared $PR_Key
			if (( $? != 0 ))
			then

				#
				: Failed to set either policy or PR Key on the VG $VG.
				#
				return 1 
			fi
		fi

		#
		:  Registering and reserving PR Key of node, $LocalNode on Volume Group, $VG.
		#
		clpr_reg_res_vg $VG $PR_Key
		if (( $? != 0 ))
		then

			#
			: Failed to register and reserve the VG $VG for node $LocalNode.
			: Unregistering from the VG in case some of the disk got reserved.
			#
			/usr/bin/errlogger "$PROGNAME : Failed to register and reserve $VG."
			clpr_removeReg_vg $VG 
			return 1
		fi

		#
		: Checking if reservation succeeded
		#
		clpr_verifyKey_vg $VG $PR_Key
		RC1=$?
		if (( $RC1 == 0 ))
		then

			#
			: Registration and reservation succeeded.
			#
			return 0
		elif (( $RC1 == 1 ))
		then

			#
			: No reservation exists on the VG $VG.
			#
		else

			#
			: Failed Verifying the PR Key of node $LocalNode on VG $VG. 
			#
		fi
	elif (( $RC == 1 ))
	then

		#
		: Volume Group $VG is not SCSIPR Type 7 capable.
		#
	else

		#
		: Failed to verify SCSIPR capability of the VG $VG.
		#
	fi

	return 1 
}

#########################################################################
#	Function: clpr_Get_policy
#	Description: Get the current 'reserve_policy'  (via lsattr -El)
#	             input Diskname whose policy needs to be retrived
#	returns: 0 on success
#	         10 otherwise
##########################################################################
function clpr_Get_policy {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset diskname=$1
	typeset policy=""	# Reserve Policy

	# Get current reserve_policy
	policy=$(lsattr -El $diskname -a reserve_policy | awk '/reserve_policy/ {print $2}')

	if [ -z "$policy" ]
	then

		#
		: Unable to get reserve_policy of $diskname from lsattr.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to get ODM reserve_policy. Cannot register/reserve $diskname."
		return 10 
	fi

  	echo "$policy"
  	return 0
}

#########################################################################
#	Function: clpr_Get_prkey
#	Description: Get current PR_key_value  (via lsattr -El)
#	Arguments: Diskname
#	returns: an error code.  (0 - success)
##########################################################################
function clpr_Get_prkey {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset diskname=$1
	typeset prkey="" 	# Persistent Reserve Key value

	# Get current pr_key
	prkey=$(lsattr -El $diskname -a PR_key_value |  awk '/PR_key_value/ {print $2}')
	if [ -z "$prkey" ]
	then

		#
		: Unable to get PR_key_value of $diskname from lsattr.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to get ODM PR_key_value. Cannot register/reserve $diskname."
		return 10 
	fi

	echo "$prkey"
	return 0
}

#########################################################################
#	Function: clpr_Set_policy_prkey
#	Description: Set reserve_policy and PR_key_value'  (via chdev)
#	Arguments: Diskname, policy  and prkey to be set
#	returns an error code.  (0 - success)
##########################################################################
function clpr_Set_policy_prkey {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset diskname=$1
	typeset policy=$2
	typeset prkey=$3
	typeset verify_policy=""
	typeset verify_prkey=""

	# Set require prkey first and later reserve_policy
	chdev -l $diskname -a PR_key_value=$prkey -a reserve_policy=$policy 1>/dev/null
	if  (( $? != 0 )) 
	then

		#
		: Unable to set prkey $prkey and policy $policy for $diskname using chdev.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $diskname."
		return 11 
	fi

	# Verify if policy is set correctly
	verify_policy=$(lsattr -El $diskname -a reserve_policy | awk '/reserve_policy/ {print $2}')
	if [ -z "$verify_policy" ]
	then

		#
		: Unable to get reserve_policy for $diskname from lsattr.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $diskname."
		return 10
	fi

	if [ $verify_policy != $policy ]
	then
		
		#
		: reserve_policy $policy for disk $diskname was not set.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $diskname."
		return 1
	fi

	# Verify if required PR_key_value is set
	verify_prkey=$(lsattr -El $diskname -a PR_key_value |  awk '/PR_key_value/ {print $2}')
	if [ -z "$verify_prkey" ]
	then

		#
		: Unable to get PR_key_value for $diskname from lsattr.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $diskname."
		return 10
	fi

	if [ $verify_prkey != $prkey ]
	then
	
		#
		: PR_key_value $prkey for $diskname was not set.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $diskname."
		return 1
	fi
	
	return 0
}

#########################################################################
#	Function: clpr_Get_policy_vg()
#	Description: Get the current 'reserve_policy'  (via lsattr -El)
#	Arguments: VGname 
#	returns: an error code.  (0 - success)
##########################################################################
function clpr_Get_policy_vg {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset vgname=$1
	typeset -i count=0
	typeset array=""
	typeset policy=""

	array=$(lspv -L | grep -w $vgname | awk '{print $1}')
	if [ -z "$array" ]
	then
	
		#
		: Unable to get the list of hdisks.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to get ODM reserve_policy. Cannot register/reserve $vgname."
		return 1
	fi

	set -A disk_list $array
	count=$(lspv -L | grep -wc $vgname)
	if (( $? != 0 ))
	then

		#
		: Unable to get the number of disks in VG $vgname.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to get ODM reserve_policy. Cannot register/reserve $vgname."
		return 1
	fi

	# Get current reserve_policy

	typeset -i i=0
	while (( $i < $count ))
	do
		policy=$(/usr/sbin/lsattr -El ${disk_list[i]} -a reserve_policy | awk '/reserve_policy/ {print $2}')
		if [ -z "$policy" ]
		then

			#
			: Unable to get reserve_policy of disk ${disk_list[i]}. 
			#
			/usr/bin/errlogger "$PROGNAME : Failed to get ODM reserve_policy. Cannot register/reserve $vgname."
			return 10
		fi

		#
		: Current reserve policy of ${disk_list[i]} is $policy.
		#
		(( i = $i + 1 )) 
	done

	return 0
}

#########################################################################
#	Function: clpr_Get_prkey_vg
#	Description: Get current PR_key_value  (via lsattr -El)
#	Arguments: VGname
#	returns: an error code.  (0 - success)
##########################################################################
function clpr_Get_prkey_vg {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset vgname=$1
	typeset array=""
	typeset pr_key=""
	typeset -i count=0
	typeset -i i=0

	array=$(lspv -L | grep -w $vgname | awk '{print $1}')
	if [ -z "$array" ]
	then

		#
		: Unable to get the list of disks for VG $vgname.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to get ODM PR_key_value. Cannot register/reserve $vgname."
		return 1
	fi

	set -A disk_list $array

	count=$(lspv -L | grep -wc $vgname)
	if (( $? != 0 ))
	then

		#
		: Unable to get the number of disks of VG $vgname.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to get ODM PR_key_value. Cannot register/reserve $vgname."
		return 1
	fi

	# Get current pr_key

	while (( $i < $count ))
	do
		pr_key=$(lsattr -El ${disk_list[i]} -a PR_key_value | awk '/PR_key_value/ {print $2}')
		if [ -z "$pr_key" ]
		then

			#
			: Unable to get PR_key_value of disk ${disk_list[i]} from lsattr.
			#
			/usr/bin/errlogger "$PROGNAME : Failed to get ODM PR_key_value. Cannot register/reserve $vgname."
			return 10
		fi

		#
		: Current pr_key of ${disk_list[i]} is $pr_key.
		#
		(( i = $i + 1 ))
	done

	return 0
}

#########################################################################
#	Function: clpr_Set_policy_prkey
#	Description: Set reserve_policy and PR_key_value'  (via chdev)
#	Arguments: VGName, policy and prkey
#	returns: an error code.  (0 - success)
##########################################################################
function clpr_Set_policy_prkey_vg { 

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset vgname=$1
	typeset policy=$2
	typeset prkey=$3
	typeset array=""
	typeset -i count=0
	typeset -i i=0
	typeset verify_prkey=""

	array=$(lspv -L | grep -w $vgname | awk '{print $1}')
	if [ -z "$array" ]
	then

		#
		: Unable to get the list of disks for VG $vgname.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $vgname."
		return 1
	fi

	set -A disk_list $array

	count=$(lspv -L | grep -wc $vgname)
	if (( $? != 0 ))
	then

		#
		: Unable to get the number of disks of VG $vgname.
		#
		/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $vgname."
		return 1
	fi

	typeset -i i=0
	while (( $i < $count ))
	do
		###########################################################################
		# Set pr_key first and later reserve_policy, a requirement on emc devices #
		###########################################################################
		chdev -l ${disk_list[i]} -a PR_key_value=$prkey -a reserve_policy=$policy 1>/dev/null
		if (( $? != 0 ))
		then

			#
			: Unable to set prkey $prkey and policy $policy for disk ${disk_list[i]} from chdev.
			#
			/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $vgname."
			return 11
		fi

		# Verify if required PR_key_value is set
		verify_prkey=$(lsattr -El ${disk_list[i]} -a PR_key_value |  awk '/PR_key_value/ {print $2}')
		if [ -z "$verify_prkey" ]
		then

			#
			: Unable to get pr_key_value for ${disk_list[i]}.
			#
			/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $vgname."
			return 10
		fi

		if [ $verify_prkey != $prkey ]
		then

			#
			: PR_key_value $prkey for disk ${disk_list[i]} was not set.
			#
			/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $vgname."
			return 1
		fi

		# Verify if policy is set correctly
		verify_policy=$(lsattr -El ${disk_list[i]} -a reserve_policy | awk '/reserve_policy/ {print $2}')
		if [ -z "$verify_policy" ]
		then

			#
			: Unable to get reserve_policy for disk ${disk_list[i]}.
			#
			/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $vgname."
			return 10
		fi

		if [ $verify_policy != $policy ]
		then

			#
			: Reserve_policy $policy for disk ${disk_list[i]} was not set.
			#
			/usr/bin/errlogger "$PROGNAME : Failed to set ODM reserve_policy and PR_key_value. Cannot register/reserve $vgname."
			return 1
		fi
		(( i = $i +1 ))
	done

	return 0
}


#########################################################################
# 	Function: clpr_obtain_prkey
#	Description: Generate unique PR_key for the node
#	PR_KEY format: "node id" "followed by three Zeros" "cluster id"
#	Arguments: Nodename 
#	returns: an error code.  (0 - success)
##########################################################################
function clpr_obtain_prkey { 

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset node=$1

	# Get Cluster ID to generate PR_Key
	cid=$(clodmget -f id HACMPcluster)
	if (( $? != 0 ))
	then

		#
		: Failed to get Cluster ID via clodmget from $node.
		#
		return 1
	fi

	if [ -z "$cid" ]
	then

		#
		: Cluster ID is empty from $node.
		#
		return 1
	fi


	# Get Node ID to generate PR_Key
	nid=$(clodmget -n -f node_id -q "name=$node and object=COMMUNICATION_PATH" HACMPnode)
	if (( $? != 0 )) 
	then

		#
		: Failed to get Node ID via clodmget from $node.
		#
		return 1
	fi

	if [ -z "$nid" ]
	then
		
		#
		: Node ID is empty for $node.
		#
		return 1
	fi
	if [ $nid -eq 0 ]
	then

		#
		: Node ID is zero for $node.
		#
		return 1
	fi


	typeset seperator=000
	# Put to PR_KEY format: "node id" "followed by three Zeros" "cluster id"
	pr_key_value=$nid$seperator$cid
	echo "0x$pr_key_value"

	return 0
}

###################################################################################
#	Function: cl_scsipr_Reg_Res
#	called from: cl_extendvg
#	Description: This function register and reserve the new disk/disks being 
#	added to the Volume Group.
#	returns: 0, on success
#	         1, otherwise
#
###################################################################################

function cl_scsipr_Reg_Res {

	typeset Nodename=$(get_local_nodename)
	typeset -i RC1=0
	typeset disk=$1

	clpr_verifycap $disk
	RC1=$?
	if (( $RC1 == 0 ))
	then
		PR_Key=$(clpr_obtain_prkey $Nodename)
		if (( $? != 0 ))
		then

			#
			: Failed generating the PR Key of the node $Nodename.
			#
			return 1
		fi	

		# Set policy and PR Key on disk being added
		clpr_Set_policy_prkey $disk PR_shared $PR_Key
		if (( $? == 0 ))
		then
			clpr_reg_res $disk $PR_Key
			if (( $? == 0 ))
			then
				# Continue to process new disks if any.
				continue  
			else

				#
				: Failed to register and reserve the hdisk $disk for node $Nodename.
				#
				return 1
			fi
		else

			#
			: Failed setting the Policy or PR Key.
			#
			return 1
		fi
	elif (( $RC1 == 1 ))
	then

		#
		: Disk $disk is not SCSIPR Type 7 capable.
		#
		return 1
	else

		#
		: Failed to verify SCSIPR capability of the Disk $hdisk.
		#
		return 1
	fi

	return 0
}
