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