Blame SOURCES/bz1504202-fence_mpath-watchdog-support.patch

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