#!/sbin/sh
# @(#) $Header: hparray,v 78.1 96/02/08 19:49:17 ssa Exp $

############################################################################
#                                                                          #
# NOTE:    This script is not configurable!  Any changes made to this      #
#          script will be overwritten when you upgrade to the next         #
#          release of HP-UX.                                               #
#                                                                          #
# WARNING: Changing this script in any way may lead to a system that       #
#          is unbootable.  Do not modify this script.                      #
############################################################################

############################################################################
#                                                                          #
# Disk array startup/shutdown script                                       #
#                                                                          #
# This script performs the required startup and shutdown functions         #
# for disk arrays.                                                         #
#                                                                          #
# "start"ing of the script should follow hfsmount and set_state.  "stop"   #
# should preceed these two functions.                                      #
#                                                                          #
############################################################################

############################################################################
#                                                                          #
# RAID parity scan and repair                                              #
#                                                                          #
############################################################################
#                                                                          #
# In certain modes of operation, the C2425 and C2430 disk arrays store     #
# redundant information from which data can be reconstructed in the event  #
# of a disk drive failure.  This information is often referred to as       #
# RAID parity.  Since this information is redundant, there is a small      #
# possibility that it can become inconsistent, under extreme operating     #
# conditions, such as an unanticipated power failure. An attempt to        #
# reconstruct data from inconsistent parity will result in incorrect data. #
# To minimize the possibility of reconstructing incorrect data, it is      #
# important to detect and correct inconsistent parity before a drive       #
# failure occurs.                                                          #
#                                                                          #
# When inconsistent parity is found, there is no way of knowing whether    #
# the parity information or the data itself is wrong, therefore, the       #
# correction is always applied to the parity.  Data with inconsistent      #
# parity does not mean that the data is incorrect.  It indicates that a    #
# transaction altering the data and parity was in progress when an event   #
# occurred that prevented the transaction from completing.  The data       #
# associated with the inconsistent parity that was corrected is as reliable#
# as any other data which was being written when the event occurred.       #
#                                                                          #
# The process of finding and repairing parity inconsistencies on the       #
# disk array is somewhat time consuming.  The most probable cause of       #
# inconsistent parity is unanticipated power failure.  Thus, a reasonable  #
# compromise is reached by performing the parity scan and repair process   #
# automatically at system boot time, when required.  A facility is also    #
# provided to allow manual execution of the process on individual arrays   #
# for situations where it is required.                                     #
#                                                                          #
# Three commands are used to implement the parity scan and repair process: #
# scn(1M), rpr(1M), and pscan(1M).  Consult the corresponding man pages for#
# detailed information on these commands.  These commands and this script  #
# have been designed to optimize the performance and reliability of the    #
# parity scan process.                                                     #
#                                                                          #
# The parity scan process is performed on all disk arrays marked "dirty".  #
# Disk arrays are marked "dirty" when the system is not shutdown           #
# properly.                                                                #
#                                                                          #
############################################################################

PATH=/usr/sbin:/usr/bin:/sbin
export PATH

hparray_stop()
{
##########################################################################
#                                                                        #
# Disk array shutdown script                                             #
#                                                                        #
# This script performs the following required functionality for the      #
# disk array during system shutdown:                                     #
#                                                                        #
#   Mark the HPC2400 disk array parity data as clean.                    #
#   Kill the disk array monitor daemon "arraymond".                      #
#   Kill the "ARMServer" daemon                                          # 
#                                                                        #
##########################################################################


   ##########################################################################
   #                                                                        #
   # Procedure to mark LUN disk array parity data as clean.                 #
   #                                                                        #
   ##########################################################################
   mark_devices()
   {
     #
     # For each array LUN device read the devfile from stdin.
     #
     while read VENDR PROD DFILE 
       do
         if [[ $PROD = "C2430D" ]]
         then
           echo "Marking HPC2400 disk array parity data clean"
           PSCAN_FAILURE=0
           ${PERFORM_PARITY_SCAN} -s 1 ${DFILE} || PSCAN_FAILURE=1
           if [ ${PSCAN_FAILURE} -eq 1 ]
           then
             echo "HPC2400 DISK ARRAY PARITY SCAN/REPAIR FAILED." 
             echo "${PERFORM_PARITY_SCAN} failed."
             echo "Unable to mark ${DFILE} parity data as clean."
             echo "Take corrective action and execute PSCAN(1M) on ${DFILE}."
           else
             echo "${PERFORM_PARITY_SCAN}: ${DFILE} - parity data marked clean."
           # perform parity scan
           fi
         # check product id
         fi
       # read DFILE
       done
   }

   ##########################################################################
   #                                                                        #
   # Main                                                                   #
   #                                                                        #
   ##########################################################################
   #
   # Mark the HPC2400 disk arrays as properly shutdown
   #
   echo ""
   date 
   mark_devices < ${UNITS_LIST} 

   #-----------------------------------------------
   # Terminate the array monitor daemon 
   #-----------------------------------------------
   ARRAYMON_PID=`ps -ef | awk '$0 ~ /.*arraymon*/ && $0 !~ /.*awk.*/ { print $2 }'`
   if [ "$ARRAYMON_PID" ]
   then
     echo "Killing disk array monitor daemon." 
     kill -9 $ARRAYMON_PID 
     sleep 2
     ARRAYMON_PID=`ps -ef | awk '$0 ~ /.*arraymon*/ && $0 !~ /.*awk.*/ { print $2 }'`
     if [ "$ARRAYMON_PID" ]
     then
       echo "ERROR:  Could not kill ${ARRAY_MONITOR_DAEMON}"
     fi
   fi
   
   #-------------------------------------------------
   # Terminate the ARMServer daemon 
   #-------------------------------------------------
   ARMSERVER_PID=`ps -ef | awk '$0 ~ /.*ARMServer*/ && $0 !~ /.*awk.*/ { print $2 }'`
   if [ "$ARMSERVER_PID" ]
   then
     echo "Killing ${ARMSERVER_DAEMON} process." 
     kill -9 $ARMSERVER_PID 
     sleep 2
     ARMSERVER_PID=`ps -ef | awk '$0 ~ /.*ARMServer*/ && $0 !~ /.*awk.*/ { print $2 }'`
     if [ "$ARMSERVER_PID" ]
     then
       echo "ERROR:  Could not kill ${ARMSERVER_DAEMON}"
     fi
   fi
}

###########################################################################
#                                                                         #
#  Main MAIN main                                                         #
#                                                                         #
#  PARAMETERS:                                                            #
#                                                                         #
#  $1 : Startup/shutdown argument [ start | stop | start_msg | stop_msg ] #
#                                                                         #
###########################################################################

  if [ $# -ne 1 ]
     then
     echo "Usage:  $0 [ start | stop | start_msg | stop_msg ]." >&2
     exit 1
  fi
  
  exit_val=0

  case "$1" in

     start_msg) 
        echo "Starting disk array monitor daemons." 
     ;; 

     stop_msg) 
        echo "Stopping disk array monitor daemons."
     ;; 

     start)                         ### START ###     
. /etc/rc.config.d/hparray
. /usr/lbin/hpC2400/arraymon.hdr
        if [ ${HPARRAY_START_STOP} -eq 1 ]
        then
          nohup ${ARRAY_STARTUP_FILE} >> ${ARRAYSCAN_OUTPUT}  2>&1 &
        fi
     ;;

     stop)                          ### STOP ###    
. /etc/rc.config.d/hparray
. /usr/lbin/hpC2400/arraymon.hdr
        if [ ${HPARRAY_START_STOP} -eq 1 ]
        then
          if [ -f ${UNITS_LIST} ]
          then 
            hparray_stop >> ${ARRAYSCAN_OUTPUT} 2>&1
          fi
        fi
     ;;

     *)         echo "ERROR:  $0 Invalid parameter \"$1\"." >&2         ### ERROR ###
                exit 1
     ;;

  esac

  exit ${exit_val}
