#!/bin/bash
# This script attempts to mount file systems on UNH iSCSI Disks

UNH_ISCSI_DEV=/dev
UNH_FSTAB=/etc/unh_iscsi_initiator/fstab.iscsi

(( mount = 0 ))
(( ecount = 0 ))

if [ ! -r $UNH_FSTAB -o ! -d $UNH_ISCSI_DEV ]
then
	echo "iscsi_mount: ERROR: fstab.iscsi and/or device files missing"
	exit 0
fi

while read TARGET MOUNT FS OPTS JUNK
do
	COM="`echo $TARGET | /bin/grep -v '[ \t]*#'`"
	[ -z "$COM" ] && continue
	set `echo $TARGET | /bin/sed 's/:/ /g'` 
	TARG_NAME=$1;LUN=$2;PART=$3

	[ -z "$TARG_NAME" ] && continue
	[ -z "$LUN" ] && lun=0

	if [ ! -z "$PART" ]
	then
		DEV=${UNH_ISCSI_DEV}/${TARG_NAME}/l${LUN}p${PART}
	else
		DEV=${UNH_ISCSI_DEV}/${TARG_NAME}/l${LUN}
	fi

	if [ ! -r ${DEV} ]
	then
		echo "iscsi_mount: Device ${DEV} for ${TARGET} not available. Skipping entry."
		continue
	fi

	(( mcount = mount + 1 ))
	if [ "$MOUNT" != "swap" ]
	then
		/sbin/fsck -a -T ${DEV} < /dev/null
		if [ $? -gt 1 ]
		then
			echo "iscsi_mount: fsck failed in ${DEV} for ${TARGET}"
			(( ecount = ecount + 1 ))
			continue
		fi

		/bin/mount -t ${FS} -o ${OPTS} ${DEV} ${MOUNT}
		if [ $? -ne 0 ] 
		then
			echo "iscsi_mount: mount failed in ${DEV} for ${TARGET}"
			(( ecount = ecount + 1 ))
			continue
		fi
	else
		/sbin/swapon ${DEV}
		if [ $? -ne 0 ] 
		then
			echo "iscsi_mount: swapon failed in ${DEV} for ${TARGET}"
			(( ecount = ecount + 1 ))
			continue
		fi
	fi

done < $UNH_FSTAB

# No errors at all
(( ecount == 0 ))  && exit 0

# Some or all mounts failed
exit 1
