[PATCH rhel7-alpha2-branch 01/16] iscsi: no discovery on each target login (#752066)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Don't redo discovery each time when logging to a node.
Skip it if already logged to a node on the same address.
Instead, store the node list on the first discovery. Next discovery
would rewrite information about credentials used for previous login
stored by iscsilib's setAuth() (and accessed later by getAuth()) in
database in /var/lib/iscsi/nodes.

This is port of following commits from rhel6-branch:
commit cc8cfdde3fb7bae7152075504674f6d0a7195fe4
commit dda2355ca424eb97fbca0a144260c9e9cb151a3a

The patches also consolidate code in iscsi.py (UI and ks case). Similar
consolidation has been already done on master with
commit 06ffb569eb417263b2936dfe97969e3d36653e39, but it went
a bit further (removal of addTarget function and moving its code
to kickstart.py) with outlook of removing some limits of kickstart
iscsi setup. I am keeping only the consolidation from rhel6-branch which
goes just to the present functionality. I prefer to have the
same code for rhel6-branch and master and to do the next step
- moving what remained of addTarget after consolidation into kickstart
so for finer ks command options - when the options will actually
be added (which would probably happen on rhel6-branch first anyway).
---
 pyanaconda/kickstart.py       |   30 ++--------
 pyanaconda/partIntfHelpers.py |    8 +--
 pyanaconda/storage/iscsi.py   |  138 +++++++++++++++++++++++++++++++---------
 3 files changed, 115 insertions(+), 61 deletions(-)

diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
index eed6470..ef9bd45 100644
--- a/pyanaconda/kickstart.py
+++ b/pyanaconda/kickstart.py
@@ -460,35 +460,15 @@ class IgnoreDisk(commands.ignoredisk.RHEL6_IgnoreDisk):
         return retval
 
 class Iscsi(commands.iscsi.F10_Iscsi):
-    class Login(object):
-        def __init__(self, iscsi_obj, tg_data):
-            self.iscsi_obj = iscsi_obj
-            self.tg_data = tg_data
-
-        def login(self, node):
-            if self.tg_data.target and self.tg_data.target != node.name:
-                log.debug("kickstart: skipping logging to iscsi node '%s'" %
-                          node.name)
-                return False
-            (rc, _) = self.iscsi_obj.log_into_node(
-                node, self.tg_data.user, self.tg_data.password,
-                self.tg_data.user_in, self.tg_data.password_in)
-            return rc
-
     def parse(self, args):
         tg = commands.iscsi.F10_Iscsi.parse(self, args)
 
         try:
-            iscsi_obj = storage.iscsi.iscsi()
-            discovered_nodes = iscsi_obj.discover(
-                tg.ipaddr, tg.port, tg.user, tg.password,
-                tg.user_in, tg.password_in)
-            login = self.Login(iscsi_obj, tg)
-            logged_into_nodes = filter(login.login, discovered_nodes)
-            if len(logged_into_nodes) < 1:
-                msg = _("Could not log into any iSCSI nodes at the portal.")
-                raise KickstartValueError, formatErrorMsg(self.lineno,
-                                                          msg=msg)
+            storage.iscsi.iscsi().addTarget(tg.ipaddr, tg.port, tg.user,
+                                            tg.password, tg.user_in,
+                                            tg.password_in,
+                                            target=tg.target)
+            log.info("added iscsi target: %s" %(tg.ipaddr,))
         except (IOError, ValueError) as e:
             raise KickstartValueError, formatErrorMsg(self.lineno,
                                                       msg=str(e))
diff --git a/pyanaconda/partIntfHelpers.py b/pyanaconda/partIntfHelpers.py
index 6dbfad9..30de3c1 100644
--- a/pyanaconda/partIntfHelpers.py
+++ b/pyanaconda/partIntfHelpers.py
@@ -463,14 +463,12 @@ def drive_iscsi_addition(anaconda, wizard):
                 discovery_dict = wizard.get_discovery_dict()
                 discovery_dict["intf"] = anaconda.intf
                 found_nodes = anaconda.storage.iscsi.discover(**discovery_dict)
-                map(lambda node: log.debug("discovered iSCSI node: %s" % node.name),
-                    found_nodes)
                 step = STEP_NODES
             elif step == STEP_NODES:
-                if len(found_nodes) < 1:
-                    log.debug("iscsi: no new iscsi nodes discovered")
+                if not found_nodes:
+                    log.debug("iscsi: no iSCSI nodes to log in")
                     anaconda.intf.messageWindow(_("iSCSI Nodes"), 
-                                                _("No new iSCSI nodes discovered"))
+                                                _("No iSCSI nodes to log in"))
                     break
                 (rc, selected_nodes) = wizard.display_nodes_dialog(found_nodes)
                 if not rc or len(selected_nodes) == 0:
diff --git a/pyanaconda/storage/iscsi.py b/pyanaconda/storage/iscsi.py
index a734e5e..cef1d73 100644
--- a/pyanaconda/storage/iscsi.py
+++ b/pyanaconda/storage/iscsi.py
@@ -28,6 +28,7 @@ import shutil
 import time
 import hashlib
 import random
+import itertools
 log = logging.getLogger("anaconda")
 
 import gettext
@@ -80,7 +81,7 @@ class iscsi(object):
         This class will automatically discover and login to iBFT (or
         other firmware) configured iscsi devices when the startup() method
         gets called. It can also be used to manually configure iscsi devices
-        through the discover() and log_into_node() methods.
+        through the addTarget() method.
 
         As this class needs to make sure certain things like starting iscsid
         and logging in to firmware discovered disks only happens once
@@ -89,8 +90,9 @@ class iscsi(object):
     """
 
     def __init__(self):
-        # This list contains all nodes
-        self.nodes = []
+        # Dictionary of discovered targets containing list of (node,
+        # logged_in) tuples.
+        self.discovered_targets = {}
         # This list contains nodes discovered through iBFT (or other firmware)
         self.ibftNodes = []
         self._initiator = ""
@@ -124,6 +126,30 @@ class iscsi(object):
 
     initiator = property(_getInitiator, _setInitiator)
 
+    def active_nodes(self, target=None):
+        """Nodes logged in to"""
+        if target and target in self.discovered_targets:
+            return [node for (node, logged_in) in
+                    self.discovered_targets[target]
+                    if logged_in]
+        else:
+            return [node for (node, logged_in) in
+                    itertools.chain(*self.discovered_targets.values())
+                    if logged_in] + self.ibftNodes
+
+    def _mark_node_active(self, node, active=True):
+        """Mark node as one logged in to
+
+           Returns False if not found
+        """
+        for target_nodes in self.discovered_targets.values():
+            for nodeinfo in target_nodes:
+                if nodeinfo[0] is node:
+                    nodeinfo[1] = active
+                    return True
+        return False
+
+
     def _startIBFT(self, intf = None):
         if not flags.ibft:
             return
@@ -131,14 +157,15 @@ class iscsi(object):
         try:
             found_nodes = libiscsi.discover_firmware()
         except Exception:
+            log.info("iscsi: No IBFT info found.");
             # an exception here means there is no ibft firmware, just return
             return
 
         for node in found_nodes:
             try:
                 node.login()
-                log.info("iscsi._startIBFT logged in to %s %s %s" % (node.name, node.address, node.port))
-                self.nodes.append(node)
+                log.info("iscsi IBFT: logged into %s at %s:%s through %s" % (
+                    node.name, node.address, node.port, node.iface))
                 self.ibftNodes.append(node)
             except IOError as e:
                 log.error("Could not log into ibft iscsi target %s: %s" %
@@ -218,35 +245,51 @@ class iscsi(object):
     def discover(self, ipaddr, port="3260", username=None, password=None,
                   r_username=None, r_password=None, intf=None):
         """
-        Discover iSCSI nodes on the target.
+        Discover iSCSI nodes on the target available for login.
+
+        If we are logged in a node discovered for specified target
+        do not do the discovery again as it can corrupt credentials
+        stored for the node (setAuth and getAuth are using database
+        in /var/lib/iscsi/nodes which is filled by discovery). Just
+        return nodes obtained and stored in the first discovery
+        instead.
 
-        Returns list of new found nodes.
+        Returns list of nodes user can log in.
         """
         authinfo = None
-        found = 0
-        logged_in = 0
 
         if not has_iscsi():
             raise IOError, _("iSCSI not available")
         if self._initiator == "":
             raise ValueError, _("No initiator name set")
 
-        if username or password or r_username or r_password:
-            # Note may raise a ValueError
-            authinfo = libiscsi.chapAuthInfo(username=username,
-                                             password=password,
-                                             reverse_username=r_username,
-                                             reverse_password=r_password)
-        self.startup(intf)
-
-        # Note may raise an IOError
-        found_nodes = libiscsi.discover_sendtargets(address=ipaddr,
-                                                    port=int(port),
-                                                    authinfo=authinfo)
-        if found_nodes is None:
-            return []
+        if self.active_nodes((ipaddr, port)):
+            log.debug("iSCSI: skipping discovery of %s:%s due to active nodes" %
+                      (ipaddr, port))
+        else:
+            if username or password or r_username or r_password:
+                # Note may raise a ValueError
+                authinfo = libiscsi.chapAuthInfo(username=username,
+                                                 password=password,
+                                                 reverse_username=r_username,
+                                                 reverse_password=r_password)
+            self.startup(intf)
+
+            # Note may raise an IOError
+            found_nodes = libiscsi.discover_sendtargets(address=ipaddr,
+                                                        port=int(port),
+                                                        authinfo=authinfo)
+            if found_nodes is None:
+                return None
+            self.discovered_targets[(ipaddr, port)] = []
+            for node in found_nodes:
+                self.discovered_targets[(ipaddr, port)].append([node, False])
+                log.debug("discovered iSCSI node: %s" % node.name)
+
         # only return the nodes we are not logged into yet
-        return [n for n in found_nodes if n not in self.nodes]
+        return [node for (node, logged_in) in
+                self.discovered_targets[(ipaddr, port)]
+                if not logged_in]
 
     def log_into_node(self, node, username=None, password=None,
                   r_username=None, r_password=None, intf=None):
@@ -270,10 +313,10 @@ class iscsi(object):
             node.setAuth(authinfo)
             node.login()
             rc = True
-            log.info("iSCSI: logged into %s %s:%s" % (node.name,
-                                                      node.address,
-                                                      node.port))
-            self.nodes.append(node)
+            log.info("iSCSI: logged into %s at %s:%s through %s" % (
+                    node.name, node.address, node.port, node.iface))
+            if not self._mark_node_active(node):
+                log.error("iSCSI: node not found among discovered")
         except (IOError, ValueError) as e:
             msg = str(e)
             log.warning("iSCSI: could not log into %s: %s" % (node.name, msg))
@@ -282,11 +325,44 @@ class iscsi(object):
 
         return (rc, msg)
 
+    # NOTE: the same credentials are used for discovery and login
+    #       (unlike in UI)
+    def addTarget(self, ipaddr, port="3260", user=None, pw=None,
+                  user_in=None, pw_in=None, intf=None, target=None):
+        found = 0
+        logged_in = 0
+
+        found_nodes = self.discover(ipaddr, port, user, pw, user_in, pw_in,
+                                    intf)
+        if found_nodes == None:
+            raise IOError, _("No iSCSI nodes discovered")
+
+        for node in found_nodes:
+            if target and target != node.name:
+                log.debug("iscsi: skipping logging to iscsi node '%s'" %
+                          node.name)
+                continue
+
+            found = found + 1
+
+            (rc, msg) = self.log_into_node(node, user, pw, user_in, pw_in,
+                                           intf)
+            if rc:
+                logged_in = logged_in +1
+
+        if found == 0:
+            raise IOError, _("No new iSCSI nodes discovered")
+
+        if logged_in == 0:
+            raise IOError, _("Could not log in to any of the discovered nodes")
+
+        self.stabilize(intf)
+
     def writeKS(self, f):
         if not self.initiatorSet:
             return
         f.write("iscsiname %s\n" %(self.initiator,))
-        for n in self.nodes:
+        for n in self.active_nodes():
             f.write("iscsi --ipaddr %s --port %s --target %s" %
                     (n.address, n.port, n.name))
             auth = n.getAuth()
@@ -305,7 +381,7 @@ class iscsi(object):
 
         # set iscsi nodes to autostart
         root = storage.rootDevice
-        for node in self.nodes:
+        for node in self.active_nodes():
             autostart = True
             disks = self.getNodeDisks(node, storage)
             for disk in disks:
@@ -330,7 +406,7 @@ class iscsi(object):
                             symlinks=True)
 
     def getNode(self, name, address, port):
-        for node in self.nodes:
+        for node in self.active_nodes():
             if node.name == name and node.address == address and \
                node.port == int(port):
                 return node
-- 
1.7.4

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list


[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux