#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/release_vg_fs.sh 1.4.13.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,2013 
# 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 
# @(#)92        1.4.13.2  src/43haes/usr/sbin/cluster/events/release_vg_fs.sh, hacmp.events, 61haes_r714 10/31/13 10:33:24
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#                                                                       #
#       Name:           release_vg_fs                                   #
#                                                                       #
#	Description:	This script varies off volume group(s) and      #
#			unmounts filesystem(s).  			#
#									#
#	Called by:	node_down_local, node_up_remote			#
#									#
#	Calls to:	cl_fs2disk, cl_deactivate_fs, cl_deactivate_vgs #
#									#
#       Arguments:      filesystem(s) volume-group(s)                   #
#			oem_filesystem(s) oem_volumegroup(s)		#
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#			2	bad argument				#
#                                                                       #
#########################################################################

. /usr/es/sbin/cluster/events/reconfig_udresources

PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"

[[ $VERBOSE_LOGGING == high ]] && set -x
[[ $VERBOSE_LOGGING == high ]] && version='1.4.13.2 $Source: 61haes_r711 43haes/usr/sbin/cluster/events/release_vg_fs.sh 4$'

STATUS=0
integer DEF_VARYON_ACTION=0     # RR method said override def varyoff action


if [[ $PRINCIPAL_ACTION != "RELEASE" && $AUXILLIARY_ACTION != "RELEASE_SECONDARY" ]]
then
   return 0
fi


FILE_SYSTEMS=$1
VOLUME_GROUPS=$2
OEM_FILE_SYSTEMS=$3
OEM_VOLUME_GROUPS=$4
VG_MOD="false"
SELECTIVE_FAILOVER="false"
integer DEF_VARYOFF_ACTION=0     # RR method said override def varyoff action


#
:   If ALL file systems are specified, find the file systems in the volume
:   groups for the resource group
#
if [[ $FILE_SYSTEMS == "ALL" ]]
then
    FILE_SYSTEMS=""
    if [[ -z $VOLUME_GROUPS ]]
    then
	#
    	:   If volume groups were not explicitly specified, get them from
	:   the resource group definition
	#
	VOLUME_GROUPS=$(clodmget -q "group=$GROUPNAME AND name like '*VOLUME_GROUP'" -f value -n HACMPresource)
	VG_MOD="true"
    fi
     
    if [[ -n $VOLUME_GROUPS ]]
    then
	#
	:   Remove any duplicates that may have crept in
	#
	VOLUME_GROUPS=$(print $VOLUME_GROUPS | tr ' ' '\n' | sort -ru)

	#
	:   Expand the file systems
        :   if this is an rg_move on selective fallover, lsvg -l might not work
        :   so try looking up the LVs in the ODM if the VG is online
	#
        if [[ ${RG_MOVE_EVENT:-false} == "true" &&
              ${RG_MOVE_ONLINE:-TMP_ERROR} != "ONLINE" ]]
	then
            export SELECTIVE_FAILOVER="true"
	fi
	date
        for VG in $VOLUME_GROUPS
	do
            if lsvg -L -o | grep -x $VG
	    then
                for LV in $(clodmget -q "name = $VG" -f dependency -n CuDep)
		do
		    LV_type=$(lsattr -E -l $LV -a type -F value)
		    if [[ $LV_type == jfs?(2) ]]
		    then 
                    	FS=$(clodmget -q "name = $LV and attribute = label" -f value -n CuAt)
                    	if [[ -n $FS ]]
			then
			    FILE_SYSTEMS="${FILE_SYSTEMS} ${FS}"
			fi
                    fi
                done
            fi
        done
    fi
    date

    #
    :	we modified the VG - set it back now.
    #
    if [[ $VG_MOD == "true" ]]
    then
	VOLUME_GROUPS=""
    fi
fi

# 
:   For OEM Filesystems, check for "Mount All Filesystems"
#
if [[ -n "$OEM_VOLUME_GROUPS" ]]
then
    #
    :	Remove any duplicates that may have crept in
    #
    OEM_VOLUME_GROUPS=$(print $OEM_VOLUME_GROUPS | tr ' ' '\n' | sort -ru)

    if [[ $OEM_FILE_SYSTEMS == "ALL" ]]
    then
        OEM_FILE_SYSTEMS=""
        for OEM_VG in $OEM_VOLUME_GROUPS
        do
	    #
            :	Get the filesytems from the VG
	    #
            if OEM_FS=$(cl_get_oem_filesystems -v $OEM_VG)
            then
		#
		:   Add "implicit" OEM Filesystems to "explicit" OEM Filesystem list
		#
                OEM_FILE_SYSTEMS="$OEM_FILE_SYSTEMS $OEM_FS" # Create a list of fs to work with
            else
	        : exit status of cl_get_oem_filesystems -v $OEM_VG is: $?
                echo "ERROR: Unable to get filesystems for $OEM_VG"
                STATUS=1
            fi
        done

	#
        :   remove any duplicates 
	#
        OEM_FILE_SYSTEMS=$(echo $OEM_FILE_SYSTEMS | tr ' ' '\n' | sort -u)
    fi
fi


#
:   If filesystems are given, figure out associated VGs.
#
for fs in $FILE_SYSTEMS
do
    vg=$(cl_fs2disk -v $fs)
    if [[ $EVENT_TYPE == reconfig* ]]
    then
	#
	:   If this is a DARE reconfig, only vary off the volume group if the
	:   user did not explicitly define the volume group in the resource
	:   group.
	#
	if [[ -n $(odmget -q "group=$GROUPNAME AND name like '*VOLUME_GROUP' AND value=$vg" HACMPresource) ]]
	then
	    #
	    :	The volume group that this filesystem belongs to
	    :	is still in the resource group configuration,
	    :	so don\'t add this volume group to the list of vg\'s
	    :	to be varied off.
	    #
	    continue
	fi
    fi
    #
    :	Append to the previous VG list.
    #
    VOLUME_GROUPS="$VOLUME_GROUPS $vg"
done

#
:   Take out any duplicate items in FILE_SYSTEMS list and VG list, and sort
:   these two lists into reverse order to deal with dependent mounts, then
:   call the individual script to unmount filesystems and varyoff volume
:   groups.
#
if [[ -n $FILE_SYSTEMS ]]
then
    #
    :	Remove any duplicates that may have crept in
    #
    FILE_SYSTEMS=$(print $FILE_SYSTEMS | tr ' ' '\n' | sort -ru)
    if ! cl_deactivate_fs $FILE_SYSTEMS
    then
	#
        :   update resource manager with the results
	#
        STATUS=1
    fi
else
    #
    :	this was added because the cl_deactivate_fs is not called for
    :	directories, so directories will not get un-exported.
    #

    cl_unexport_fs "$EXPORT_FILESYSTEM" "$EXPORT_FILESYSTEM_V4"
fi

if [[ -n $OEM_FILE_SYSTEMS ]]
then
    if ! cl_deactivate_fs -c $OEM_FILE_SYSTEMS
    then
        STATUS=1;
    fi
fi

release_udresources BEFORE_VOLUME_GROUP
RC=$?
: exit status of release_udresources is: $RC
if (( $RC != 0 ))
then
   (( $STATUS == 0 )) && STATUS=1
fi

#
:   clsetrepenv utility sets up the environment for replicated methods.
#
set -a
eval $(clsetrepenv $GROUPNAME)
set +a
#
:   Call replicated resource prevg-offline method associated
:   with any replicated resource defined in the resource group
:   we arecurrently processing. Note that a return code of 3 from
:   the prevg-offline method indicates the we should override the
:   default action. The default action for offline_primary is to 
:   varyoff the VG and the default action for offline_secondary
:   is to NOT varyoff the VG
#
METHODS=$(cl_rrmethods2call prevg_offline)
SKIPVARYOFF=0

for method in $METHODS
do
  if [[ -x $method ]]
  then
      $method $VOLUME_GROUPS
      RC=$?
      : exit status of $method $VOLUME_GROUPS is: $RC
      
      case $RC in
      0)
          continue
          ;;
      3)
          DEF_VARYOFF_ACTION=1
          ;;
      *)
          exit 1
          ;;
      esac 
  fi
done

if [[ $PRINCIPAL_ACTION == "RELEASE" ]]
then
    #
    :	This is an offline_primary case so an override
    :	from the RR method means we skip the varyoff
    :	since the default action is to varyoff
    #
    SKIPVARYOFF=0

    if (( $DEF_VARYOFF_ACTION == 1 ))
    then
        SKIPVARYOFF=1
    fi
else
    #
    :	This is the offline_secondary case so an override
    :	from the RR method means we do the varyoff
    :	since the default action is to NOT varyoff
    #
    SKIPVARYOFF=1

    if (( $DEF_VARYOFF_ACTION == 1 ))
    then
        SKIPVARYOFF=0
    fi
fi

if [[ -n "$VOLUME_GROUPS" ]]
then
    #
    :	Remove any duplicates that may have crept in
    #
    VOLUME_GROUPS=$(print $VOLUME_GROUPS | tr ' ' '\n' | sort -ru)
     
    if (( $SKIPVARYOFF == 0 ))
    then
        if ! cl_deactivate_vgs $VOLUME_GROUPS
        then
	    STATUS=1
        fi
    fi
fi

#
:   Determine list of "implicit OEM Volume Groups" if OEM Filesystems configured
#
for OEM_FS in $OEM_FILE_SYSTEMS
do
    if [[ $OEM_FS != "ALL" ]] 
    then

        OEM_FS_TYPE=$(cl_get_oem_type -f $OEM_FS);
        if [[ -z $OEM_FS_TYPE ]]
        then
	    echo "ERROR: Unable to get OEM Type for filesystem $OEM_FS"
            STATUS=1
        fi

        OEM_METHOD_FS2VG=$(cl_get_oem_method -t $OEM_FS_TYPE -m LSVOLUMES);
        if [[ -z $OEM_METHOD_FS2VG ]]
        then
	    echo "ERROR: Unable to get OEM Method to list host volume groups for filesystem $OEM_FS"
            STATUS=1
        fi

        IMPLICIT_OEM_VOLUME_GROUP=$($OEM_METHOD_FS2VG $OEM_FS);
        if [[ -z $IMPLICIT_OEM_VOLUME_GROUP ]]
        then
	    echo "ERROR: Unable to get OEM host volume groups for filesystem $OEM_FS"
            STATUS=1
        fi

        OEM_VOLUME_GROUPS="$OEM_VOLUME_GROUPS $IMPLICIT_OEM_VOLUME_GROUP"

	#
        :   Remove any duplicates that may have crept in
	#
        OEM_VOLUME_GROUPS=$(print $OEM_VOLUME_GROUPS | tr ' ' '\n' | sort -ru)
     
    fi
done

if [[ -n $OEM_VOLUME_GROUPS ]]
then
     if ! cl_deactivate_vgs -c $OEM_VOLUME_GROUPS
     then
        STATUS=1
     fi
fi
#
:   clsetrepenv utility sets up the environment for replicated methods.
#
set -a
eval $(clsetrepenv $GROUPNAME)
set +a
#
:   Call replicated resource postvg-offline method associated
:   with any replicated resource defined in the resource group
:   we arecurrently processing. 
#
METHODS=$(cl_rrmethods2call postvg_offline)

for method in $METHODS
do
  if [[ -x $method ]]
  then
      if ! $method $VOLUME_GROUPS
      then
        exit 1
      fi
  fi
done

exit $STATUS

