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

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