#!/bin/sh

PS3_IQN=`iscsi-name`
IPV4_ADDR=`ifconfig eth0 | grep "inet addr" | awk '{ print $2 }' | awk 'BEGIN { FS=":" } { print $2 }'`

HOST_ID=`ls /sys/block/sr0/device/scsi_device/ | awk -F':' '{ print $1 }'`
CHANNEL_ID=`ls /sys/block/sr0/device/scsi_device/ | awk -F':' '{ print $2 }'`
TARGET_ID=`ls /sys/block/sr0/device/scsi_device/ | awk -F':' '{ print $3 }'`
LUN_ID=`ls /sys/block/sr0/device/scsi_device/ | awk -F':' '{ print $4 }'`

# Reference the TYPE_ROM by SCSI HCTL 1:0:0:0
tcm_node --pscsi pscsi_$HOST_ID/bdrom $CHANNEL_ID:$TARGET_ID:$LUN_ID 
# Create the iSCSI Target endpoint
lio_node --addtpg $PS3_IQN 1
# Create the iSCSI LUN to port lio_bdrom from TCM object pscsi_$HOST_ID/bdrom
lio_node --addlun $PS3_IQN 1 0 lio_bdrom pscsi_$HOST_ID/bdrom
# Create the IPv4 network portal
lio_node --addnp $PS3_IQN 1 $IPV4_ADDR
# Enable demo mode status for TPG
lio_node --demomode $PS3_IQN 1
# Disable strict authentication for TPG
lio_node --disableauth $PS3_IQN 1
# Allow iSCSI Initiators to access iSCSI Target endpoint
lio_node --enabletpg $PS3_IQN 1

# Display current status
/etc/init.d/target status
# Save newly generated configuration across reboots
tcm_dump --o
