def discover(self, ipaddr, port="3260", username=None, password=None,
                  r_username=None, r_password=None, intf=None):
        """
        Discover iSCSI nodes on the target.

        Returns list of new found nodes.
        """
        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)
        # only return the nodes we are not logged into yet
        return [n for n in found_nodes if n not in self.nodes]
Exemplo n.º 2
0
def _call_discover_targets(con_write, con_recv, ipaddr, port, authinfo):
    """ Function to separate iscsi :py:func:`libiscsi.discover_sendtargets` call to it's own process.

        The call must be started on special process because the :py:mod:`libiscsi`
        library is not thread safe. When thread is used or it's called on
        main thread the main thread will freeze.

        Also the discover is about four times slower (only for timeout)
        this is caused by signals which are delivered to bad (python)
        thread instead of C library thread.

        .. note::

            To transfer data to main process ``con_write`` (write only) is used.
            Pipe returns tuple (ok, data).

            ``con_recv`` should be immediately closed so that the reading end
            of the pipe is not used in the child process.

            * ``ok``: True if everything was ok
            * ``data``: Dictionary with :py:func:`libiscsi.node` parameters if ``ok`` was True.
              Exception if ``ok`` was False

        :param con_write: Pipe to the main process (write only)
        :type con_write: :py:func:`multiprocessing.Pipe`
        :param con_recv: Reading end of pipe from main process (close only)
        :type con_recv: :py:func:`multiprocessing.Pipe`
        :param str ipaddr: target IP address
        :param str port: target port
        :param authinfo: CHAP authentication data for node login
        :type authinfo: Object returned by :py:func:`libiscsi.chapAuthInfo` or None
    """

    # Close the reading end of the pipe in the child process
    con_recv.close()

    with con_write:
        try:
            found_nodes = libiscsi.discover_sendtargets(address=ipaddr,
                                                        port=int(port),
                                                        authinfo=authinfo)
            if found_nodes is None:
                found_nodes = []

            nodes = []

            # the node object is not pickable so it can't be send with pipe
            # TODO: change libiscsi.node to pickable object
            for node in found_nodes:
                nodes.append({'name': node.name,
                             'tpgt': node.tpgt,
                             'address': node.address,
                             'port': node.port,
                             'iface': node.iface})

            con_write.send((True, nodes))

        except Exception as ex: # pylint: disable=broad-except
            con_write.send((False, ex))
Exemplo n.º 3
0
    def discover(self,
                 ipaddr,
                 port="3260",
                 username=None,
                 password=None,
                 r_username=None,
                 r_password=None,
                 intf=None):
        """
        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 nodes user can log in.
        """
        authinfo = None

        if not has_iscsi():
            raise IOError, _("iSCSI not available")
        if self._initiator == "":
            raise ValueError, _("No initiator name set")

        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 [
            node
            for (node, logged_in) in self.discovered_targets[(ipaddr, port)]
            if not logged_in
        ]
Exemplo n.º 4
0
    def addTarget(self,
                  ipaddr,
                  port="3260",
                  user=None,
                  pw=None,
                  user_in=None,
                  pw_in=None,
                  intf=None):
        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 user or pw or user_in or pw_in:
            # Note may raise a ValueError
            authinfo = libiscsi.chapAuthInfo(username=user,
                                             password=pw,
                                             reverse_username=user_in,
                                             reverse_password=pw_in)
        self.startup(intf)

        # Note may raise an IOError
        found_nodes = libiscsi.discover_sendtargets(address=ipaddr,
                                                    port=int(port),
                                                    authinfo=authinfo)
        if found_nodes == None:
            raise IOError, _("No iSCSI nodes discovered")

        if intf:
            w = intf.waitWindow(_("Logging in to iSCSI nodes"),
                                _("Logging in to iSCSI nodes"))

        for node in found_nodes:
            # skip nodes we already have
            if node in self.nodes:
                continue

            found = found + 1
            try:
                if (authinfo):
                    node.setAuth(authinfo)
                node.login()
                log.info("iscsi.addTarget logged in to %s %s %s" %
                         (node.name, node.address, node.port))
                self.nodes.append(node)
                logged_in = logged_in + 1
            except IOError, e:
                log.warning(
                    "Could not log into discovered iscsi target %s: %s" %
                    (node.name, str(e)))
                # some nodes may require different credentials
                pass
Exemplo n.º 5
0
	def TargetLogin(self, addr, target_name):
		nodelist = libiscsi.discover_sendtargets(addr)
		if nodelist is not None :
			for node in nodelist:
				if node.name == target_name:
					nodelist[0].login()
					return '/dev/'+scandev.get_blockdev_by_targetname(nodelist[0].name)
			print 'Target %s not found!' % (target_name)
		else:
			print 'no node found!'
Exemplo n.º 6
0
    def addTarget(self, ipaddr, port="3260", user=None, pw=None,
                  user_in=None, pw_in=None, intf=None):
        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 user or pw or user_in or pw_in:
            # Note may raise a ValueError
            authinfo = libiscsi.chapAuthInfo(username=user, password=pw,
                                             reverse_username=user_in,
                                             reverse_password=pw_in)
        self.startup(intf)

        # Note may raise an IOError
        found_nodes = libiscsi.discover_sendtargets(address=ipaddr,
                                                    port=int(port),
                                                    authinfo=authinfo)
        if found_nodes == None:
            raise IOError, _("No iSCSI nodes discovered")

        if intf:
            w = intf.waitWindow(_("Logging in to iSCSI nodes"),
                                _("Logging in to iSCSI nodes"))

        for node in found_nodes:
            # skip nodes we already have
            if node in self.nodes:
                continue

            found = found + 1
            try:
                if (authinfo):
                    node.setAuth(authinfo)
                node.login()
                self.nodes.append(node)
                logged_in = logged_in + 1
            except:
                # some nodes may require different credentials
                pass

        if intf:
            w.pop()

        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")

        stabilize(intf)
Exemplo n.º 7
0
def _call_discover_targets(conn_pipe, ipaddr, port, authinfo):
    """ Function to separate iscsi :py:func:`libiscsi.discover_sendtargets` call to it's own process.

        The call must be started on special process because the :py:mod:`libiscsi`
        library is not thread safe. When thread is used or it's called on
        main thread the main thread will freeze.

        Also the discover is about four times slower (only for timeout)
        this is caused by signals which are delivered to bad (python)
        thread instead of C library thread.

        .. note::

            To transfer data to main process ``conn_pipe`` (write only) is used.
            Pipe returns tuple (ok, data).

            * ``ok``: True if everything was ok
            * ``data``: Dictionary with :py:func:`libiscsi.node` parameters if ``ok`` was True.
              Exception if ``ok`` was False

        :param conn_pipe: Pipe to the main process (write only)
        :type conn_pipe: :py:func:`multiprocessing.Pipe`
        :param str ipaddr: target IP address
        :param str port: target port
        :param authinfo: CHAP authentication data for node login
        :type authinfo: Object returned by :py:func:`libiscsi.chapAuthInfo` or None
    """
    try:
        found_nodes = libiscsi.discover_sendtargets(address=ipaddr,
                                                    port=int(port),
                                                    authinfo=authinfo)
        if found_nodes is None:
            found_nodes = []

    except IOError as ex:
        conn_pipe.send((False, ex))
        conn_pipe.close()
        return

    nodes = []

    # the node object is not pickable so it can't be send with pipe
    # TODO: change libiscsi.node to pickable object
    for node in found_nodes:
        nodes.append({
            'name': node.name,
            'tpgt': node.tpgt,
            'address': node.address,
            'port': node.port,
            'iface': node.iface
        })

    conn_pipe.send((True, nodes))
Exemplo n.º 8
0
	def GetChunkNode(self, name, addr, port=3260):
		try:
			nodelist = libiscsi.discover_sendtargets(addr, port)
		except IOError as ex:
			#This IOError is because there's no iscsi node is found now
			if str(ex).find('connection login retries (reopen_max) 5 exceeded')>-1:
				return None
			else:
				raise ex
		for node in nodelist:
			if name == node.name:
				return node
		return None
Exemplo n.º 9
0
    def discover(self, ipaddr, port="3260", username=None, password=None,
                 r_username=None, r_password=None):
        """
        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 nodes user can log in.
        """
        authinfo = None

        if not has_iscsi():
            raise IOError, _("iSCSI not available")
        if self._initiator == "":
            raise ValueError, _("No initiator name set")

        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()

            # Note may raise an IOError
            found_nodes = libiscsi.discover_sendtargets(address=ipaddr,
                                                        port=int(port),
                                                        authinfo=authinfo)
            if found_nodes is None:
                return []
            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 [node for (node, logged_in) in
                self.discovered_targets[(ipaddr, port)]
                if not logged_in]
Exemplo n.º 10
0
    def discover(self,
                 ipaddr,
                 port="3260",
                 username=None,
                 password=None,
                 r_username=None,
                 r_password=None,
                 intf=None):
        """
        Discover iSCSI nodes on the target.

        Returns list of new found nodes.
        """
        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)
        # only return the nodes we are not logged into yet
        return [n for n in found_nodes if n not in self.nodes]
Exemplo n.º 11
0
import os
import libiscsi
import scandev

# check the following functions
# libiscsi.discover_sendtargets  | iscsi discovery
# node.login()                   | iscsi login
# node.logout()                  | iscsi logout 
# scandev.get_blockdev_by_targetname()  | get block device name

#discover nodes from ipaddress '192.168.0.12'
nodelist = libiscsi.discover_sendtargets('192.168.0.12')
if nodelist is not None :
	print nodelist[0].address
	print nodelist[0].name
else:
	print 'no node found!'

#login node 0
nodelist[0].login()

#get the locak blockdevicename of this target
print scandev.get_blockdev_by_targetname(nodelist[0].name)

#logout
nodelist[0].logout()
Exemplo n.º 12
0
import os
import libiscsi
import scandev

# check the following functions
# libiscsi.discover_sendtargets  | iscsi discovery
# node.login()                   | iscsi login
# node.logout()                  | iscsi logout
# scandev.get_blockdev_by_targetname()  | get block device name

#discover nodes from ipaddress '192.168.0.12'
nodelist = libiscsi.discover_sendtargets('192.168.0.12')
if nodelist is not None:
    print nodelist[0].address
    print nodelist[0].name
else:
    print 'no node found!'

#login node 0
nodelist[0].login()

#get the locak blockdevicename of this target
print scandev.get_blockdev_by_targetname(nodelist[0].name)

#logout
nodelist[0].logout()
Exemplo n.º 13
0
	def TargetLogout(self, addr, target_name):
		nodelist = libiscsi.discover_sendtargets(addr)
		if nodelist is not None :
			for node in nodelist:
				if node.name == target_name:
					nodelist[0].logout()