neil / rpms / python-blivet

Forked from rpms/python-blivet a year ago
Clone
d891b6
From 5d54b2ede698d5084aa6c780295fcc9aafbfa357 Mon Sep 17 00:00:00 2001
d891b6
From: Vojtech Trefny <vtrefny@redhat.com>
d891b6
Date: Mon, 9 May 2022 13:38:50 +0200
d891b6
Subject: [PATCH] Add a very simple NVMe module
d891b6
d891b6
This covers only the basic functionallity needed by Anaconda right
d891b6
now: populating the config files in /etc/nvme and copying them to
d891b6
the installed system. The API for the NVMe singleton is based on
d891b6
the similar modules for iSCSI and FCoE.
d891b6
d891b6
Resolves: rhbz#2073008
d891b6
---
d891b6
 blivet/errors.py | 14 +++++++++
d891b6
 blivet/nvme.py   | 81 ++++++++++++++++++++++++++++++++++++++++++++++++
d891b6
 2 files changed, 95 insertions(+)
d891b6
 create mode 100644 blivet/nvme.py
d891b6
d891b6
diff --git a/blivet/errors.py b/blivet/errors.py
d891b6
index fd51283f..b16cf2c5 100644
d891b6
--- a/blivet/errors.py
d891b6
+++ b/blivet/errors.py
d891b6
@@ -307,3 +307,17 @@ class EventHandlingError(StorageError):
d891b6
 
d891b6
 class ThreadError(StorageError):
d891b6
     """ An error occurred in a non-main thread. """
d891b6
+
d891b6
+# other
d891b6
+
d891b6
+
d891b6
+class FCoEError(StorageError, OSError):
d891b6
+    pass
d891b6
+
d891b6
+
d891b6
+class ISCSIError(StorageError, OSError):
d891b6
+    pass
d891b6
+
d891b6
+
d891b6
+class NVMeError(StorageError, OSError):
d891b6
+    pass
d891b6
diff --git a/blivet/nvme.py b/blivet/nvme.py
d891b6
new file mode 100644
d891b6
index 00000000..17bead15
d891b6
--- /dev/null
d891b6
+++ b/blivet/nvme.py
d891b6
@@ -0,0 +1,81 @@
d891b6
+#
d891b6
+# nvme.py - NVMe class
d891b6
+#
d891b6
+# Copyright (C) 2022  Red Hat, Inc.  All rights reserved.
d891b6
+#
d891b6
+# This program is free software; you can redistribute it and/or modify
d891b6
+# it under the terms of the GNU General Public License as published by
d891b6
+# the Free Software Foundation; either version 2 of the License, or
d891b6
+# (at your option) any later version.
d891b6
+#
d891b6
+# This program is distributed in the hope that it will be useful,
d891b6
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
d891b6
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d891b6
+# GNU General Public License for more details.
d891b6
+#
d891b6
+# You should have received a copy of the GNU General Public License
d891b6
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
d891b6
+#
d891b6
+
d891b6
+import os
d891b6
+import shutil
d891b6
+
d891b6
+from . import errors
d891b6
+from . import util
d891b6
+
d891b6
+import logging
d891b6
+log = logging.getLogger("blivet")
d891b6
+
d891b6
+HOSTNQN_FILE = "/etc/nvme/hostnqn"
d891b6
+HOSTID_FILE = "/etc/nvme/hostid"
d891b6
+
d891b6
+
d891b6
+class NVMe(object):
d891b6
+    """ NVMe utility class.
d891b6
+
d891b6
+        .. warning::
d891b6
+            Since this is a singleton class, calling deepcopy() on the instance
d891b6
+            just returns ``self`` with no copy being created.
d891b6
+    """
d891b6
+
d891b6
+    def __init__(self):
d891b6
+        self.started = False
d891b6
+
d891b6
+    # So that users can write nvme() to get the singleton instance
d891b6
+    def __call__(self):
d891b6
+        return self
d891b6
+
d891b6
+    def __deepcopy__(self, memo_dict):  # pylint: disable=unused-argument
d891b6
+        return self
d891b6
+
d891b6
+    def startup(self):
d891b6
+        if self.started:
d891b6
+            return
d891b6
+
d891b6
+        rc, nqn = util.run_program_and_capture_output(["nvme", "gen-hostnqn"])
d891b6
+        if rc != 0:
d891b6
+            raise errors.NVMeError("Failed to generate hostnqn")
d891b6
+
d891b6
+        with open(HOSTNQN_FILE, "w") as f:
d891b6
+            f.write(nqn)
d891b6
+
d891b6
+        rc, hid = util.run_program_and_capture_output(["dmidecode", "-s", "system-uuid"])
d891b6
+        if rc != 0:
d891b6
+            raise errors.NVMeError("Failed to generate host ID")
d891b6
+
d891b6
+        with open(HOSTID_FILE, "w") as f:
d891b6
+            f.write(hid)
d891b6
+
d891b6
+        self.started = True
d891b6
+
d891b6
+    def write(self, root):  # pylint: disable=unused-argument
d891b6
+        # copy the hostnqn and hostid files
d891b6
+        if not os.path.isdir(root + "/etc/nvme"):
d891b6
+            os.makedirs(root + "/etc/nvme", 0o755)
d891b6
+        shutil.copyfile(HOSTNQN_FILE, root + HOSTNQN_FILE)
d891b6
+        shutil.copyfile(HOSTID_FILE, root + HOSTID_FILE)
d891b6
+
d891b6
+
d891b6
+# Create nvme singleton
d891b6
+nvme = NVMe()
d891b6
+""" An instance of :class:`NVMe` """
d891b6
-- 
d891b6
2.34.3
d891b6