Tomas Bzatek 1618b0
From 63da3cb8a40500c889c8faa4326f81d16997a3c8 Mon Sep 17 00:00:00 2001
Tomas Bzatek 1618b0
From: Tomas Bzatek <tbzatek@redhat.com>
Tomas Bzatek 1618b0
Date: Mon, 27 Nov 2023 18:55:55 +0100
Tomas Bzatek 1618b0
Subject: [PATCH] nvme: Retrieve HostNQN from a first active fabrics connection
Tomas Bzatek 1618b0
Tomas Bzatek 1618b0
When no /etc/hostnqn exists, look for any active NVMe over Fabrics
Tomas Bzatek 1618b0
connections and take the values from a first one, rather than
Tomas Bzatek 1618b0
generating new ones.
Tomas Bzatek 1618b0
---
Tomas Bzatek 1618b0
 blivet/nvme.py | 21 +++++++++++++++++++++
Tomas Bzatek 1618b0
 1 file changed, 21 insertions(+)
Tomas Bzatek 1618b0
Tomas Bzatek 1618b0
diff --git a/blivet/nvme.py b/blivet/nvme.py
Tomas Bzatek 1618b0
index 5ac41cffa..2e4686e68 100644
Tomas Bzatek 1618b0
--- a/blivet/nvme.py
Tomas Bzatek 1618b0
+++ b/blivet/nvme.py
Tomas Bzatek 1618b0
@@ -18,6 +18,7 @@
Tomas Bzatek 1618b0
 #
Tomas Bzatek 1618b0
 
Tomas Bzatek 1618b0
 import os
Tomas Bzatek 1618b0
+import glob
Tomas Bzatek 1618b0
 
Tomas Bzatek 1618b0
 from . import errors
Tomas Bzatek 1618b0
 
Tomas Bzatek 1618b0
@@ -54,6 +55,22 @@ def __call__(self):
Tomas Bzatek 1618b0
     def __deepcopy__(self, memo_dict):  # pylint: disable=unused-argument
Tomas Bzatek 1618b0
         return self
Tomas Bzatek 1618b0
 
Tomas Bzatek 1618b0
+    def _retrieve_fabrics_hostnqn(self):
Tomas Bzatek 1618b0
+        for d in glob.glob('/sys/class/nvme-fabrics/ctl/nvme*/'):
Tomas Bzatek 1618b0
+            try:
Tomas Bzatek 1618b0
+                # invalidate old values
Tomas Bzatek 1618b0
+                self._hostnqn = None
Tomas Bzatek 1618b0
+                self._hostid = None
Tomas Bzatek 1618b0
+                # read from sysfs
Tomas Bzatek 1618b0
+                with open(os.path.join(d, 'hostnqn')) as f:
Tomas Bzatek 1618b0
+                    self._hostnqn = f.readline().strip()
Tomas Bzatek 1618b0
+                with open(os.path.join(d, 'hostid')) as f:
Tomas Bzatek 1618b0
+                    self._hostid = f.readline().strip()
Tomas Bzatek 1618b0
+                if self._hostnqn:
Tomas Bzatek 1618b0
+                    break
Tomas Bzatek 1618b0
+            except Exception:  # pylint: disable=broad-except
Tomas Bzatek 1618b0
+                pass
Tomas Bzatek 1618b0
+
Tomas Bzatek 1618b0
     def startup(self):
Tomas Bzatek 1618b0
         if self.started:
Tomas Bzatek 1618b0
             return
Tomas Bzatek 1618b0
@@ -61,6 +78,10 @@ def startup(self):
Tomas Bzatek 1618b0
         self._hostnqn = blockdev.nvme_get_host_nqn()
Tomas Bzatek 1618b0
         self._hostid = blockdev.nvme_get_host_id()
Tomas Bzatek 1618b0
         if not self._hostnqn:
Tomas Bzatek 1618b0
+            # see if there are any active fabrics connections and take their values over
Tomas Bzatek 1618b0
+            self._retrieve_fabrics_hostnqn()
Tomas Bzatek 1618b0
+        if not self._hostnqn:
Tomas Bzatek 1618b0
+            # generate new values
Tomas Bzatek 1618b0
             self._hostnqn = blockdev.nvme_generate_host_nqn()
Tomas Bzatek 1618b0
         if not self._hostnqn:
Tomas Bzatek 1618b0
             raise errors.NVMeError("Failed to generate HostNQN")