def _login_target_portal(self, target_portal): (target_address, target_port) = _utils.parse_server_string(target_portal) output = self.execute("iscsicli.exe", "ListTargetPortals") pattern = r"Address and Socket *: (.*)" portals = [addr.split() for addr in re.findall(pattern, output)] LOG.debug("Ensuring connection to portal: %s" % target_portal) if [target_address, str(target_port)] in portals: self.execute("iscsicli.exe", "RefreshTargetPortal", target_address, target_port) else: # Adding target portal to iscsi initiator. Sending targets self.execute( "iscsicli.exe", "AddTargetPortal", target_address, target_port, "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", )
def login_storage_target(self, target_lun, target_iqn, target_portal, auth_username=None, auth_password=None, auth_type=None, mpio_enabled=False, ensure_lun_available=True, initiator_name=None, rescan_attempts=_DEFAULT_RESCAN_ATTEMPTS): portal_addr, portal_port = _utils.parse_server_string(target_portal) portal_port = (int(portal_port) if portal_port else iscsi_struct.DEFAULT_ISCSI_PORT) known_targets = self.get_targets() if target_iqn not in known_targets: self._add_static_target(target_iqn) login_required = self._new_session_required( target_iqn, portal_addr, portal_port, initiator_name, mpio_enabled) if login_required: LOG.debug("Logging in iSCSI target %(target_iqn)s", dict(target_iqn=target_iqn)) # If the multipath flag is set, multiple sessions to the same # target may be estabilished. MPIO must be enabled and configured # to claim iSCSI disks, otherwise data corruption can occur. login_flags = (iscsi_struct.ISCSI_LOGIN_FLAG_MULTIPATH_ENABLED if mpio_enabled else 0) login_opts = self._get_login_opts(auth_username, auth_password, auth_type, login_flags) portal = iscsi_struct.ISCSI_TARGET_PORTAL(Address=portal_addr, Socket=portal_port) # Note(lpetrut): The iscsidsc documentation states that if a # persistent session is requested, the initiator should login # the target only after saving the credentials. # # The issue is that although the Microsoft iSCSI service saves # the credentials, it does not automatically login the target, # for which reason we have two calls, one meant to save the # credentials and another one actually creating the session. self._login_iscsi_target(target_iqn, portal, login_opts, is_persistent=True) sid, cid = self._login_iscsi_target(target_iqn, portal, login_opts, is_persistent=False) if ensure_lun_available: self.ensure_lun_available(target_iqn, target_lun, rescan_attempts)
def _login_target_portal(self, target_portal): (target_address, target_port) = _utils.parse_server_string(target_portal) # Checking if the portal is already connected. portal = self._conn_storage.query("SELECT * FROM " "MSFT_iSCSITargetPortal " "WHERE TargetPortalAddress='%s' " "AND TargetPortalPortNumber='%s'" % (target_address, target_port)) if portal: portal[0].Update() else: # Adding target portal to iscsi initiator. Sending targets portal = self._conn_storage.MSFT_iSCSITargetPortal portal.New(TargetPortalAddress=target_address, TargetPortalPortNumber=target_port)
def test_parse_server_string(self): result = _utils.parse_server_string('::1') self.assertEqual(('::1', ''), result) result = _utils.parse_server_string('[::1]:8773') self.assertEqual(('::1', '8773'), result) result = _utils.parse_server_string('2001:db8::192.168.1.1') self.assertEqual(('2001:db8::192.168.1.1', ''), result) result = _utils.parse_server_string('[2001:db8::192.168.1.1]:8773') self.assertEqual(('2001:db8::192.168.1.1', '8773'), result) result = _utils.parse_server_string('192.168.1.1') self.assertEqual(('192.168.1.1', ''), result) result = _utils.parse_server_string('192.168.1.2:8773') self.assertEqual(('192.168.1.2', '8773'), result) result = _utils.parse_server_string('192.168.1.3') self.assertEqual(('192.168.1.3', ''), result) result = _utils.parse_server_string('www.example.com:8443') self.assertEqual(('www.example.com', '8443'), result) result = _utils.parse_server_string('www.example.com') self.assertEqual(('www.example.com', ''), result) # error case result = _utils.parse_server_string('www.exa:mple.com:8443') self.assertEqual(('', ''), result) result = _utils.parse_server_string('') self.assertEqual(('', ''), result)