From e599674d4d4bac187d164ed192362325edb6a043 Mon Sep 17 00:00:00 2001
From: Hannes Reinecke <hare@suse.de>
Date: Fri, 1 Feb 2013 13:06:00 +0100
Subject: lio_node: Silence output

Add option '--debug' to silence lio_node for the standard case.

Signed-off-by: Hannes Reinecke <hare@suse.de>
---
 lio-py/lio_node.py |   89 ++++++++++++++++++++++++++++++----------------------
 1 file changed, 51 insertions(+), 38 deletions(-)

diff --git a/lio-py/lio_node.py b/lio-py/lio_node.py
index 343f62a..55a9208 100755
--- a/lio-py/lio_node.py
+++ b/lio-py/lio_node.py
@@ -21,12 +21,19 @@ try:
 except ConfigParser.NoSectionError:
     tcm_var = "/var/target"
 
+lio_debug = 0
 alua_secondary_md_dir = tcm_var + "/alua/iSCSI/"
 
 def lio_err(msg):
 	print msg
 	sys.exit(1)
 
+def lio_dbg(msg):
+	global lio_debug
+
+	if lio_debug:
+		print msg
+
 def lio_alua_check_secondary_md(iqn, tpgt):
 	alua_sec_md_path = alua_secondary_md_dir + iqn + "+" + tpgt + "/"
 	if os.path.isdir(alua_sec_md_path) == False:
@@ -99,7 +106,7 @@ def lio_alua_process_secondary_md(option, opt_str, value, parser):
 
 	p = open(alua_sec_md_file, 'rU')
 	if not p:
-		print "Unable to process ALUA secondary metadata for: " + alua_sec_md_file
+		lio_err("Unable to process ALUA secondary metadata for: " + alua_sec_md_file)
 
 	line = p.readline()
 	while line:
@@ -166,7 +173,7 @@ def __lio_target_del_iqn(option, opt_str, value, parser, delete_tpg_md):
 #	print "rmdir_op: " + rmdir_op
 	ret = os.system(rmdir_op)
 	if not ret:
-		print "Successfully released iSCSI Target Endpoint IQN: " + iqn
+		lio_dbg("Successfully released iSCSI Target Endpoint IQN: " + iqn)
 	else:
 		lio_err("Unable to release iSCSI Target Endpoint IQN: " + iqn)
 
@@ -198,7 +205,7 @@ def __lio_target_del_tpg(option, opt_str, value, parser, delete_tpg_md):
 	disable_op = "echo 0 > " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/enable"
 	ret = os.system(disable_op)
 	if ret:
-		print "Unable to disable TPG: " + iqn + " TPGT: " + tpgt
+		lio_err("Unable to disable TPG: " + iqn + " TPGT: " + tpgt)
 
 	np_root = os.listdir(lio_root + "/" + iqn + "/tpgt_" + tpgt + "/np")
 	for np in np_root:
@@ -228,7 +235,7 @@ def __lio_target_del_tpg(option, opt_str, value, parser, delete_tpg_md):
 #	print "rmdir_op: " + rmdir_op
 	ret = os.system(rmdir_op)
 	if not ret:
-		print "Successfully released iSCSI Target Portal Group: " + iqn + " TPGT: " + tpgt
+		lio_dbg("Successfully released iSCSI Target Portal Group: " + iqn + " TPGT: " + tpgt)
 	else:
 		lio_err("Unable to release iSCSI Target Portal Group: " + iqn + " TPGT: " + tpgt)
 	
@@ -280,7 +287,7 @@ def lio_target_add_np(option, opt_str, value, parser):
 #	print "mkdir_op: " + mkdir_op
 	ret = os.system(mkdir_op)
 	if not ret:
-		print "Successfully created network portal: " + np + " created " + iqn + " TPGT: " + tpgt 
+		lio_dbg("Successfully created network portal: " + np + " created " + iqn + " TPGT: " + tpgt)
 		lio_alua_check_secondary_md(iqn, tpgt)
 	else:
 		lio_err("Unable to create network portal: " + np + " created " + iqn + " TPGT: " + tpgt)
@@ -297,7 +304,7 @@ def lio_target_del_np(option, opt_str, value, parser):
 #	print "rmdir_op: " + rmdir_op
 	ret = os.system(rmdir_op)
 	if not ret:
-		print "Successfully released network portal: " + np + " created " + iqn + " TPGT: " + tpgt
+		lio_dbg("Successfully released network portal: " + np + " created " + iqn + " TPGT: " + tpgt)
 	else:
 		lio_err("Unable to release network portal: " + np + " created " + iqn + " TPGT: " + tpgt)
 
@@ -327,7 +334,7 @@ def lio_target_add_port(option, opt_str, value, parser):
 #	print "link_op: " + link_op
 	ret = os.system(link_op)
 	if not ret:
-		print "Successfully created iSCSI Target Logical Unit"
+		lio_dbg("Successfully created iSCSI Target Logical Unit")
 		lio_alua_check_secondary_md(iqn, tpgt)
 		lio_alua_set_secondary_write_md(iqn, tpgt, lun)
 	else:
@@ -350,7 +357,7 @@ def lio_target_add_tpg(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to create iSCSI Target Portal Group ConfigFS directory")
 	else:
-		print "Successfully created iSCSI Target Portal Group"
+		lio_dbg("Successfully created iSCSI Target Portal Group")
 		lio_alua_check_secondary_md(iqn, tpgt)
 
 	return
@@ -387,7 +394,7 @@ def __lio_target_del_port(option, opt_str, value, parser):
 #	print "del_port rmdir_op: " + rmdir_op
 	ret = os.system(rmdir_op);
 	if not ret:
-		print "Successfully deleted iSCSI Target Logical Unit"
+		lio_dbg("Successfully deleted iSCSI Target Logical Unit")
 	else:
 		lio_err("Unable to rmdir iSCSI Target Logical Unit configfs directory")
 
@@ -420,7 +427,7 @@ def lio_target_tpg_disableauth(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to disable iSCSI Authentication on iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully disabled iSCSI Authentication on iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully disabled iSCSI Authentication on iSCSI Target Portal Group: " + iqn + " " + tpgt)
 
 	return
 
@@ -434,7 +441,7 @@ def lio_target_tpg_demomode(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to disable Initiator ACL mode (Enable DemoMode) on iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully disabled Initiator ACL mode (Enabled DemoMode) on iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully disabled Initiator ACL mode (Enabled DemoMode) on iSCSI Target Portal Group: " + iqn + " " + tpgt)
 
 	return
 
@@ -451,7 +458,7 @@ def lio_target_disable_lunwp(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to disable WriteProtect for Mapped LUN: " + mapped_lun + " for " + initiator_iqn + " on iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully disabled WRITE PROTECT for Mapped LUN: " + mapped_lun + " for " + initiator_iqn + " on iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully disabled WRITE PROTECT for Mapped LUN: " + mapped_lun + " for " + initiator_iqn + " on iSCSI Target Portal Group: " + iqn + " " + tpgt)
 
 	return
 
@@ -465,7 +472,7 @@ def lio_target_enable_auth(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to enable iSCSI Authentication on iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully enabled iSCSI Authentication on iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully enabled iSCSI Authentication on iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	return
 
 def lio_target_enable_lunwp(option, opt_str, value, parser):
@@ -481,7 +488,7 @@ def lio_target_enable_lunwp(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to enable WriteProtect for Mapped LUN: " + mapped_lun + " for " + initiator_iqn + " on iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully enabled WRITE PROTECT for Mapped LUN: " + mapped_lun + " for " + initiator_iqn + " on iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully enabled WRITE PROTECT for Mapped LUN: " + mapped_lun + " for " + initiator_iqn + " on iSCSI Target Portal Group: " + iqn + " " + tpgt)
 
 	return
 
@@ -495,7 +502,7 @@ def lio_target_enable_tpg(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to enable iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully enabled iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully enabled iSCSI Target Portal Group: " + iqn + " " + tpgt)
 
 	return
 
@@ -509,7 +516,7 @@ def lio_target_disable_tpg(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to disable iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully disabled iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully disabled iSCSI Target Portal Group: " + iqn + " " + tpgt)
 
 	return
 
@@ -523,7 +530,7 @@ def lio_target_enableaclmode(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to enable Initiator ACL mode (Disabled DemoMode) on iSCSI Target Portal Group: " + iqn + " " + tpgt)
         else:
-                print "Successfully enabled Initiator ACL mode (Disabled DemoMode) on iSCSI Target Portal Group: " + iqn + " " + tpgt
+                lio_dbg("Successfully enabled Initiator ACL mode (Disabled DemoMode) on iSCSI Target Portal Group: " + iqn + " " + tpgt)
         return
 
 
@@ -547,7 +554,7 @@ def lio_target_add_lunacl(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to add iSCSI Initiator Mapped LUN: " + mapped_lun + " ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully added iSCSI Initiator Mapped LUN: " + mapped_lun + " ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully added iSCSI Initiator Mapped LUN: " + mapped_lun + " ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 		lio_alua_check_secondary_md(iqn, tpgt)
 
 	return
@@ -569,7 +576,6 @@ def lio_target_del_lunacl(option, opt_str, value, parser):
 			continue;
 
 		unlink_op = "unlink " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/lun_" + mapped_lun + "/" + lun_acl_link
-#		print "unlink_op: " + unlink_op
 		ret = os.system(unlink_op)
 		if ret:
 			lio_err("Unable to unlink iSCSI Initiator Mapped LUN: " + mapped_lun + " ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
@@ -579,7 +585,7 @@ def lio_target_del_lunacl(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to delete iSCSI Initiator Mapped LUN: " + mapped_lun + " ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully deleted iSCSI Initiator Mapped LUN: " + mapped_lun + " ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully deleted iSCSI Initiator Mapped LUN: " + mapped_lun + " ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 
 	return
 
@@ -595,7 +601,7 @@ def lio_target_add_nodeacl(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to add iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully added iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully added iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 		lio_alua_check_secondary_md(iqn, tpgt)
 
 	return
@@ -621,7 +627,7 @@ def lio_target_del_nodeacl(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to delete iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully deleted iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully deleted iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 
 	return
 
@@ -649,7 +655,7 @@ def lio_target_set_chap_auth(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to set CHAP password for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully set CHAP authentication for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully set CHAP authentication for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 
 	return
 
@@ -677,7 +683,7 @@ def lio_target_set_chap_mutual_auth(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to set mutual CHAP password for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully set mutual CHAP authentication for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt	
+		lio_dbg("Successfully set mutual CHAP authentication for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt	)
 	
 	return
 
@@ -699,7 +705,7 @@ def lio_target_set_chap_discovery_auth(option, opt_str, value, parser):
         if ret:
                 lio_err("Unable to set CHAP password for iSCSI Discovery Authentication")
         else:
-                print "Successfully set CHAP authentication for iSCSI Discovery Authentication"
+                lio_dbg("Successfully set CHAP authentication for iSCSI Discovery Authentication")
 
 	return
 
@@ -721,7 +727,7 @@ def lio_target_set_chap_mutual_discovery_auth(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to set mutual CHAP password for iSCSI Discovery Authentication")
 	else:
-		print "Successfully set mutual CHAP authentication for iSCSI Discovery Authentication"
+		lio_dbg("Successfully set mutual CHAP authentication for iSCSI Discovery Authentication")
 
 	return
 
@@ -738,9 +744,9 @@ def lio_target_set_enforce_discovery_auth(option, opt_str, value, parser):
 		lio_err("Unable to set da_attr: " + da_attr)
 
 	if value == "1":
-		print "Successfully enabled iSCSI Discovery Authentication enforcement"
+		lio_dbg("Successfully enabled iSCSI Discovery Authentication enforcement")
 	else:
-		print "Successfully disabled iSCSI Discovery Authentication enforcement"
+		lio_dbg("Successfully disabled iSCSI Discovery Authentication enforcement")
 
 	return
 
@@ -758,7 +764,7 @@ def lio_target_set_node_tcq(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to set TCQ: " + depth + " for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 	else:
-		print "Successfully set TCQ: " + depth + " for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt
+		lio_dbg("Successfully set TCQ: " + depth + " for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)
 
 	return
 
@@ -778,7 +784,7 @@ def lio_target_alua_set_tgptgp(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to set ALUA Target Port Group: " + tg_pt_gp_name + " for LUN: " + lun + " on " + iqn + " " + tpgt)
 	else:
-		print "Successfully set ALUA Target Port Group: " + tg_pt_gp_name + " for LUN: " + lun + " on " + iqn + " " + tpgt
+		lio_dbg("Successfully set ALUA Target Port Group: " + tg_pt_gp_name + " for LUN: " + lun + " on " + iqn + " " + tpgt)
 
 	return
 
@@ -797,7 +803,7 @@ def lio_target_alua_set_tgpt_offline(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to set ALUA secondary state OFFLINE bit for LUN: " + lun + " on " + iqn + " " + tpgt)
 	else:
-		print "Successfully set ALUA secondary state OFFLINE for LUN: " + lun + " on " + iqn + " " + tpgt
+		lio_dbg("Successfully set ALUA secondary state OFFLINE for LUN: " + lun + " on " + iqn + " " + tpgt)
 
 	return
 
@@ -816,7 +822,7 @@ def lio_target_alua_clear_tgpt_offline(option, opt_str, value, parser):
 	if ret:
 		lio_err("Unable to clear ALUA secondary state OFFLINE for LUN: " + lun + " on " + iqn + " " + tpgt)
 	else:
-		print "Successfully cleared ALUA secondary state OFFLINE for LUN: " + lun + " on " + iqn + " " + tpgt
+		lio_dbg("Successfully cleared ALUA secondary state OFFLINE for LUN: " + lun + " on " + iqn + " " + tpgt)
 	return
 
 def lio_target_show_chap_auth(option, opt_str, value, parser):
@@ -1107,7 +1113,7 @@ def lio_target_set_node_attr(option, opt_str, value, parser):
 		lio_err("Unable to set node attr: " + attr_dir + "/" + attr)
 
 	p.close()
-	print "Successfully set Initiator Node attribute: " + attr + " to: " + val
+	lio_dbg("Successfully set Initiator Node attribute: " + attr + " to: " + val)
 
 	return
 
@@ -1175,7 +1181,7 @@ def lio_target_set_tpg_attr(option, opt_str, value, parser):
 		lio_err("Unable to set tpg attr: " + attr_dir + "/" + attr)
 
 	p.close()
-	print "Successfully set TPG attribute: " + attr + " to: " + val
+	lio_dbg("Successfully set TPG attribute: " + attr + " to: " + val)
 
 	return
 
@@ -1221,7 +1227,7 @@ def lio_target_set_tpg_param(option, opt_str, value, parser):
 		lio_err("Unable to write tpg attr: " + param_dir + "/" + param)
 
 	p.close()
-	print "Successfully set TPG parameter: " + param + " to: " + val
+	lio_dbg("Successfully set TPG parameter: " + param + " to: " + val)
 
 	return
 
@@ -1257,12 +1263,12 @@ def lio_target_unload(option, opt_str, value, parser):
 		rmdir_op = "rmdir " + lio_root
 		ret = os.system(rmdir_op)
 		if ret:
-			print "Unable to remove lio_root: " + lio_root
+			lio_err("Unable to remove lio_root: " + lio_root)
 
 	rmmod_op = "rmmod iscsi_target_mod"
 	ret = os.system(rmmod_op)
 	if ret:
-		print "Unable to unload iscsi_target_mod"
+		lio_err("Unable to unload iscsi_target_mod")
 
 	return
 
@@ -1271,8 +1277,13 @@ def lio_target_version(option, opt_str, value, parser):
 	os.system("cat /sys/kernel/config/target/iscsi/lio_version")
 	return
 
-def main():
+def lio_target_enable_debug(option, opt_str, value, parser):
+	global lio_debug
 
+	lio_debug = 1
+	return
+
+def main():
 	parser = OptionParser()
 	parser.add_option("--addlunacl", action="callback", callback=lio_target_add_lunacl, nargs=5,
 		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN TPG_LUN MAPPED_LUN", help="Add iSCSI Initiator LUN ACL to LIO-Target Portal Group LUN")
@@ -1366,6 +1377,8 @@ def main():
 		type="string", dest="TARGET_IQN TPGT LUN", help="Show ALUA Target Port Group for LIO-Target Port/LUN")
 	parser.add_option("--unload", action="callback", callback=lio_target_unload, nargs=0,
 		help="Unload LIO-Target")
+	parser.add_option("--debug", action="callback", callback=lio_target_enable_debug, nargs=0,
+			help="Enable debugging output")
 	parser.add_option("--version", action="callback", callback=lio_target_version, nargs=0,
 		help="Display LIO-Target version information")
 
-- 
1.7.10.4

