[Stgt-devel] [PATCH v2] iscsi: add setup_lun script

Erez Zilber erezz
Thu Jan 3 08:08:31 CET 2008


Here's a better version that also installs the script:

Add a script that will create a target, add a device to
the target and define the initiator that can connect to it.

Signed-off-by: Erez Zilber <erezz at voltaire.com>
---
 usr/Makefile          |    2 +-
 usr/scripts/setup_lun |  130 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 131 insertions(+), 1 deletions(-)
 create mode 100644 usr/scripts/setup_lun

diff --git a/usr/Makefile b/usr/Makefile
index 6854057..16bd314 100644
--- a/usr/Makefile
+++ b/usr/Makefile
@@ -51,7 +51,7 @@ CFLAGS += -g -O2 -Wall -Wstrict-prototypes -fPIC
 
 LIBS += -lpthread
 
-PROGRAMS += tgtd tgtadm
+PROGRAMS += tgtd tgtadm scripts/setup_lun
 TGTD_OBJS += tgtd.o mgmt.o target.o scsi.o log.o driver.o util.o work.o \
 		parser.o spc.o sbc.o mmc.o osd.o scc.o smc.o bs.o
 
diff --git a/usr/scripts/setup_lun b/usr/scripts/setup_lun
new file mode 100644
index 0000000..e35d623
--- /dev/null
+++ b/usr/scripts/setup_lun
@@ -0,0 +1,130 @@
+# Copyright (C) Voltaire Ltd. 2001-2008.  ALL RIGHTS RESERVED.
+#
+# This source code (the "Code") is a proprietary product of Voltaire Ltd.
+# (the "Company") and all right, title, and interest in and to the Code,
+# including all associated intellectual property rights, are and shall
+# remain exclusively with the Company.
+#
+# The Code is provided "as is". To the maximum extent permitted by law,
+# the Company disclaims any warranties of any kind, either expressed or
+# implied, including, without limitation, implied warranties of
+# merchantability, fitness for a particular purpose and non-infringement.
+# The Company does not warrant that the Code will operate error free, or
+# in an uninterrupted fashion or that it is compatible with any particular
+# platform. In no event shall the Company be liable for any damages
+# whatsoever (including, without limitation, incidental, direct, indirect,
+# special and consequential damages, damages for loss of business profits,
+# business interruption, loss of business information, or other pecuniary
+# loss) arising out of the use or inability to use the Code, even if
+# advised of the possibility of such damages.
+#
+# Author: Erez Zilber <erezz at voltaire.com>
+
+#!/bin/bash
+
+verify_params()
+{
+	# Make sure that the device exists
+	if ! [ -b $dev -o -f $dev ]; then
+		echo "$dev is not a device";
+		exit 1;
+	fi
+}
+
+find_vacant_tgt_id()
+{
+	id_list=$(tgtadm --lld iscsi --op show --mode target | grep Target | cut -d" " -f2 | sed s/://)
+
+	next_vacant_id=1
+
+	for id in $id_list; do
+		if (($id > $next_vacant_id)); then
+			break;
+		else
+			next_vacant_id=$((next_vacant_id+1))
+		fi
+	done
+
+	return $next_vacant_id
+}
+
+find_vacant_lun()
+{
+	tid=$1
+	tgt_found=0
+	next_vacant_lun=0
+	tmp_file=/tmp/target_list.txt
+
+	tgtadm --lld iscsi --op show --mode target > $tmp_file
+
+	while read line; do
+		# Check if we finished going over this target
+		if ((tgt_found == 1 && $(echo $line | grep -c "^Target") == 1)); then
+			break
+		fi
+
+		# Check if we found the requested target
+		if (($(echo $line | grep -c "Target $tid:") == 1)); then
+			tgt_found=1
+			continue
+		fi
+
+		if ((tgt_found == 1 && $(echo $line | grep -c "LUN:") == 1)); then
+			curr_lun=$(echo $line | cut -d" " -f2)
+			if (($curr_lun > $next_vacant_lun)); then
+				break
+			else
+				next_vacant_lun=$((next_vacant_lun+1))
+			fi
+		fi
+	done < $tmp_file
+
+	rm -f $tmp_file
+
+	if ((tgt_found == 0)); then
+		echo "Error: could not find a LUN for target $tid"
+		return -1
+	fi
+
+	return $next_vacant_lun
+}
+
+if [ $# -ne 3 ]; then
+	name=$(basename $0)
+	echo "usage: $name <dev> <initiator name> <initiator IP>";
+	echo "example: $name /dev/sdb1 noni 192.168.10.63";
+	exit 1;
+fi
+
+dev=$1
+initiator=$2
+initiator_ip=$3
+
+verify_params
+
+# Check if tgtd is running (we should have 2 daemons)
+tgtd_count=$(ps aux|grep -c tgtd)
+if [ $tgtd_count -ne 3 ]; then
+	echo "Starting tgtd"
+	tgtd
+fi
+
+tgt_name="iqn.2001-04.com.$(hostname -s)-$initiator"
+
+find_vacant_tgt_id
+tid=$?
+
+# Create the new target
+echo "Creating the new target"
+tgtadm --lld iscsi --op new --mode target --tid $tid -T $tgt_name
+
+find_vacant_lun $tid
+lun=$?
+
+# Add a logical unit to the target
+echo "Adding a logical unit to the target"
+tgtadm --lld iscsi --op new --mode logicalunit --tid $tid --lun $lun -b $dev
+
+# Accept only a specific initiator
+echo "Accepting connections only from $initiator_ip"
+tgtadm --lld iscsi --op bind --mode target --tid $tid -I $initiator_ip
-- 
1.5.3





More information about the stgt mailing list