neil / rpms / python-blivet

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