[Stgt-devel] [PATCH 2/7] Add option to delete all the targets

Doron Shoham dorons
Sun Jul 27 15:30:50 CEST 2008


Modify delete function to delete only the active targets
without touching the configuration file.

Signed-off-by: Doron Shoham <dorons at voltaire.com>
---
 scripts/tgt-admin |   25 +++++++++++++++++++------
 1 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/scripts/tgt-admin b/scripts/tgt-admin
index 76fab79..9083417 100755
--- a/scripts/tgt-admin
+++ b/scripts/tgt-admin
@@ -328,10 +328,23 @@ if (($execute == 1) || ($pretend == 1)) {
 # Delete all the targets and backup the current conf file
 sub delete {
 	print "deleting targets...\n";
-	print "a copy of the conf file is on:/etc/tgt/targets.conf.bkp\n";
-	`mv $configfile /etc/tgt/targets.conf.bkp`;
-	open FILE, ">", "$configfile" or die $!;
-	print FILE "<target>\n";
-	print FILE "</target>\n";
-	close FILE;
+	# We need to run as root
+	if ( $> ) {
+		die("You must be root to run this program.\n");
+	}	
+	
+	my @show_target = `tgtadm --lld iscsi --op show --mode target`;
+	my @tids=();
+	
+	# Find all the active targets' tids
+	foreach my $show_target_line (@show_target) {
+		if ( $show_target_line =~ m/^Target (\d*): (.+)/ ) {
+			push(@tids,$1);
+		}
+	}
+        # Run over all the active targets and delete them
+        foreach my $tid (@tids) {
+		execute("tgtadm --op update --mode target --tid=$tid -n state -v offline");
+		execute("tgtadm --mode target --op delete --tid=$tid");
+	}
 }
-- 
1.5.2






More information about the stgt mailing list