File: lio_node.py

package info (click to toggle)
lio-utils 3.1%2Bgit2.fd0b34fd-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 992 kB
  • sloc: ansic: 7,031; python: 3,039; perl: 885; sh: 414; makefile: 118
file content (1350 lines) | stat: -rwxr-xr-x 49,019 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
#!/usr/bin/python
import os, sys
import subprocess as sub
import string
import re
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/"

def lio_err(msg):
	print msg
	sys.exit(1)

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:
		mkdir_op = "mkdir -p " + alua_sec_md_path	
		ret = os.system(mkdir_op)
		if ret:
			lio_err("Unable to create secondary ALUA MD directory: " + alua_sec_md_path)

	return

def lio_alua_delete_secondary_md(iqn, tpgt):
	alua_sec_md_path = alua_secondary_md_dir + iqn + "+" + tpgt + "/" 
	if os.path.isdir(alua_sec_md_path) == False:
		return

	rm_op = "rm -rf " + alua_sec_md_path
	ret = os.system(rm_op)
	if ret:
		lio_err("Unable to remove secondary ALUA MD directory: " + alua_sec_md_path)

	return

def lio_alua_delete_secondary_md_port(iqn, tpgt, lun):

	alua_sec_md_file = alua_secondary_md_dir + iqn + "+" + tpgt + "/lun_" + lun
	if os.path.isfile(alua_sec_md_file) == False:
		return

	rm_op = "rm -rf "+ alua_sec_md_file
	ret = os.system(rm_op)
	if ret:
		lio_err("Unable to delete ALUA secondary metadata file: " + alua_sec_md_file)

	return

def lio_alua_set_secondary_write_md(iqn, tpgt, lun):
	alua_write_md_file = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun + "/alua_tg_pt_write_md"
	if os.path.isfile(alua_write_md_file) == False:
		return

	p = open(alua_write_md_file, 'w')
	if not p:
		lio_err("Unable to open: " + alua_write_md_file)

	ret = p.write("1")
	if ret:
		lio_err("Unable to enable writeable ALUA secondary metadata for " + alua_write_md_file)

	p.close()
	return

def lio_alua_process_secondary_md(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	lun = str(value[2]);

	alua_sec_md_file = alua_secondary_md_dir + iqn + "+" + tpgt + "/lun_" + lun
	if os.path.isfile(alua_sec_md_file) == False:
		# Use --aluasecmd as a chance to make sure the directory for this
		# LIO-Target endpoint (iqn+tpgt) exists..
		lio_alua_check_secondary_md(iqn, tpgt)
		lio_alua_set_secondary_write_md(iqn, tpgt, lun)
		lio_err("Unable to locate ALUA secondary metadata file: " + alua_sec_md_file)
		return

#	print "Using alua_sec_md_file: " + alua_sec_md_file
	alua_sec_cfs_path = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun
#	print "Using alua_sec_cfs_path: " + alua_sec_cfs_path

	p = open(alua_sec_md_file, 'rU')
	if not p:
		print "Unable to process ALUA secondary metadata for: " + alua_sec_md_file

	line = p.readline()
	while line:
		buf = line.rstrip()

		if re.search('alua_tg_pt_offline=', buf):
			alua_tg_pt_offline = buf[19:]
#			print "Extracted alua_tg_pt_offline: " + alua_tg_pt_offline
			cfs = open(alua_sec_cfs_path + "/alua_tg_pt_offline", 'w')
			if not cfs:
				p.close()
				lio_err("Unable to open " + alua_sec_cfs_path + "/alua_tg_pt_offline")

			ret = cfs.write(alua_tg_pt_offline)
			cfs.close()
			if ret:
				p.close()
				lio_err("Unable to write " + alua_sec_cfs_path + "/alua_tg_pt_offline")

		elif re.search('alua_tg_pt_status=', buf):
			alua_tg_pt_status = buf[18:]
#			print "Extracted alua_tg_pt_status: " + alua_tg_pt_status
			cfs = open(alua_sec_cfs_path + "/alua_tg_pt_status", 'w')
			if not cfs:
				p.close()
				lio_err("Unable to open " + alua_sec_cfs_path + "/alua_tg_pt_status")
			
			ret = cfs.write(alua_tg_pt_status)
			cfs.close()
			if ret:
				p.close()
				lio_err("Unable to write " + alua_sec_cfs_path + "/alua_tg_pt_status")

		line = p.readline()

	p.close()
	# Now enable the alua_tg_pt_write_md bit to allow for new updates
	# to ALUA secondary metadata in struct file for this port
	lio_alua_set_secondary_write_md(iqn, tpgt, lun)
	return

def __lio_target_del_iqn(option, opt_str, value, parser, delete_tpg_md):
	iqn = str(value);
	iqn = iqn.lower();

# Loop through LIO-Target IQN+TPGT list
	tpg_root = os.listdir(lio_root + "/" + iqn); 
	for tpgt_tmp in tpg_root:
		if tpgt_tmp == "fabric_statistics":
			continue

		tpgt_tmp2 = tpgt_tmp.split('_')
		tpgt = tpgt_tmp2[1]

		tpg_val = [iqn,tpgt]
		if delete_tpg_md == 1:
			lio_target_del_tpg(None, None, tpg_val, None)   
		else:
			__lio_target_del_tpg(None, None, tpg_val, None, 0)

	rmdir_op = "rmdir " + lio_root + "/" + iqn
#	print "rmdir_op: " + rmdir_op
	ret = os.system(rmdir_op)
	if not ret:
		print "Successfully released iSCSI Target Endpoint IQN: " + iqn
	else:
		lio_err("Unable to release iSCSI Target Endpoint IQN: " + iqn)

	return

def lio_target_del_iqn(option, opt_str, value, parser):
	iqn = str(value);
	iqn = iqn.lower();

	iqn_dir = lio_root + "/" + iqn
	if os.path.isdir(iqn_dir) == False:
		lio_err("Unable to locate iSCSI Target IQN: " + iqn_dir)

	# Passing 1 for delete_tpg_md here means lio_target_del_tpg()
	# with lio_alua_delete_secondary_md() will get called to delete
	# all of the secondary ALUA directories for the LIO-Target endpoint
	# when an explict --deliqn is called.
	__lio_target_del_iqn(option, opt_str, value, parser, 1)

	return

def __lio_target_del_tpg(option, opt_str, value, parser, delete_tpg_md):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	# This will set TPG Status to INACTIVE force all of the iSCSI sessions for this
	# tiqn+tpgt tuple to be released.
	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

	np_root = os.listdir(lio_root + "/" + iqn + "/tpgt_" + tpgt + "/np")
	for np in np_root:
		np_val = [iqn,tpgt,np]

		lio_target_del_np(None, None, np_val, None)

	nacl_root = os.listdir(lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls")
	for nacl in nacl_root:
		nacl_val = [iqn,tpgt,nacl]

		lio_target_del_nodeacl(None, None, nacl_val, None)

	lun_root = os.listdir(lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun")
	for lun_tmp in lun_root:
		lun_tmp2 = lun_tmp.split('_')
		lun = lun_tmp2[1]

		lun_val = [iqn,tpgt,lun]
		if delete_tpg_md == 1:
			lio_target_del_port(None, None, lun_val, None)
		else:
			__lio_target_del_port(None, None, lun_val, None)


	rmdir_op = "rmdir " + lio_root + "/" + iqn + "/tpgt_" + tpgt
#	print "rmdir_op: " + rmdir_op
	ret = os.system(rmdir_op)
	if not ret:
		print "Successfully released iSCSI Target Portal Group: " + iqn + " TPGT: " + tpgt
	else:
		lio_err("Unable to release iSCSI Target Portal Group: " + iqn + " TPGT: " + tpgt)
	
	return

def lio_target_del_tpg(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	tpgt_file = lio_root + "/" + iqn + "/tpgt_" + tpgt
	if os.path.isdir(tpgt_file) == False:
		lio_err("iSCSI Target Port Group: " + tpgt_file + " does not exist")
	
	__lio_target_del_tpg(option, opt_str, value, parser, 1)
	# Delete the ALUA secondary metadata directory on explict --deltpg or
	# called from --deliqn
	lio_alua_delete_secondary_md(iqn, tpgt)
	return

def lio_target_add_np(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	np = str(value[2]);

	# Append default iSCSI Port is non is given.
	if re.search(']', np)and not re.search(']:', np):
		np = np + ":3260"
	elif re.search(':\\Z', np):
		np = np + "3260"
	elif not re.search(':', np):
		np = np + ":3260"

	# Extract the iSCSI port and make sure it is a valid u16 value
	if re.search(']:', np):
		off = np.index(']:')
		off += 2 
		port = int(np[off:])
	else:
		off = np.index(':')
		off += 1
		port = int(np[off:])

	if port == 0 or port > 65535:
		lio_err("Illegal port value: " + str(port) + " for iSCSI network portal")

	mkdir_op = "mkdir -p " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/np/" + np
#	print "mkdir_op: " + mkdir_op
	ret = os.system(mkdir_op)
	if not ret:
		print "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)

	return

def lio_target_del_np(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	np = str(value[2]);

	rmdir_op = "rmdir " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/np/" + np
#	print "rmdir_op: " + rmdir_op
	ret = os.system(rmdir_op)
	if not ret:
		print "Successfully released network portal: " + np + " created " + iqn + " TPGT: " + tpgt
	else:
		lio_err("Unable to release network portal: " + np + " created " + iqn + " TPGT: " + tpgt)

	return

def lio_target_add_port(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	lun = str(value[2]);
	port_name = str(value[3]);
	tcm_obj = str(value[4]);

	lun_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun
	if os.path.isdir(lun_dir):
		lio_err("iSCSI Target Logical Unit ConfigFS directory already exists")

	mkdir_op = "mkdir -p " + lun_dir
#	print "mkdir_op: " + mkdir_op
	ret = os.system(mkdir_op)
	if ret:
		lio_err("Unable to create iSCSI Target Logical Unit ConfigFS directory")

	port_src = tcm_root + "/" + tcm_obj
	port_dst = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun + "/" + port_name
	link_op = "ln -s " + port_src + " " + port_dst
#	print "link_op: " + link_op
	ret = os.system(link_op)
	if not ret:
		print "Successfully created iSCSI Target Logical Unit"
		lio_alua_check_secondary_md(iqn, tpgt)
		lio_alua_set_secondary_write_md(iqn, tpgt, lun)
	else:
		os.rmdir(lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun)
		lio_err("Unable to create iSCSI Target Logical Unit symlink")

	return

def lio_target_add_tpg(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	tpg_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt
	if os.path.isdir(tpg_dir):
		lio_err("iSCSI Target Portal Group directory already exists")

	mkdir_op = "mkdir -p " + tpg_dir
	ret = os.system(mkdir_op)
	if ret:
		lio_err("Unable to create iSCSI Target Portal Group ConfigFS directory")
	else:
		print "Successfully created iSCSI Target Portal Group"
		lio_alua_check_secondary_md(iqn, tpgt)

	return

def __lio_target_del_port(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	lun = str(value[2]);

	lun_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun
	port_root = os.listdir(lun_dir)

	for port in port_root:
		if port == "alua_tg_pt_gp":
			continue
		if port == "alua_tg_pt_offline":
			continue
		if port == "alua_tg_pt_status":
			continue
		if port == "alua_tg_pt_write_md":
			continue

		if not os.path.islink(lun_dir + "/" + port):
			continue

		unlink_op = lun_dir + "/" + port
#		print "del_portunlink_op: " + unlink_op
		ret = os.unlink(unlink_op)
		if ret:
			lio_err("Unable to unlink iSCSI Target Logical Unit")

	rmdir_op= "rmdir " + lun_dir
#	print "del_port rmdir_op: " + rmdir_op
	ret = os.system(rmdir_op);
	if not ret:
		print "Successfully deleted iSCSI Target Logical Unit"
	else:
		lio_err("Unable to rmdir iSCSI Target Logical Unit configfs directory")

	return

def lio_target_del_port(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	lun = str(value[2]);

	lun_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun
	if os.path.isdir(lun_dir) == False:
		lio_err("LIO-Target Port/LUN directory: " + lun_dir + " does not exist")

	__lio_target_del_port(option, opt_str, value, parser)
	# Delete the ALUA secondary metadata file for this Port/LUN
	# during an explict --dellun
	lio_alua_delete_secondary_md_port(iqn, tpgt, lun)

	return

def lio_target_tpg_disableauth(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	
	enable_op = "echo 0 > " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/attrib/authentication"
	ret = os.system(enable_op)
	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

	return

def lio_target_tpg_demomode(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	enable_op = "echo 1 > " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/attrib/generate_node_acls"
	ret = os.system(enable_op)
	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

	return

def lio_target_disable_lunwp(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();
	mapped_lun = str(value[3]);

	disable_op = "echo 0 > " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/lun_" + mapped_lun + "/write_protect"
	ret = os.system(disable_op)
	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

	return

def lio_target_enable_auth(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	enable_op = "echo 1 > " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/attrib/authentication"
	ret = os.system(enable_op)
	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
	return

def lio_target_enable_lunwp(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();
	mapped_lun = str(value[3]);

	enable_op = "echo 1 > " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/lun_" + mapped_lun + "/write_protect"
	ret = os.system(enable_op)
	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

	return

def lio_target_enable_tpg(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	enable_op = "echo 1 > " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/enable"
	ret = os.system(enable_op)
	if ret:
		lio_err("Unable to enable iSCSI Target Portal Group: " + iqn + " " + tpgt)
	else:
		print "Successfully enabled iSCSI Target Portal Group: " + iqn + " " + tpgt

	return

def lio_target_disable_tpg(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	disable_op = "echo 0 > " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/enable"
	ret = os.system(disable_op)
	if ret:
		lio_err("Unable to disable iSCSI Target Portal Group: " + iqn + " " + tpgt)
	else:
		print "Successfully disabled iSCSI Target Portal Group: " + iqn + " " + tpgt

	return

def lio_target_enableaclmode(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	enable_op = "echo 0 > " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/attrib/generate_node_acls"
	ret = os.system(enable_op)
	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
        return


def lio_target_add_lunacl(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();
	tpg_lun = str(value[3]);
	mapped_lun = str(value[4]);

	mkdir_op = "mkdir -p " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/lun_" + mapped_lun
	ret = os.system(mkdir_op)
	if ret:
		lio_err("Unable to add iSCSI Initiator Mapped LUN: " + mapped_lun + " ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)

	addlunacl_op = "ln -s " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + tpg_lun + " " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/lun_" + mapped_lun + "/lio_lun"

	ret = os.system(addlunacl_op)
	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_alua_check_secondary_md(iqn, tpgt)

	return

def lio_target_del_lunacl(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();
	mapped_lun = str(value[3]);

	lun_link_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/lun_" + mapped_lun
	for lun_acl_link in os.listdir(lun_link_dir):
		if lun_acl_link == "write_protect":
			continue

		if not os.path.islink(lun_link_dir + "/" + lun_acl_link):
			continue;

		unlink_op = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/lun_" + mapped_lun + "/" + lun_acl_link
#		print "unlink_op: " + unlink_op
		ret = os.unlink(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)
		
	dellunacl_op = "rmdir " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/lun_" + mapped_lun
	ret = os.system(dellunacl_op)
	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

	return

def lio_target_add_nodeacl(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();
	
	addnodeacl_op = "mkdir -p " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn
	ret = os.system(addnodeacl_op)
	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_alua_check_secondary_md(iqn, tpgt)

	return

def lio_target_del_nodeacl(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();

	nacl_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn
	lun_acl_root = os.listdir(nacl_dir)
	for lun_acl in lun_acl_root:
		ret = re.search('lun_', lun_acl)
		if not ret:
			continue
		lun_delacl_val = [iqn,tpgt,initiator_iqn,lun_acl[4:]]
		lio_target_del_lunacl(None, None, lun_delacl_val, None)

	delnodeacl_op = "rmdir " + nacl_dir
	ret = os.system(delnodeacl_op)
	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

	return

def lio_target_set_chap_auth(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();
	user = str(value[3]);
	password = str(value[4]);

	auth_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/auth/"

	if not os.path.isdir(auth_dir):
		lio_err("iSCSI Initiator ACL " + initiator_iqn + " does not exist for iSCSI Target Portal Group: " + iqn + " " + tpgt)

	setuser_op = "echo -n " + user + " > " + auth_dir + "/userid"
	ret = os.system(setuser_op)	
	if ret:
		lio_err("Unable to set CHAP username for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)

	setpassword_op = "echo -n " + password + " > " + auth_dir + "/password"
	ret = os.system(setpassword_op)
	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

	return

def lio_target_set_chap_mutual_auth(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();
	user_mutual = str(value[3]);
	password_mutual = str(value[4]);

	auth_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/auth/"

	if not os.path.isdir(auth_dir):
		lio_err("iSCSI Initiator ACL " + initiator_iqn + " does not exist for iSCSI Target Portal Group: " + iqn + " " + tpgt)

	setuser_op = "echo -n " + user_mutual + " > " + auth_dir + "/userid_mutual"
	ret = os.system(setuser_op)
	if ret:
		lio_err("Unable to set mutual CHAP username for iSCSI Initaitor ACL " + initiator_iqn + " for iSCSI Target Portal Group: " + iqn + " " + tpgt)

	setpassword_op = "echo -n " + password_mutual + " > " + auth_dir + "/password_mutual"
	ret = os.system(setpassword_op)
	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	
	
	return

def lio_target_set_chap_discovery_auth(option, opt_str, value, parser):
	user = str(value[0]);
	password = str(value[1]);

        auth_dir = lio_root + "/discovery_auth"
        if not os.path.isdir(auth_dir):
                lio_err("iSCSI Discovery Authentication directory " + auth_dir + " does not exist")

        setuser_op = "echo -n " + user + " > " + auth_dir + "/userid"
        ret = os.system(setuser_op)
        if ret:
		lio_err("Unable to set CHAP username for iSCSI  Discovery Authentication")

        setpassword_op = "echo -n " + password + " > " + auth_dir + "/password"
        ret = os.system(setpassword_op)
        if ret:
                lio_err("Unable to set CHAP password for iSCSI Discovery Authentication")
        else:
                print "Successfully set CHAP authentication for iSCSI Discovery Authentication"

	return

def lio_target_set_chap_mutual_discovery_auth(option, opt_str, value, parser):
	user_mutual = str(value[0]);
	password_mutual = str(value[1]);

	auth_dir = lio_root + "/discovery_auth"
	if not os.path.isdir(auth_dir):
		lio_err("iSCSI Discovery Authentication directory " + auth_dir + " does not exist")

	setuser_op = "echo -n " + user_mutual + " > " + auth_dir + "/userid_mutual"
	ret = os.system(setuser_op)
	if ret:
		lio_err("Unable to set mutual CHAP username for iSCSI Discovery Authentication")

	setpassword_op = "echo -n " + password_mutual + " > " + auth_dir + "/password_mutual"
	ret = os.system(setpassword_op)
	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"

	return

def lio_target_set_enforce_discovery_auth(option, opt_str, value, parser):
	value = str(value);
	
	da_attr = lio_root + "/discovery_auth/enforce_discovery_auth"
	if not os.path.isfile(da_attr):
		lio_err("iSCSI Discovery Authentication directory does not exist")
	
	da_op = "echo " + value + " > " + da_attr;
	ret = os.system(da_op)
	if ret:
		lio_err("Unable to set da_attr: " + da_attr)

	if value == "1":
		print "Successfully enabled iSCSI Discovery Authentication enforcement"
	else:
		print "Successfully disabled iSCSI Discovery Authentication enforcement"

	return


def lio_target_set_node_tcq(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();
	depth = str(value[3]);

	setnodetcq_op = "echo " + depth + " > " + lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/cmdsn_depth"
	ret = os.system(setnodetcq_op)
	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

	return

def lio_target_alua_set_tgptgp(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	lun = str(value[2]);
	tg_pt_gp_name = str(value[3])

	lun_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun
	if not os.path.isdir(lun_dir):
		lio_err("LIO-Target Port/LUN: " + lun + " does not exist on: " + iqn + " " + tpgt)

	set_tp_pt_gp_op = "echo " + tg_pt_gp_name + " > " + lun_dir + "/alua_tg_pt_gp"
	ret = os.system(set_tp_pt_gp_op)
	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

	return

def lio_target_alua_set_tgpt_offline(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	lun = str(value[2]);

	lun_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun
	if not os.path.isdir(lun_dir):
		lio_err("LIO-Target Port/LUN: " + lun + " does not exist on: " + iqn + " " + tpgt)

	set_tg_pt_gp_offline_op = "echo 1 > " + lun_dir + "/alua_tg_pt_offline"
	ret = os.system(set_tg_pt_gp_offline_op)
	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

	return

def lio_target_alua_clear_tgpt_offline(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	lun = str(value[2]);

	lun_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun
	if not os.path.isdir(lun_dir):
		lio_err("LIO-Target Port/LUN: " + lun + " does not exist on: " + iqn + " " + tpgt)

	set_tg_pt_gp_offline_op = "echo 0 > " + lun_dir + "/alua_tg_pt_offline"
	ret = os.system(set_tg_pt_gp_offline_op)
	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
	return

def lio_target_show_chap_auth(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();

	auth_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/auth/"

	if not os.path.isdir(auth_dir):
		lio_err("iSCSI Initiator ACL " + initiator_iqn + " does not exist for iSCSI Target Portal Group: " + iqn + " " + tpgt)

	for auth in os.listdir(auth_dir):
		p = os.open(auth_dir + "/" + auth, 0)
		value = os.read(p, 256)
		print auth + ": " + value.rstrip()
		os.close(p)	

	return

def lio_target_show_chap_discovery_auth(option, opt_str, value, parser):

	auth_dir = lio_root + "/discovery_auth"
	if not os.path.isdir(auth_dir):
		lio_err("iSCSI Discovery Authentication directory " + auth_dir + " does not exist")

	for auth in os.listdir(auth_dir):
		p = os.open(auth_dir + "/" + auth, 0)
		value = os.read(p, 256)
		print auth + ": " + value.rstrip()
		os.close(p)

	return

def lio_target_show_node_tcq(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2]);
	initiator_iqn = initiator_iqn.lower();

	nacl = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn
	if not os.path.isdir(nacl):
		lio_err("iSCSI Initiator ACL: " + initiator_iqn + " does not exist for iSCSI Target Portal Group: " + iqn + " " + tpgt)

	tcq_depth_file = nacl + "/cmdsn_depth"
	p = os.open(tcq_depth_file, 0)
	value = os.read(p, 8)
	print value.rstrip()
	os.close(p)

	return

def lio_target_alua_show_tgptgp(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	lun = str(value[2]);

	lun_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/lun/lun_" + lun
	if not os.path.isdir(lun_dir):
		lio_err("LIO-Target Port/LUN: " + lun + " does not exist on: " + iqn + " " + tpgt)

	show_tp_pt_gp_op = "cat " + lun_dir + "/alua_tg_pt_gp"
	ret = os.system(show_tp_pt_gp_op)
	if ret:
		lio_err("Unable to show ALUA Target Port Group: " + tg_pt_gp_name + " for LUN: " + lun + " on " + iqn + " " + tpgt)

	return

def lio_target_list_endpoints(option, opt_str, value, parser):

	iqn_root = os.listdir(lio_root)
	
	for iqn in iqn_root:
		if iqn == "lio_version":
			continue
		if iqn == "discovery_auth":
			continue

		print "\------> " + iqn

		tpg_root = lio_root + "/" + iqn
		for tpg in os.listdir(tpg_root):
			if tpg == "fabric_statistics":
				continue

			p = os.open(tpg_root + "/" + tpg + "/param/TargetAlias", 0)
			value = os.read(p, 256)
			print "        \-------> " + tpg + "  TargetAlias: " + value.rstrip()
			os.close(p)

			print "         TPG Status:",
			p = os.open(tpg_root + "/" + tpg + "/enable", 0)
			value = os.read(p, 8)
			enable_bit = value.rstrip();
			if enable_bit == '1':
				print "ENABLED"
			else:
				print "DISABLED"
			os.close(p)

			print "         TPG Network Portals:"
			np_root = tpg_root + "/" + tpg + "/np"
			for np in os.listdir(np_root):
				print "                 \-------> " + np

			print "         TPG Logical Units:"
			lun_root = tpg_root + "/" + tpg + "/lun"
			for lun in os.listdir(lun_root):
				port_dir = lun_root + "/" + lun
				for port in os.listdir(port_dir):
					if port == "alua_tg_pt_gp":
						continue
					if port == "alua_tg_pt_offline":
						continue
					if port == "alua_tg_pt_status":
						continue
					if port == "alua_tg_pt_write_md":
						continue
					if port == "statistics":
						continue
					
					port_link = port_dir + "/" + port
					if not os.path.islink(port_link):
						continue

					sourcelink = os.readlink(port_link)
					# Skip over ../../../../../ in sourcelink"		
					print "                 \-------> " + lun + "/" + port + " -> " + sourcelink[18:]
					

	return

def lio_target_list_lunacls(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	iqn_root = os.listdir(lio_root)

	nacl_root_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls"
	nacl_root = os.listdir(nacl_root_dir)
	for nacl in nacl_root:
		print "\------> InitiatorName ACL: " + nacl
		print "         Logical Unit ACLs: "
		lun_root_dir = nacl_root_dir + "/" + nacl
		lun_root = os.listdir(lun_root_dir)
		for lun in lun_root:
			ret = re.search('lun_', lun)
			if not ret:
				continue

			wp_attrib = lun_root_dir + "/" + lun + "/write_protect"
			wp_file = open(wp_attrib);
			line = wp_file.readline()
			wp_bit = line.rstrip()
			if wp_bit == '1':
				wp_info = "ENABLED"
			else:
				wp_info = "DISABLED"
			
			lun_link_dir = lun_root_dir + "/" + lun
			for lun_link in os.listdir(lun_link_dir):
				if lun_link == "write_protect":
					continue
				if lun_link == "statistics":
					continue

				if not os.path.islink(lun_link_dir + "/" + lun_link):
					continue
				
				sourcelink = os.readlink(lun_link_dir + "/" + lun_link)
				# Skip over ../../../../../../ in sourcelink"
				print "         \-------> " + lun + " -> " + sourcelink[21:]
				print "                   \-------> Write Protect for " + lun + ": " + wp_info

	return

def lio_target_list_nodeacls(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	iqn_root = os.listdir(lio_root)
	
	nacl_root_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls"
	nacl_root = os.listdir(nacl_root_dir)
	for nacl in nacl_root:
		print "\------> InitiatorName: " + nacl
		info_attrib = nacl_root_dir + "/" + nacl + "/info"
		file = open(info_attrib, "r")
		line = file.readline()
		ret = re.search('No active iSCSI Session for Initiator Endpoint', line)
		if ret:	
			print "         No active iSCSI Session for Initiator Endpoint"
		else:	
			line = file.readline()
			while line:
				print "         " + line.rstrip()
				line = file.readline()

		file.close()
		
	return

def lio_target_list_nps(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	np_root = os.listdir(lio_root + "/" + iqn + "/tpgt_" + tpgt + "/np")
	for np in np_root:
		print np

	return

def lio_target_list_targetnames(option, opt_str, value, parser):
	
	iqn_root = os.listdir(lio_root)
	
	# Loop through LIO-Target IQN list
	for iqn in iqn_root:
		if iqn == "lio_version":
			continue
		if iqn == "discovery_auth":
			continue

		print iqn	

	return

def lio_target_list_node_attr(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2])

	attr_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/attrib/"
	if os.path.isdir(attr_dir) == False:
		lio_err("Unable to locate node attr_dir: " + attr_dir)

	for attr in os.listdir(attr_dir):
		p = open(attr_dir + "/" + attr, 'rU')
		if not p:
			lio_err("Unable to open attr: " + attr_dir + "/" + attr)

		val = p.read()
		p.close()

		print attr + "=" + val.rstrip()
	
	return

def lio_target_set_node_attr(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2])
	attr = str(value[3])
	val = str(value[4])

	attr_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/attrib/"
	if os.path.isdir(attr_dir) == False:
		lio_err("Unable to locate node attr_dir: " + attr_dir)

	p = open(attr_dir + "/" + attr, 'w')
	if not p:
		lio_err("Unable to open node attr: " + attr_dir + "/" + attr)

	ret = p.write(val)
	if ret:
		lio_err("Unable to set node attr: " + attr_dir + "/" + attr)

	p.close()
	print "Successfully set Initiator Node attribute: " + attr + " to: " + val

	return

def lio_target_list_node_param(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	initiator_iqn = str(value[2])

	param_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/acls/" + initiator_iqn + "/param"
	if os.path.isdir(param_dir) == False:
		lio_err("Unable to locate node param_dir: " + param_dir)

	for param in os.listdir(param_dir):
		p = open(param_dir + "/" + param, 'rU')
		if not p:
			lio_err("Unable to open attr: " + param_dir + "/" + param)

		val = p.read()
		p.close()

		print param + "=" + val.rstrip()

	return


def lio_target_list_tpg_attr(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	attr_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/attrib"
	if os.path.isdir(attr_dir) == False:
		lio_err("Unable to locate tpg attr_dir: " + attr_dir)

	for attr in os.listdir(attr_dir):
		p = open(attr_dir + "/" + attr, 'rU')
		if not p:
			lio_err("Unable to open attr: " + attr_dir + "/" + attr)

		val = p.read()
		p.close()

		print attr + "=" + val.rstrip()

	return

def lio_target_set_tpg_attr(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	attr = str(value[2]);
	val = str(value[3]);

	attr_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/attrib"
	if os.path.isdir(attr_dir) == False:
		lio_err("Unable to locate tpg attr_dir: " + attr_dir)

	p = open(attr_dir + "/" + attr, 'w')
	if not p:
		lio_err("Unable to open tpg attr: " + attr_dir + "/" + attr)

	ret = p.write(val)
	if ret:
		lio_err("Unable to set tpg attr: " + attr_dir + "/" + attr)

	p.close()
	print "Successfully set TPG attribute: " + attr + " to: " + val

	return

def lio_target_list_tpg_param(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);

	param_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/param"
	if os.path.isdir(param_dir) == False:
		lio_err("Unable to locate tpg param dir: " + param_dir)

	for param in os.listdir(param_dir):
		p = open(param_dir + "/" + param, 'rU')
		if not p:
			lio_err("Unable to open param: " + param_dir + "/" + param)

		val = p.read()
		p.close()

		print param + "=" + val.rstrip()

	return

def lio_target_set_tpg_param(option, opt_str, value, parser):
	iqn = str(value[0]);
	iqn = iqn.lower();
	tpgt = str(value[1]);
	param = str(value[2]);
	val = str(value[3]);

	param_dir = lio_root + "/" + iqn + "/tpgt_" + tpgt + "/param"
	if os.path.isdir(param_dir) == False:
		lio_err("Unable to locate tpg param dir: " + param_dir)

	p = open(param_dir + "/" + param, 'w')
	if not p:
		lio_err("Unable to open tpg attr: " + param_dir + "/" + param)

	val = val + " "
	ret = p.write(val)
	if ret:
		lio_err("Unable to write tpg attr: " + param_dir + "/" + param)

	p.close()
	print "Successfully set TPG parameter: " + param + " to: " + val

	return

def lio_target_unload(option, opt_str, value, parser):

	if not os.path.isdir(lio_root):
		lio_err("Unable to access lio_root: " + lio_root)

	iqn_root = os.listdir(lio_root)

	# Loop through LIO-Target IQN list
	for iqn in iqn_root:
		if iqn == "lio_version":
			continue
		if iqn == "discovery_auth":
			continue

		# Loop through LIO-Target IQN+TPGT list
		tpg_root = os.listdir(lio_root + "/" + iqn);
		for tpgt_tmp in tpg_root:
			if tpgt_tmp == "fabric_statistics": 
				continue

			tpgt_tmp2 = tpgt_tmp.split('_')
			tpgt = tpgt_tmp2[1]

			tpg_val = [iqn,tpgt]
			__lio_target_del_tpg(None, None, tpg_val, None, 0)	

		__lio_target_del_iqn(None, None, iqn, None, 0)

	rmdir_op = "rmdir " + lio_root
	ret = os.system(rmdir_op)
	if ret:
		print "Unable to release lio_root: " + lio_root

	rmmod_op = "rmmod iscsi_target_mod"
	ret = os.system(rmmod_op)
	if ret:
		print "Unable to unload iscsi_target_mod"

	return

def lio_target_version(option, opt_str, value, parser):

	os.system("cat /sys/kernel/config/target/iscsi/lio_version")
	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")
	parser.add_option("--addnodeacl", action="callback", callback=lio_target_add_nodeacl, nargs=3,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN", help="Add iSCSI Initiator ACL to LIO-Target Portal Group")
	parser.add_option("--addnp", action="callback", callback=lio_target_add_np, nargs=3,
		type="string", dest="TARGET_IQN TPGT IP:PORT", help="Add LIO-Target IPv6 or IPv4 network portal")
	parser.add_option("--addlun", action="callback", callback=lio_target_add_port, nargs=5,
		type="string", dest="TARGET_IQN TPGT LUN PORT_ALIAS TCM_HBA/DEV ", help="Create LIO-Target Logical Unit")
	parser.add_option("--addtpg", action="callback", callback=lio_target_add_tpg, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="Create LIO-Target portal group")
	parser.add_option("--aluasecmd", action="callback", callback=lio_alua_process_secondary_md, nargs=3,
		type="string", dest="TARGET_IQN TPGT LUN", help="Process ALUA secondary metadata for Port/LUN");
        parser.add_option("--cleartgptoff","--clearaluaoff", action="callback", callback=lio_target_alua_clear_tgpt_offline, nargs=3,
                type="string", dest="TARGET_IQN TPGT LUN", help="Clear ALUA Target Port Secondary State OFFLINE")
	parser.add_option("--dellunacl", action="callback", callback=lio_target_del_lunacl, nargs=4,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN MAPPED_LUN", help="Delete iSCSI Initiator LUN ACL from LIO-Target Portal Group LUN")
	parser.add_option("--delnodeacl", action="callback", callback=lio_target_del_nodeacl, nargs=3,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN", help="Delete iSCSI Initiator ACL from LIO-Target Portal Group")
	parser.add_option("--delnp", action="callback", callback=lio_target_del_np, nargs=3,
		type="string", dest="TARGET_IQN TPGT IP:PORT", help="Delete LIO-Target IPv6 or IPv4 network portal")
	parser.add_option("--deliqn", action="callback", callback=lio_target_del_iqn, nargs=1,
		type="string", dest="TARGET_IQN", help="Delete LIO-Target IQN Endpoint")
	parser.add_option("--dellun",  action="callback", callback=lio_target_del_port, nargs=3,
		type="string", dest="TARGET_IQN TPGT LUN", help="Delete LIO-Target Logical Unit")
	parser.add_option("--deltpg", action="callback", callback=lio_target_del_tpg, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="Delete LIO-Target Portal Group")
	parser.add_option("--demomode", "--permissive", action="callback", callback=lio_target_tpg_demomode, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="Disable all iSCSI Initiator ACL requirements (enable DemoMode) for LIO-Target Portal Group (Disabled by default)")
	parser.add_option("--disableauth", action="callback", callback=lio_target_tpg_disableauth, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="Disable iSCSI Authentication for LIO-Target Portal Group (Enabled by default)")
	parser.add_option("--disablelunwp", action="callback", callback=lio_target_disable_lunwp, nargs=4,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN MAPPED_LUN", help="Clear Write Protect bit for iSCSI Initiator LUN ACL")
	parser.add_option("--disabletpg", action="callback", callback=lio_target_disable_tpg, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="Disable LIO-Target Portal Group")
	parser.add_option("--enableaclmode", action="callback", callback=lio_target_enableaclmode, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="Enable iSCSI Initiator ACL requirement mode for LIO-Target Portal Group (Enabled by default)")
	parser.add_option("--enableauth", action="callback", callback=lio_target_enable_auth, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="Enable iSCSI Authentication for LIO-Target Portal Group (Enabled by default)")
	parser.add_option("--enablelunwp", action="callback", callback=lio_target_enable_lunwp, nargs=4,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN MAPPED_LUN", help="Set Write Protect bit for iSCSI Initiator LUN ACL")
	parser.add_option("--enabletpg", action="callback", callback=lio_target_enable_tpg, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="Enable LIO-Target Portal Group")
	parser.add_option("--listendpoints", action="callback", callback=lio_target_list_endpoints, nargs=0,
		help="List iSCSI Target Endpoints")
	parser.add_option("--listlunacls", action="callback", callback=lio_target_list_lunacls, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="List iSCSI Initiator LUN ACLs for LIO-Target Portal Group")
	parser.add_option("--listnodeacls", action="callback", callback=lio_target_list_nodeacls, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="List iSCSI Initiator ACLs for LIO-Target Portal Group")
	parser.add_option("--listnodeattr", action="callback", callback=lio_target_list_node_attr, nargs=3,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN", help="List iSCSI Initiator ACL attributes for LIO-Target Portal Group")
	parser.add_option("--listnodeparam", action="callback", callback=lio_target_list_node_param, nargs=3,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN", help="List iSCSI Initiator ACL RFC-3720 parameters for LIO-Target Portal Group")
	parser.add_option("--listnps", action="callback", callback=lio_target_list_nps, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="List LIO-Target Portal Group Network Portals")
	parser.add_option("--listtargetnames", action="callback", callback=lio_target_list_targetnames, nargs=0,
		help="List iSCSI Target Names")
	parser.add_option("--listtpgattr", action="callback", callback=lio_target_list_tpg_attr, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="List LIO-Target Portal Group attributes")
	parser.add_option("--listtpgparam", action="callback", callback=lio_target_list_tpg_param, nargs=2,
		type="string", dest="TARGET_IQN TPGT", help="List LIO-Target Portal Group RFC-3720 parameters")
	parser.add_option("--setchapauth", action="callback", callback=lio_target_set_chap_auth, nargs=5,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN USER PASS", help="Set CHAP authentication information for iSCSI Initiator Node ACL");
	parser.add_option("--setchapmutualauth", action="callback", callback=lio_target_set_chap_mutual_auth, nargs=5,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN USER_IN PASS_IN", help="Set CHAP mutual authentication information for iSCSI Initiator Node ACL");
        parser.add_option("--setchapdiscenforce", action="callback", callback=lio_target_set_enforce_discovery_auth, nargs=1,
                type="string", dest="Enforce=1, NoEnforcement=0", help="Set CHAP authentication enforcement for iSCSI Discovery Sessions");
        parser.add_option("--setchapdiscauth", action="callback", callback=lio_target_set_chap_discovery_auth, nargs=2,
                type="string", dest="USER PASS", help="Set CHAP authentication information for iSCSI Discovery Authentication")
        parser.add_option("--setchapdiscmutualauth", action="callback", callback=lio_target_set_chap_mutual_discovery_auth, nargs=2,
		type="string", dest="USER PASS", help="Set CHAP mutual authentication information for iSCSI Discovery Authentication")
	parser.add_option("--setnodeattr", action="callback", callback=lio_target_set_node_attr, nargs=5,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN <ATTRIBUTE> <VALUE>",  help="Set iSCSI Initiator ACL Attribute")
	parser.add_option("--setnodetcq", action="callback", callback=lio_target_set_node_tcq, nargs=4,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN DEPTH", help="Set iSCSI Initiator ACL TCQ Depth for LIO-Target Portal Group")
	parser.add_option("--settpgattr", action="callback", callback=lio_target_set_tpg_attr, nargs=4,
		type="string", dest="TARGET_IQN TPGT <ATTRIB> <VALUE>", help="Set LIO-Target Port Group Attribute")
	parser.add_option("--settpgparam", action="callback", callback=lio_target_set_tpg_param, nargs=4,
		type="string", dest="TARGET_IQN TPGT <PARAMETER> <VALUE>", help="Set LIO-Target Port Group RFC-3720 parameter")
	parser.add_option("--settgptgp","--setaluatpg", action="callback", callback=lio_target_alua_set_tgptgp, nargs=4,
		type="string", dest="TARGET_IQN TPGT LUN TG_PT_GP_NAME", help="Set ALUA Target Port Group for LIO-Target Port/LUN")
	parser.add_option("--settgptoff","--setaluaoff", action="callback", callback=lio_target_alua_set_tgpt_offline, nargs=3,
		type="string", dest="TARGET_IQN TPGT LUN", help="Set ALUA Target Port Secondary State OFFLINE")
	parser.add_option("--showchapauth", action="callback", callback=lio_target_show_chap_auth, nargs=3,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN", help="Show CHAP authentication information for iSCSI Initiator Node ACL");
	parser.add_option("--showchapdiscauth", action="callback", callback=lio_target_show_chap_discovery_auth, nargs=0,
		help="Show CHAP authentication information for iSCSI Discovery portal");
	parser.add_option("--shownodetcq", action="callback", callback=lio_target_show_node_tcq, nargs=3,
		type="string", dest="TARGET_IQN TPGT INITIATOR_IQN", help="Show iSCSI Initiator ACL TCQ Depth for LIO-Target Portal Group")
	parser.add_option("--showtgptgp", action="callback", callback=lio_target_alua_show_tgptgp, nargs=3,
		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("--version", action="callback", callback=lio_target_version, nargs=0,
		help="Display LIO-Target version information")

	(options, args) = parser.parse_args()
	if len(sys.argv) == 1:
		parser.print_help()
		sys.exit(0)
	elif not re.search('--', sys.argv[1]):
		lio_err("Unknown CLI option: " + sys.argv[1])

if __name__ == "__main__":
	main()