Blame SOURCES/bz1102727-fence_mpath.patch

44709c
From 1e1c7ecf7b3190e24d8e99d46c886c0b17df6bbe Mon Sep 17 00:00:00 2001
44709c
From: Marek 'marx' Grac <mgrac@redhat.com>
44709c
Date: Wed, 19 Nov 2014 10:51:57 +0100
44709c
Subject: [PATCH 3/6] fence_mpath: new fence agent for dm-multipath based on
44709c
 mpathpersist
44709c
44709c
Previously, scenario with multipath and underlying SCSI devices was solved by using
44709c
fence_scsi what works correctly but there are some limitation. The most important
44709c
is that unfencing has to be done when all paths are available as it is executed only once.
44709c
This new fence agent solve this situation properly as most of this situations are solved
44709c
by mpathpersist which is part of dm-multipath.
44709c
44709c
Conflicts:
44709c
	configure.ac
44709c
	make/fencebuild.mk
44709c
---
44709c
 configure.ac                        |   4 +
44709c
 fence/agents/mpath/Makefile.am      |  17 +++
44709c
 fence/agents/mpath/fence_mpath.py   | 244 ++++++++++++++++++++++++++++++++++++
44709c
 make/fencebuild.mk                  |   6 +
44709c
 tests/data/metadata/fence_mpath.xml |  96 ++++++++++++++
44709c
 5 files changed, 367 insertions(+)
44709c
 create mode 100644 fence/agents/mpath/Makefile.am
44709c
 create mode 100644 fence/agents/mpath/fence_mpath.py
44709c
 create mode 100644 tests/data/metadata/fence_mpath.xml
44709c
44709c
diff --git a/configure.ac b/configure.ac
44709c
index 7abd701..c7259da 100644
44709c
--- a/configure.ac
44709c
+++ b/configure.ac
44709c
@@ -168,6 +168,9 @@ AC_PATH_PROG([SG_PERSIST_PATH], [sg_persist], [/usr/bin/sg_persist])
44709c
 AC_PATH_PROG([SG_TURS_PATH], [sg_turs], [/usr/bin/sg_turs])
44709c
 AC_PATH_PROG([VGS_PATH], [vgs], [/usr/sbin/vgs])
44709c
 AC_PATH_PROG([NOVA_PATH], [nova], [/usr/bin/nova])
44709c
+AC_PATH_PROG([MPATH_PATH], [mpathpersist], [/usr/sbin/mpathpersist])
44709c
+AC_PATH_PROG([SUDO_PATH], [mpathpersist], [/usr/bin/sudo])
44709c
+
44709c
 ## do subst
44709c
 
44709c
 AC_SUBST([DEFAULT_CONFIG_DIR])
44709c
@@ -289,6 +292,7 @@ AC_CONFIG_FILES([Makefile
44709c
 		 fence/agents/lib/Makefile
44709c
 		 fence/agents/lpar/Makefile
44709c
 		 fence/agents/manual/Makefile
44709c
+		 fence/agents/mpath/Makefile
44709c
 		 fence/agents/netio/Makefile
44709c
 		 fence/agents/ovh/Makefile
44709c
 		 fence/agents/pve/Makefile
44709c
diff --git a/fence/agents/mpath/Makefile.am b/fence/agents/mpath/Makefile.am
44709c
new file mode 100644
44709c
index 0000000..fd3d8d2
44709c
--- /dev/null
44709c
+++ b/fence/agents/mpath/Makefile.am
44709c
@@ -0,0 +1,17 @@
44709c
+MAINTAINERCLEANFILES	= Makefile.in
44709c
+
44709c
+TARGET			= fence_mpath
44709c
+
44709c
+SRC			= $(TARGET).py
44709c
+
44709c
+EXTRA_DIST		= $(SRC)
44709c
+
44709c
+sbin_SCRIPTS		= $(TARGET)
44709c
+
44709c
+man_MANS		= $(TARGET).8
44709c
+
44709c
+FENCE_TEST_ARGS         = -k 1
44709c
+
44709c
+include $(top_srcdir)/make/fencebuild.mk
44709c
+include $(top_srcdir)/make/fenceman.mk
44709c
+include $(top_srcdir)/make/agentpycheck.mk
44709c
diff --git a/fence/agents/mpath/fence_mpath.py b/fence/agents/mpath/fence_mpath.py
44709c
new file mode 100644
44709c
index 0000000..8a4811c
44709c
--- /dev/null
44709c
+++ b/fence/agents/mpath/fence_mpath.py
44709c
@@ -0,0 +1,244 @@
44709c
+#!/usr/bin/python -tt
44709c
+
44709c
+import sys
44709c
+import stat
44709c
+import re
44709c
+import os
44709c
+import logging
44709c
+import atexit
44709c
+sys.path.append("@FENCEAGENTSLIBDIR@")
44709c
+from fencing import fail_usage, run_command, atexit_handler, check_input, process_input, show_docs
44709c
+from fencing import fence_action, all_opt, run_delay
44709c
+
44709c
+#BEGIN_VERSION_GENERATION
44709c
+RELEASE_VERSION=""
44709c
+REDHAT_COPYRIGHT=""
44709c
+BUILD_DATE=""
44709c
+#END_VERSION_GENERATION
44709c
+
44709c
+def get_status(conn, options):
44709c
+	del conn
44709c
+	status = "off"
44709c
+	for dev in options["devices"]:
44709c
+		is_block_device(dev)
44709c
+		if options["--key"] in get_registration_keys(options, dev):
44709c
+			status = "on"
44709c
+		else:
44709c
+			logging.debug("No registration for key "\
44709c
+				+ options["--key"] + " on device " + dev + "\n")
44709c
+	return status
44709c
+
44709c
+
44709c
+def set_status(conn, options):
44709c
+	del conn
44709c
+	count = 0
44709c
+	if options["--action"] == "on":
44709c
+		for dev in options["devices"]:
44709c
+			is_block_device(dev)
44709c
+
44709c
+			register_dev(options, dev)
44709c
+			if options["--key"] not in get_registration_keys(options, dev):
44709c
+				count += 1
44709c
+				logging.debug("Failed to register key "\
44709c
+					+ options["--key"] + "on device " + dev + "\n")
44709c
+				continue
44709c
+			dev_write(options, dev)
44709c
+
44709c
+			if get_reservation_key(options, dev) is None \
44709c
+			and not reserve_dev(options, dev) \
44709c
+			and get_reservation_key(options, dev) is None:
44709c
+				count += 1
44709c
+				logging.debug("Failed to create reservation (key="\
44709c
+					+ options["--key"] + ", device=" + dev + ")\n")
44709c
+
44709c
+	else:
44709c
+		dev_keys = dev_read(options)
44709c
+
44709c
+		for dev in options["devices"]:
44709c
+			is_block_device(dev)
44709c
+
44709c
+			if options["--key"] in get_registration_keys(options, dev):
44709c
+				preempt_abort(options, dev_keys[dev], dev)
44709c
+
44709c
+		for dev in options["devices"]:
44709c
+			if options["--key"] in get_registration_keys(options, dev):
44709c
+				count += 1
44709c
+				logging.debug("Failed to remove key "\
44709c
+					+ options["--key"] + " on device " + dev + "\n")
44709c
+				continue
44709c
+
44709c
+			if not get_reservation_key(options, dev):
44709c
+				count += 1
44709c
+				logging.debug("No reservation exists on device " + dev + "\n")
44709c
+	if count:
44709c
+		logging.error("Failed to verify " + str(count) + " device(s)")
44709c
+		sys.exit(1)
44709c
+
44709c
+
44709c
+#run command, returns dict, ret["err"] = exit code; ret["out"] = output
44709c
+def run_cmd(options, cmd):
44709c
+	ret = {}
44709c
+
44709c
+	if options.has_key("--use-sudo"):
44709c
+		prefix = options["--sudo-path"] + " "
44709c
+	else:
44709c
+		prefix = ""
44709c
+
44709c
+	(ret["err"], ret["out"], _) = run_command(options, prefix + cmd)
44709c
+	ret["out"] = "".join([i for i in ret["out"] if i is not None])
44709c
+	return ret
44709c
+
44709c
+
44709c
+# check if device exist and is block device
44709c
+def is_block_device(dev):
44709c
+	if not os.path.exists(dev):
44709c
+		fail_usage("Failed: device \"" + dev + "\" does not exist")
44709c
+	if not stat.S_ISBLK(os.stat(dev).st_mode):
44709c
+		fail_usage("Failed: device \"" + dev + "\" is not a block device")
44709c
+
44709c
+# cancel registration
44709c
+def preempt_abort(options, host, dev):
44709c
+	cmd = options["--mpathpersist-path"] + " -o --preempt-abort --prout-type=5 --param-rk=" + host +" --param-sark=" + options["--key"] +"-d " + dev
44709c
+	return not bool(run_cmd(options, cmd)["err"])
44709c
+
44709c
+def register_dev(options, dev):
44709c
+	cmd = options["--mpathpersist-path"] + " -o --register --param-sark=" + options["--key"] + " -d " + dev
44709c
+	#cmd return code != 0 but registration can be successful
44709c
+	return not bool(run_cmd(options, cmd)["err"])
44709c
+
44709c
+def reserve_dev(options, dev):
44709c
+	cmd = options["--mpathpersist-path"] + " -o --reserv --prout-type=5 --param-rk=" + options["--key"] + " -d " + dev
44709c
+	return not bool(run_cmd(options, cmd)["err"])
44709c
+
44709c
+def get_reservation_key(options, dev):
44709c
+	cmd = options["--mpathpersist-path"] + " -i -r -d " + dev
44709c
+	out = run_cmd(options, cmd)
44709c
+	if out["err"]:
44709c
+		fail_usage("Cannot get reservation key")
44709c
+	match = re.search(r"\s+key\s*=\s*0x(\S+)\s+", out["out"], re.IGNORECASE)
44709c
+	return match.group(1) if match else None
44709c
+
44709c
+def get_registration_keys(options, dev):
44709c
+	keys = []
44709c
+	cmd = options["--mpathpersist-path"] + " -i -k -d " + dev
44709c
+	out = run_cmd(options, cmd)
44709c
+	if out["err"]:
44709c
+		fail_usage("Cannot get registration keys")
44709c
+	for line in out["out"].split("\n"):
44709c
+		match = re.search(r"\s+0x(\S+)\s*", line)
44709c
+		if match:
44709c
+			keys.append(match.group(1))
44709c
+	return keys
44709c
+
44709c
+def dev_write(options, dev):
44709c
+	file_path = options["--store-path"] + "/mpath.devices"
44709c
+
44709c
+	if not os.path.isdir(os.path.dirname(options["--store-path"])):
44709c
+		os.makedirs(os.path.dirname(options["--store-path"]))
44709c
+
44709c
+	try:
44709c
+		store_fh = open(file_path, "a+")
44709c
+	except IOError:
44709c
+		fail_usage("Failed: Cannot open file \""+ file_path + "\"")
44709c
+	out = store_fh.read()
44709c
+	if not re.search(r"^" + dev + r"\s+", out):
44709c
+		store_fh.write(dev + "\t" + options["--key"] + "\n")
44709c
+	store_fh.close()
44709c
+
44709c
+def dev_read(options):
44709c
+	dev_key = {}
44709c
+	file_path = options["--store-path"] + "/mpath.devices"
44709c
+	try:
44709c
+		store_fh = open(file_path, "r")
44709c
+	except IOError:
44709c
+		fail_usage("Failed: Cannot open file \"" + file_path + "\"")
44709c
+	# get not empty lines from file
44709c
+	for (device, key) in [line.strip().split() for line in store_fh if line.strip()]:
44709c
+		dev_key[device] = key
44709c
+	store_fh.close()
44709c
+	return dev_key
44709c
+
44709c
+def define_new_opts():
44709c
+	all_opt["devices"] = {
44709c
+		"getopt" : "d:",
44709c
+		"longopt" : "devices",
44709c
+		"help" : "-d, --devices=[devices]        List of devices to use for current operation",
44709c
+		"required" : "0",
44709c
+		"shortdesc" : "List of devices to use for current operation. Devices can \
44709c
+be comma-separated list of device-mapper multipath devices (eg. /dev/dm-3). \
44709c
+Each device must support SCSI-3 persistent reservations.",
44709c
+		"order": 1
44709c
+	}
44709c
+	all_opt["key"] = {
44709c
+		"getopt" : "k:",
44709c
+		"longopt" : "key",
44709c
+		"help" : "-k, --key=[key]                Key to use for the current operation",
44709c
+		"required" : "1",
44709c
+		"shortdesc" : "Key to use for the current operation. This key should be \
44709c
+unique to a node and have to be written in /etc/multipath.conf. For the \"on\" action, the key specifies the key use to \
44709c
+register the local node. For the \"off\" action, this key specifies the key to \
44709c
+be removed from the device(s).",
44709c
+		"order": 1
44709c
+	}
44709c
+	all_opt["mpathpersist_path"] = {
44709c
+		"getopt" : "X:",
44709c
+		"longopt" : "mpathpersist-path",
44709c
+		"help" : "--mpathpersist-path=[path]     Path to mpathpersist binary",
44709c
+		"required" : "0",
44709c
+		"shortdesc" : "Path to mpathpersist binary",
44709c
+		"default" : "@MPATH_PATH@",
44709c
+		"order": 200
44709c
+	}
44709c
+	all_opt["store_path"] = {
44709c
+		"getopt" : "X:",
44709c
+		"longopt" : "store-path",
44709c
+		"help" : "--store-path=[path]            Path to directory containing cached keys",
44709c
+		"required" : "0",
44709c
+		"shortdesc" : "Path to directory where fence agent can store information",
44709c
+		"default" : "@STORE_PATH@",
44709c
+		"order": 200
44709c
+	}
44709c
+
44709c
+def main():
44709c
+	atexit.register(atexit_handler)
44709c
+
44709c
+	device_opt = ["no_login", "no_password", "devices", "key", "sudo", \
44709c
+	        "fabric_fencing", "on_target", "store_path", "mpathpersist_path"]
44709c
+
44709c
+	define_new_opts()
44709c
+
44709c
+	options = check_input(device_opt, process_input(device_opt))
44709c
+
44709c
+	docs = {}
44709c
+	docs["shortdesc"] = "Fence agent for multipath persistent reservation"
44709c
+	docs["longdesc"] = "fence_mpath is an I/O fencing agent that uses SCSI-3 \
44709c
+persistent reservations to control access multipath devices. Underlying \
44709c
+devices must support SCSI-3 persistent reservations (SPC-3 or greater) as \
44709c
+well as the \"preempt-and-abort\" subcommand.\nThe fence_mpath agent works by \
44709c
+having an unique key for each pair of node and device that has to be set also \
44709c
+in /etc/multipath.conf. Once registered, a single node will become the reservation holder \
44709c
+by creating a \"write exclusive, registrants only\" reservation on the \
44709c
+device(s). The result is that only registered nodes may write to the \
44709c
+device(s). When a node failure occurs, the fence_mpath agent will remove the \
44709c
+key belonging to the failed node from the device(s). The failed node will no \
44709c
+longer be able to write to the device(s). A manual reboot is required."
44709c
+	docs["vendorurl"] = "https://www.sourceware.org/dm/"
44709c
+	show_docs(options, docs)
44709c
+
44709c
+	run_delay(options)
44709c
+
44709c
+	# Input control BEGIN
44709c
+	if not "--key" in options:
44709c
+		fail_usage("Failed: key is required")
44709c
+
44709c
+	options["devices"] = options["--devices"].split(",")
44709c
+
44709c
+	if not options["devices"]:
44709c
+		fail_usage("Failed: No devices found")
44709c
+	# Input control END
44709c
+
44709c
+	result = fence_action(None, options, set_status, get_status)
44709c
+	sys.exit(result)
44709c
+
44709c
+if __name__ == "__main__":
44709c
+	main()
44709c
diff --git a/make/fencebuild.mk b/make/fencebuild.mk
44709c
index b59c069..7df0fc7 100644
44709c
--- a/make/fencebuild.mk
44709c
+++ b/make/fencebuild.mk
44709c
@@ -17,6 +17,12 @@ $(TARGET): $(SRC)
44709c
 		-e 's#@''SG_TURS_PATH@#${SG_TURS_PATH}#g' \
44709c
 		-e 's#@''VGS_PATH@#${VGS_PATH}#g' \
44709c
 		-e 's#@''NOVA_PATH@#${NOVA_PATH}#g' \
44709c
+		-e 's#@''SUDO_PATH@#${SUDO_PATH}#g' \
44709c
+		-e 's#@''SSH_PATH@#${SSH_PATH}#g' \
44709c
+		-e 's#@''TELNET_PATH@#${TELNET_PATH}#g' \
44709c
+		-e 's#@''MPATH_PATH@#${MPATH_PATH}#g' \
44709c
+		-e 's#@''STORE_PATH@#${CLUSTERVARRUN}#g' \
44709c
+		-e 's#@''SUDO_PATH@#${SUDO_PATH}#g' \
44709c
 	> $@
44709c
 
44709c
 	if [ 0 -eq `echo "$(SRC)" | grep fence_ &> /dev/null; echo $$?` ]; then \
44709c
diff --git a/tests/data/metadata/fence_mpath.xml b/tests/data/metadata/fence_mpath.xml
44709c
new file mode 100644
44709c
index 0000000..c62dd49
44709c
--- /dev/null
44709c
+++ b/tests/data/metadata/fence_mpath.xml
44709c
@@ -0,0 +1,96 @@
44709c
+
44709c
+<resource-agent name="fence_mpath" shortdesc="Fence agent for multipath persistent reservation" >
44709c
+<longdesc>fence_mpath is an I/O fencing agent that uses SCSI-3 persistent reservations to control access multipath devices. Underlying devices must support SCSI-3 persistent reservations (SPC-3 or greater) as well as the "preempt-and-abort" subcommand.
44709c
+The fence_mpath agent works by having an unique key for each pair of node and device that has to be set also in /etc/multipath.conf. 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_mpath 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.</longdesc>
44709c
+<vendor-url>https://www.sourceware.org/dm/</vendor-url>
44709c
+<parameters>
44709c
+	<parameter name="devices" unique="0" required="0">
44709c
+		<getopt mixed="-d, --devices=[devices]" />
44709c
+		<content type="string"  />
44709c
+		<shortdesc lang="en">List of devices to use for current operation. Devices can be comma-separated list of device-mapper multipath devices (eg. /dev/dm-3). Each device must support SCSI-3 persistent reservations.</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="key" unique="0" required="1">
44709c
+		<getopt mixed="-k, --key=[key]" />
44709c
+		<content type="string"  />
44709c
+		<shortdesc lang="en">Key to use for the current operation. This key should be unique to a node and have to be written in /etc/multipath.conf. 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).</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="action" unique="0" required="1">
44709c
+		<getopt mixed="-o, --action=[action]" />
44709c
+		<content type="string" default="off"  />
44709c
+		<shortdesc lang="en">Fencing Action</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="verbose" unique="0" required="0">
44709c
+		<getopt mixed="-v, --verbose" />
44709c
+		<content type="boolean"  />
44709c
+		<shortdesc lang="en">Verbose mode</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="debug" unique="0" required="0">
44709c
+		<getopt mixed="-D, --debug-file=[debugfile]" />
44709c
+		<content type="string"  />
44709c
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="version" unique="0" required="0">
44709c
+		<getopt mixed="-V, --version" />
44709c
+		<content type="boolean"  />
44709c
+		<shortdesc lang="en">Display version information and exit</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="help" unique="0" required="0">
44709c
+		<getopt mixed="-h, --help" />
44709c
+		<content type="boolean"  />
44709c
+		<shortdesc lang="en">Display help and exit</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="delay" unique="0" required="0">
44709c
+		<getopt mixed="--delay=[seconds]" />
44709c
+		<content type="string" default="0"  />
44709c
+		<shortdesc lang="en">Wait X seconds before fencing is started</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="shell_timeout" unique="0" required="0">
44709c
+		<getopt mixed="--shell-timeout=[seconds]" />
44709c
+		<content type="string" default="3"  />
44709c
+		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="store_path" unique="0" required="0">
44709c
+		<getopt mixed="--store-path=[path]" />
44709c
+		<content type="string" default="/var/run/cluster"  />
44709c
+		<shortdesc lang="en">Path to directory where fence agent can store information</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="power_timeout" unique="0" required="0">
44709c
+		<getopt mixed="--power-timeout=[seconds]" />
44709c
+		<content type="string" default="20"  />
44709c
+		<shortdesc lang="en">Test X seconds for status change after ON/OFF</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="power_wait" unique="0" required="0">
44709c
+		<getopt mixed="--power-wait=[seconds]" />
44709c
+		<content type="string" default="0"  />
44709c
+		<shortdesc lang="en">Wait X seconds after issuing ON/OFF</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="login_timeout" unique="0" required="0">
44709c
+		<getopt mixed="--login-timeout=[seconds]" />
44709c
+		<content type="string" default="5"  />
44709c
+		<shortdesc lang="en">Wait X seconds for cmd prompt after login</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="mpathpersist_path" unique="0" required="0">
44709c
+		<getopt mixed="--mpathpersist-path=[path]" />
44709c
+		<content type="string" default="/usr/sbin/mpathpersist"  />
44709c
+		<shortdesc lang="en">Path to mpathpersist binary</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="retry_on" unique="0" required="0">
44709c
+		<getopt mixed="--retry-on=[attempts]" />
44709c
+		<content type="string" default="1"  />
44709c
+		<shortdesc lang="en">Count of attempts to retry power on</shortdesc>
44709c
+	</parameter>
44709c
+	<parameter name="sudo" unique="0" required="0">
44709c
+		<getopt mixed="--use-sudo" />
44709c
+		<content type="boolean"  />
44709c
+		<shortdesc lang="en">Use sudo (without password) when calling 3rd party sotfware.</shortdesc>
44709c
+	</parameter>
44709c
+</parameters>
44709c
+<actions>
44709c
+	<action name="on" on_target="1" automatic="1"/>
44709c
+	<action name="off" />
44709c
+	<action name="status" />
44709c
+	<action name="list" />
44709c
+	<action name="monitor" />
44709c
+	<action name="metadata" />
44709c
+</actions>
44709c
+</resource-agent>
44709c
-- 
44709c
1.9.3
44709c