#!/bin/bash

###############################################################################
# Set up target 
###############################################################################

# Create Target ID 1..
tgtadm --lld iscsi --mode target --op new --tid 1 -T iqn.2008-09.com.example:server.tape
sleep 1

###############################################################################
# Set up tape drives
###############################################################################

tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 1 -b /root/tapes/notape --device-type tape
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 1 --params vendor_id=HP,product_id="LTO3 ULTRIUM",product_rev=0001,scsi_sn=HUM1A00001,scsi_id="HP LTO3 ULTRIUM",removable=1
tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 2 -b /root/tapes/notape --device-type tape
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 2 --params vendor_id=HP,product_id="LTO3 ULTRIUM",product_rev=0001,scsi_sn=HUM1A00002,scsi_id="HP LTO3 ULTRIUM",removable=1
tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 3 -b /root/tapes/notape --device-type=tape
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 3 --params vendor_id=HP,product_id="LTO3 ULTRIUM",product_rev=0001,scsi_sn=HUM1A00003,scsi_id="HP LTO3 ULTRIUM",removable=1


###############################################################################
# Set up SMC Medium Changer
###############################################################################
if [ ! -f $HOME/smc ]; then
	dd if=/dev/zero of=$HOME/smc bs=1k count=1
fi

tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 4 -b $HOME/smc --device-type=changer

#### Set up mode pages ####
# From smc3-06.pdf
# Page 0x02: Disconnect/Reconnect SPC-3
# Page 0x0a: Control SPC-3
# Page 0x18: Protocol Specific LUN SPC-3
# Page 0x19: Protocol Specific Port SPC-3
# Page 0x1a: Power Condition SPC-3
# Page 0x1c: Informational Exceptions Control SPC-3
# Page 0x1d: Element Address Assignment SMC-3 7.3.4
# Page 0x1e: Transport Geometry Parameters SMC-3 7.3.5
# Page 0x1f: Device Capabilities SMC-3 7.3.2
# Page 0x1f/Subpage 0x41: Extended Device Capabilities SMC-3 7.3.3

# Dummy 'page 0'
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params mode_page=0:0:0

# Disconnect/Reconnect
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params mode_page=2:0:14:0x80:0x80:0:0xa:0:0:0:0:0:0:0:0:0:0

# Power Condition
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params mode_page=0x1a:0:18:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0

# Informational Exceptions Control Mode Page
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params mode_page=0x1c:0:10:8:0:0:0:0:0:0:0:0:0

# Element Address Assignment - Setup afterwards.
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params mode_page=0x1d:0:0x12:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0

# Transport Geometry Parameters
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params mode_page=0x1e:0:2:0:0

# Device Capabilities
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params mode_page=0x1f:0:0x12:0x0f:7:0x0f:0x0f:0x0f:0x0f:0:0:0:0:0x0f:0x0f:0x0f:0x0f:0:0:0:0

tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params vendor_id=STK,product_id=L700,product_rev=0001,scsi_sn="123:456:789:001",scsi_id="STK L700",removable=0

## Add Data Transfer devices (3 drives)
# Define slot address for devices.
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=4,start_address=500,quantity=3

# Now define which device at each address.
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=4,address=500,tid=1,lun=1
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=4,address=501,tid=1,lun=2
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=4,address=502,tid=1,lun=3

# Medium Transport Elements (robot arm / picker)
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=1,start_address=1,quantity=1

## Storage Elements - 24 starting at addr 1000
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=2,start_address=1000,quantity=24

# Add 'media' to slots
#tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=2,address=1000,barcode=A0000001,sides=1
#tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=2,address=1001,barcode=A0000002,sides=1
#tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=2,address=1002,barcode=A0000003,sides=1
#tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=2,address=1003,barcode=A0000004,sides=1
#tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=2,address=1004,barcode=A0000005,sides=1
#tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=2,address=1005,barcode=A0000006,sides=1
#tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=2,address=1006,barcode=A0000007,sides=1
#tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=2,address=1007,barcode=A0000008,sides=1
#tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=2,address=1023,barcode=CLN00001,sides=1

# Import/Export Elements - 5 starting at addr 10
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params element_type=3,start_address=10,quantity=5

# define path to virtual media
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params media_home=/root/tapes

# Dump the list of configured slots to syslog...
tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 4 --params dump=1


# Allow ALL initiators to connect to this target
tgtadm --lld iscsi --mode target --op bind --tid 1 -I ALL


# Show all our good work.
tgtadm --lld iscsi --mode target --op show


