Blame SOURCES/bz1841087-fence_scsi-dont-write-key-device-to-file.patch

92e554
From 431e8bc40288d97d80f07ec195c0a07c5f8c065a Mon Sep 17 00:00:00 2001
92e554
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
92e554
Date: Tue, 10 Nov 2020 12:46:50 +0100
92e554
Subject: [PATCH] fence_scsi: dont write key to device if it's already
92e554
 registered, and open file correctly to avoid using regex against end-of-file
92e554
92e554
---
92e554
 agents/scsi/fence_scsi.py | 12 +++++-------
92e554
 1 file changed, 5 insertions(+), 7 deletions(-)
92e554
92e554
diff --git a/agents/scsi/fence_scsi.py b/agents/scsi/fence_scsi.py
92e554
index 77817f35..9a86689d 100644
92e554
--- a/agents/scsi/fence_scsi.py
92e554
+++ b/agents/scsi/fence_scsi.py
92e554
@@ -135,6 +135,8 @@ def register_dev(options, dev):
92e554
 		for slave in get_mpath_slaves(dev):
92e554
 			register_dev(options, slave)
92e554
 		return True
92e554
+	if get_reservation_key(options, dev, False) == options["--key"]:
92e554
+		return True
92e554
 	reset_dev(options, dev)
92e554
 	cmd = options["--sg_persist-path"] + " -n -o -I -S " + options["--key"] + " -d " + dev
92e554
 	cmd += " -Z" if "--aptpl" in options else ""
92e554
@@ -148,14 +150,14 @@ def reserve_dev(options, dev):
92e554
 	return not bool(run_cmd(options, cmd)["err"])
92e554
 
92e554
 
92e554
-def get_reservation_key(options, dev):
92e554
+def get_reservation_key(options, dev, fail=True):
92e554
 	reset_dev(options,dev)
92e554
 	opts = ""
92e554
 	if "--readonly" in options:
92e554
 		opts = "-y "
92e554
 	cmd = options["--sg_persist-path"] + " -n -i " + opts + "-r -d " + dev
92e554
 	out = run_cmd(options, cmd)
92e554
-	if out["err"]:
92e554
+	if out["err"] and fail:
92e554
 		fail_usage("Cannot get reservation key")
92e554
 	match = re.search(r"\s+key=0x(\S+)\s+", out["out"], re.IGNORECASE)
92e554
 	return match.group(1) if match else None
92e554
@@ -257,6 +259,7 @@ def dev_write(dev, options):
92e554
 		f = open(file_path, "a+")
92e554
 	except IOError:
92e554
 		fail_usage("Failed: Cannot open file \""+ file_path + "\"")
92e554
+	f.seek(0)
92e554
 	out = f.read()
92e554
 	if not re.search(r"^" + dev + "\s+", out, flags=re.MULTILINE):
92e554
 		f.write(dev + "\n")
92e554
@@ -277,11 +280,6 @@ def dev_read(fail=True):
92e554
 	return devs
92e554
 
92e554
 
92e554
-def dev_delete(options):
92e554
-	file_path = options["store_path"] + ".dev"
92e554
-	os.remove(file_path) if os.path.exists(file_path) else None
92e554
-
92e554
-
92e554
 def get_clvm_devices(options):
92e554
 	devs = []
92e554
 	cmd = options["--vgs-path"] + " " +\