#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 1999,2006 
# 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 
# @(#)13 1.3.3.20 src/avs/fs/mmfs/ts/admin/mmshutdown.sh, mmfs, avs_rgpfs24, rgpfs240610b 1/23/06 02:25:22
##############################################################################
#
#  GPFS filesystems on the specified nodes will be unmounted and then
#  the GPFS daemons on the specified nodes will be shut down.
#
#  Usage:
#
#    mmshutdown [ -t unmountTimeout ]
#               [-a | -N {Node[,Node...] | NodeFile | NodeClass}]
#
#  where:
#
#    -t timeout    Will timeout the unmount command after this amount of time
#                  and shut the GPFS daemons down anyway.
#
#    -a            Stop the daemon on all nodes in the GPFS cluster.
#
#    -N Node,Node,...  Specify the nodes to be shut down.
#    -N NodeFile       NodeClass may be one of several possible node classes
#    -N NodeClass      (e.g., quorumnodes, managernodes, nsdnodes, etc.)
#
#
#  If not explicitly specified otherwise, the daemon is shut down on the local
#  node only.
#
#
#  Obsolete options:
#
#    -C NodesetId  Start the daemon on all nodes in the specified nodeset.
#                  Assumed to be the same as -a.
#
#    -W NodeFile   Start the daemon on all nodes whose reliable hostnames
#                  are listed one per line in NodeFilename.
#                  Cannot be specified with -a, -N, -w, or -n.
#
#    -w nodenames  Start the daemon on all nodes whose reliable hostnames
#                  are in the comma-separated nodenames list.
#                  Cannot be specified with -a, -N or -W.
#                  If both -w and -n are specified, the lists are combined.
#
#    -n nodenums   Start the daemon on all nodes whose node numbers are
#                  in the comma-separated nodenums list.
#                  Cannot be specified with -a, -N, or -W.
#                  If both -w and -n are specified, the lists are combined.
#
##############################################################################

# Include global declarations and service routines.
. /usr/lpp/mmfs/bin/mmglobfuncs
. /usr/lpp/mmfs/bin/mmsdrfsdef

sourceFile="mmshutdown.sh"
[[ -n $DEBUG || -n $DEBUGmmshutdown ]] && set -x
$mmTRACE_ENTER "$*"


# Local variables
usageMsg=246
typeset -i maxTimeout=900
typeset -i timeout=0
typeset -i nodes=0
rc=0


#######################
# Mainline processing
#######################


##################################
# Process each of the arguments.
##################################
[[ $arg1 = '-?' || $arg1 = '-h' || $arg1 = '--help' || $arg1 = '--' ]] &&  \
  syntaxError "help" $usageMsg

while getopts :aC:n:N:t:W:w: OPT
do
  case $OPT in

    a) [[ -n $aflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       aflag="-$OPT"
       [[ -n $Cflag || -n $Wflag || -n $wflag || -n $nflag || -n $Nflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $aflag $Cflag $Wflag $wflag $nflag $Nflag
       callGpfsInit=yes
       ;;

    C) # syntaxError "obsoleteOption" $usageMsg "-$OPT"
       [[ -n $Cflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       Cflag="-$OPT"
       nodesetId="$OPTARG"
       [[ -n $aflag || -n $Wflag || -n $wflag || -n $nflag || -n $Nflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $Cflag $aflag $Wflag $wflag $nflag $Nflag
       callGpfsInit=yes
       ;;

    n) [[ -n $nflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       nflag="-$OPT"
       nodenums="$OPTARG"
       [[ -n $Cflag || -n $Wflag || -n $aflag || -n $Nflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $nflag $Cflag $Wflag $aflag $Nflag
       callGpfsInit=yes
       ;;

    N) [[ -n $Nflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       Nflag="-$OPT"
       nodenames="$OPTARG"
       [[ -n $Cflag || -n $Wflag || -n $aflag || -n $nflag || -n $wflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $Nflag $Cflag $Wflag $aflag $nflag $wflag
       callGpfsInit=yes
       ;;

    t) [[ -n $tflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       tflag="-$OPT"
       timeout=$(checkIntRange $tflag $OPTARG)
       [[ $? -ne 0 ]] && cleanupAndExit
       ;;

    W) [[ -n $Wflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       Wflag="-$OPT"
       Wcoll="$OPTARG"
       [[ -n $Cflag || -n $aflag || -n $wflag || -n $nflag || -n $Nflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $Wflag $Cflag $aflag $wflag $nflag $Nflag
       ;;

    w) [[ -n $wflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       wflag="-$OPT"
       nodenames="$OPTARG"
       [[ -n $Cflag || -n $Wflag || -n $aflag || -n $Nflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $wflag $Cflag $Wflag $aflag $Nflag
       ;;

    :) syntaxError "missingValue" $usageMsg $OPTARG
       ;;

    +[aCnNtwW])
       syntaxError "invalidOption" $usageMsg "$OPT"
       ;;

    *) syntaxError "invalidOption" $usageMsg $OPTARG
       ;;

  esac
done  # end of while getopts :aC:n:N:t:W:w: OPT do

shift OPTIND-1
[[ $# != 0 ]] && syntaxError "extraArg" $usageMsg $1

# Rather than fail, convert the obsolete -C option to -a.
[[ -n $Cflag ]] && aflag="-a"


###########################################################################
# Set up trap exception handling and call the gpfsInit function if needed.
# It will ensure that the local copy of the mmsdrfs and the rest of the
# GPFS system files are up-to-date.  There is no need to lock the sdr.
# Note: There is no need to call gpfsInit if we are trying to quickly
#       shut down the daemon on the local node only.
###########################################################################
trap pretrap2 HUP INT QUIT KILL
if [[ -n $callGpfsInit ]]
then
  gpfsInitOutput=$(gpfsInit nolock)
  setGlobalVar $? $gpfsInitOutput
fi


#######################################################
# Create a file containing all of the specified nodes.
#######################################################
if [[ -n $aflag ]]
then
  # Get a list of the nodes.
  getNodeList $REL_HOSTNAME_Field $GLOBAL_ID $mmsdrfsFile > $nodefile

elif [[ -n $Nflag ]]
then
  # Convert the passed data into a file containing admin node names.
  createVerifiedNodefile $nodenames $REL_HOSTNAME_Field no $nodefile
  [[ $? -ne 0 ]] && cleanupAndExit

elif [[ -n $Wflag ]]
then
  # Verify the input file is readable.
  if [[ ! -f $Wcoll || ! -r $Wcoll ]]
  then
    printErrorMsg 43 $mmcmd $Wcoll
    cleanupAndExit
  fi

  # Filter out comment lines and localhost entries.
  $grep -v -e "localhost" -e "^#" "$Wcoll" > $tmpfile

  # Convert any entries in the node file into admin node names.
  if [[ -s $tmpfile ]]
  then
    createVerifiedNodefile $tmpfile $REL_HOSTNAME_Field no $nodefile
    [[ $? -ne 0 ]] && cleanupAndExit
  else
    # No node names were specified.
    printErrorMsg 328 $mmcmd $Wcoll
    cleanupAndExit
  fi

else
  # Either no option was specified, or we have some combination of -w and -n.

  # Convert the node names list (if any) into a file.
  $rm -f $tmpfile
  if [[ -n $nodenames ]]
  then
    for i in $(print $nodenames | $tr "," " ")
    do
      print -- "$i" >> $tmpfile
    done
  fi

  # Append the node number list (if any) to the node file.
  if [[ -n $nodenums ]]
  then
    for i in $(print $nodenums | $tr "," " ")
    do
      print -- "$i" >> $tmpfile
    done
  fi

  # Convert the entries in the node file into admin node names.
  if [[ -s $tmpfile ]]
  then
    createVerifiedNodefile $tmpfile $REL_HOSTNAME_Field no $nodefile
    [[ $? -ne 0 ]] && cleanupAndExit
  fi

fi  # end of if [[ -n $aflag ]]

# Determine the number of nodes that are affected by the command.
[[ -s $nodefile ]] && nodes=$($cat $nodefile | $wc -l)

# If a timeout value was not set, estimate one.
if [[ $timeout = 0 ]]
then
  (( timeout = 60 + 3 * nodes ))
  [[ $nodes -gt 500 ]] &&  \
    (( maxTimeout = maxTimeout + nodes / 500 * 300 ))
  [[ $timeout -gt $maxTimeout ]] && timeout=$maxTimeout
fi

# Ensure we have the proper credentials.
[[ $getCredCalled = no ]] && getCred


################################
# Unmount the GPFS filesystems.
################################

# Starting force unmount ...
printInfoMsg 241 "$(date)" $mmcmd

if [[ ! -s $nodefile ]]
then
  # Unmount the gpfs file systems on the local node only.
  $mmremote unmountFileSystems all -f  &
  pid=$!
else
  # Unmount the gpfs file systems on a number of nodes.
  $mmcommon onall $nodefile $unreachedNodes unmountFileSystems all -f  &
  pid=$!
fi

# Wait until the unmount command finishes or the timeout expires.
waited=0
mincount=0
retrydelay=5
while [[ $waited -lt $timeout ]]
do
  $sleep $retrydelay
  (( waited = waited + retrydelay ))
  (( mincount = mincount + retrydelay ))

  if ! kill -0 $pid 2>/dev/null
  then
    # unmount finished.
    waited=-1
    break
  fi

  if [[ $mincount -ge 60 ]]
  then
    (( togo = timeout - waited ))
    mincount=0
    printInfoMsg 242 "$(date)" $mmcmd $waited $togo
  fi
done  # while [[ $waited -lt $timeout ]]

# Inform the user if the unmount did not finish.
[[ $waited -gt 0 ]] &&  \
  printInfoMsg 243 "$(date)" $mmcmd $waited


##############################
# Shut down the GPFS daemons.
##############################
printInfoMsg 244 "$(date)" $mmcmd

if [[ ! -s $nodefile ]]
then
  # Shut down GPFS on the local node only.
  $mmremote shutdownDaemon
  rc=$?
else
  # Shut down GPFS on a number of nodes.
  $mmcommon onall $nodefile $unreachedNodes shutdownDaemon
  rc=$?
fi

# If any nodes could not be reached, tell the user which ones.
if [[ -s $unreachedNodes ]]
then
  # The following nodes could not be reached: . . .
  printErrorMsg 270 $mmcmd
  $cat $unreachedNodes 1>&2
fi

# All finished.
printInfoMsg 245 "$(date)" $mmcmd

cleanupAndExit $rc

