[Stgt-devel] [PATCH 4/4] iscsi: do not create target if it already exists

Erez Zilber erezz
Mon Jan 7 16:07:28 CET 2008


In tgt-setup-lun, if the user tries to create a target
with a name that already exists, the script should fail.

Signed-off-by: Erez Zilber <erezz at voltaire.com>
---
 usr/scripts/tgt-setup-lun |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/usr/scripts/tgt-setup-lun b/usr/scripts/tgt-setup-lun
index df5418a..824c7d3 100755
--- a/usr/scripts/tgt-setup-lun
+++ b/usr/scripts/tgt-setup-lun
@@ -116,6 +116,19 @@ err_exit()
 	exit 1
 }
 
+check_if_tgt_exists()
+{
+	tgt_list=$(tgtadm --lld iscsi --op show --mode target | grep Target | cut -d" " -f3)
+
+	for curr_tgt in $tgt_list; do
+		if [ $tgt_name = $curr_tgt ]; then
+			return 1
+		fi
+	done
+
+	return 0
+}
+
 while getopts "d:n:" opt
 do
 	case ${opt} in
@@ -144,6 +157,14 @@ fi
 
 tgt_name="iqn.2001-04.com.$(hostname -s)-$tgt_name"
 
+# Make sure that a target with the same name doesn't exist
+check_if_tgt_exists
+if [ $? -eq 1 ]; then
+	echo "Error: a target named $tgt_name already exists"
+	echo "Please select a different target name"
+	exit 1
+fi
+
 find_vacant_tgt_id
 tid=$?
 
-- 
1.5.3.7





More information about the stgt mailing list