Coverage for libs/sm/core/libiscsi.py: 0%
40 statements
« prev ^ index » next coverage.py v7.3.2, created at 2026-07-10 07:59 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2026-07-10 07:59 +0000
1# Compatibility layer providing sm-core-libs methods in terms of sm-libs
2# as much as possible
3import re
5from sm.core import iscsi
6from sm.core import util
7from sm.core import f_exceptions
9# Import specific functions to satisfy callers using this name
10from sm.core.iscsi import parse_IP_port, discovery, get_iscsi_interfaces
12# COMPATIBILITY FUNCTIONS BELOW
13# =============================
14# Instead of importing these functions, provide methods which match the
15# sm-core-libs interface (which is different, but with the same function names)
17def login(portal, target, username, password, uuid, rescan=False):
18 """This is a slightly revised version of iscsi.login
20 We are trying to stick as close as possible to the original one but fixing
21 or improving whatever can be improved, in order to later fix it to the
22 original one and start using that one both for the transport code and
23 the old SM code.
25 For example, 'uuid' is a new addition in order to be able to automatically
26 refcount in here, instead of relying on the caller to do that.
27 """
29 if username != "" and password != "":
30 iscsi.set_chap_settings(portal, target, username, password, "", "")
33 refcount = 0
34 legacy_refcount = 0
36 portal_target_ref = iscsi.get_portal_target_ref(portal, target)
38 # Increment portal/target ref count
39 refcount = util._incr_iscsiSR_refcount(portal_target_ref, uuid)
41 if refcount == 1:
42 cmd = ["iscsiadm", "-m", "node", "-p", portal, "-T", target, "-l"]
43 try:
44 failuremessage = "Failed to login to target."
45 (stdout, stderr) = iscsi.exn_on_failure(cmd, failuremessage)
46 # Increment legacy refcounter
47 if uuid:
48 legacy_refcount = util._incr_iscsiSR_refcount(target, uuid)
49 except Exception as exc:
50 util.SMlog("Failed: {}".format(" ".join(cmd)),
51 ident="Transport")
52 # Rollback refcount if needed
53 if uuid:
54 # This should be in its own try/block and be chained
55 # with main exception below
56 util._decr_iscsiSR_refcount(portal_target_ref, uuid)
57 raise f_exceptions.XenError('ISCSILogin', reason=str(exc))
59 # Rescan if requested and more than one refcount of either form
60 if rescan and (refcount > 1 or legacy_refcount > 1):
61 util.SMlog("Session already logged in for {}, rescan requested".
62 format(target))
63 iscsi.rescan_target(portal, target)
65def logout(portal, target, uuid):
66 """Modified version of iscsi.py:logout to handle refcounting
68 Given the nature of this refcounting, it is not possible to specifically
69 logout from one ip/iqn pair without breaking the refcounting.
70 For this reason, this version does not accept a specific ip and the
71 parameter 'all'.
72 """
73 util.SMlog("decrease logout refcount of {} ({})".
74 format(target, uuid),
75 ident="Transport")
77 portal_target_ref = iscsi.get_portal_target_ref(portal, target)
79 if util._decr_iscsiSR_refcount(portal_target_ref, uuid):
80 # still logged in
81 return
83 # Decrement the legacy refcount
84 if not util._decr_iscsiSR_refcount(target, uuid):
85 try:
86 cmd = ["iscsiadm", "-m", "node", "-T", target, "-u"]
87 failuremessage = "Failed to log out of target"
88 util.SMlog("logging out {} for {}".
89 format(target, uuid),
90 ident="Transport")
91 (_, _) = iscsi.exn_on_failure(cmd, failuremessage)
92 except Exception as exc:
93 raise f_exceptions.XenError('ISCSILogout', reason=str(exc))