[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Cluster-devel] [PATCH 2/2] fence_scsi: port to fencing lib



Port of fence_scsi to fencing library

---
 fence/agents/scsi/fence_scsi.py | 328 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 328 insertions(+)
 create mode 100644 fence/agents/scsi/fence_scsi.py

diff --git a/fence/agents/scsi/fence_scsi.py b/fence/agents/scsi/fence_scsi.py
new file mode 100644
index 0000000..18b5e60
--- /dev/null
+++ b/fence/agents/scsi/fence_scsi.py
@@ -0,0 +1,328 @@
+#!/usr/bin/python
+
+import sys, stat, shlex, subprocess, re, os
+sys.path.append("@FENCEAGENTSLIBDIR@")
+from fencing import *
+
+#BEGIN_VERSION_GENERATION
+RELEASE_VERSION=""
+REDHAT_COPYRIGHT=""
+BUILD_DATE=""
+#END_VERSION_GENERATION
+
+def get_status(_, options):
+	status = "off"
+	for dev in options["devices"]:
+		is_block_device(dev)
+		reset_dev(dev)
+		if options["--key"] in get_registration_keys(dev):
+			status = "on"
+		elif options["log"] >= LOG_MODE_VERBOSE:
+			options["debug_fh"].write("No registration for key "\
+			+ options["--key"] + " on device " + dev + "\n")
+	return status
+
+def set_status(_, options):
+	count = 0
+	if options["--action"] == "on":
+		set_key(options)
+		for dev in options["devices"]:
+			is_block_device(dev)
+
+			register_dev(options["--key"], dev, options.has_key("--atptl"))
+			if options["--key"] not in get_registration_keys(dev):
+				count += 1
+				if options["log"] >= LOG_MODE_VERBOSE:
+					options["debug_fh"].write("Failed to register key "\
+					+ options["--key"] + "on device " + dev + "\n")
+				continue
+			dev_write(dev, options)
+
+			if get_reservation_key(dev) is None \
+			and not reserve_dev(options["--key"], dev) \
+			and get_reservation_key(dev) is None:
+				count += 1
+				if options["log"] >= LOG_MODE_VERBOSE:
+					options["debug_fh"].write("Failed to create reservation (key="\
+					+ options["--key"] + ", device=" + dev + ")\n")
+
+	else:
+		host_key = get_key(options)
+		if host_key == options["--key"].lower():
+			fail_usage("Failed: keys cannot be same")
+		for dev in options["devices"]:
+			is_block_device(dev)
+
+			if get_reservation_key(dev) == options["--key"]:
+				release_dev(options["--key"], dev)
+
+			if options["--key"] in get_registration_keys(dev):
+				preempt(host_key, options["--key"], dev)
+
+			if get_reservation_key(dev) == options["--key"]:
+				count += 1
+				if options["log"] >= LOG_MODE_VERBOSE:
+					options["debug_fh"].write("Failed to release device "\
+					+ dev + " from key" + options["--key"] + "\n")
+
+			if options["--key"] in get_registration_keys(dev):
+				count += 1
+				if options["log"] >= LOG_MODE_VERBOSE:
+					options["debug_fh"].write("Failed to remove key "\
+					+ options["--key"] + " on device " + dev + "\n")
+	if count:
+		fail_usage("Failed to verify " + count + " device(s)")
+
+#run command, returns dict, ret["err"] = exit code; ret["out"] = output
+def run_cmd(cmd):
+	cmd = shlex.split(cmd)
+	cmd[0] = find_binary(cmd[0])
+	ret = {}
+	null = open("/dev/null", "w")
+	try:
+		process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=null)
+	except OSError:
+		null.close()
+		fail_usage("Failed: Cannot run '" + cmd + "'")
+
+	ret["err"] = process.wait()
+	ret["out"] = "".join([i for i in process.communicate() if i is not None])
+
+	process.stdout.close()
+	null.close()
+	return ret
+
+# check if device exist and is block device
+def is_block_device(dev):
+	if not os.path.exists(dev):
+		fail_usage("Failed: device \"" + dev + "\" does not exist")
+	if not stat.S_ISBLK(os.stat(dev).st_mode):
+		fail_usage("Failed: device \"" + dev + "\" is not a block device")
+
+# cancel registration
+def preempt(host, node, dev):
+	cmd = "sg_persist -n -o -A -T 5 -K " + host + " -S " + node + " -d " + dev
+	return not bool(run_cmd(cmd)["err"])
+
+def reset_dev(dev):
+	return run_cmd("sg_turs " + dev)["err"]
+
+def register_dev(node_key, dev, atptl):
+	dev = os.path.realpath(dev)
+	if re.search("^dm", dev[5:]):
+		register_dev(i for i in get_mpath_slaves(dev))
+		return True
+	reset_dev(dev)
+	cmd = "sg_persist -n -o -I -S " + node_key + " -d " + dev
+	cmd += " -Z" if atptl else ""
+	#cmd return code != 0 but registration can be successfull
+	return not bool(run_cmd(cmd)["err"])
+
+def reserve_dev(node_key, dev):
+	cmd = "sg_persist -n -o -R -T 5 -K " + node_key + " -d " + dev
+	return not bool(run_cmd(cmd)["err"])
+
+def release_dev(node_key, dev):
+	cmd = "sg_persist -n -o -L -T 5 -K " + node_key + " -d " + dev
+	return not bool(run_cmd(cmd)["err"])
+
+def get_reservation_key(dev):
+	cmd = "sg_persist -n -i -r -d " + dev
+	out = run_cmd(cmd)
+	if out["err"]:
+		fail_usage("error")
+	match = re.search("\s+key=0x(\S+)\s+", out["out"], re.IGNORECASE)
+	return match.group(1) if match else None
+
+def get_registration_keys(dev):
+	keys = []
+	cmd = "sg_persist -n -i -k -d " + dev
+	out = run_cmd(cmd)
+	if out["err"]:
+		fail_usage("error")
+	for line in out["out"].split("\n"):
+		match = re.search("\s+0x(\S+)\s*", line)
+		if match:
+			keys.append(match.group(1))
+	return keys
+
+def get_cluster_id():
+	cmd = "corosync-cmapctl totem.cluster_name"
+
+	match = re.search("\(str\) = (\S+)\n", run_cmd(cmd)["out"])
+	return match.group(1).encode("hex") if match else fail_usage("Failed: cannot get cluster name")
+
+def get_node_id(node):
+	cmd = "corosync-cmapctl nodelist."
+
+	match = re.search(".(\d).ring._addr \(str\) = " + node + "\n", run_cmd(cmd)["out"])
+	return match.group(1) if match else fail_usage("Failed: unable to parse output of corosync-cmapctl or node does not exist")
+
+def generate_key(node):
+	return "%.4s%.4d" % (get_cluster_id(), int(get_node_id(node)))
+
+# save node key to file
+def set_key(options):
+	file_path = options["store_path"] + ".key"
+	if not os.path.isdir(os.path.dirname(options["store_path"])):
+		os.makedirs(os.path.dirname(options["store_path"]))
+	try:
+		f = open(file_path, "w")
+	except IOError:
+		fail_usage("Failed: Cannot open file \""+ file_path + "\"")
+	f.write(options["--key"].lower() + "\n")
+	f.close()
+
+# read node key from file
+def get_key(options):
+	file_path = options["store_path"] + ".key"
+	try:
+		f = open(file_path, "r")
+	except IOError:
+		fail_usage("Failed: Cannot open file \""+ file_path + "\"")
+	return f.readline().strip().lower()
+
+def dev_write(dev, options):
+	file_path = options["store_path"] + ".dev"
+	if not os.path.isdir(os.path.dirname(options["store_path"])):
+		os.makedirs(os.path.dirname(options["store_path"]))
+	try:
+		f = open(file_path, "a+")
+	except IOError:
+		fail_usage("Failed: Cannot open file \""+ file_path + "\"")
+	out = f.read()
+	if not re.search("^" + dev + "\s+", out):
+		f.write(dev + "\n")
+	f.close()
+
+def dev_delete(options):
+	file_path = options["store_path"] + ".dev"
+	os.remove(file_path) if os.path.exists(file_path) else None
+
+def get_clvm_devices():
+	devs = []
+	cmd = "vgs " +\
+	"--noheadings " +\
+	"--separator : " +\
+	"--sort pv_uuid " +\
+	"--options vg_attr,pv_name "+\
+	"--config 'global { locking_type = 0 } devices { preferred_names = [ \"^/dev/dm\" ] }'"
+	out = run_cmd(cmd)
+	if out["err"]:
+		fail_usage("Failed: Cannot get clvm devices")
+	for line in out["out"].split("\n"):
+		if 'c' in line.split(":")[0]:
+			devs.append(line.split(":")[1])
+	return devs
+
+def get_mpath_slaves(dev):
+	if dev[:5] == "/dev/":
+		dev = dev[5:]
+	slaves = [i for i in os.listdir("/sys/block/" + dev + "/slaves/") if i[:1] != "."]
+	if slaves[0][:2] == "dm":
+		slaves = get_mpath_slaves(slaves[0])
+	else:
+		slaves = ["/dev/" + x for x in slaves]
+	return slaves
+
+def find_binary(orig_name):
+	name = shlex.split(orig_name.strip())[0]
+	if name[1:] != "/":
+		os_paths = [i for i in os.environ['PATH'].split(os.pathsep) if len(i.strip())] if "PATH" in os.environ else None
+		for p in os_paths:
+			path = os.path.join(p, name)
+			if is_executable(path):
+				return path
+		name = ""
+	return name if is_executable(name) else	fail_usage("Failed: cannot find executable binary for "+orig_name)
+
+def define_new_opts():
+	all_opt["devices"] = {
+		"getopt" : "d:",
+		"longopt" : "devices",
+		"help" : "-d, --devices=[devices]        List of devices to use for current operation. Devices can be comma-separated list of raw device (eg. /dev/sdc) or device-mapper multipath devices (eg. /dev/dm-3). Each device must support SCSI-3 persistent reservations.",
+		"required" : "0",
+		"shortdesc" : "List of devices to use for current operation. Devices can be comma-separated list of raw device (eg. /dev/sdc) or device-mapper multipath devices (eg. /dev/dm-3). Each device must support SCSI-3 persistent reservations.",
+		"order": 1
+	}
+	all_opt["nodename"] = {
+		"getopt" : "n:",
+		"longopt" : "nodename",
+		"help" : "-n, --nodename=[nodename]      Name of the node to be fenced. The node name is used to generate the key value used for the current operation. This option will be ignored when used with the -k option.",
+		"required" : "0",
+		"shortdesc" : "Name of the node to be fenced. The node name is used to generate the key value used for the current operation. This option will be ignored when used with the -k option.",
+		"order": 1
+	}
+	all_opt["key"] = {
+		"getopt" : "k:",
+		"longopt" : "key",
+		"help" : "-k, --key=[key]                Key to use for the current operation. This key should be unique to a node. For the \"on\" action, the key specifies the key use to register the local node. For the \"off\" action, this key specifies the key to be removed from the device(s).",
+		"required" : "0",
+		"shortdesc" : "Key to use for the current operation. This key should be unique to a node. For the \"on\" action, the key specifies the key use to register the local node. For the \"off\" action, this key specifies the key to be removed from the device(s).",
+		"order": 1
+	}
+	all_opt["atptl"] = {
+		"getopt" : "a",
+		"longopt" : "atptl",
+		"help" : "-a, --atptl                    Use the APTPL flag for registrations. This option is only used for the 'on' action.",
+		"required" : "0",
+		"shortdesc" : "Use the APTPL flag for registrations. This option is only used for the 'on' action.",
+		"order": 1
+	}
+
+def main():
+
+	atexit.register(atexit_handler)
+
+	device_opt = ["no_login", "no_password", "devices", "nodename", "key", "atptl"]
+
+	define_new_opts()
+
+	all_opt["action"]["help"] = "-o, --action=[action]          Action: status, off (default) or on"
+	all_opt["action"]["default"] = "off"
+
+	options = check_input(device_opt, process_input(device_opt))
+
+	docs = { }
+	docs["shortdesc"] = "Fence agent for SCSI persistent reservation"
+	docs["longdesc"] = "fence_scsi is an I/O fencing agent that uses SCSI-3 \
+persistent reservations to control access to shared storage devices. These \
+devices must support SCSI-3 persistent reservations (SPC-3 or greater) as \
+well as the \"preempt-and-abort\" subcommand.\nThe fence_scsi agent works by \
+having each node in the cluster register a unique key with the SCSI \
+devive(s). Once registered, a single node will become the reservation holder \
+by creating a \"write exclusive, registrants only\" reservation on the \
+device(s). The result is that only registered nodes may write to the \
+device(s). When a node failure occurs, the fence_scsi agent will remove the \
+key belonging to the failed node from the device(s). The failed node will no \
+longer be able to write to the device(s). A manual reboot is required."
+	docs["vendorurl"] = ""
+	show_docs(options, docs)
+
+	options["store_path"] = "/var/run/cluster/fence_scsi"
+
+	# Input control BEGIN
+	if (options["--action"] not in ["on", "off", "status"]):
+		fail_usage("Failed: Unrecognised action '" + options["--action"] + "'")
+
+	if not ((options.has_key("--nodename") and options["--nodename"])\
+	or (options.has_key("--key") and options["--key"])):
+		fail_usage("Failed: nodename or key is required")
+
+	if not (options.has_key("--key") and options["--key"]):
+		options["--key"] = generate_key(options["--nodename"])
+
+	if options["--key"] == "0" or not options["--key"]:
+		fail_usage("Failed: key cannot be 0")
+
+	if not (options.has_key("--devices") and options["--devices"].split(",")):
+		options["devices"] = get_clvm_devices()
+	else:
+		options["devices"] = options["--devices"].split(",")
+	# Input control END
+
+	result = fence_action(None, options, set_status, get_status)
+	sys.exit(result)
+
+if __name__ == "__main__":
+	main()
-- 
1.8.3.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]