#! /bin/sh 

### BEGIN INIT INFO
# Provides: iscsiclsetup 
# Required-Start: $network $syslog $remote_fs smartd
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: ISCSI client setup 
### END INIT INFO

IPLIST="192.168.142.101"
 
# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status


# catch mis-use right here at the start
if [  "$1" != "start"  -a  "$1" != "stop"  -a  "$1" != "status" -a "$1" != "restart" -a "$1" != "rescan" -a "$1" != "mountall" ]; then
    echo "Usage: $0 {start|stop|status|restart|rescan|mountall}"
    exit 1
fi

# First reset status of this service
rc_reset

iscsimount() {
	rc_reset
	echo -n "Mounting $1: "
	/usr/lpp/mmfs/bin/mmmount $1
	rc_status -v
	return $? 
}

iscsiumount() {
	rc_reset
	echo -n "Umounting $1: "
	/usr/lpp/mmfs/bin/mmumount $1
	rc_status -v
	return $?
}

iscsicheck() {
	rc_reset
	echo -n "Verify if $1 is mounted: "
	mount | grep "on $1\b" > /dev/null
	rc_status -v
	return $?
}

iscsimountall() {
	# Find all fstab lines with gpfs as fstype
	for mountpoint in `grep "gpfs" /etc/fstab | awk '{print $2}'`
  	do
	   # Only try to mount filesystems that are not currently mounted
	   if ! mount | grep "on $mountpoint\b" > /dev/null 
	   then
	      iscsimount $mountpoint || overallstatus=$?
	   fi
	done
	return $overallstatus
}
 	
iscsiumountall() {
	# Find all fstab lines with gpfs as fstype
	for mountpoint in `grep "gpfs" /etc/fstab | awk '{print $2}'`
  	do
	   # Only try to umount filesystems that are currently mounted
	   if mount | grep "on $mountpoint\b" > /dev/null 
	   then
	      iscsiumount $mountpoint || overallstatus=$?
	   fi
	done
	return $overallstatus
}

iscsicheckall() {
	# Find all fstab lines with gpfs as fstype
	for mountpoint in `grep "gpfs" /etc/fstab | awk '{print $2}'`
  	do
	   iscsicheck $mountpoint || overallstatus=$?
	done
	return $overallstatus
}

case "$1" in
  start)
	modprobe -q iscsi_tcp
        iscsid
	for IP in $IPLIST
	do
   	   ping -q $IP -c 1 -W 1 > /dev/null
	   RETURN_ON_PING=$?
	   if [ ${RETURN_ON_PING} == 0 ]; then
		ISCSI_VALUES=`iscsiadm -m discovery -t st -p $IP \
                           | awk '{print $2}' | uniq`
	        if [ "${ISCSI_VALUES}" != "" ] ; then
		   for target in $ISCSI_VALUES
		   do
		      echo "Logging into $target on $IP"
		      iscsiadm --mode node --targetname $target \
                          --portal $IP:3260 --login
		   done
		else
		   echo "No iscsitarget were discovered"
		fi 
 	   else
	       echo "iscsitarget is not available"
	   fi
	done
        if [ ${RETURN_ON_PING} == 0 ]; then
           if [ "${ISCSI_VALUES}" != "" ] ; then
	      /usr/lpp/mmfs/bin/mmstartup -a &> /dev/null
              iscsimountall
           fi
	fi
    	;;
  stop)
	for IP in $IPLIST
	do
	   ping -q $IP -c 1 -W 1 > /dev/null
           RETURN_ON_PING=$?
           if [ ${RETURN_ON_PING} == 0 ]; then
		ISCSI_VALUES=`iscsiadm -m discovery -t st --portal $IP \
                      | awk '{print $2}' | uniq`
	        if [ "${ISCSI_VALUES}" != "" ] ; then
		   for target in $ISCSI_VALUES
		   do
		      echo "Logging out for $target from $IP"
		      iscsiadm -m node --targetname $target \
                         --portal $IP:3260 --logout
		   done
		else
		   echo "No iscsitarget were discovered"
		fi
           fi
	done
        if [ ${RETURN_ON_PING} == 0 ]; then
           if [ "${ISCSI_VALUES}" != "" ] ; then
              iscsiumountall
	   fi
        fi
	;;
  status)
	echo "Running sessions"
	iscsiadm -m session -P 1
        iscsicheckall
        rc_status -v
	;;	

  rescan)
	echo "Perform a SCSI rescan on a session"
	iscsiadm -m session -r 1 --rescan
	rc_status -v
	;;

  mountall)
	iscsimountall
	rc_status -v
	;;
	
  restart)
    ## Stop the service and regardless of whether it was
    ## running or not, start it again.
    $0 stop
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|rescan|mountall}"
    exit 1
esac

rc_status -r

rc_exit

