Blame SOURCES/fence_mpath-watchdog-support.patch

674a4f
From 199b1efee0ba1f01c27ca689a15465cf4a258ee6 Mon Sep 17 00:00:00 2001
674a4f
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
674a4f
Date: Mon, 22 Jan 2018 11:27:28 +0100
674a4f
Subject: [PATCH] fence_mpath: add watchdog support
674a4f
674a4f
---
674a4f
 agents/Makefile.am                  | 11 ++++++++
674a4f
 agents/mpath/fence_mpath.py         | 50 ++++++++++++++++++++++++++++++++++---
674a4f
 configure.ac                        |  6 +++++
674a4f
 make/fencebuild.mk                  |  2 +-
674a4f
 tests/data/metadata/fence_mpath.xml |  2 +-
674a4f
 5 files changed, 66 insertions(+), 5 deletions(-)
674a4f
674a4f
diff --git a/agents/Makefile.am b/agents/Makefile.am
674a4f
index 2524a3ab..833d2af5 100644
674a4f
--- a/agents/Makefile.am
674a4f
+++ b/agents/Makefile.am
674a4f
@@ -50,6 +50,11 @@ zvm_fence_zvm_SOURCES		= zvm/fence_zvm.c
674a4f
 zvm_fence_zvm_CFLAGS		= -D_GNU_SOURCE -Izvm
674a4f
 endif
674a4f
 
674a4f
+if BUILD_FENCE_MPATH
674a4f
+mpathdatadir			= $(CLUSTERDATA)
674a4f
+mpathdata_SCRIPTS		= mpath/fence_mpath_check mpath/fence_mpath_check_hardreboot
674a4f
+endif
674a4f
+
674a4f
 if BUILD_FENCE_SCSI
674a4f
 scsidatadir			= $(CLUSTERDATA)
674a4f
 scsidata_SCRIPTS		= scsi/fence_scsi_check scsi/fence_scsi_check_hardreboot
674a4f
@@ -72,6 +77,12 @@ manual/fence_ack_manual: manual/fence_ack_manual.in
674a4f
 		-e 's#@clustervarrun@#${CLUSTERVARRUN}#g' \
674a4f
 	> $@
674a4f
 
674a4f
+mpath/fence_mpath_check: mpath/fence_mpath
674a4f
+	cp $^ $@
674a4f
+
674a4f
+mpath/fence_mpath_check_hardreboot: mpath/fence_mpath
674a4f
+	cp $^ $@
674a4f
+
674a4f
 scsi/fence_scsi_check: scsi/fence_scsi
674a4f
 	cp $^ $@
674a4f
 
674a4f
diff --git a/agents/mpath/fence_mpath.py b/agents/mpath/fence_mpath.py
674a4f
index ac5bc794..d9ac2ef5 100644
674a4f
--- a/agents/mpath/fence_mpath.py
674a4f
+++ b/agents/mpath/fence_mpath.py
674a4f
@@ -143,25 +143,63 @@ def dev_write(options, dev):
674a4f
 		store_fh.write(dev + "\t" + options["--key"] + "\n")
674a4f
 	store_fh.close()
674a4f
 
674a4f
-def dev_read(options):
674a4f
+def dev_read(options, fail=True):
674a4f
 	dev_key = {}
674a4f
 	file_path = options["--store-path"] + "/mpath.devices"
674a4f
 	try:
674a4f
 		store_fh = open(file_path, "r")
674a4f
 	except IOError:
674a4f
-		fail_usage("Failed: Cannot open file \"" + file_path + "\"")
674a4f
+		if fail:
674a4f
+			fail_usage("Failed: Cannot open file \"" + file_path + "\"")
674a4f
+		else:
674a4f
+			return None
674a4f
 	# get not empty lines from file
674a4f
 	for (device, key) in [line.strip().split() for line in store_fh if line.strip()]:
674a4f
 		dev_key[device] = key
674a4f
 	store_fh.close()
674a4f
 	return dev_key
674a4f
 
674a4f
+def mpath_check_get_verbose():
674a4f
+	try:
674a4f
+		f = open("/etc/sysconfig/watchdog", "r")
674a4f
+	except IOError:
674a4f
+		return False
674a4f
+	match = re.search(r"^\s*verbose=yes", "".join(f.readlines()), re.MULTILINE)
674a4f
+	f.close()
674a4f
+	return bool(match)
674a4f
+
674a4f
+def mpath_check(hardreboot=False):
674a4f
+	if len(sys.argv) >= 3 and sys.argv[1] == "repair":
674a4f
+		return int(sys.argv[2])
674a4f
+	options = {}
674a4f
+	options["--mpathpersist-path"] = "/usr/sbin/mpathpersist"
674a4f
+	options["--store-path"] = "/var/run/cluster"
674a4f
+	options["--power-timeout"] = "5"
674a4f
+	if mpath_check_get_verbose():
674a4f
+		logging.getLogger().setLevel(logging.DEBUG)
674a4f
+	devs = dev_read(options, fail=False)
674a4f
+	if not devs:
674a4f
+		logging.error("No devices found")
674a4f
+		return 0
674a4f
+	for dev, key in list(devs.items()):
674a4f
+		if key in get_registration_keys(options, dev):
674a4f
+			logging.debug("key " + key + " registered with device " + dev)
674a4f
+			return 0
674a4f
+		else:
674a4f
+			logging.debug("key " + key + " not registered with device " + dev)
674a4f
+	logging.debug("key " + key + " registered with any devices")
674a4f
+
674a4f
+	if hardreboot == True:
674a4f
+		libc = ctypes.cdll['libc.so.6']
674a4f
+		libc.reboot(0x1234567)
674a4f
+	return 2
674a4f
+
674a4f
 def define_new_opts():
674a4f
 	all_opt["devices"] = {
674a4f
 		"getopt" : "d:",
674a4f
 		"longopt" : "devices",
674a4f
 		"help" : "-d, --devices=[devices]        List of devices to use for current operation",
674a4f
-		"required" : "1",
674a4f
+		"required" : "0",
674a4f
 		"shortdesc" : "List of devices to use for current operation. Devices can \
674a4f
 be comma-separated list of device-mapper multipath devices (eg. /dev/mapper/3600508b400105df70000e00000ac0000 or /dev/mapper/mpath1). \
674a4f
 Each device must support SCSI-3 persistent reservations.",
674a4f
@@ -205,6 +243,12 @@ def main():
674a4f
 
674a4f
 	define_new_opts()
674a4f
 
674a4f
+	# fence_mpath_check
674a4f
+	if os.path.basename(sys.argv[0]) == "fence_mpath_check":
674a4f
+		sys.exit(mpath_check())
674a4f
+	elif os.path.basename(sys.argv[0]) == "fence_mpath_check_hardreboot":
674a4f
+		sys.exit(mpath_check(hardreboot=True))
674a4f
+
674a4f
 	options = check_input(device_opt, process_input(device_opt), other_conditions=True)
674a4f
 
674a4f
 	docs = {}
674a4f
diff --git a/configure.ac b/configure.ac
674a4f
index e8b24211..24b857b3 100644
674a4f
--- a/configure.ac
674a4f
+++ b/configure.ac
674a4f
@@ -148,6 +148,11 @@ if echo "$AGENTS_LIST" | grep -q -E "all|manual"; then
674a4f
 	AGENTS_LIST=$(echo "$AGENTS_LIST" | sed -E "s/manual( |$)//")
674a4f
 fi
674a4f
 
674a4f
+FENCE_MPATH=0
674a4f
+if echo "$AGENTS_LIST" | grep -q -E "all|mpath"; then
674a4f
+	FENCE_MPATH=1
674a4f
+fi
674a4f
+
674a4f
 FENCE_SCSI=0
674a4f
 if echo "$AGENTS_LIST" | grep -q -E "all|scsi"; then
674a4f
 	FENCE_SCSI=1
674a4f
@@ -312,6 +317,7 @@ AC_SUBST([SNMPBIN])
674a4f
 AC_SUBST([AGENTS_LIST])
674a4f
 AM_CONDITIONAL(BUILD_FENCE_KDUMP, test $FENCE_KDUMP -eq 1)
674a4f
 AM_CONDITIONAL(BUILD_FENCE_MANUAL, test $FENCE_MANUAL -eq 1)
674a4f
+AM_CONDITIONAL(BUILD_FENCE_MPATH, test $FENCE_MPATH -eq 1)
674a4f
 AM_CONDITIONAL(BUILD_FENCE_SCSI, test $FENCE_SCSI -eq 1)
674a4f
 AM_CONDITIONAL(BUILD_FENCE_ZVM, test $FENCE_ZVM -eq 1)
674a4f
 AM_CONDITIONAL(BUILD_XENAPILIB, test $XENAPILIB -eq 1)
674a4f
diff --git a/make/fencebuild.mk b/make/fencebuild.mk
674a4f
index e08d5200..6a7c6f63 100644
674a4f
--- a/make/fencebuild.mk
674a4f
+++ b/make/fencebuild.mk
674a4f
@@ -51,7 +51,7 @@ $(TARGET):
674a4f
 	$(call gen_agent_from_py)
674a4f
 
674a4f
 clean: clean-man
674a4f
-	rm -f $(CLEAN_TARGET:%.8=%) $(CLEAN_TARGET_ADDITIONAL) $(scsidata_SCRIPTS) */*.pyc *.pyc */*.wiki
674a4f
+	rm -f $(CLEAN_TARGET:%.8=%) $(CLEAN_TARGET_ADDITIONAL) $(mpathdata_SCRIPTS) $(scsidata_SCRIPTS) */*.pyc */*.wiki
674a4f
 
674a4f
 	if [ "$(abs_builddir)" = "$(abs_top_builddir)/lib" ]; then \
674a4f
 		rm -f $(TARGET); \
674a4f
diff --git a/tests/data/metadata/fence_mpath.xml b/tests/data/metadata/fence_mpath.xml
674a4f
index f384e50b..bbe9ad2b 100644
674a4f
--- a/tests/data/metadata/fence_mpath.xml
674a4f
+++ b/tests/data/metadata/fence_mpath.xml
674a4f
@@ -9,7 +9,7 @@ The fence_mpath agent works by having a unique key for each node that has to be
674a4f
 		<content type="string" default="off"  />
674a4f
 		<shortdesc lang="en">Fencing action</shortdesc>
674a4f
 	</parameter>
674a4f
-	<parameter name="devices" unique="0" required="1">
674a4f
+	<parameter name="devices" unique="0" required="0">
674a4f
 		<getopt mixed="-d, --devices=[devices]" />
674a4f
 		<content type="string"  />
674a4f
 		<shortdesc lang="en">List of devices to use for current operation. Devices can be comma-separated list of device-mapper multipath devices (eg. /dev/mapper/3600508b400105df70000e00000ac0000 or /dev/mapper/mpath1). Each device must support SCSI-3 persistent reservations.</shortdesc>