neil / rpms / python-blivet

Forked from rpms/python-blivet a year ago
Clone

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

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