#!/bin/sh

TO_IP=192.168.0.232
TO_PORT=3260

MMTGTIQN=iqn.2003-01.org.linux-iscsi.bbtest2.x86_64:sn.b314b6b0bbca
MMINITIQN=iqn.2003-01.org.linux-iscsi.bbtest2.x86_64:sn.319bc052efca

ifconfig eth0:0 down
sleep 3

target-ctl coredeltiqn targetname=$MMTGTIQN
target-ctl coredelnp ip=$TO_IP port=$TO_PORT
target-ctl freevirtdev lvm_uuid=JYElqI-kJOD-QwRo-A68B-s6X9-jw6g-Jfyy1p

echo "Calling lvchange -a n LIO-NR1-VOL /dev/LIO-NR1-VOL/NR1-PRIMARY-VOL"
lvchange -a n LIO-NR1-VOL /dev/LIO-NR1-VOL/NR1-PRIMARY-VOL

mdadm -S /dev/md0
RET=$?
if [ $RET != 0 ]; then
	echo "Shutdown of secondary mirror failed!"
	exit 1
fi

echo "Calling lvchange -a n LIO-NR1-Elements /dev/LIO-NR1-Elements/NR1-Remote-Element"
lvchange -a n LIO-NR1-Elements /dev/LIO-NR1-Elements/NR1-Remote-Element

# Needs to be from local LVM UUID..
target-ctl createvirtdev hba_id=0 lvm_uuid=7IBK5i-znZs-h9hN-Hzbc-9WDl-Z3sT-j7yVGB
target-ctl addluntodev tpgt=1 iscsi_lun=2 lvm_uuid=7IBK5i-znZs-h9hN-Hzbc-9WDl-Z3sT-j7yVGB

/sbin/target-ctl addnodetolun tpgt=1 iscsi_lun=2 initiatorname=$INITIQN mapped_lun=0 lun_access=1

/sbin/target-ctl addnodetotpg tpgt=1 queue_depth=32 initiatorname=$MMINITIQN
/sbin/target-ctl setnodeattrib tpgt=1 initiatorname=$MMINITIQN dataout_timeout=3
/sbin/target-ctl setnodeattrib tpgt=1 initiatorname=$MMINITIQN dataout_timeout_retries=5
/sbin/target-ctl setnodeattrib tpgt=1 initiatorname=$MMINITIQN default_erl=2
/sbin/target-ctl setnodeattrib tpgt=1 initiatorname=$MMINITIQN nopin_timeout=5
/sbin/target-ctl setnodeattrib tpgt=1 initiatorname=$MMINITIQN nopin_response_timeout=5
/sbin/target-ctl setnodeattrib tpgt=1 initiatorname=$MMINITIQN random_datain_pdu_offsets=0
/sbin/target-ctl setnodeattrib tpgt=1 initiatorname=$MMINITIQN random_datain_seq_offsets=0
/sbin/target-ctl setnodeattrib tpgt=1 initiatorname=$MMINITIQN random_r2t_offsets=0

/sbin/target-ctl addnodetolun tpgt=1 iscsi_lun=2 initiatorname=$MMINITIQN mapped_lun=0 lun_access=1

echo "Calling vgreduce --removemissing LIO-NR1-Elements"
sleep 5
vgreduce --removemissing LIO-NR1-Elements

