#!/bin/sh
#
# Sample setup script that loads a 
# target driver and initiator on the same machine.
#
# Note that the target is **generally **
# built as a Memory RAM device that will appear
# as a /dev/sdX where X is the next available
# SCSI unit on the next HBA. It reads nothing
# and writes to nowhere ( however data transfer
# does occur ). 
#
# You can use FILE I/O or direct SCSI I/O
# by changing the defines in 
# /opt/unh/iscsi/src/target/scsi_target.h
#
# See the README in the src/target directory for
# slightly more detail.
#
# If this scripts succeeds , Check /var/log/messages
# for the new device names :
# 
#  Attached scsi disk sda at scsi0, channel 0, id 0
#
# fdisk /dev/sda 
#
# You can at least do I/O using dd if=/dev/sda of=/dev/null
# 


export PATH=$PATH:/opt/unh/iscsi/bin

#Set the HBA number . 0 if no other SCSI HBA's present


# this starts a init/target setup on a local loopback 

# These are the SCSI mid level modules

/usr/local/sbin/insmod scsi_mod
/usr/local/sbin/insmod sd_mod
                     	

# remove previous versions 
/usr/local//sbin/rmmod unh_iscsi_target
/usr/local/sbin/rmmod unh_scsi_target

HOSTPATH="`ls /proc/scsi/iscsi_initiator/* | /bin/egrep -v "target|conn|stat" 2>/dev/null`"
export HBA="`/bin/basename $HOSTPATH`"
HBA=0


# load new ones 
here=`pwd`

cd /tmp
/usr/local//sbin/insmod /opt/unh/iscsi/modules/`uname -r`/unh_scsi_target.ko
/usr/local//sbin/insmod  /opt/unh/iscsi/modules/`uname -r`/unh_iscsi_target.ko
cd $here

############## Configure Target ###############
iscsi_manage target set TargetPortalGroupTag=1 host=$HBA

iscsi_manage target set HeaderDigest=CRC32C,None host=$HBA
iscsi_manage target set DataDigest=CRC32C,None host=$HBA

iscsi_manage target set MaxConnections=2 host=$HBA

############## Error Recovery #################
iscsi_manage target set ErrorRecoveryLevel=1 host=$HBA
iscsi_manage target force r2tp=2 host=$HBA
iscsi_manage target snack d=y host=$HBA
iscsi_manage target snack s=y host=$HBA
iscsi_manage target snack h=1 host=$HBA


# Initiator Conf paramters

/usr/local/sbin/rmmod  unh_iscsi_initiator
/usr/local/sbin/insmod  /opt/unh/iscsi/modules/`uname -r`/unh_iscsi_initiator.ko

HOSTPATH="`ls /proc/scsi/iscsi_initiator/* | /bin/egrep -v "target|conn|stat" 2>/dev/null`"
export HBA="`/bin/basename $HOSTPATH`"

iscsi_manage init restore host=$HBA
iscsi_manage init force host=$HBA
iscsi_manage init set TargetName="`uname -n`_Target" host=$HBA
iscsi_manage init set InitiatorName=`uname -n` host=$HBA
iscsi_manage init set ErrorRecoveryLevel=1 host=$HBA
iscsi_manage init set InitiatorAlias=UNH-Draft20-Initiator host=$HBA
iscsi_manage init set ImmediateData=Yes host=$HBA
iscsi_manage init set MaxConnections=2 host=$HBA

iscsi_config up ip=`uname -n` port=3260 target=0 host=$HBA lun=1
sleep 2
#
# bring up second connection
#
iscsi_manage init restore host=$HBA
iscsi_manage init set TargetName="`uname -n`_Target" host=$HBA
iscsi_manage init set InitiatorName=`uname -n` host=$HBA
iscsi_config up ip=`uname -n` port=3260 target=0 host=$HBA lun=1 cid=2

tail -60 /var/log/messages  | grep SCSI | grep device 



