#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2005,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 
# @(#)21 1.10.1.3 src/avs/fs/mmfs/ts/admin/mmmount.sh, mmfs, avs_rgpfs24, rgpfs24s007a 10/5/06 17:58:00
##############################################################################
#
#  Mount GPFS file systems on one or more nodes in the cluster.
#
#  Usage:
#    mmmount {Device | DefaultMountPoint | all} [-o MountOptions]
#            [-a | -N {Node[,Node...] | NodeFile | NodeClass}]
#  or
#    mmmount Device MountPoint [-o MountOptions]
#            [-a | -N {Node[,Node...] | NodeFile | NodeClass}]
#
#  where:
#    -a                Mount the file systems on all nodes in the cluster.
#
#    -N Node,Node,...  Specify the nodes on which the mount is to take place.
#    -N NodeFile       NodeClass may be one of several possible node classes
#    -N NodeClass      (e.g., quorumnodes, managernodes, nsdnodes, etc.)
#
#    -o MountOptions   is a comma-separated list of mount options.
#
#  If not explicitly-specified otherwise, the file systems are mounted on
#  the local node only.
#
#
# Undocumented option:
#
#   -y                 Suppress internal remounting.
#
##############################################################################

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

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


# Local variables

usageMsg=390
rc=0



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


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

[[ $argc -lt 1  ]] &&  \
  syntaxError "missingArgs" $usageMsg

# Figure out what was specified on the command line.
if [[ $arg1 = /* && $arg1 != +(/)dev+(/)* ]]
then
  # If the first argument starts with a "/" but not "/dev/",
  # it is assumed to be a fully-qualified mount point.
  defaultMountPoint=$arg1
  shift 1

else
  # Otherwise, the argument is assumed to be the device name
  # of a file system (or one of the "all" the keywords).
  device=$arg1
  shift 1

  # If a device name was specified, the second argument may be
  # the fully-qualified path name of a mount point to use.
  if [[ -n $arg2 ]]
  then
    if [[ $arg2 = -* ]]
    then
      : # This must be an option; we'll handle it later.
    elif [[ $arg1 = all || $arg1 = all_local || $arg1 = all_remote ]]
    then
      # Mount point cannot be specified when mounting all file systems.
      printErrorMsg 281 $mmcmd
      syntaxError "help" $usageMsg
    elif [[ $arg2 != /* ]]
    then
      # The mount point must be a fully-qualified path name.
      syntaxError "absolutePath" $noUsageMsg "$arg2"
    else
      # The second argument must be a mount pount.
      newMountPoint=$arg2
      shift 1
    fi  # end of if [[ $arg2 = -* ]]
  fi  # end of if [[ -n $arg2 ]]
fi  # end of if [[ $arg1 = "/"* && $arg1 != +(/)dev+(/)* ]]

# Process the rest of the options.
while getopts :aN:o:y OPT
do
  case $OPT in

    a) [[ -n $aflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       aflag="-$OPT"
       ;;

    N) [[ -n $Nflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       Nflag="-$OPT"
       nodenames="$OPTARG"
       ;;

    o) [[ -n $oflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       oflag="-$OPT"
       mountOptions="$OPTARG"
       ;;

    y) [[ -n $yflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       yflag="doNotRemount"
       ;;

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

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

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

  esac
done

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

[[ -z $mountOptions  ]] && mountOptions=DEFAULT
[[ -z $newMountPoint ]] && newMountPoint=DEFAULT


########################################################################
# Set up trap exception handling and call the gpfsInit function.
# 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.
########################################################################
trap pretrap2 HUP INT QUIT KILL
gpfsInitOutput=$(gpfsInit nolock)
setGlobalVar $? $gpfsInitOutput


#######################################################
# 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

else
  : # Nothing to do.  Only the local node is affected.
fi  # end of if [[ -n $aflag ]]


#################################
# Verify the file system exists.
#################################
if [[ $device = all || $device = all_local || $device = all_remote ]]
then
  # Ensure there is at least one file systems to mount.
  $awk -F: '                                                                 \
    BEGIN { rc = '$MM_FsNotFound' }                                          \
    $'$LINE_TYPE_Field' == "'$SG_HEADR'" {                                   \
      if ( device == "all"                                               ||  \
           device == "all_local"  && $'$FS_TYPE_Field' == "'$localfs'"   ||  \
           device == "all_remote" && $'$FS_TYPE_Field' == "'$remotefs'" ) {  \
        { rc = 0 }                                                           \
        { exit }                                                             \
      }                                                                      \
    }                                                                        \
  END { exit rc }                                                            \
  ' device=$device $mmsdrfsFile
  rc=$?

  if [[ $rc = $MM_FsNotFound ]]
  then
    # No file systems were found in the cluster.
    if [[ $device = all || $device = all_local ]]
    then
      printErrorMsg 200 $mmcmd
    else
      printErrorMsg 193 $mmcmd
    fi
    cleanupAndExit $rc
  else
    # Check for unexpected errors
    checkForErrors awk $rc
  fi  # end of if [[ $rc = $MM_FsNotFound ]]

  deviceName=$device
  defaultOptions=DEFAULT

else
  # Look for the file system either by device name or by mount point.
  if [[ -n $device ]]
  then
    findFSoutput=$(findFS "$device" $mmsdrfsFile device)
  else
    findFSoutput=$(findFS "$defaultMountPoint" $mmsdrfsFile mountPoint)
  fi
  [[ -z $findFSoutput ]] && cleanupAndExit

  # Parse the output from the findFS function.
  set -f ; set -- $findFSoutput ; set +f
  fqDeviceName=$1
  deviceName=$2
  fsHomeCluster=$3
  defaultMountPoint=$6
  defaultOptions=$7
fi  # end of if [[ $device = all || ...

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


#################################################
# Mount the file systems on the specified nodes.
#################################################

# Mounting file systems ...
printInfoMsg 373 "$(date)" $mmcmd

if [[ ! -s $nodefile ]]
then
  # The request is to mount file systems on the local node only.
  $mmremote mountFileSystems  \
     $deviceName "$newMountPoint" "$defaultOptions" "$mountOptions" $yflag
  rc=$?
else
  # The request is to mount file systems on a number of nodes.
  # Note:  The two option strings may contain a semi-colon
  #        and must be double-quoted.
  $mmcommon onall $nodefile $unreachedNodes mountFileSystems  \
     $deviceName "$newMountPoint" "\"$defaultOptions\"" "\"$mountOptions\"" $yflag
  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

cleanupAndExit $rc

