#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2018,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/cspoc/utilities/cllvmcmd.sh 1.3.1.8 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1998,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 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/cspoc/utilities/cllvmcmd.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
###############################################################################
#
# Name:
#       cllvmcmd.sh
#
# Description:
#    Wrapper for all of the LVM commands that have an optional list of
#    physical volumes.  Translates the physical volume id's into physical
#    volume names that can be used by the AIX LVM commands.
#
#    Arguments for the various commands are passed in as follows
#
#       -extendvg Switches VolumeGroup DiskList
#
#       -reducevg Switches VolumeGroup DiskList
#
#       -unmirrorvg Switches VolumeGroup [DiskList]
#
#       -mklvcopy Switches LogicalVolume Copies VolumeGroup [DiskList]
#
#       -rmlvcopy Switches LogicalVolume Copies VolumeGroup [DiskList]
#
#       -rmlv Switches LogicalVolume
#
#       -mklv Switches VolumeGroup NumberOfLP's
#
#       -extendlv Switches LogicalVolume NumberOfLP's [DiskList]
#
#       -chlv Switches LogicalVolume...
#
#       -rmlv Switches LogicalVolume...
#
#       -extendvg4vp Switches VolumeGroup DiskList
#
#       DiskList is a comma separated list of physical volume id's
#
# Return Values:
#    0   Success
#    1   Failure
#
###############################################################################

#
:   Grab the command first
#
CMD="$1"
CMD=${CMD#-}
shift

#
:   By default, the VG will be passed on to the command
#
REMOVE="false"

#
:   Define the valid switches based on the command
#

case $CMD in
    extendvg)  OPTS="f"  ;;
    reducevg)  OPTS="dfk" ;;
    unmirrorvg) OPTS="c:p:" ;;
    mklvcopy)   OPTS="a:e:km:u:s:"; REMOVE="true" ;;
    rmlvcopy)   OPTS="p:" ; REMOVE="true" ;;
    rmlv)       OPTS="f"; REMOVE="true" ;;
    chlv)       OPTS="a:b:d:e:L:n:p:r:s:t:u:v:w:x:"; REMOVE="true" ;;
    rmfs)       OPTS="r"; REMOVE="true" ;;
    chfs)       OPTS="A:m:u:p:a:d:t:"; REMOVE="true" ;;
    mklv)       OPTS="a:b:c:d:e:L:m:r:s:t:u:v:w:x:y:Y:S:" ;;
    extendlv)   OPTS="a:e:m:s:u:"; REMOVE="true" ;;
    extendvg4vp)  OPTS="f"  ;;
esac

# Remove switches for those commands that have them
SWITCHES=""

[[ "$CMD" != "rmlvcopy" ]] && {
    set -- $(getopt $OPTS $*)

    SWITCHES=""

    while [[ $1 != -- ]]
    do
	[[ $1 == "-k" ]] && {
	    shift
	    continue
	}
	    
	SWITCHES=${SWITCHES:+"${SWITCHES} "}$1
	shift
    done

    shift
}

VG="$1"

#
:   Do not pass the VG to certain commands
#
[[ "$REMOVE" == "true" ]] && shift

if [[ $CMD == "extendvg" || $CMD == "reducevg" ]]
then
    typeset SCSIPR_ENABLED=$(clodmget -n -q "policy=scsi" -f value HACMPsplitmerge)
    if [[ $SCSIPR_ENABLED == Yes ]]
    then
        typeset resgrp=$(clodmget -q "name like *VOLUME_GROUP and value=$VG" -f group -n HACMPresource)
        if [ $CMD == "reducevg" ] && [ $resgrp ];then

           #
           : Clear the SCSIPR reservations from the removed PVs
           #
           for pvid in $*
           do
               typeset hdisk=$(lspv -L | grep -w $pvid | awk '{print $1}')
               /usr/es/sbin/cluster/events/utils/clpr_clear $hdisk
           done
        elif [ $CMD == "extendvg" ] && [ $resgrp ];then

            #
            :  Register and Reserve new PVs.
            #
            /usr/es/sbin/cluster/events/cl_scsipr_extendvg "$*"
            if (( $? != 0 ))
            then
                exit 1
            fi
        fi
    fi
fi


#
:   Run the command
#
$CMD $SWITCHES $* || {
    /bin/dspmsg -s 2 cspoc.cat 46 "Error executing %s %s %s!\n" $CMD "$SWITCHES" "$*"
    exit 1
}

if [[ $CMD == "extendvg" || $CMD == "reducevg" ]]
then
    #
    :	If the membership of the volume group has changed, rebuild the fence group
    #
    VG_PV_list=$(lspv | grep -w $VG | cut -f1 -d' ' | paste -s -)
    cl_vg_fence_init -c $VG rw $VG_PV_list
    RC=$?
    :	exit status of cl_vg_fence_init $VG is $RC
    if (( 0 != $RC ))
    then
	#
	:   Log any error, but continue.  If this is a real problem,
	:   manual intervention may be needed.
	#
	rw=$(dspmsg -s 103 cspoc.cat 350 'read/write' | cut -f2 -d,)
	dspmsg -s cspoc.cat 10511 "$PROGNAME: Volume group $vg fence height could not be set to read/write" $PROGNAME $vg $rw
    fi
fi

exit 0
