[Stgt-devel] [PATCH] iscsi: add assign_lun script

Erez Zilber erezz
Thu Jan 3 07:41:19 CET 2008


FUJITA Tomonori wrote:
> On Tue, 01 Jan 2008 13:50:07 +0200
> Erez Zilber <erezz at Voltaire.COM> wrote:
> 
>> FUJITA Tomonori wrote:
>>> From: Erez Zilber <erezz at voltaire.com>
>>> Subject: [Stgt-devel] [PATCH] iscsi: add assign_lun script
>>> Date: Sun, 30 Dec 2007 10:51:16 +0200
>>>
>>>   
>>>> Add a script that will create a target, add a device to
>>>> the target and define the initiator that can connect to it.
>>>>     
>>> Can you move this to usr/scripts? I guess that this could be improved
>>> to handle other protocols later on.
>>>   
>> I will.
>>
>>> There would be a better name since this script does more than
>>> assigning a logical unit, that is, creates a new target, add a lun,
>>> and enable an initiator to access to the target.
>>>   
>> In the (near) future I would like to enhance this script, so it will not
>> always create a new target (e.g. add a device to a target that already
>> exists if the user wants). So, the main thing that this script does is
>> to setup a lun. How about "setup_lun"? Do you have other ideas?
> 
> I see. Then, you can choose whatever you like. I'm not good at
> naming. :)
> 
> Thanks,

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/scripts/setup_lun |  130 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 130 insertions(+), 0 deletions(-)
 create mode 100755 usr/scripts/setup_lun

diff --git a/usr/scripts/setup_lun b/usr/scripts/setup_lun
new file mode 100755
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.7





More information about the stgt mailing list