#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 1997,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 
# @(#)01 1.57.1.1 src/avs/fs/mmfs/ts/admin/mmautoload.sh, mmfs, avs_rgpfs24, rgpfs24s001a 4/5/06 11:34:58
#########################################################################
#                                                                       #
#  Usage:  mmautoload                                                   #
#                                                                       #
#  Called once from inittab.  Performs GPFS configuration               #
#  initialization.  If everything goes smoothly, and the                #
#  autoload parameter is set to yes, starts the GPFS daemon.            #
#                                                                       #
#########################################################################

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

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

# Local variables

mmfslog=${rasDir}mmfs.log.latest # daemon log (/var/adm/ras/mmfs.log.latest)
interval=10                      # 10 6-second intervals is a message per minute
integer waitPeriod               # wait period (in minutes)
integer timeout


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

# If the node does not appear to belong to a GPFS cluster,
# go away quietly.
if [[ ! -f $mmsdrfsFile ]]
then
  $rm -f $GLOBAL_FILES $LOCAL_FILES
  return 0
fi

# Determine the type of cluster environment.
determineMode

# If the cluster type is not known, go away quietly.
if [[ $MMMODE != lc && $MMMODE != single ]]
then
  $rm -f $GLOBAL_FILES $LOCAL_FILES
  return 0
fi

# Put a message in the daemon log to reflect the node reboot.
[[ -f $mmfslog ]] &&  \
  print "\n\n$(date): Node rebooted.  Starting mmautoload..." >> $mmfslog

# Force the recreation of the local system files.
$rm -f $mmfsNodeData $mmfscfgFile ${mmfsNewKeyLevel}+([0-9])  \
       ${mmfsCommittedKeyLevel}+([0-9]) $nsdpvol

# If AIX, verify there is an mmfs entry in /etc/vfs.
# If Linux, figure out what is the correct major number.
checkVfsNumber
rc=$?
if [[ $rc -ne 0 ]]
then
  $rm -f $GLOBAL_FILES $LOCAL_FILES
  return 0
fi

# Verify that the GPFS environment is properly initialized.
# If not successful, keep trying for up to 5 minutes before giving up.
waitPeriod=5
timeout=1
$rm -f $errMsg
while true
do
  export MOUNT_POINT_CHECK=all
  gpfsInitOutput=$(gpfsInit nolock 2>$errMsg)
  rc=$?
  unset MOUNT_POINT_CHECK
  if [[ $rc -ne 0 || -z $gpfsInitOutput ]]
  then
    # gpfsInit failed but there still may be a chance
    # for things to get fixed - we'll try again.
    whatfor="cluster data repository"
  else
    # The gpfsInit call worked.
    # Parse the result and get out of the loop.
    setGlobalVar 0 $gpfsInitOutput
    $rm -f $errMsg
    break
  fi  # end of if [[ $rc -ne 0 || -z $gpfsInitOutput ]]

  # Something is not right.  Put out a message every 60 seconds.
  timeout=timeout-1
  if [[ $timeout -le 0 ]]
  then
    # Show detailed error messages if any.
    [[ -s $errMsg ]] && $cat $errMsg 1>&2
    # Tell what it is that we are waiting for.
    printErrorMsg 142 $mmcmd "$whatfor"
    # Put the messages in the daemon log as well.
    if [[ -f $mmfslog ]]
    then
      [[ -s $errMsg ]] && $cat $errMsg     >> $mmfslog
      printErrorMsg 142 $mmcmd "$whatfor" 2>> $mmfslog
    fi
    $rm -f $errMsg

    # Reset the counters and see if it is time to give up.
    if [[ $waitPeriod -gt 0 ]]
    then
      waitPeriod=waitPeriod-1
      timeout=$interval
    else
      # The problem did not clear in the last 5 minutes.
      # Tell the guy to use mmstartup and give up.
      print -u2 "mmautoload: The GPFS environment cannot be initialized."
      print -u2 "mmautoload: Correct the problem and use mmstartup to start GPFS."
      # Put the messages in the daemon log as well.
      if [[ -f $mmfslog ]]
      then
        print "mmautoload: The GPFS environment cannot be initialized." >> $mmfslog
        print "mmautoload: Correct the problem and use mmstartup to start GPFS." >> $mmfslog
      fi
      $rm -f $GLOBAL_FILES $LOCAL_FILES
      return 0
    fi  # end of if [[ $waitPeriod -gt 0 ]]
  fi  # end of if [[ $timeout -le 0 ]]
  $rm -f $errMsg

  # Wait a while before retrying.
  $sleep 6
done  # end while true


# If this is a primary or backup config server,
# ensure the mmsdrserv daemon is running.
if [[ $ourNodeName = $primaryServer || $ourNodeName = $backupServer ]]
then
  startSdrServ CURRENT 2>$errMsg

  # Show detailed error messages if any.
  if [[ -s $errMsg ]]
  then
    $cat $errMsg 1>&2
    [[ -f $mmfslog ]] && $cat $errMsg >> $mmfslog
  fi
  $rm -f $errMsg
fi  # end of if [[ $ourNodeName = $primaryServer || $ourNodeName = $backupServer ]]


# Find the setting of the autoload parameter.
autoload=$(showCfgValue autoload)


# Go away if the autoload parameter is not set to yes.
if [[ $autoload != yes ]]
then
  $rm -f $GLOBAL_FILES $LOCAL_FILES
  return 0
fi


# Start GPFS.
[[ -f $mmfslog ]] &&  \
  print "\n$(date): mmautoload: Starting GPFS..." >> $mmfslog

# Check whether we are already running.
pid=$($ps -eo "pid args" |  \
      $awk '/\/runmmfs/ && !/this process/ {print $1}')
if [[ -n $pid ]]
then
  # GPFS is already active.
  printInfoMsg 586 GPFS
  [[ -f $mmfslog ]] &&  \
    printInfoMsg 586 GPFS >> $mmfslog
else
  # Start the daemon.
  # In non-Linux systems, pass the output of gpfsInit
  # to runmmfs to avoid a second initialization call.
  # In Linux, this is not done because the device major
  # number is not available at this time.
  if [[ $osName = Linux ]]
  then
    $daemonize -c $runmmfs
  else
    $daemonize -e "GPFSINIT=\"$gpfsInitOutput\"" -c $runmmfs
  fi
fi

$rm -f $GLOBAL_FILES $LOCAL_FILES
return 0

