[Stgt-devel] [PATCH] changing state of targets via tgt-admin

Tomasz Chmielewski mangoo
Tue Jul 29 12:26:27 CEST 2008


Add a possibility to change the state (offline or ready) of targets.

It is possible to offline/ready all targets, target specified with its name (i.e. iqn.2008-08.com.example:some.target) or tid.

This is an example usage help for the --offline option (--ready works in the same way):

# ./tgt-admin --offline help
"./tgt-admin --offline <value>" - offline all or selected targets.

Example usage:
        --offline help  - display this help
        --offline ALL   - offline all targets
        --offline tid=4 - offline target 4 (target with tid 4)
        --offline iqn.2008-08.com.example:some.target - offline this target


Signed-off-by: Tomasz Chmielewski <mangoo at wpkg.org>

diff --git a/scripts/tgt-admin b/scripts/tgt-admin
index 162cac2..eceea83 100755
--- a/scripts/tgt-admin
+++ b/scripts/tgt-admin
@@ -25,6 +25,10 @@ This tool configures tgt targets.
 
   -e, --execute              read $configfile and execute tgtadm commands
   -d, --delete               delete all the targets
+      --offline <value>      put all or selected targets in offline state
+                             (see "--offline help" for more info)
+      --ready <value>        put all or selected targets in ready state
+                             (see "--ready help" for more info)
   -s, --show                 show all the targets
   -c, --conf <conf file>     specify an alternative configuration file
   -f, --force                don't exit on tgtadm errors
@@ -41,6 +45,8 @@ my %conf;
 my $param = $ARGV[0];
 my $execute = 0;
 my $delete = 0;
+my $offline = 0;
+my $ready = 0;
 my $show = 0;
 my $alternate_conf="0";
 my $force = 0;
@@ -51,6 +57,8 @@ my $help = 0;
 my $result = GetOptions (
 	"e|execute" => \$execute,
 	"d|delete"  => \$delete,
+	"offline=s" => \$offline,
+	"ready=s"   => \$ready,
 	"s|show"    => \$show,
 	"c|conf=s"  => \$alternate_conf,
 	"f|force"   => \$force,
@@ -403,6 +411,50 @@ sub dump_config {
 	}
 }
 
+# Offline or ready targets
+sub ready_offline_targets {
+	my $var = $_[0]; # This variable is either "offline" or "ready"
+	my $off_ready;
+	if ($ready eq 0) {
+		$off_ready = $offline
+	} elsif ($offline eq 0) {
+		$off_ready = $ready
+	} else {
+		print "Invalid value (you can't use both ready and offline)!\n";
+		exit 1
+	}
+	if ($off_ready eq "help") {
+		print <<EOF;
+"$ENV{_} --$var <value>" - $var all or selected targets.
+
+Example usage:
+	--$var help  - display this help
+	--$var ALL   - $var all targets
+	--$var tid=4 - $var target 4 (target with tid 4)
+	--$var iqn.2008-08.com.example:some.target - $var this target
+
+EOF
+	} elsif ($off_ready eq "ALL") {
+		&process_targets;
+		# Run over all targets and offline/ready them
+		my @all_targets = keys %tgtadm_output_tid;
+		foreach my $existing_target (@all_targets) {
+			execute("tgtadm --op update --mode target --tid=$tgtadm_output_tid{$existing_target} -n state -v $var");
+		}
+	} elsif ($off_ready =~ m/tid=(.+)/) {
+		&process_targets;
+		execute("tgtadm --op update --mode target --tid=$1 -n state -v $var");
+	} else {
+		&process_targets;
+		if (length $tgtadm_output_tid{$off_ready}) {
+			execute("tgtadm --op update --mode target --tid=$tgtadm_output_tid{$off_ready} --name=\"$off_ready\" -n state -v $var");
+		} else {
+			print "There is no target with name \"$off_ready\", can't $var it!\n";
+			exit 1;
+		}
+	}
+}
+
 # Some checks
 sub check {
 	if ( not defined $_[0] or not length $_[0] ) {
@@ -439,7 +491,7 @@ sub execute {
 	}
 }
 
-if (($execute == 1) || ($pretend == 1)) {
+if ($execute == 1) {
 	&process_targets;
 	&parse_configs;
 	&add_targets;
@@ -450,6 +502,10 @@ if (($execute == 1) || ($pretend == 1)) {
 	&remove_targets;
 } elsif ($dump == 1) {
 	&dump_config;
+} elsif ($offline ne 0) {
+	&ready_offline_targets("offline");
+} elsif ($ready ne 0) {
+	&ready_offline_targets("ready");
 } else {
 	print "No action specified.\n";
 }




-- 
Tomasz Chmielewski 
http://wpkg.org



More information about the stgt mailing list