#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/cspoc/utilities/cli_assign_pvids.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2003,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 
# @(#)22        1.3  src/43haes/usr/sbin/cluster/cspoc/utilities/cli_assign_pvids.sh, hacmp.cspoc, 61haes_r720, 1517B_hacmp720 4/14/15 17:33:29

###############################################################################
#
#   COMPONENT_NAME: CSPOC Command Line Interface
#
# Name:
#   cli_assign_pvids
#
# Description:
#   Assign a PVID to each of the disks passed as arguements, then use C-SPOC
#   to pick up those PVIDs on all the other cluster nodes
#
# Arguments:
#   A list of physical volumes
#
#   Setting the environment variable _DEBUG to 1 -> 9 turns on levels of
#   C-SPOC debugging
#
# Return Values:
#   As set by cl_assign_pvids
#
# Notes:
#   The '-f' flag is passed on cl_assign_pvids to surpress unnecessary checking.
#   As a consequence, the operation will proceed even if not all nodes are
#   accessable
#
################################################################################

#
: Load the "CL_PVID_ASSIGNMENT" from the environment file, if
: present, allowing for any unexpected leading whitespace.
#
typeset TAB=$'\t'
typeset INDENT="$TAB "
eval $(grep -w '^[$INDENT]*CL_PVID_ASSIGNMENT' /etc/environment)

#
: Recognized values for CL_PVID_ASSIGNMENT are "false", "no",
: "disable", or zero. Any other value preserved the default
: behavior, allowing PVIDs to be automatically assigned, as
: needed.
#
if [[ -n $CL_PVID_ASSIGNMENT &&
      $CL_PVID_ASSIGNMENT == @(f|F|n|N|d|D|0)* ]]
then
    dspmsg -s 144 cspoc.cat 2 '\nERROR: \"%1$s\" cannot be used due to the current value\n       of environment variable "CL_PVID_ASSIGNMENT" ("%2$s").\n\n' "${0##*/}" "$CL_PVID_ASSIGNMENT" 1>&2
    exit 1
fi

typeset DEBUG=""
typeset assigned_pvids=""

#
#   A list of disks is required
#
if (( $# == 0 )) ; then
    print "USAGE: cli_assign_pvids disk [disk ...]\n"
    exit 1
fi

#
#   Variables needed by cl_assign_pvids
#	_CSPOC_MODE - working with shared (as opposed to concurrent) volume
#			groups
#	_CSPOC_CALLED_FROM_SMIT - can skip checks on input
#
export _CSPOC_MODE="shared"
export _CSPOC_CALLED_FROM_SMIT="true"

#
#   If a _DEBUG value has been set, pass it through
#
if [[ -n $_DEBUG ]] ; then

    #
    #	The debug level is a number, 1 through 9.  Those values are passed
    #	through.  Anything else is turned into '1'
    #
    integer dbg_level=$_DEBUG
    if (( $dbg_level < 10 && $dbg_level >= 1 )) ; then
        DEBUG="-d $_DEBUG"
    else
        DEBUG="-d 1"
    fi
fi

#
#   Assign a PVID for each of the given hdisks and remember what it is
#
for hdisk in $* ; do
    if chdev -l $hdisk -a pv=yes ; then
        pvid=$(/usr/es/sbin/cluster/events/utils/cl_querypv /dev/$hdisk)
        assigned_pvids=${assigned_pvids:+"$assigned_pvids "}$pvid
    else
        dspmsg -s 144 cspoc.cat 1 '%1$s: ERROR: could not assign a PVID to disk "%2$s".\n\n' "${0##*/}" "$hdisk" 1>&2
    fi
done

#
#   It is possible that there are other hdisk profiles present that correspond
#   to other physical paths.  Get their ODM entries updated, too.
#
/usr/es/sbin/cluster/cspoc/clgetpvids $assigned_pvids

#   Construct the node list.  This is a comma separated, quote delimited list
#   of all the other nodes in the cluster.
#
local_node=$(/usr/es/sbin/cluster/utilities/get_local_nodename)
export _NODE_LIST=$(clodmget -q "object = VERBOSE_LOGGING and name != $local_node" -n -f name HACMPnode | \
                    paste -s -d ',' -)

#
#   Invoke the cspoc code to pick up those PVIDs on the other cluster nodes
#
if [[ -n $assigned_pvids ]] ; then
    /usr/es/sbin/cluster/sbin/cl_assign_pvids -cspoc "-f $DEBUG -n $_NODE_LIST" $assigned_pvids
fi
return $?