#!/bin/bash

IQN=$1
HOST=$2

if [[ -z "${IQN}" || -z "${HOST}" ]]; then
	echo "usage: $0 TargetIQN Host"
        echo ""

	exit
fi

iscsiadm -m discovery -t sendtargets -o update -p ${HOST} &> /dev/null
iscsiadm -m node -T ${IQN} -p ${HOST} -l

sleep 0.5

DEVICEPATH=$(ls /dev/disk/by-path | grep "${IQN}" | head -n 1)
DEVICE=$(readlink -nf /dev/disk/by-path/${DEVICEPATH})

FILESYSTEM=$(blkid ${DEVICE})
if [ -z "${FILESYSTEM}" ]; then
	mkfs.ext4 ${DEVICE}
fi

mkdir -p /mnt/iSCSI_${IQN}
mount ${DEVICE} /mnt/iSCSI_${IQN}

#umount /mnt/iSCSI_${IQN}
#rm -rf /mnt/iSCSI_${IQN}
#iscsiadm -m node -T ${IQN} -p ${HOST} -u

