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