#!/bin/sh
# @(#) Serviceguard Safety Timer Initialization Startup Script
# @(#) Product Name                :  HPE Serviceguard
# @(#) Product Version             :  A.12.10.00
#
#           Copyright (C) 2004-2012 Hewlett-Packard Company
#                      ALL RIGHTS RESERVED.
#
# NOTE:    This script is not configurable!  Any changes made to this
#          script will be overwritten when you upgrade to the next
#          release of Serviceguard.
#
# WARNING: Changing this script in any way may lead to a system that
#          is unbootable.  Do not modify this script.
#
### BEGIN INIT INFO
# Provides:          Safety_timer
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# X-Start-Before:
# X-Stop-After:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# X-Interactive:
# Short-Description: Load Safety timer
# Description:       Starts Safety Timer service for HP Serviceguard.
### END INIT INFO

. ${SGCONFFILE:=/etc/cmcluster.conf}

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

LOGGER="echo"
test -x /sbin/initlog && LOGGER="/sbin/initlog -n cmcluster -s "

# action() will display the message using echo then
# logs the message to the system logger (using initlog).
action () {
 STRING=$1
 echo "$STRING"
 test -x /sbin/initlog && ${LOGGER} "$STRING"
}
awk=/bin/awk
lspci=/sbin/lspci
if [ -f /etc/debian_version ]; then
    awk=/usr/bin/awk
    lspci=/usr/bin/lspci
fi

case "$1" in

start)

NODE_TOC_BEHAVIOR=""
if [ -f ${SGCONF}/cmcluster.rc ]
then
    . ${SGCONF}/cmcluster.rc
else
    action "ERROR: ${SGCONF}/cmcluster.rc defaults file MISSING"
    exit 1
fi

# This is to set node to either reboot or panic
# when its safety time expires
# In case if NODE_TOC_BEHAVIOR is not specified, the default will
# be to reboot the node.
SETMODE="reboot"
if [ "$NODE_TOC_BEHAVIOR" = "panic" ]
then
    SETMODE="panic"
fi

VER=""
KERNPATH="/lib/modules"
dh_platform_info=0
bigsmp=0

VER=$(/bin/uname -r)
KERNPATH=${KERNPATH}/${VER}/extra

# Make sure that the .cm_start_time file exists when the system
# reboots. This is in case the /var/run directory is cleaned up
# as in the case of sles9.
/bin/rm -f ${SGRUN}/.cm_start_time
/bin/touch ${SGRUN}/.cm_start_time

/sbin/lsmod | /bin/grep deadman >/dev/null 2>&1
is_deadman_loaded=$?
/usr/sbin/dmidecode -t 1 | $awk '$1=="Family:"{print $2}' | /bin/grep -q -E "\<103CPID03010201\>" 2>/dev/null
if [ $? -eq 0 ]
then
    dh_platform_info=1
fi

if [ -f /etc/redhat-release ]; then
    rhrelease_line=`grep "Red Hat" /etc/redhat-release`
    rhrelease=${rhrelease_line##*release }
    rhrelease_number=${rhrelease%% (*}
    rhrelease_number=${rhrelease_number%%.*}
fi

${LOGGER} "Loading deadman with platform info parameter:$dh_platform_info."
if [ $is_deadman_loaded -gt 0 ]; then
  # load the driver. If it load ok then set is_deadman_loaded
  # to 0, otherwise leave it as its previous value.
  if [ -f /boot/config-`uname -r` ]
  then
      tmp=$(/bin/cat /boot/config-`uname -r`| /bin/grep '^CONFIG_HZ=') > /dev/null 2>&1
      confighz=${tmp##CONFIG_HZ=}
      if [ "$confighz" != "" ]; then
          if [ $confighz -le 1000 ]; then
              /sbin/modprobe deadman mode=$SETMODE systemhz=$confighz platform_info=$dh_platform_info && is_deadman_loaded=0
              if [[ $dh_platform_info -eq 1 ]]
              then
                  if [ -f /etc/redhat-release ]; then
                      echo "Retrying loading deadman."
                      cd %{sgsbin}/../drivers/RHEL${rhrelease_number}/NON_ACPI
                      /bin/cp deadman.ko ${KERNPATH}
                      /sbin/depmod -a 
                      /sbin/modprobe deadman mode=$SETMODE systemhz=$confighz platform_info=$dh_platform_info && is_deadman_loaded=0
                  fi
              fi
          else
              is_deadman_loaded=1
          fi
      else
          ${LOGGER} "Cannot find CONFIG_HZ, will load deadman with default value."
          /sbin/modprobe deadman mode=$SETMODE platform_info=$dh_platform_info && is_deadman_loaded=0
          if [[ $dh_platform_info -eq 1 ]]
          then
              if [ -f /etc/redhat-release ]; then
                  echo "Retrying loading deadman."
                  cd %{sgsbin}/../drivers/RHEL${rhrelease_number}/NON_ACPI
                  /bin/cp deadman.ko ${KERNPATH}
                  /sbin/depmod -a
                  /sbin/modprobe deadman mode=$SETMODE platform_info=$dh_platform_info && is_deadman_loaded=0
              fi
          fi
      fi
  else
      ${LOGGER} "Cannot find CONFIG_HZ, will load deadman with default value."
      /sbin/modprobe deadman mode=$SETMODE platform_info=$dh_platform_info && is_deadman_loaded=0
      if [[ $dh_platform_info -eq 1 ]]
      then
          if [ -f /etc/redhat-release ]; then
              echo "Retrying loading deadman."
              cd %{sgsbin}/../drivers/RHEL${rhrelease_number}/NON_ACPI
              /bin/cp deadman.ko ${KERNPATH}
              /sbin/depmod -a
              /sbin/modprobe deadman mode=$SETMODE platform_info=$dh_platform_info && is_deadman_loaded=0
          fi
      fi
  fi
fi

# If deadman module is still not loaded and if it is SLES system, there is a chance
# that system has been upgraded to SP3 from SP2, in which case right deadman module
# has to be loaded.
if [ $is_deadman_loaded -gt 0 ]; then
    if [ -f /etc/SuSE-release ]; then
        if [ ! -f /lib/modules/${VER}/extra/deadman.ko ]
        then
            if [ ! -f /lib/modules/${VER}/weak-updates/deadman.ko ]
            then
                version=`/bin/grep "VERSION" /etc/SuSE-release`
                slesrelease=`echo $version| $awk '{ print $3}'`
                patchlevel=`/bin/grep "PATCHLEVEL" /etc/SuSE-release`
                sprelease=`echo $patchlevel| $awk '{ print $3}'`
                if [ $slesrelease -eq 12 ]|| [ $slesrelease -eq 11 && $sprelease -gt 2 ] ; then
                    /bin/cat /boot/config-`uname -r` | /bin/grep '^CONFIG_LOCALVERSION=' \
                            | /bin/grep -q '\-bigsmp' 2> /dev/null
                    if [ $? -eq 0 ] ; then
                        echo "Platform has bigsmp kernel loading deadman from BIGSMP directory"
                        bigsmp=1
                        cd ${SGSBIN}/../drivers/SLES${slesrelease}/BIGSMP_SP3/ACPI
                    elif [ $dh_platform_info -eq 1 ]
                    then
                        cd ${SGSBIN}/../drivers/SLES${slesrelease}/SP${sprelease}/ACPI
                    else
                        cd ${SGSBIN}/../drivers/SLES${slesrelease}/SP${sprelease}/NON_ACPI
                    fi
                    /bin/mkdir -p ${KERNPATH}
                    /bin/cp deadman.ko ${KERNPATH}
                    /sbin/depmod -a
                    ${LOGGER} "Loading deadman with platform info parameter:$dh_platform_info."
                    if [ -f /boot/config-`uname -r` ]
                    then
                        tmp=$(/bin/cat /boot/config-`uname -r`| /bin/grep '^CONFIG_HZ=') > /dev/null 2>&1
                        confighz=${tmp##CONFIG_HZ=}
                        if [ "$confighz" != "" ]; then
                            if [ $confighz -le 1000 ]; then
                                /sbin/modprobe deadman mode=$SETMODE systemhz=$confighz platform_info=$dh_platform_info && is_deadman_loaded=0
                                if [[ $is_deadman_loaded -ne 0 ]]
                                then
                                    if [ $dh_platform_info -eq 1 ]; then
                                        echo "Retrying loading deadman."
                                        cd %{sgsbin}/../drivers/SLES${slesrelease}/SP${sprelease}/NON_ACPI
                                        /bin/cp deadman.ko ${KERNPATH}
                                        /sbin/depmod -a
                                        /sbin/modprobe deadman mode=$SETMODE systemhz=$confighz platform_info=$dh_platform_info && is_deadman_loaded=0 
                                    fi 
                                fi
                            else
                                is_deadman_loaded=1
                            fi
                        else
                            ${LOGGER} "Cannot find CONFIG_HZ, will load deadman with default value."
                            /sbin/modprobe deadman mode=$SETMODE platform_info=$dh_platform_info && is_deadman_loaded=0
                            if [[ $is_deadman_loaded -ne 0 ]]
                            then
                                if [ $dh_platform_info -eq 1 ]; then
                                    echo "Retrying loading deadman."
                                    cd %{sgsbin}/../drivers/SLES${slesrelease}/SP${sprelease}/NON_ACPI
                                    /bin/cp deadman.ko ${KERNPATH}
                                    /sbin/depmod -a
                                    /sbin/modprobe deadman mode=$SETMODE platform_info=$dh_platform_info && is_deadman_loaded=0 
                                fi 
                            fi
                        fi
                    else
                        ${LOGGER} "Cannot find CONFIG_HZ, will load deadman with default value."
                        /sbin/modprobe deadman mode=$SETMODE platform_info=$dh_platform_info && is_deadman_loaded=0
                        if [[ $is_deadman_loaded -ne 0 ]]
                        then
                            if [ $dh_platform_info -eq 1 ]; then
                                echo "Retrying loading deadman."
                                cd %{sgsbin}/../drivers/SLES${slesrelease}/SP${sprelease}/NON_ACPI
                                /bin/cp deadman.ko ${KERNPATH}
                                /sbin/depmod -a
                                /sbin/modprobe deadman mode=$SETMODE platform_info=$dh_platform_info && is_deadman_loaded=0 
                            fi 
                        fi
                    fi
                fi
            fi
        fi
    fi
fi

if [ $is_deadman_loaded -eq 0 ]; then
     defaulthz=$(/bin/cat /proc/deadman/info | /bin/grep CONFIG_HZ | $awk '{print $NF}')
     ${LOGGER} "Loaded deadman with CONFIG_HZ value $defaulthz."
     # Now lets create the /dev/deadman node. The major number
     # for this driver is 10, which is the major number of
     # the misc device driver. The minor number for the /dev/deadman
     # device is stored in the /proc/misc file.
     minor=$(/bin/grep deadman /proc/misc | $awk '{print $1}')
     /bin/rm -rf /dev/deadman
     /bin/mknod /dev/deadman c 10 ${minor} -m u=rw -m og=-r
     ${LOGGER} "Created /dev/deadman c 10 ${minor}"
else
     ${LOGGER} "Deadman failed to install."
     exit 0
fi

ia64=0
hpvm_guest=0
vmware_guest=0
kvm_guest=0
rhev_guest=0
keystring="none"
tmp_file=""

/bin/rm -f ${SGSBIN}/cmvminfo
if [ $hpvm_guest -eq 0 ]; then
    /usr/sbin/dmidecode | /bin/grep -i vmware > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
        vmware_guest=1
        keystring="vmware"
        /bin/ln -s ${SGSBIN}/vminfo ${SGSBIN}/cmvminfo
    else
        /usr/sbin/dmidecode | /bin/grep -w -i KVM > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
            kvm_guest=1
            keystring="kvm"
        else
            /usr/sbin/dmidecode | /bin/grep -w -i RHEV > /dev/null 2>&1
            if [ $? -eq 0 ]
            then
                rhev_guest=1
                keystring="rhev"
            fi
        fi
    fi
fi

#if there is a line in /etc/cmcluster.conf that looks like
#SG_VIRTUAL_NODE_TYPE=*, make sure it is keystring, otherwise,
#add the line
tmp_file=/etc/tmp.$$
if [ -f ${SGCONFFILE} ]; then
    /bin/grep -i "^\W*SG_VIRTUAL_NODE_TYPE=$keystring" ${SGCONFFILE} >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
        /bin/grep -i "^.*SG_VIRTUAL_NODE_TYPE[^[:alnum:]]*=.*" ${SGCONFFILE} >/dev/null 2>&1
        if [ $? -eq 0 ]
        then
            sed -e 's/^.*SG_VIRTUAL_NODE_TYPE.*$/SG_VIRTUAL_NODE_TYPE='$keystring'/I' ${SGCONFFILE} > $tmp_file
            /bin/mv -f $tmp_file ${SGCONFFILE}
        else
            if [ $hpvm_guest -eq 1 ] || [ $vmware_guest -eq 1 ] || [ $kvm_guest -eq 1 ] || [ $rhev_guest -eq 1 ]
            then
                echo "SG_VIRTUAL_NODE_TYPE=$keystring" >> ${SGCONFFILE}
            fi
        fi
    fi
fi

;;

stop)
;;

restart)
$0 stop
$0 start
;;

*)
        echo "usage: $0 {start|stop|restart}"
        ;;
esac

exit 0
