#!/bin/sh
#
# This is a script to set up a SCSI DVD JUKEBOX for all your ISO images.
#
# This script should be run from a directory that contains ISO images
# with the extension .iso .
# When run, the script will create an iSCSI target containing one DVD drive
# and one media changer. The DVD drive will not have a disk mounted but
# the medium changer will have one slot for each ISO image found.
# Use the 'mtx' tool to load/unload media.
#
# By default, any ISO images found will be populated with a barcode of the
# form DVD_%04d and a volume_tag based on the filename.
#
# CON: This is the connection index for TGTD. This is needed if you run
# multiple instances of TGTD on the same host. Each instance must use a unique
# CON value.
#
# TARGETNAME: The iSCSI name to use for the target.
#
# TARGETPORT: The TCP port that this target will be hosted from.

CON=1
TARGETNAME=iqn.ronnie.iso:distros
TARGETPORT=3261

nc -z 127.0.0.1 $TARGETPORT || {
	echo "Starting iSCSI target on port $TARGETPORT"
	tgtd -C $CON --iscsi portal=0.0.0.0:$TARGETPORT
	sleep 3
}

tgtadm -C $CON --op delete --mode target --tid 1 --force 2>/dev/null
tgtadm -C $CON --op new --mode target --tid 1 -T $TARGETNAME

# Create a DVD drive
tgtadm -C $CON --op new --mode logicalunit --tid 1 --lun 1 -Y cd
tgtadm -C $CON --op update --mode logicalunit --tid 1 --lun 1 --params vendor_id=STGT_DVD,product_id=DVD101,product_rev=0010,scsi_sn=STGTDVD01,removable=1

# We need a backend store file for the media changer
SMC=`pwd`/smc-$CON
if [ ! -f $SMC ]; then
	dd if=/dev/zero of=$SMC bs=1k count=1
fi

# Create the SMC device and give it a nice name
tgtadm -C $CON --mode logicalunit --op new --tid 1 --lun 2 -b $SMC --device-type=changer
tgtadm -C $CON --mode logicalunit --op update --tid 1 --lun 2 --params vendor_id=STK,product_id=L700,product_rev=0010,scsi_sn=XYZZY_0,removable=1

# Add a Data Transfer devices (1 drive)
tgtadm -C $CON --mode logicalunit --op update --tid 1 --lun 2 --params element_type=4,start_address=1,quantity=1

# Specify that the DVD above (LUN 1) is the data transfer device we created
tgtadm -C $CON --mode logicalunit --op update --tid 1 --lun 2 --params element_type=4,address=1,tid=1,lun=1

# Medium Transport Elements (robot arm / picker)
tgtadm -C $CON --mode logicalunit --op update --tid 1 --lun 2 --params element_type=1,start_address=16,quantity=1


# define path to virtual media
VTL=`pwd`/vtl
mkdir -p ${VTL} 2>/dev/null
rm ${VTL}/*
tgtadm -C $CON --mode logicalunit --op update --tid 1 --lun 2 --params media_home=${VTL}

NUM_ELEM=`ls *.iso | wc -l`
echo "Creating $NUM_ELEM storage elements for media"

# Storage Elements - starting at addr 1024
tgtadm -C $CON --mode logicalunit --op update --tid 1 --lun 2 --params element_type=2,start_address=1024,quantity=$NUM_ELEM

# Populate the slots
SLOT=0
ls *.iso | sed -e "s/.iso$//" | while read ISO; do
	BARCODE=`echo $SLOT | awk '{printf "DVD_%04d", $1}'`
	ln `pwd`/${ISO}.iso ${VTL}/${BARCODE}
	tgtadm -C $CON --mode logicalunit --op update --tid 1 --lun 2 --params element_type=2,address=`expr "1024" "+" "$SLOT"`,barcode="${BARCODE}",volume_tag="${ISO}",sides=1

	SLOT=`expr "$SLOT" "+" "1"`
done

tgtadm -C $CON --op bind --mode target --tid 1 -I ALL
tgtadm -C $CON --op show --mode target
