Coverage for sm/core/libiscsi : 24%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"""Temporary module to mock libiscsi"""
"""Executes via util.doexec the command specified. If the return code is non-zero, raises an ISCSIError with the given message"""
# _lock = None # if os.path.basename(cmd[0]) == 'iscsiadm': # _lock = lock.Lock(LOCK_TYPE_RUNNING, 'iscsiadm') # _lock.acquire()
# if _lock <> None and _lock.held(): # _lock.release() else:
interface_array=("default",)): """Run iscsiadm in discovery mode to obtain a list of the TargetIQNs available on the specified target and port. Returns a list of triples - the portal (ip:port), the tpgt (target portal group tag) and the target name"""
# Save configuration of root LUN nodes and restore after discovery # otherwise when we do a discovery on the same filer as is hosting # our root disk we'll reset the config of the root LUNs # save_rootdisk_nodes()
if ':' in target: targetstring = "[%s]:%s" % (target, str(port)) else: targetstring = "%s:%s" % (target, str(port)) cmd_base = ["-t", "st", "-p", targetstring] for interface in interface_array: cmd_base.append("-I") cmd_base.append(interface) cmd_disc = ["iscsiadm", "-m", "discovery"] + cmd_base cmd_discdb = ["iscsiadm", "-m", "discoverydb"] + cmd_base auth_args = ["-n", "discovery.sendtargets.auth.authmethod", "-v", "CHAP", "-n", "discovery.sendtargets.auth.username", "-v", chapuser, "-n", "discovery.sendtargets.auth.password", "-v", chappass] fail_msg = "Discovery failed. Check target settings and " \ "username/password (if applicable)" try: if chapuser!="" and chappass!="": # Unfortunately older version of iscsiadm won't fail on new modes # it doesn't recognize (rc=0), so we have to test it out support_discdb = "discoverydb" in util.pread2(["iscsiadm", "-h"]) if support_discdb: exn_on_failure(cmd_discdb + ["-o", "new"], fail_msg) exn_on_failure(cmd_discdb + ["-o", "update"] + auth_args, fail_msg) cmd = cmd_discdb + ["--discover"] else: cmd = cmd_disc + ["-X", chapuser, "-x", chappass] else: cmd = cmd_disc (stdout,stderr) = exn_on_failure(cmd, fail_msg) except Exception as exc: # restore_rootdisk_nodes() raise f_exceptions.XenError('ISCSILogin', message=exc.message) # else: # restore_rootdisk_nodes()
return parse_node_output(stdout, target_iqn)
"""Sets the username and password on the session identified by the portal/targetIQN combination"""
failuremessage = "Failed to set CHAP settings" cmd = ["iscsiadm", "-m", "node", "-p", portal, "-T", targetIQN, "--op", "update", "-n", "node.session.auth.authmethod","-v", "CHAP"] (stdout,stderr) = exn_on_failure(cmd, failuremessage)
cmd = ["iscsiadm", "-m", "node", "-p", portal, "-T", targetIQN, "--op", "update", "-n", "node.session.auth.username","-v", username] (stdout,stderr) = exn_on_failure(cmd, failuremessage)
cmd = ["iscsiadm", "-m", "node", "-p", portal, "-T", targetIQN, "--op", "update", "-n", "node.session.auth.password","-v", password] (stdout,stderr) = exn_on_failure(cmd, failuremessage)
"""doc placeholder""" if not len(tgt_string): return False if tgt_iqn == "any": return True # Extract IQN from iscsiadm -m session # Ex: tcp: [17] 10.220.98.9:3260,1 iqn.2009-01.xenrt.test:iscsi4181a93e siqn = tgt_string.split(",")[1].split()[1] return siqn == tgt_iqn
"""helper function - parses the output of iscsiadm for discovery and get_node_records""" def dotrans(x): (rec, iqn) = x.split() (portal, tpgt) = rec.split(',') return (portal, tpgt, iqn) unfiltered_map = map( dotrans, (filter( lambda x: match_target_iqn(target_iqn, x), text.split('\n')) ) ) # We need to filter duplicates orignating from doing the discovery using # multiple interfaces filtered_map = [] for input_value in unfiltered_map: if input_value not in filtered_map: filtered_map.append(input_value) return filtered_map
return '{}#{}'.format(portal, target)
"""This is a slightly revised version of iscsilib.login in SM.
We are trying to stick as close as possible to the original one but fixing or improving whatever can be improved, in order to later fix it to the original one and start using that one both for the transport code and the old SM code.
For example, 'uuid' is a new addition in order to be able to automatically refcount in here, instead of relying on the caller to do that. """
if username != "" and password != "": set_chap_settings(portal, target, username, password)
cmd = ["iscsiadm", "-m", "node", "-p", portal, "-T", target, "-l"] failuremessage = "Failed to login to target."
refcount = 0
portal_target_ref = get_portal_target_ref(portal, target)
# Increment portal/target ref count refcount = util._incr_iscsiSR_refcount(portal_target_ref, uuid)
if refcount == 1: try: (stdout, stderr) = exn_on_failure(cmd, failuremessage) # Increment legacy refcounter if uuid: util._incr_iscsiSR_refcount(target, uuid) except Exception as exc: util.SMlog("Failed: {}".format(" ".join(cmd)), ident="Transport") # Rollback refcount if needed if uuid: # This should be in its own try/block and be chained # with main exception below util._decr_iscsiSR_refcount(portal_target_ref, uuid) raise f_exceptions.XenError('ISCSILogin', message=exc.message) elif rescan: util.SMlog("Session already logged in for {}, rescan requested". format(target)) rescan_target(portal, target)
"""Modified version of iscsilib.py:logout to handle refcounting
Given the nature of this refcounting, it is not possible to specifically logout from one ip/iqn pair without breaking the refcounting. For this reason, this version does not accept a specific ip and the parameter 'all'. """
cmd = ["iscsiadm", "-m", "node", "-T", target, "-u"] failuremessage = "Failed to log out of target"
util.SMlog("decrease logout refcount of {} ({})". format(target, uuid), ident="Transport")
portal_target_ref = get_portal_target_ref(portal, target)
if util._decr_iscsiSR_refcount(portal_target_ref, uuid): # still logged in return
if not util._decr_iscsiSR_refcount(target, uuid): try: util.SMlog("logging out {} for {}". format(target, uuid), ident="Transport") (_, _) = exn_on_failure(cmd, failuremessage) except Exception as exc: raise f_exceptions.XenError('ISCSILogout', message=exc.message)
""" Rescan the given iscsi portal and target """ sessions_cmd = ["iscsiadm", "-m", "session"] failure_message = "Failed to read iscsi sessions" (stdout, _) = exn_on_failure(sessions_cmd, failure_message)
session_re = r"^[^\[]*\[(\d+)\] " + portal + r",\d+ " + target
for line in stdout.splitlines(): session_match = re.match(session_re, line) if session_match: session_id = session_match.group(1) rescan_cmd = ["iscsiadm", "-m", "session", '-r', session_id, '--rescan'] (_, _) = exn_on_failure(rescan_cmd, "Failed to rescan session")
# Get all configured iscsiadm interfaces "Failure occured querying iscsi daemon") # Get the interface (first column) from a line such as default # tcp,<empty>,<empty>,<empty>,<empty> # ignore interfaces which aren't marked as starting with # c_. # Ignore exception from exn on failure, still return the default # interface # In case there are no configured interfaces, still add the default # interface |