neil / rpms / python-blivet

Forked from rpms/python-blivet a year ago
Clone

Blame SOURCES/0025-Add-support-for-NPIV-enabled-zFCP-devices.patch

d891b6
From 35643156c511c8120f2d562c1664a3c7a5a48cfb Mon Sep 17 00:00:00 2001
d891b6
From: Jan Stodola <jstodola@redhat.com>
d891b6
Date: Thu, 28 Oct 2021 21:17:25 +0200
d891b6
Subject: [PATCH 1/8] Fix removing zFCP SCSI devices
d891b6
d891b6
Values parsed from /proc/scsi/scsi were not correctly used to assemble
d891b6
paths to SCSI devices.
d891b6
d891b6
For example:
d891b6
/sys/bus/scsi/devices/0:0:00:00/
d891b6
was incorrectly accessed instead of:
d891b6
/sys/bus/scsi/devices/0:0:0:0/
d891b6
d891b6
Switch to a more reliable way of listing the available SCSI devices.
d891b6
d891b6
Related: rhbz#1937030
d891b6
---
d891b6
 blivet/zfcp.py | 17 ++++-------------
d891b6
 1 file changed, 4 insertions(+), 13 deletions(-)
d891b6
d891b6
diff --git a/blivet/zfcp.py b/blivet/zfcp.py
d891b6
index 93af5419..3747290e 100644
d891b6
--- a/blivet/zfcp.py
d891b6
+++ b/blivet/zfcp.py
d891b6
@@ -20,6 +20,7 @@
d891b6
 #
d891b6
 
d891b6
 import os
d891b6
+import re
d891b6
 from . import udev
d891b6
 from . import util
d891b6
 from .i18n import _
d891b6
@@ -167,20 +168,10 @@ class ZFCPDevice:
d891b6
         return True
d891b6
 
d891b6
     def offline_scsi_device(self):
d891b6
-        f = open("/proc/scsi/scsi", "r")
d891b6
-        lines = f.readlines()
d891b6
-        f.close()
d891b6
-        # alternatively iterate over /sys/bus/scsi/devices/*:0:*:*/
d891b6
+        # A list of existing SCSI devices in format Host:Bus:Target:Lun
d891b6
+        scsi_devices = [f for f in os.listdir(scsidevsysfs) if re.search(r'^[0-9]+:[0-9]+:[0-9]+:[0-9]+$', f)]
d891b6
 
d891b6
-        for line in lines:
d891b6
-            if not line.startswith("Host"):
d891b6
-                continue
d891b6
-            scsihost = line.split()
d891b6
-            host = scsihost[1]
d891b6
-            channel = "0"
d891b6
-            devid = scsihost[5]
d891b6
-            lun = scsihost[7]
d891b6
-            scsidev = "%s:%s:%s:%s" % (host[4:], channel, devid, lun)
d891b6
+        for scsidev in scsi_devices:
d891b6
             fcpsysfs = "%s/%s" % (scsidevsysfs, scsidev)
d891b6
             scsidel = "%s/%s/delete" % (scsidevsysfs, scsidev)
d891b6
 
d891b6
-- 
d891b6
2.34.3
d891b6
d891b6
d891b6
From 771cbf623030b1fa51ec193a2b5e2db229420a7a Mon Sep 17 00:00:00 2001
d891b6
From: Jan Stodola <jstodola@redhat.com>
d891b6
Date: Sun, 21 Nov 2021 02:47:45 +0100
d891b6
Subject: [PATCH 2/8] Refactor the ZFCPDevice class
d891b6
d891b6
Add a new base class for zFCP devices.
d891b6
Move code to the new base class.
d891b6
Improve documentation.
d891b6
d891b6
Related: rhbz#1937030
d891b6
---
d891b6
 blivet/zfcp.py | 131 +++++++++++++++++++++++++++++++++++--------------
d891b6
 1 file changed, 95 insertions(+), 36 deletions(-)
d891b6
d891b6
diff --git a/blivet/zfcp.py b/blivet/zfcp.py
d891b6
index 3747290e..4a50f65f 100644
d891b6
--- a/blivet/zfcp.py
d891b6
+++ b/blivet/zfcp.py
d891b6
@@ -21,6 +21,7 @@
d891b6
 
d891b6
 import os
d891b6
 import re
d891b6
+from abc import ABC
d891b6
 from . import udev
d891b6
 from . import util
d891b6
 from .i18n import _
d891b6
@@ -46,29 +47,19 @@ zfcpsysfs = "/sys/bus/ccw/drivers/zfcp"
d891b6
 scsidevsysfs = "/sys/bus/scsi/devices"
d891b6
 zfcpconf = "/etc/zfcp.conf"
d891b6
 
d891b6
+class ZFCPDeviceBase(ABC):
d891b6
+    """An abstract base class for zFCP storage devices."""
d891b6
 
d891b6
-class ZFCPDevice:
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, devnum, wwpn, fcplun):
d891b6
+    def __init__(self, devnum):
d891b6
         self.devnum = blockdev.s390.sanitize_dev_input(devnum)
d891b6
-        self.wwpn = blockdev.s390.zfcp_sanitize_wwpn_input(wwpn)
d891b6
-        self.fcplun = blockdev.s390.zfcp_sanitize_lun_input(fcplun)
d891b6
-
d891b6
         if not self.devnum:
d891b6
             raise ValueError(_("You have not specified a device number or the number is invalid"))
d891b6
-        if not self.wwpn:
d891b6
-            raise ValueError(_("You have not specified a worldwide port name or the name is invalid."))
d891b6
-        if not self.fcplun:
d891b6
-            raise ValueError(_("You have not specified a FCP LUN or the number is invalid."))
d891b6
+
d891b6
+        self._device_online_path = os.path.join(zfcpsysfs, self.devnum, "online")
d891b6
 
d891b6
     # Force str and unicode types in case any of the properties are unicode
d891b6
     def _to_string(self):
d891b6
-        return "%s %s %s" % (self.devnum, self.wwpn, self.fcplun)
d891b6
+        return str(self.devnum)
d891b6
 
d891b6
     def __str__(self):
d891b6
         return stringize(self._to_string())
d891b6
@@ -76,33 +67,97 @@ class ZFCPDevice:
d891b6
     def __unicode__(self):
d891b6
         return unicodeize(self._to_string())
d891b6
 
d891b6
-    def online_device(self):
d891b6
-        online = "%s/%s/online" % (zfcpsysfs, self.devnum)
d891b6
-        portadd = "%s/%s/port_add" % (zfcpsysfs, self.devnum)
d891b6
-        portdir = "%s/%s/%s" % (zfcpsysfs, self.devnum, self.wwpn)
d891b6
-        unitadd = "%s/unit_add" % (portdir)
d891b6
-        unitdir = "%s/%s" % (portdir, self.fcplun)
d891b6
-        failed = "%s/failed" % (unitdir)
d891b6
+    def _free_device(self):
d891b6
+        """Remove the device from the I/O ignore list to make it visible to the system.
d891b6
+
d891b6
+        :raises: ValueError if the device cannot be removed from the I/O ignore list
d891b6
+        """
d891b6
 
d891b6
-        if not os.path.exists(online):
d891b6
+        if not os.path.exists(self._device_online_path):
d891b6
             log.info("Freeing zFCP device %s", self.devnum)
d891b6
             util.run_program(["zfcp_cio_free", "-d", self.devnum])
d891b6
 
d891b6
-        if not os.path.exists(online):
d891b6
+        if not os.path.exists(self._device_online_path):
d891b6
             raise ValueError(_("zFCP device %s not found, not even in device ignore list.") %
d891b6
                              (self.devnum,))
d891b6
 
d891b6
+    def _set_zfcp_device_online(self):
d891b6
+        """Set the zFCP device online.
d891b6
+
d891b6
+        :raises: ValueError if the device cannot be set online
d891b6
+        """
d891b6
+
d891b6
         try:
d891b6
-            f = open(online, "r")
d891b6
-            devonline = f.readline().strip()
d891b6
-            f.close()
d891b6
+            with open(self._device_online_path) as f:
d891b6
+                devonline = f.readline().strip()
d891b6
             if devonline != "1":
d891b6
-                logged_write_line_to_file(online, "1")
d891b6
+                logged_write_line_to_file(self._device_online_path, "1")
d891b6
         except OSError as e:
d891b6
             raise ValueError(_("Could not set zFCP device %(devnum)s "
d891b6
                                "online (%(e)s).")
d891b6
                              % {'devnum': self.devnum, 'e': e})
d891b6
 
d891b6
+    def _set_zfcp_device_offline(self):
d891b6
+        """Set the zFCP device offline.
d891b6
+
d891b6
+        :raises: ValueError if the device cannot be set offline
d891b6
+        """
d891b6
+
d891b6
+        try:
d891b6
+            logged_write_line_to_file(self._device_online_path, "0")
d891b6
+        except OSError as e:
d891b6
+            raise ValueError(_("Could not set zFCP device %(devnum)s "
d891b6
+                               "offline (%(e)s).")
d891b6
+                             % {'devnum': self.devnum, 'e': e})
d891b6
+
d891b6
+    def online_device(self):
d891b6
+        """Initialize the device and make its storage block device(s) ready to use.
d891b6
+
d891b6
+        :returns: True if success
d891b6
+        :raises: ValueError if the device cannot be initialized
d891b6
+        """
d891b6
+
d891b6
+        self._free_device()
d891b6
+        self._set_zfcp_device_online()
d891b6
+        return True
d891b6
+
d891b6
+
d891b6
+class ZFCPDevice(ZFCPDeviceBase):
d891b6
+    """A class for zFCP devices that are not configured in NPIV mode. Such
d891b6
+    devices have to be specified by a device number, WWPN and LUN.
d891b6
+    """
d891b6
+
d891b6
+    def __init__(self, devnum, wwpn, fcplun):
d891b6
+        super().__init__(devnum)
d891b6
+
d891b6
+        self.wwpn = blockdev.s390.zfcp_sanitize_wwpn_input(wwpn)
d891b6
+        if not self.wwpn:
d891b6
+            raise ValueError(_("You have not specified a worldwide port name or the name is invalid."))
d891b6
+
d891b6
+        self.fcplun = blockdev.s390.zfcp_sanitize_lun_input(fcplun)
d891b6
+        if not self.fcplun:
d891b6
+            raise ValueError(_("You have not specified a FCP LUN or the number is invalid."))
d891b6
+
d891b6
+    # Force str and unicode types in case any of the properties are unicode
d891b6
+    def _to_string(self):
d891b6
+        return "{} {} {}".format(self.devnum, self.wwpn, self.fcplun)
d891b6
+
d891b6
+    def online_device(self):
d891b6
+        """Initialize the device and make its storage block device(s) ready to use.
d891b6
+
d891b6
+        :returns: True if success
d891b6
+        :raises: ValueError if the device cannot be initialized
d891b6
+        """
d891b6
+
d891b6
+        super().online_device()
d891b6
+
d891b6
+        portadd = "%s/%s/port_add" % (zfcpsysfs, self.devnum)
d891b6
+        portdir = "%s/%s/%s" % (zfcpsysfs, self.devnum, self.wwpn)
d891b6
+        unitadd = "%s/unit_add" % (portdir)
d891b6
+        unitdir = "%s/%s" % (portdir, self.fcplun)
d891b6
+        failed = "%s/failed" % (unitdir)
d891b6
+
d891b6
+        # create the sysfs directory for the WWPN/port
d891b6
         if not os.path.exists(portdir):
d891b6
             if os.path.exists(portadd):
d891b6
                 # older zfcp sysfs interface
d891b6
@@ -127,6 +182,7 @@ class ZFCPDevice:
d891b6
                          "there.", {'wwpn': self.wwpn,
d891b6
                                     'devnum': self.devnum})
d891b6
 
d891b6
+        # create the sysfs directory for the LUN/unit
d891b6
         if not os.path.exists(unitdir):
d891b6
             try:
d891b6
                 logged_write_line_to_file(unitadd, self.fcplun)
d891b6
@@ -144,6 +200,7 @@ class ZFCPDevice:
d891b6
                                  'wwpn': self.wwpn,
d891b6
                                  'devnum': self.devnum})
d891b6
 
d891b6
+        # check the state of the LUN
d891b6
         fail = "0"
d891b6
         try:
d891b6
             f = open(failed, "r")
d891b6
@@ -168,6 +225,8 @@ class ZFCPDevice:
d891b6
         return True
d891b6
 
d891b6
     def offline_scsi_device(self):
d891b6
+        """Find SCSI devices associated to the zFCP device and remove them from the system."""
d891b6
+
d891b6
         # A list of existing SCSI devices in format Host:Bus:Target:Lun
d891b6
         scsi_devices = [f for f in os.listdir(scsidevsysfs) if re.search(r'^[0-9]+:[0-9]+:[0-9]+:[0-9]+$', f)]
d891b6
 
d891b6
@@ -196,7 +255,8 @@ class ZFCPDevice:
d891b6
                     self.devnum, self.wwpn, self.fcplun)
d891b6
 
d891b6
     def offline_device(self):
d891b6
-        offline = "%s/%s/online" % (zfcpsysfs, self.devnum)
d891b6
+        """Remove the zFCP device from the system."""
d891b6
+
d891b6
         portadd = "%s/%s/port_add" % (zfcpsysfs, self.devnum)
d891b6
         portremove = "%s/%s/port_remove" % (zfcpsysfs, self.devnum)
d891b6
         unitremove = "%s/%s/%s/unit_remove" % (zfcpsysfs, self.devnum, self.wwpn)
d891b6
@@ -212,6 +272,7 @@ class ZFCPDevice:
d891b6
                              % {'devnum': self.devnum, 'wwpn': self.wwpn,
d891b6
                                  'fcplun': self.fcplun, 'e': e})
d891b6
 
d891b6
+        # remove the LUN
d891b6
         try:
d891b6
             logged_write_line_to_file(unitremove, self.fcplun)
d891b6
         except OSError as e:
d891b6
@@ -221,6 +282,7 @@ class ZFCPDevice:
d891b6
                              % {'fcplun': self.fcplun, 'wwpn': self.wwpn,
d891b6
                                  'devnum': self.devnum, 'e': e})
d891b6
 
d891b6
+        # remove the WWPN only if there are no other LUNs attached
d891b6
         if os.path.exists(portadd):
d891b6
             # only try to remove ports with older zfcp sysfs interface
d891b6
             for lun in os.listdir(portdir):
d891b6
@@ -238,6 +300,7 @@ class ZFCPDevice:
d891b6
                                  % {'wwpn': self.wwpn,
d891b6
                                      'devnum': self.devnum, 'e': e})
d891b6
 
d891b6
+        # check if there are other WWPNs existing for the zFCP device number
d891b6
         if os.path.exists(portadd):
d891b6
             # older zfcp sysfs interface
d891b6
             for port in os.listdir(devdir):
d891b6
@@ -256,12 +319,8 @@ class ZFCPDevice:
d891b6
                          self.devnum, luns[0])
d891b6
                 return True
d891b6
 
d891b6
-        try:
d891b6
-            logged_write_line_to_file(offline, "0")
d891b6
-        except OSError as e:
d891b6
-            raise ValueError(_("Could not set zFCP device %(devnum)s "
d891b6
-                               "offline (%(e)s).")
d891b6
-                             % {'devnum': self.devnum, 'e': e})
d891b6
+        # no other WWPNs/LUNs exists for this device number, it's safe to bring it offline
d891b6
+        self._set_zfcp_device_offline()
d891b6
 
d891b6
         return True
d891b6
 
d891b6
-- 
d891b6
2.34.3
d891b6
d891b6
d891b6
From 2dc44c00f170d64458a7c89abc91cda61af8387f Mon Sep 17 00:00:00 2001
d891b6
From: Jan Stodola <jstodola@redhat.com>
d891b6
Date: Sun, 21 Nov 2021 02:35:05 +0100
d891b6
Subject: [PATCH 3/8] Move offline_scsi_device() to the base class
d891b6
d891b6
Related: rhbz#1937030
d891b6
---
d891b6
 blivet/zfcp.py | 74 ++++++++++++++++++++++++++++++--------------------
d891b6
 1 file changed, 44 insertions(+), 30 deletions(-)
d891b6
d891b6
diff --git a/blivet/zfcp.py b/blivet/zfcp.py
d891b6
index 4a50f65f..af8f841d 100644
d891b6
--- a/blivet/zfcp.py
d891b6
+++ b/blivet/zfcp.py
d891b6
@@ -110,6 +110,15 @@ class ZFCPDeviceBase(ABC):
d891b6
                                "offline (%(e)s).")
d891b6
                              % {'devnum': self.devnum, 'e': e})
d891b6
 
d891b6
+    def _is_scsi_associated_with_fcp(self, fcphbasysfs, _fcpwwpnsysfs, _fcplunsysfs):
d891b6
+        """Decide if the SCSI device with the provided SCSI attributes
d891b6
+        corresponds to the zFCP device.
d891b6
+
d891b6
+        :returns: True or False
d891b6
+        """
d891b6
+
d891b6
+        return fcphbasysfs == self.devnum
d891b6
+
d891b6
     def online_device(self):
d891b6
         """Initialize the device and make its storage block device(s) ready to use.
d891b6
 
d891b6
@@ -121,6 +130,30 @@ class ZFCPDeviceBase(ABC):
d891b6
         self._set_zfcp_device_online()
d891b6
         return True
d891b6
 
d891b6
+    def offline_scsi_device(self):
d891b6
+        """Find SCSI devices associated to the zFCP device and remove them from the system."""
d891b6
+
d891b6
+        # A list of existing SCSI devices in format Host:Bus:Target:Lun
d891b6
+        scsi_devices = [f for f in os.listdir(scsidevsysfs) if re.search(r'^[0-9]+:[0-9]+:[0-9]+:[0-9]+$', f)]
d891b6
+
d891b6
+        for scsidev in scsi_devices:
d891b6
+            fcpsysfs = os.path.join(scsidevsysfs, scsidev)
d891b6
+
d891b6
+            with open(os.path.join(fcpsysfs, "hba_id")) as f:
d891b6
+                fcphbasysfs = f.readline().strip()
d891b6
+            with open(os.path.join(fcpsysfs, "wwpn")) as f:
d891b6
+                fcpwwpnsysfs = f.readline().strip()
d891b6
+            with open(os.path.join(fcpsysfs, "fcp_lun")) as f:
d891b6
+                fcplunsysfs = f.readline().strip()
d891b6
+
d891b6
+            if self._is_scsi_associated_with_fcp(fcphbasysfs, fcpwwpnsysfs, fcplunsysfs):
d891b6
+                scsidel = os.path.join(scsidevsysfs, scsidev, "delete")
d891b6
+                logged_write_line_to_file(scsidel, "1")
d891b6
+                udev.settle()
d891b6
+                return
d891b6
+
d891b6
+        log.warning("No scsi device found to delete for zfcp %s", self)
d891b6
+
d891b6
 
d891b6
 class ZFCPDevice(ZFCPDeviceBase):
d891b6
     """A class for zFCP devices that are not configured in NPIV mode. Such
d891b6
@@ -142,6 +175,17 @@ class ZFCPDevice(ZFCPDeviceBase):
d891b6
     def _to_string(self):
d891b6
         return "{} {} {}".format(self.devnum, self.wwpn, self.fcplun)
d891b6
 
d891b6
+    def _is_scsi_associated_with_fcp(self, fcphbasysfs, fcpwwpnsysfs, fcplunsysfs):
d891b6
+        """Decide if the SCSI device with the provided SCSI attributes
d891b6
+        corresponds to the zFCP device.
d891b6
+
d891b6
+        :returns: True or False
d891b6
+        """
d891b6
+
d891b6
+        return (fcphbasysfs == self.devnum and
d891b6
+                fcpwwpnsysfs == self.wwpn and
d891b6
+                fcplunsysfs == self.fcplun)
d891b6
+
d891b6
     def online_device(self):
d891b6
         """Initialize the device and make its storage block device(s) ready to use.
d891b6
 
d891b6
@@ -224,36 +268,6 @@ class ZFCPDevice(ZFCPDeviceBase):
d891b6
 
d891b6
         return True
d891b6
 
d891b6
-    def offline_scsi_device(self):
d891b6
-        """Find SCSI devices associated to the zFCP device and remove them from the system."""
d891b6
-
d891b6
-        # A list of existing SCSI devices in format Host:Bus:Target:Lun
d891b6
-        scsi_devices = [f for f in os.listdir(scsidevsysfs) if re.search(r'^[0-9]+:[0-9]+:[0-9]+:[0-9]+$', f)]
d891b6
-
d891b6
-        for scsidev in scsi_devices:
d891b6
-            fcpsysfs = "%s/%s" % (scsidevsysfs, scsidev)
d891b6
-            scsidel = "%s/%s/delete" % (scsidevsysfs, scsidev)
d891b6
-
d891b6
-            f = open("%s/hba_id" % (fcpsysfs), "r")
d891b6
-            fcphbasysfs = f.readline().strip()
d891b6
-            f.close()
d891b6
-            f = open("%s/wwpn" % (fcpsysfs), "r")
d891b6
-            fcpwwpnsysfs = f.readline().strip()
d891b6
-            f.close()
d891b6
-            f = open("%s/fcp_lun" % (fcpsysfs), "r")
d891b6
-            fcplunsysfs = f.readline().strip()
d891b6
-            f.close()
d891b6
-
d891b6
-            if fcphbasysfs == self.devnum \
d891b6
-                    and fcpwwpnsysfs == self.wwpn \
d891b6
-                    and fcplunsysfs == self.fcplun:
d891b6
-                logged_write_line_to_file(scsidel, "1")
d891b6
-                udev.settle()
d891b6
-                return
d891b6
-
d891b6
-        log.warning("no scsi device found to delete for zfcp %s %s %s",
d891b6
-                    self.devnum, self.wwpn, self.fcplun)
d891b6
-
d891b6
     def offline_device(self):
d891b6
         """Remove the zFCP device from the system."""
d891b6
 
d891b6
-- 
d891b6
2.34.3
d891b6
d891b6
d891b6
From f194c6e591c3e409f227fd10d3e9923af91ea893 Mon Sep 17 00:00:00 2001
d891b6
From: Jan Stodola <jstodola@redhat.com>
d891b6
Date: Sat, 6 Nov 2021 21:27:52 +0100
d891b6
Subject: [PATCH 4/8] Allow to delete more than one SCSI device
d891b6
d891b6
NPIV zFCP devices can attach more than one SCSI device, so allow to
d891b6
delete them all. For non-NPIV devices it means possible slowdown, since
d891b6
all SCSI devices would now be checked.
d891b6
d891b6
Related: rhbz#1937030
d891b6
---
d891b6
 blivet/zfcp.py | 6 ++++--
d891b6
 1 file changed, 4 insertions(+), 2 deletions(-)
d891b6
d891b6
diff --git a/blivet/zfcp.py b/blivet/zfcp.py
d891b6
index af8f841d..3b3f623b 100644
d891b6
--- a/blivet/zfcp.py
d891b6
+++ b/blivet/zfcp.py
d891b6
@@ -136,6 +136,7 @@ class ZFCPDeviceBase(ABC):
d891b6
         # A list of existing SCSI devices in format Host:Bus:Target:Lun
d891b6
         scsi_devices = [f for f in os.listdir(scsidevsysfs) if re.search(r'^[0-9]+:[0-9]+:[0-9]+:[0-9]+$', f)]
d891b6
 
d891b6
+        scsi_device_found = False
d891b6
         for scsidev in scsi_devices:
d891b6
             fcpsysfs = os.path.join(scsidevsysfs, scsidev)
d891b6
 
d891b6
@@ -147,12 +148,13 @@ class ZFCPDeviceBase(ABC):
d891b6
                 fcplunsysfs = f.readline().strip()
d891b6
 
d891b6
             if self._is_scsi_associated_with_fcp(fcphbasysfs, fcpwwpnsysfs, fcplunsysfs):
d891b6
+                scsi_device_found = True
d891b6
                 scsidel = os.path.join(scsidevsysfs, scsidev, "delete")
d891b6
                 logged_write_line_to_file(scsidel, "1")
d891b6
                 udev.settle()
d891b6
-                return
d891b6
 
d891b6
-        log.warning("No scsi device found to delete for zfcp %s", self)
d891b6
+        if not scsi_device_found:
d891b6
+            log.warning("No scsi device found to delete for zfcp %s", self)
d891b6
 
d891b6
 
d891b6
 class ZFCPDevice(ZFCPDeviceBase):
d891b6
-- 
d891b6
2.34.3
d891b6
d891b6
d891b6
From f6615be663434079b3f2a86be5db88b816d8a9e1 Mon Sep 17 00:00:00 2001
d891b6
From: Jan Stodola <jstodola@redhat.com>
d891b6
Date: Sun, 21 Nov 2021 03:01:02 +0100
d891b6
Subject: [PATCH 5/8] Add a function for reading the value of a kernel module
d891b6
 parameter
d891b6
d891b6
Related: rhbz#1937030
d891b6
---
d891b6
 blivet/util.py     | 33 +++++++++++++++++++++++++++++++++
d891b6
 tests/util_test.py | 11 +++++++++++
d891b6
 2 files changed, 44 insertions(+)
d891b6
d891b6
diff --git a/blivet/util.py b/blivet/util.py
d891b6
index af60210b..cbef65e0 100644
d891b6
--- a/blivet/util.py
d891b6
+++ b/blivet/util.py
d891b6
@@ -1131,3 +1131,36 @@ def detect_virt():
d891b6
         return False
d891b6
     else:
d891b6
         return vm[0] in ('qemu', 'kvm', 'xen')
d891b6
+
d891b6
+
d891b6
+def natural_sort_key(device):
d891b6
+    """ Sorting key for devices which makes sure partitions are sorted in natural
d891b6
+        way, e.g. 'sda1, sda2, ..., sda10' and not like 'sda1, sda10, sda2, ...'
d891b6
+    """
d891b6
+    if device.type == "partition" and device.parted_partition and device.disk:
d891b6
+        part_num = getattr(device.parted_partition, "number", -1)
d891b6
+        return [device.disk.name, part_num]
d891b6
+    else:
d891b6
+        return [device.name, 0]
d891b6
+
d891b6
+
d891b6
+def get_kernel_module_parameter(module, parameter):
d891b6
+    """ Return the value of a given kernel module parameter
d891b6
+
d891b6
+    :param str module: a kernel module
d891b6
+    :param str parameter: a module parameter
d891b6
+    :returns: the value of the given kernel module parameter or None
d891b6
+    :rtype: str
d891b6
+    """
d891b6
+
d891b6
+    value = None
d891b6
+
d891b6
+    parameter_path = os.path.join("/sys/module", module, "parameters", parameter)
d891b6
+    try:
d891b6
+        with open(parameter_path) as f:
d891b6
+            value = f.read().strip()
d891b6
+    except IOError as e:
d891b6
+        log.warning("Couldn't get the value of the parameter '%s' from the kernel module '%s': %s",
d891b6
+                    parameter, module, str(e))
d891b6
+
d891b6
+    return value
d891b6
diff --git a/tests/util_test.py b/tests/util_test.py
d891b6
index b4f82c1b..805447c7 100644
d891b6
--- a/tests/util_test.py
d891b6
+++ b/tests/util_test.py
d891b6
@@ -182,3 +182,14 @@ class GetSysfsAttrTestCase(unittest.TestCase):
d891b6
             # the unicode replacement character (U+FFFD) should be used instead
d891b6
             model = util.get_sysfs_attr(sysfs, "model")
d891b6
             self.assertEqual(model, "test model\ufffd")
d891b6
+
d891b6
+
d891b6
+class GetKernelModuleParameterTestCase(unittest.TestCase):
d891b6
+
d891b6
+    def test_nonexisting_kernel_module(self):
d891b6
+        self.assertIsNone(util.get_kernel_module_parameter("unknown_module", "unknown_parameter"))
d891b6
+
d891b6
+    def test_get_kernel_module_parameter_value(self):
d891b6
+        with mock.patch('blivet.util.open', mock.mock_open(read_data='value\n')):
d891b6
+            value = util.get_kernel_module_parameter("module", "parameter")
d891b6
+        self.assertEqual(value, "value")
d891b6
-- 
d891b6
2.34.3
d891b6
d891b6
d891b6
From 17c99a2444ef750bdbf5b24665c5fd3c52e687d9 Mon Sep 17 00:00:00 2001
d891b6
From: Jan Stodola <jstodola@redhat.com>
d891b6
Date: Sun, 21 Nov 2021 03:01:46 +0100
d891b6
Subject: [PATCH 6/8] LUN and WWPN should not be used for NPIV zFCP devices
d891b6
d891b6
Log a warning if activating a zFCP device in NPIV mode and WWPN or
d891b6
LUN have been provided. They are superfluous for NPIV devices.
d891b6
d891b6
Related: rhbz#1937030
d891b6
---
d891b6
 blivet/zfcp.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++-
d891b6
 1 file changed, 57 insertions(+), 1 deletion(-)
d891b6
d891b6
diff --git a/blivet/zfcp.py b/blivet/zfcp.py
d891b6
index 3b3f623b..726e9364 100644
d891b6
--- a/blivet/zfcp.py
d891b6
+++ b/blivet/zfcp.py
d891b6
@@ -22,6 +22,7 @@
d891b6
 import os
d891b6
 import re
d891b6
 from abc import ABC
d891b6
+import glob
d891b6
 from . import udev
d891b6
 from . import util
d891b6
 from .i18n import _
d891b6
@@ -47,6 +48,55 @@ zfcpsysfs = "/sys/bus/ccw/drivers/zfcp"
d891b6
 scsidevsysfs = "/sys/bus/scsi/devices"
d891b6
 zfcpconf = "/etc/zfcp.conf"
d891b6
 
d891b6
+
d891b6
+def _is_lun_scan_allowed():
d891b6
+    """Return True if automatic LUN scanning is enabled by the kernel."""
d891b6
+
d891b6
+    allow_lun_scan = util.get_kernel_module_parameter("zfcp", "allow_lun_scan")
d891b6
+    return allow_lun_scan == "Y"
d891b6
+
d891b6
+
d891b6
+def _is_port_in_npiv_mode(device_id):
d891b6
+    """Return True if the device ID is configured in NPIV mode. See
d891b6
+    https://www.ibm.com/docs/en/linux-on-systems?topic=devices-use-npiv
d891b6
+    """
d891b6
+
d891b6
+    port_in_npiv_mode = False
d891b6
+    port_type_path = "/sys/bus/ccw/devices/{}/host*/fc_host/host*/port_type".format(device_id)
d891b6
+    port_type_paths = glob.glob(port_type_path)
d891b6
+    try:
d891b6
+        for filename in port_type_paths:
d891b6
+            with open(filename) as f:
d891b6
+                port_type = f.read()
d891b6
+            if re.search(r"(^|\s)NPIV(\s|$)", port_type):
d891b6
+                port_in_npiv_mode = True
d891b6
+    except OSError as e:
d891b6
+        log.warning("Couldn't read the port_type attribute of the %s device: %s", device_id, str(e))
d891b6
+        port_in_npiv_mode = False
d891b6
+
d891b6
+    return port_in_npiv_mode
d891b6
+
d891b6
+
d891b6
+def is_npiv_enabled(device_id):
d891b6
+    """Return True if the given zFCP device ID is configured and usable in
d891b6
+    NPIV (N_Port ID Virtualization) mode.
d891b6
+
d891b6
+    :returns: True or False
d891b6
+    """
d891b6
+
d891b6
+    # LUN scanning disabled by the kernel module prevents using the device in NPIV mode
d891b6
+    if not _is_lun_scan_allowed():
d891b6
+        log.warning("Automatic LUN scanning is disabled by the zfcp kernel module.")
d891b6
+        return False
d891b6
+
d891b6
+    # The port itself has to be configured in NPIV mode
d891b6
+    if not _is_port_in_npiv_mode(device_id):
d891b6
+        log.warning("The zFCP device %s is not configured in NPIV mode.", device_id)
d891b6
+        return False
d891b6
+
d891b6
+    return True
d891b6
+
d891b6
+
d891b6
 class ZFCPDeviceBase(ABC):
d891b6
     """An abstract base class for zFCP storage devices."""
d891b6
 
d891b6
@@ -203,6 +253,13 @@ class ZFCPDevice(ZFCPDeviceBase):
d891b6
         unitdir = "%s/%s" % (portdir, self.fcplun)
d891b6
         failed = "%s/failed" % (unitdir)
d891b6
 
d891b6
+        # Activating an NPIV enabled device using devnum, WWPN and LUN should still be possible
d891b6
+        # as this method was used as a workaround until the support for NPIV enabled devices has
d891b6
+        # been implemented. Just log a warning message and continue.
d891b6
+        if is_npiv_enabled(self.devnum):
d891b6
+            log.warning("zFCP device %s in NPIV mode brought online. All LUNs will be activated "
d891b6
+                        "automatically although WWPN and LUN have been provided.", self.devnum)
d891b6
+
d891b6
         # create the sysfs directory for the WWPN/port
d891b6
         if not os.path.exists(portdir):
d891b6
             if os.path.exists(portadd):
d891b6
@@ -327,7 +384,6 @@ class ZFCPDevice(ZFCPDeviceBase):
d891b6
                     return True
d891b6
         else:
d891b6
             # newer zfcp sysfs interface with auto port scan
d891b6
-            import glob
d891b6
             luns = glob.glob("%s/0x????????????????/0x????????????????"
d891b6
                              % (devdir,))
d891b6
             if len(luns) != 0:
d891b6
-- 
d891b6
2.34.3
d891b6
d891b6
d891b6
From a8f97bd0d74e3da9c18bd03d968f5d2f0c3ee46f Mon Sep 17 00:00:00 2001
d891b6
From: Jan Stodola <jstodola@redhat.com>
d891b6
Date: Sat, 6 Nov 2021 21:27:52 +0100
d891b6
Subject: [PATCH 7/8] Add new class for NPIV-enabled devices
d891b6
d891b6
Related: rhbz#1937030
d891b6
---
d891b6
 blivet/zfcp.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++---
d891b6
 1 file changed, 50 insertions(+), 3 deletions(-)
d891b6
d891b6
diff --git a/blivet/zfcp.py b/blivet/zfcp.py
d891b6
index 726e9364..e6c0e48a 100644
d891b6
--- a/blivet/zfcp.py
d891b6
+++ b/blivet/zfcp.py
d891b6
@@ -397,6 +397,44 @@ class ZFCPDevice(ZFCPDeviceBase):
d891b6
         return True
d891b6
 
d891b6
 
d891b6
+class ZFCPNPIVDevice(ZFCPDeviceBase):
d891b6
+    """Class for zFCP devices configured in NPIV mode. Only a zFCP device number is
d891b6
+    needed for such devices.
d891b6
+    """
d891b6
+
d891b6
+    def online_device(self):
d891b6
+        """Initialize the device and make its storage block device(s) ready to use.
d891b6
+
d891b6
+        :returns: True if success
d891b6
+        :raises: ValueError if the device cannot be initialized
d891b6
+        """
d891b6
+
d891b6
+        super().online_device()
d891b6
+
d891b6
+        if not is_npiv_enabled(self.devnum):
d891b6
+            raise ValueError(_("zFCP device %s cannot be used in NPIV mode.") % self)
d891b6
+
d891b6
+        return True
d891b6
+
d891b6
+    def offline_device(self):
d891b6
+        """Remove the zFCP device from the system.
d891b6
+
d891b6
+        :returns: True if success
d891b6
+        :raises: ValueError if the device cannot be brought offline
d891b6
+         """
d891b6
+
d891b6
+        try:
d891b6
+            self.offline_scsi_device()
d891b6
+        except OSError as e:
d891b6
+            raise ValueError(_("Could not correctly delete SCSI device of "
d891b6
+                               "zFCP %(zfcpdev)s (%(e)s).")
d891b6
+                             % {'zfcpdev': self, 'e': e})
d891b6
+
d891b6
+        self._set_zfcp_device_offline()
d891b6
+
d891b6
+        return True
d891b6
+
d891b6
+
d891b6
 class zFCP:
d891b6
 
d891b6
     """ ZFCP utility class.
d891b6
@@ -439,7 +477,12 @@ class zFCP:
d891b6
 
d891b6
             fields = line.split()
d891b6
 
d891b6
-            if len(fields) == 3:
d891b6
+            # NPIV enabled device
d891b6
+            if len(fields) == 1:
d891b6
+                devnum = fields[0]
d891b6
+                wwpn = None
d891b6
+                fcplun = None
d891b6
+            elif len(fields) == 3:
d891b6
                 devnum = fields[0]
d891b6
                 wwpn = fields[1]
d891b6
                 fcplun = fields[2]
d891b6
@@ -458,8 +501,12 @@ class zFCP:
d891b6
             except ValueError as e:
d891b6
                 log.warning("%s", str(e))
d891b6
 
d891b6
-    def add_fcp(self, devnum, wwpn, fcplun):
d891b6
-        d = ZFCPDevice(devnum, wwpn, fcplun)
d891b6
+    def add_fcp(self, devnum, wwpn=None, fcplun=None):
d891b6
+        if wwpn and fcplun:
d891b6
+            d = ZFCPDevice(devnum, wwpn, fcplun)
d891b6
+        else:
d891b6
+            d = ZFCPNPIVDevice(devnum)
d891b6
+
d891b6
         if d.online_device():
d891b6
             self.fcpdevs.add(d)
d891b6
 
d891b6
-- 
d891b6
2.34.3
d891b6
d891b6
d891b6
From 963822ff989c938e74d582216f4f7ded595eccc1 Mon Sep 17 00:00:00 2001
d891b6
From: Jan Stodola <jstodola@redhat.com>
d891b6
Date: Sat, 20 Nov 2021 23:12:43 +0100
d891b6
Subject: [PATCH 8/8] Generate correct dracut boot arguments for NPIV devices
d891b6
d891b6
NPIV enabled devices need only the device ID. WWPNs/LUNs are discovered
d891b6
automatically by the kernel module.
d891b6
d891b6
Resolves: rhbz#1937030
d891b6
---
d891b6
 blivet/devices/disk.py | 10 +++++++++-
d891b6
 1 file changed, 9 insertions(+), 1 deletion(-)
d891b6
d891b6
diff --git a/blivet/devices/disk.py b/blivet/devices/disk.py
d891b6
index 67a01ba6..36278507 100644
d891b6
--- a/blivet/devices/disk.py
d891b6
+++ b/blivet/devices/disk.py
d891b6
@@ -577,7 +577,15 @@ class ZFCPDiskDevice(DiskDevice):
d891b6
                   'lun': self.fcp_lun}
d891b6
 
d891b6
     def dracut_setup_args(self):
d891b6
-        return set(["rd.zfcp=%s,%s,%s" % (self.hba_id, self.wwpn, self.fcp_lun,)])
d891b6
+        from ..zfcp import is_npiv_enabled
d891b6
+
d891b6
+        # zFCP devices in NPIV mode need only the device ID
d891b6
+        if is_npiv_enabled(self.hba_id):
d891b6
+            dracut_args = set(["rd.zfcp=%s" % self.hba_id])
d891b6
+        else:
d891b6
+            dracut_args = set(["rd.zfcp=%s,%s,%s" % (self.hba_id, self.wwpn, self.fcp_lun,)])
d891b6
+
d891b6
+        return dracut_args
d891b6
 
d891b6
 
d891b6
 class DASDDevice(DiskDevice):
d891b6
-- 
d891b6
2.34.3
d891b6