From 571271e4c40a67eb6c4e9ba63723868179dec8ab Mon Sep 17 00:00:00 2001
From: Hannes Reinecke <hare@suse.de>
Date: Thu, 4 Oct 2012 14:07:12 +0200
Subject: Configuration file parsing

Some installations might have different installation paths, so
provide a configuration file to modify these settings.

Signed-off-by: Hannes Reinecke <hare@suse.de>
---
 lio-py/lio_node.py     |   20 +++++++++++++++++---
 scripts/rc.target.suse |    4 ++++
 tcm-py/setup.py        |    3 ++-
 tcm-py/tcm.cfg         |    4 ++++
 tcm-py/tcm_fabric.py   |   17 ++++++++++++++---
 tcm-py/tcm_node.py     |   45 ++++++++++++++++++++++++++++++++-------------
 6 files changed, 73 insertions(+), 20 deletions(-)
 create mode 100644 tcm-py/tcm.cfg

Index: lio-utils-4.0/lio-py/lio_node.py
===================================================================
--- lio-utils-4.0.orig/lio-py/lio_node.py
+++ lio-utils-4.0/lio-py/lio_node.py
@@ -3,11 +3,25 @@ import os, sys
 import subprocess as sub
 import string
 import re
+import ConfigParser
 from optparse import OptionParser
 
-tcm_root = "/sys/kernel/config/target/core"
-lio_root = "/sys/kernel/config/target/iscsi"
-alua_secondary_md_dir = "/var/target/alua/iSCSI/"
+config = ConfigParser.ConfigParser()
+config.read('/etc/target/tcm.cfg')
+try:
+    tcm_cfg = config.get('tcm', 'tcm_root')
+except ConfigParser.NoSectionError:
+    tcm_cfg = "/sys/kernel/config/target"
+
+tcm_root = tcm_cfg + "/core"
+lio_root = tcm_cfg + "/iscsi"
+
+try:
+    tcm_var = config.get('tcm', 'tcm_var')
+except ConfigParser.NoSectionError:
+    tcm_var = "/var/target"
+
+alua_secondary_md_dir = tcm_var + "/alua/iSCSI/"
 
 def lio_err(msg):
 	print msg
Index: lio-utils-4.0/scripts/rc.target.suse
===================================================================
--- lio-utils-4.0.orig/scripts/rc.target.suse
+++ lio-utils-4.0/scripts/rc.target.suse
@@ -113,6 +113,10 @@ function run_fabric_cfs_ops() {
 		if [[ $i =~ ~$ ]]; then
 			continue
 		fi
+		# Skip configuration file
+		if [ $i == "tcm.cfg" ] ; then
+			continue
+		fi
 
 		echo -n $"Calling ConfigFS script $CONFIGFS_SCRIPT_PATH: "
 		sh ${CONFIGFS_SCRIPT_PATH} > /dev/null 2>&1
Index: lio-utils-4.0/tcm-py/setup.py
===================================================================
--- lio-utils-4.0.orig/tcm-py/setup.py
+++ lio-utils-4.0/tcm-py/setup.py
@@ -12,6 +12,7 @@ setup (name='tcm',
       author_email='nab@linux-iscsi.org',
       url='http://linux-iscsi.org',
       license='GPL',
-      py_modules=['tcm_dump', 'tcm_fileio','tcm_iblock','tcm_node','tcm_pscsi','tcm_ramdisk','tcm_loop','tcm_fabric']
+      py_modules=['tcm_dump', 'tcm_fileio','tcm_iblock','tcm_node','tcm_pscsi','tcm_ramdisk','tcm_loop','tcm_fabric'],
+      data_files=[('/etc/target',['tcm.cfg'])]
 )
 
Index: lio-utils-4.0/tcm-py/tcm.cfg
===================================================================
--- /dev/null
+++ lio-utils-4.0/tcm-py/tcm.cfg
@@ -0,0 +1,4 @@
+# TCM configuration
+[tcm]
+tcm_root: /sys/kernel/config/target
+tcm_var: /var/lib/target
Index: lio-utils-4.0/tcm-py/tcm_fabric.py
===================================================================
--- lio-utils-4.0.orig/tcm-py/tcm_fabric.py
+++ lio-utils-4.0/tcm-py/tcm_fabric.py
@@ -5,9 +5,20 @@ import string
 import re
 import datetime, time
 import optparse
+import ConfigParser
 
-target_root = "/sys/kernel/config/target/"
-spec_root = "/var/target/fabric/"
+config = ConfigParser.ConfigParser()
+config.read('/etc/target/tcm.cfg')
+try:
+	tcm_root = config.get('tcm', 'tcm_root')
+except ConfigParser.NoSectionError:
+	tcm_root = "/sys/kernel/config/target"
+target_root = tcm_root + "/"
+
+try:
+	spec_root = config.get('tcm', 'tcm_var') + "/"
+except ConfigParser.NoSectionError:
+	spec_root = "/var/target/fabric/"
 
 def fabric_configfs_dump(fabric_name, fabric_root, module_name):
 
Index: lio-utils-4.0/tcm-py/tcm_node.py
===================================================================
--- lio-utils-4.0.orig/tcm-py/tcm_node.py
+++ lio-utils-4.0/tcm-py/tcm_node.py
@@ -4,6 +4,7 @@ import subprocess as sub
 import string
 import re
 import errno
+import ConfigParser
 from optparse import OptionParser
 
 import tcm_pscsi
@@ -11,7 +12,17 @@ import tcm_iblock
 import tcm_ramdisk
 import tcm_fileio
 
-tcm_root = "/sys/kernel/config/target/core"
+config = ConfigParser.ConfigParser()
+config.read('/etc/target/tcm.cfg')
+try:
+    tcm_cfg = config.get('tcm', 'tcm_root')
+except ConfigParser.NoSectionError:
+    tcm_cfg = "/sys/kernel/config/target"
+try:
+    tcm_var = config.get('tcm', 'tcm_var')
+except ConfigParser.NoSectionError:
+    tcm_var = "/var/target"
+tcm_root = tcm_cfg + "/core"
 
 def tcm_cat(fname):
     return file(fname).xreadlines()
@@ -21,7 +32,7 @@ def tcm_err(msg):
 	sys.exit(1)
 
 def tcm_get_cfs_prefix(arg):
-	path = "/sys/kernel/config/target/core/" + arg
+	path = tcm_root + "/" + arg
 	return path
 
 def tcm_add_alua_lugp(option, opt_str, value, parser):
@@ -78,7 +89,7 @@ def tcm_add_alua_tgptgp(option, opt_str,
 def tcm_alua_check_metadata_dir(cfs_dev_path):
 
 	unit_serial = tcm_get_unit_serial(cfs_dev_path)
-	alua_path = "/var/target/alua/tpgs_" + unit_serial + "/"
+	alua_path = tcm_var + "/alua/tpgs_" + unit_serial + "/"
 	if os.path.isdir(alua_path) == True:
 		return
 
@@ -93,7 +104,7 @@ def tcm_alua_check_metadata_dir(cfs_dev_
 
 def tcm_alua_delete_metadata_dir(unit_serial):
 
-	alua_path = "/var/target/alua/tpgs_" + unit_serial + "/"
+	alua_path = tcm_var + "/alua/tpgs_" + unit_serial + "/"
 	if os.path.isdir(alua_path) == False:
 		return;
 
@@ -122,7 +133,7 @@ def tcm_alua_process_metadata(cfs_dev_pa
 	alua_cfs_path = cfs_dev_path + "/alua/" + tg_pt_gp_name
 	
 	unit_serial = tcm_get_unit_serial(cfs_dev_path)
-	alua_path = "/var/target/alua/tpgs_" + unit_serial + "/" + tg_pt_gp_name
+	alua_path = tcm_var + "/alua/tpgs_" + unit_serial + "/" + tg_pt_gp_name
 #	print "Using tg_pt_gp alua_path " + alua_path
 
 	if os.path.isfile(alua_path) == False:
@@ -175,8 +186,9 @@ def tcm_alua_process_metadata(cfs_dev_pa
 
 		line = p.readline()
 
-	# Now allow changes to ALUA target port group update the struct file metadata
-	# in /var/target/alua/tpgs_$T10_UNIT_SERIAL/$TG_PT_GP_NAME
+	# Now allow changes to ALUA target port group update
+        # the struct file metadata in
+	# $TCM_VAR/alua/tpgs_$T10_UNIT_SERIAL/$TG_PT_GP_NAME
 	tcm_alua_set_write_metadata(alua_cfs_path)
 	p.close()
 	return	
@@ -282,7 +294,7 @@ def tcm_del_alua_tgptgp(option, opt_str,
 
 	unit_serial = tcm_get_unit_serial(cfs_dev_path)
 	tg_pt_gp_name = str(value[1])
-	alua_md_path = "/var/target/alua/tpgs_" + unit_serial + "/" + tg_pt_gp_name
+	alua_md_path = tcm_var + "/alua/tpgs_" + unit_serial + "/" + tg_pt_gp_name
 
 	__tcm_del_alua_tgptgp(option, opt_str, value, parser)
 
@@ -414,7 +426,7 @@ def tcm_show_aptpl_metadata(option, opt_
 		tcm_err("TCM/ConfigFS storage object does not exist: " + cfs_dev_path)
 
 	unit_serial = tcm_get_unit_serial(cfs_dev_path)
-	aptpl_file = "/var/target/pr/aptpl_" + unit_serial
+	aptpl_file = tcm_var + "/pr/aptpl_" + unit_serial
 	if (os.path.isfile(aptpl_file) == False):
 		tcm_err("Unable to dump PR APTPL metadata file: " + aptpl_file);
 	
@@ -423,7 +435,7 @@ def tcm_show_aptpl_metadata(option, opt_
 
 def tcm_delete_aptpl_metadata(unit_serial):
 
-	aptpl_file = "/var/target/pr/aptpl_" + unit_serial
+	aptpl_file = tcm_var + "/pr/aptpl_" + unit_serial
 	if (os.path.isfile(aptpl_file) == False):
 		return
 
@@ -445,7 +457,7 @@ def tcm_process_aptpl_metadata(option, o
 		tcm_err("TCM/ConfigFS storage object does not exist: " + cfs_dev_path)
 
 	unit_serial = tcm_get_unit_serial(cfs_dev_path)
-	aptpl_file = "/var/target/pr/aptpl_" + unit_serial
+	aptpl_file = tcm_var + "/pr/aptpl_" + unit_serial
 	if (os.path.isfile(aptpl_file) == False):
 		return
 
@@ -1078,10 +1090,15 @@ def tcm_unload(option, opt_str, value, p
 	return
 
 def tcm_version(option, opt_str, value, parser):
-
-	os.system("cat /sys/kernel/config/target/version")
+ 
+	os.system("cat " + tcm_cfg + "/version")
 	return
 
+def tcm_config(option, opt_str, value, parser):
+        print("tcm_root: " + tcm_cfg)
+        print("tcm_var: " + tcm_var)
+        return
+
 def main():
 
 	parser = OptionParser()
@@ -1158,6 +1175,8 @@ def main():
 			help="Unload target_core_mod")
 	parser.add_option("--version", action="callback", callback=tcm_version, nargs=0,
 			help="Display target_core_mod version information")
+	parser.add_option("--config", action="callback", callback=tcm_config, nargs=0,
+			help="Display target_core_mod configuration paths")
 	parser.add_option("--wwn", action="callback", callback=tcm_show_wwn_info, nargs=1,
 			type="string", dest="HBA/DEV", help="Show WWN info")
 
