#!/bin/ksh93 
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/utils/cl_vg_fence_redo.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2013,2014 
# 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 
# @(#)93  1.3  src/43haes/usr/sbin/cluster/events/utils/cl_vg_fence_redo.sh, hacmp.events, 61haes_r714, 1442A_hacmp714 10/10/14 00:47:45
#
###############################################################################
#
#   Name:	cl_vg_fence_redo
#
#   Function:	Remove any fence group created by PowerHA for a given volume
#		group, and recreate the fence group reflecting the current 
#		volume group disk membership.
#
#		This is necessary if the disk membership in a volume group has
#		changed, by extendvg or reducevg, there being no convenient
#		mechanism to modify a fence group membership.
#
#
#   Input:	Optional '-c' operand, indicating that failures due to current
#		fence group not found should be ignored
#
#		Volume group name.  If the volume group is not known on the
#		current node, any existing fence group will be removed.
#		Conversely, even if there is no current fence group for this
#		volume group, a new one will be created.
#
#		Optional fence height (as accepted by cl_vg_fence_init) to 
#		leave the new fence group.  If this value is not specified,
#		the fence height will be set according to the current
#		activation state of the volume group.
#
#
#   Output:	Any messages from fence group operations.
#
###############################################################################
PROGNAME=${0##*/}
if [[ $VERBOSE_LOGGING == "high" ]]
then
    set -x
    version='1.3'
fi

integer RC=0		    #	Saved return code

#
:   Check for optional '-c' parameter
#
if [[ $1 == '-c' ]]
then
    c_flag='-c'		    #	Ignore fence group not found errors
    shift
fi

VG=$1			    #	Volume group name
UUID_file="/usr/es/sbin/cluster/etc/vg/${VG}.uuid"  #	UUID tracking file for $VG
fence_height=$2		    #	New fence height

if [[ -s $UUID_file ]]	    #	UUID tracking file exists and is non-empty
then
    #
    :	If there is a UUID tracking file for this volume group $VG, 
    :	remove any fence group currently in place
    #
    #	These operations could fail if the UUID tracking file is stale,
    #	dating from a prior IPL
    #
	cl_vg_fence_term $c_flag $VG
    rm -f $UUID_file				    #	clean up UUID tracking file
fi

if [[ -z $fence_height ]]
then
    #
    :	No value given for the fence height.  Determine it from the current
    :	volume group activation state
    #
    if lsvg -L -o | grep -qx $VG
    then
	fence_height=rw	    #	read/write if volume group is online
    else
	fence_height=ro	    #	read-only if volume group is offline
    fi
fi

#
:   Rebuild the fence group for $VG
:   First, find the disks in the volume group
#
VGID=$(/usr/sbin/getlvodm -v $VG)	#   LVM requires VGID for other commands

if [[ -n $VGID ]]	    #	Volume group known
then
    #
    :	Create a fence group for $VG
    #
    PV_disk_list=$(/usr/sbin/getlvodm -w $VGID | cut -f2 -d' ')
    cl_vg_fence_init -c $VG $fence_height $PV_disk_list
    RC=$?
    :	Exit status is $RC from cl_vg_fence_init $VG $fence_height $PV_disk_list

    if (( $RC != 0 )) 
    then
	if [[ $fence_height == 'rw' ]]
	then
	    dsp_fh=$(dspmsg -s 103 cspoc.cat 350 'read only,read/write' | cut -f2 -d,)
	else
	    dsp_fh=$(dspmsg -s 103 cspoc.cat 350 'read only,read/write' | cut -f1 -d,)
	fi
	dspmsg scripts.cat 10511 "$PROGNAME: Volume group $VG fence height could not be set to $dsp_fh" $PROGNAME $VG $dsp_fh
    fi
    return $RC		    #	Pass back any return code
else
    #
    :	LVM does not have a VGID for $VG.  The volume group is 
    :	not known.
    #
    dspmsg scripts.cat 503 "Volume group %s not found\n" $VG
    return 1		    #	Volume group not known to LVM
fi
