示例#1
0
    def get_iscsi_params(self, xml_file_path, connector):
        """Get target iSCSI params, including iqn, IP."""
        initiator = connector['initiator']
        iscsi_conf = huawei_utils.get_iscsi_conf(xml_file_path)
        target_ip = None
        portgroup = None
        portgroup_id = None
        for ini in iscsi_conf['Initiator']:
            if ini['Name'] == initiator:
                for key in ini:
                    if key == 'TargetPortGroup':
                        portgroup = ini['TargetPortGroup']
                    elif key == 'TargetIP':
                        target_ip = ini['TargetIP']

        if portgroup:
            portgroup_id = self.find_tgt_port_group(portgroup)
            target_ip = self._get_tgt_ip_from_portgroup(portgroup_id)

        # If not specify target IP for some initiators, use default IP.
        if not target_ip:
            if iscsi_conf['DefaultTargetIP']:
                target_ip = iscsi_conf['DefaultTargetIP']

            else:
                msg = (_(
                    'get_iscsi_params: Failed to get target IP '
                    'for initiator %(ini)s, please check config file.')
                    % {'ini': initiator})
                LOG.error(msg)
                raise exception.InvalidInput(reason=msg)

        LOG.info(_LI('Get the default ip: %s.'), target_ip)
        # If didn't get target IP for rest, Automated assembly target ip.
        target_iqn = self._get_tgt_iqn_from_rest(target_ip)

        if not target_iqn:
            target_iqn = self._get_tgt_iqn(target_ip)

        return (target_iqn, target_ip, portgroup_id)
示例#2
0
    def terminate_connection(self, volume, connector, **kwargs):
        """Delete map between a volume and a host."""
        initiator_name = connector['initiator']
        volume_name = huawei_utils.encode_name(volume['id'])
        lun_id = volume.get('provider_location')
        host_name = connector['host']
        lungroup_id = None

        LOG.info(
            _LI('terminate_connection: volume name: %(volume)s, '
                'initiator name: %(ini)s, '
                'lun_id: %(lunid)s.'),
            {
                'volume': volume_name,
                'ini': initiator_name,
                'lunid': lun_id
            },
        )

        iscsi_conf = huawei_utils.get_iscsi_conf(self.xml_file_path)
        portgroup = None
        portgroup_id = None
        view_id = None
        left_lunnum = -1
        for ini in iscsi_conf['Initiator']:
            if ini['Name'] == initiator_name:
                for key in ini:
                    if key == 'TargetPortGroup':
                        portgroup = ini['TargetPortGroup']
                        break

        if portgroup:
            portgroup_id = self.restclient.find_tgt_port_group(portgroup)
        if host_name and (len(host_name) > constants.MAX_HOSTNAME_LENGTH):
            host_name = six.text_type(hash(host_name))
        host_id = self.restclient.find_host(host_name)
        if host_id:
            mapping_view_name = constants.MAPPING_VIEW_PREFIX + host_id
            view_id = self.restclient.find_mapping_view(mapping_view_name)
            if view_id:
                lungroup_id = self.restclient.find_lungroup_from_map(view_id)

        # Remove lun from lungroup.
        if lun_id and self.restclient.check_lun_exist(lun_id):
            if lungroup_id:
                lungroup_ids = self.restclient.get_lungroupids_by_lunid(lun_id)
                if lungroup_id in lungroup_ids:
                    self.restclient.remove_lun_from_lungroup(
                        lungroup_id, lun_id)
                else:
                    LOG.warning(
                        _LW("Lun is not in lungroup. "
                            "Lun id: %(lun_id)s. "
                            "lungroup id: %(lungroup_id)s."), {
                                "lun_id": lun_id,
                                "lungroup_id": lungroup_id
                            })
        else:
            LOG.warning(_LW("Can't find lun on the array."))

        # Remove portgroup from mapping view if no lun left in lungroup.
        if lungroup_id:
            left_lunnum = self.restclient.get_lunnum_from_lungroup(lungroup_id)

        if portgroup_id and view_id and (int(left_lunnum) <= 0):
            if self.restclient.is_portgroup_associated_to_view(
                    view_id, portgroup_id):
                self.restclient.delete_portgroup_mapping_view(
                    view_id, portgroup_id)
        if view_id and (int(left_lunnum) <= 0):
            self.restclient.remove_chap(initiator_name)

            if self.restclient.lungroup_associated(view_id, lungroup_id):
                self.restclient.delete_lungroup_mapping_view(
                    view_id, lungroup_id)
            self.restclient.delete_lungroup(lungroup_id)
            if self.restclient.is_initiator_associated_to_host(initiator_name):
                self.restclient.remove_iscsi_from_host(initiator_name)
            hostgroup_name = constants.HOSTGROUP_PREFIX + host_id
            hostgroup_id = self.restclient.find_hostgroup(hostgroup_name)
            if hostgroup_id:
                if self.restclient.hostgroup_associated(view_id, hostgroup_id):
                    self.restclient.delete_hostgoup_mapping_view(
                        view_id, hostgroup_id)
                self.restclient.remove_host_from_hostgroup(
                    hostgroup_id, host_id)
                self.restclient.delete_hostgroup(hostgroup_id)
            self.restclient.remove_host(host_id)
            self.restclient.delete_mapping_view(view_id)
示例#3
0
    def initialize_connection(self, volume, connector):
        """Map a volume to a host and return target iSCSI information."""
        LOG.info(_LI('Enter initialize_connection.'))
        initiator_name = connector['initiator']
        volume_name = huawei_utils.encode_name(volume['id'])

        LOG.info(
            _LI('initiator name: %(initiator_name)s, '
                'volume name: %(volume)s.'), {
                    'initiator_name': initiator_name,
                    'volume': volume_name
                })

        (iscsi_iqns, target_ips,
         portgroup_id) = self.restclient.get_iscsi_params(
             self.xml_file_path, connector)
        LOG.info(
            _LI('initialize_connection, iscsi_iqn: %(iscsi_iqn)s, '
                'target_ip: %(target_ip)s, '
                'portgroup_id: %(portgroup_id)s.'),
            {
                'iscsi_iqn': iscsi_iqns,
                'target_ip': target_ips,
                'portgroup_id': portgroup_id
            },
        )

        # Create hostgroup if not exist.
        host_name = connector['host']
        host_name_before_hash = None
        if host_name and (len(host_name) > constants.MAX_HOSTNAME_LENGTH):
            host_name_before_hash = host_name
            host_name = six.text_type(hash(host_name))
        host_id = self.restclient.add_host_with_check(host_name,
                                                      host_name_before_hash)

        # Add initiator to the host.
        self.restclient.ensure_initiator_added(self.xml_file_path,
                                               initiator_name, host_id)
        hostgroup_id = self.restclient.add_host_into_hostgroup(host_id)

        lun_id = self.restclient.get_lunid(volume, volume_name)

        # Mapping lungroup and hostgroup to view.
        self.restclient.do_mapping(lun_id, hostgroup_id, host_id, portgroup_id)

        hostlun_id = self.restclient.find_host_lun_id(host_id, lun_id)

        LOG.info(_LI("initialize_connection, host lun id is: %s."), hostlun_id)

        iscsi_conf = huawei_utils.get_iscsi_conf(self.xml_file_path)
        chapinfo = self.restclient.find_chap_info(iscsi_conf, initiator_name)
        # Return iSCSI properties.
        properties = {}
        properties['target_discovered'] = False
        properties['volume_id'] = volume['id']
        multipath = connector.get('multipath', False)
        hostlun_id = int(hostlun_id)
        if not multipath:
            properties['target_portal'] = ('%s:3260' % target_ips[0])
            properties['target_iqn'] = iscsi_iqns[0]
            properties['target_lun'] = hostlun_id
        else:
            properties['target_iqns'] = [iqn for iqn in iscsi_iqns]
            properties['target_portals'] = [
                '%s:3260' % ip for ip in target_ips
            ]
            properties['target_luns'] = [hostlun_id] * len(target_ips)

        # If use CHAP, return CHAP info.
        if chapinfo:
            chap_username, chap_password = chapinfo.split(';')
            properties['auth_method'] = 'CHAP'
            properties['auth_username'] = chap_username
            properties['auth_password'] = chap_password

        LOG.info(_LI("initialize_connection success. Return data: %s."),
                 properties)
        return {'driver_volume_type': 'iscsi', 'data': properties}
示例#4
0
    def terminate_connection_iscsi(self, volume, connector):
        """Delete map between a volume and a host."""
        initiator_name = connector['initiator']
        volume_name = huawei_utils.encode_name(volume['id'])
        lun_id = volume.get('provider_location', None)
        host_name = connector['host']

        LOG.info(_LI(
            'terminate_connection_iscsi: volume name: %(volume)s, '
            'initiator name: %(ini)s, '
            'lun_id: %(lunid)s.'),
            {'volume': volume_name,
             'ini': initiator_name,
             'lunid': lun_id},)

        iscsi_conf = huawei_utils.get_iscsi_conf(self.xml_file_path)
        portgroup = None
        portgroup_id = None
        left_lunnum = -1
        for ini in iscsi_conf['Initiator']:
            if ini['Name'] == initiator_name:
                for key in ini:
                    if key == 'TargetPortGroup':
                        portgroup = ini['TargetPortGroup']
                        break
        # Remove lun from lungroup.
        if lun_id:
            if self.restclient.check_lun_exist(lun_id):
                # Get lungroup id by lun id.
                lungroup_id = self.restclient.get_lungroupid_by_lunid(lun_id)
                if lungroup_id:
                    self.restclient.remove_lun_from_lungroup(lungroup_id,
                                                             lun_id)
            else:
                LOG.warning(_LW("Can't find lun on the array."))
        # Remove portgroup from mapping view if no lun left in lungroup.
        if portgroup:
            portgroup_id = self.restclient.find_tgt_port_group(portgroup)
        host_id = self.restclient.find_host(host_name)
        if host_id:
            mapping_view_name = constants.MAPPING_VIEW_PREFIX + host_id
            view_id = self.restclient.find_mapping_view(mapping_view_name)
            if view_id:
                lungroup_id = self.restclient.find_lungroup_from_map(view_id)
        if lungroup_id:
            left_lunnum = self.restclient.get_lunnum_from_lungroup(lungroup_id)

        if portgroup_id and view_id and (int(left_lunnum) <= 0):
            if self.restclient.is_portgroup_associated_to_view(view_id,
                                                               portgroup_id):
                self.restclient.delete_portgroup_mapping_view(view_id,
                                                              portgroup_id)
        if view_id and (int(left_lunnum) <= 0):
            self.restclient.remove_chap(initiator_name)

            if self.restclient.lungroup_associated(view_id, lungroup_id):
                self.restclient.delete_lungroup_mapping_view(view_id,
                                                             lungroup_id)
            self.restclient.delete_lungroup(lungroup_id)
            if self.restclient.is_initiator_associated_to_host(initiator_name):
                self.restclient.remove_iscsi_from_host(initiator_name)
            hostgroup_name = constants.HOSTGROUP_PREFIX + host_id
            hostgroup_id = self.restclient.find_hostgroup(hostgroup_name)
            if hostgroup_id:
                if self.restclient.hostgroup_associated(view_id, hostgroup_id):
                    self.restclient.delete_hostgoup_mapping_view(view_id,
                                                                 hostgroup_id)
                self.restclient.remove_host_from_hostgroup(hostgroup_id,
                                                           host_id)
                self.restclient.delete_hostgroup(hostgroup_id)
            self.restclient.remove_host(host_id)
            self.restclient.delete_mapping_view(view_id)
示例#5
0
    def initialize_connection_iscsi(self, volume, connector):
        """Map a volume to a host and return target iSCSI information."""
        LOG.info(_LI('Enter initialize_connection_iscsi.'))
        initiator_name = connector['initiator']
        volume_name = huawei_utils.encode_name(volume['id'])

        LOG.info(_LI(
            'initiator name: %(initiator_name)s, '
            'volume name: %(volume)s.'),
            {'initiator_name': initiator_name,
             'volume': volume_name})

        (iscsi_iqns,
         target_ips,
         portgroup_id) = self.restclient.get_iscsi_params(self.xml_file_path,
                                                          connector)
        LOG.info(_LI('initialize_connection_iscsi, iscsi_iqn: %(iscsi_iqn)s, '
                     'target_ip: %(target_ip)s, '
                     'portgroup_id: %(portgroup_id)s.'),
                 {'iscsi_iqn': iscsi_iqns,
                  'target_ip': target_ips,
                  'portgroup_id': portgroup_id},)

        # Create hostgroup if not exist.
        host_name = connector['host']
        host_name_before_hash = None
        if host_name and (len(host_name) > constants.MAX_HOSTNAME_LENGTH):
            host_name_before_hash = host_name
            host_name = six.text_type(hash(host_name))
        host_id = self.restclient.add_host_with_check(host_name,
                                                      host_name_before_hash)

        # Add initiator to the host.
        self.restclient.ensure_initiator_added(self.xml_file_path,
                                               initiator_name,
                                               host_id)
        hostgroup_id = self.restclient.add_host_into_hostgroup(host_id)

        # Mapping lungroup and hostgroup to view.
        lun_id = self.restclient.mapping_hostgroup_and_lungroup(volume_name,
                                                                hostgroup_id,
                                                                host_id,
                                                                portgroup_id)

        hostlun_id = self.restclient.find_host_lun_id(host_id, lun_id)

        LOG.info(_LI("initialize_connection_iscsi, host lun id is: %s."),
                 hostlun_id)

        iscsi_conf = huawei_utils.get_iscsi_conf(self.xml_file_path)
        chapinfo = self.restclient.find_chap_info(iscsi_conf,
                                                  initiator_name)
        # Return iSCSI properties.
        properties = {}
        properties['target_discovered'] = False
        properties['volume_id'] = volume['id']
        multipath = connector.get('multipath', False)
        hostlun_id = int(hostlun_id)
        if not multipath:
            properties['target_portal'] = ('%s:3260' % target_ips[0])
            properties['target_iqn'] = iscsi_iqns[0]
            properties['target_lun'] = hostlun_id
        else:
            properties['target_iqns'] = [iqn for iqn in iscsi_iqns]
            properties['target_portals'] = [
                '%s:3260' % ip for ip in target_ips]
            properties['target_luns'] = [hostlun_id] * len(target_ips)

        # If use CHAP, return CHAP info.
        if chapinfo:
            chap_username, chap_password = chapinfo.split(';')
            properties['auth_method'] = 'CHAP'
            properties['auth_username'] = chap_username
            properties['auth_password'] = chap_password

        LOG.info(_LI("initialize_connection_iscsi success. Return data: %s."),
                 properties)
        return {'driver_volume_type': 'iscsi', 'data': properties}
示例#6
0
 def tset_get_iscsi_conf(self):
     self.driver.restclient.login()
     iscsiinfo = huawei_utils.get_iscsi_conf(self.xml_file_path)
     self.assertEqual('iqn.1993-08.debian:01:ec2bff7ac3a3',
                      iscsiinfo['Initiator'])
示例#7
0
 def tset_get_iscsi_conf(self):
     self.driver.restclient.login()
     iscsiinfo = huawei_utils.get_iscsi_conf(self.xml_file_path)
     self.assertEqual('iqn.1993-08.debian:01:ec2bff7ac3a3',
                      iscsiinfo['Initiator'])
示例#8
0
    def terminate_connection(self, volume, connector, **kwargs):
        """Delete map between a volume and a host."""
        initiator_name = connector["initiator"]
        volume_name = huawei_utils.encode_name(volume["id"])
        lun_id = volume.get("provider_location", None)
        host_name = connector["host"]
        lungroup_id = None

        LOG.info(
            _LI("terminate_connection: volume name: %(volume)s, " "initiator name: %(ini)s, " "lun_id: %(lunid)s."),
            {"volume": volume_name, "ini": initiator_name, "lunid": lun_id},
        )

        iscsi_conf = huawei_utils.get_iscsi_conf(self.xml_file_path)
        portgroup = None
        portgroup_id = None
        view_id = None
        left_lunnum = -1
        for ini in iscsi_conf["Initiator"]:
            if ini["Name"] == initiator_name:
                for key in ini:
                    if key == "TargetPortGroup":
                        portgroup = ini["TargetPortGroup"]
                        break

        if portgroup:
            portgroup_id = self.restclient.find_tgt_port_group(portgroup)
        if host_name and (len(host_name) > constants.MAX_HOSTNAME_LENGTH):
            host_name = six.text_type(hash(host_name))
        host_id = self.restclient.find_host(host_name)
        if host_id:
            mapping_view_name = constants.MAPPING_VIEW_PREFIX + host_id
            view_id = self.restclient.find_mapping_view(mapping_view_name)
            if view_id:
                lungroup_id = self.restclient.find_lungroup_from_map(view_id)

        # Remove lun from lungroup.
        if lun_id and self.restclient.check_lun_exist(lun_id):
            if lungroup_id:
                lungroup_ids = self.restclient.get_lungroupids_by_lunid(lun_id)
                if lungroup_id in lungroup_ids:
                    self.restclient.remove_lun_from_lungroup(lungroup_id, lun_id)
                else:
                    LOG.warning(
                        _LW("Lun is not in lungroup. " "Lun id: %(lun_id)s. " "lungroup id: %(lungroup_id)s."),
                        {"lun_id": lun_id, "lungroup_id": lungroup_id},
                    )
        else:
            LOG.warning(_LW("Can't find lun on the array."))

        # Remove portgroup from mapping view if no lun left in lungroup.
        if lungroup_id:
            left_lunnum = self.restclient.get_lunnum_from_lungroup(lungroup_id)

        if portgroup_id and view_id and (int(left_lunnum) <= 0):
            if self.restclient.is_portgroup_associated_to_view(view_id, portgroup_id):
                self.restclient.delete_portgroup_mapping_view(view_id, portgroup_id)
        if view_id and (int(left_lunnum) <= 0):
            self.restclient.remove_chap(initiator_name)

            if self.restclient.lungroup_associated(view_id, lungroup_id):
                self.restclient.delete_lungroup_mapping_view(view_id, lungroup_id)
            self.restclient.delete_lungroup(lungroup_id)
            if self.restclient.is_initiator_associated_to_host(initiator_name):
                self.restclient.remove_iscsi_from_host(initiator_name)
            hostgroup_name = constants.HOSTGROUP_PREFIX + host_id
            hostgroup_id = self.restclient.find_hostgroup(hostgroup_name)
            if hostgroup_id:
                if self.restclient.hostgroup_associated(view_id, hostgroup_id):
                    self.restclient.delete_hostgoup_mapping_view(view_id, hostgroup_id)
                self.restclient.remove_host_from_hostgroup(hostgroup_id, host_id)
                self.restclient.delete_hostgroup(hostgroup_id)
            self.restclient.remove_host(host_id)
            self.restclient.delete_mapping_view(view_id)
示例#9
0
    def initialize_connection(self, volume, connector):
        """Map a volume to a host and return target iSCSI information."""
        LOG.info(_LI("Enter initialize_connection."))
        initiator_name = connector["initiator"]
        volume_name = huawei_utils.encode_name(volume["id"])

        LOG.info(
            _LI("initiator name: %(initiator_name)s, " "volume name: %(volume)s."),
            {"initiator_name": initiator_name, "volume": volume_name},
        )

        (iscsi_iqns, target_ips, portgroup_id) = self.restclient.get_iscsi_params(self.xml_file_path, connector)
        LOG.info(
            _LI(
                "initialize_connection, iscsi_iqn: %(iscsi_iqn)s, "
                "target_ip: %(target_ip)s, "
                "portgroup_id: %(portgroup_id)s."
            ),
            {"iscsi_iqn": iscsi_iqns, "target_ip": target_ips, "portgroup_id": portgroup_id},
        )

        # Create hostgroup if not exist.
        host_name = connector["host"]
        host_name_before_hash = None
        if host_name and (len(host_name) > constants.MAX_HOSTNAME_LENGTH):
            host_name_before_hash = host_name
            host_name = six.text_type(hash(host_name))
        host_id = self.restclient.add_host_with_check(host_name, host_name_before_hash)

        # Add initiator to the host.
        self.restclient.ensure_initiator_added(self.xml_file_path, initiator_name, host_id)
        hostgroup_id = self.restclient.add_host_into_hostgroup(host_id)

        # Mapping lungroup and hostgroup to view.
        lun_id = self.restclient.mapping_hostgroup_and_lungroup(volume_name, hostgroup_id, host_id, portgroup_id)

        hostlun_id = self.restclient.find_host_lun_id(host_id, lun_id)

        LOG.info(_LI("initialize_connection, host lun id is: %s."), hostlun_id)

        iscsi_conf = huawei_utils.get_iscsi_conf(self.xml_file_path)
        chapinfo = self.restclient.find_chap_info(iscsi_conf, initiator_name)
        # Return iSCSI properties.
        properties = {}
        properties["target_discovered"] = False
        properties["volume_id"] = volume["id"]
        multipath = connector.get("multipath", False)
        hostlun_id = int(hostlun_id)
        if not multipath:
            properties["target_portal"] = "%s:3260" % target_ips[0]
            properties["target_iqn"] = iscsi_iqns[0]
            properties["target_lun"] = hostlun_id
        else:
            properties["target_iqns"] = [iqn for iqn in iscsi_iqns]
            properties["target_portals"] = ["%s:3260" % ip for ip in target_ips]
            properties["target_luns"] = [hostlun_id] * len(target_ips)

        # If use CHAP, return CHAP info.
        if chapinfo:
            chap_username, chap_password = chapinfo.split(";")
            properties["auth_method"] = "CHAP"
            properties["auth_username"] = chap_username
            properties["auth_password"] = chap_password

        LOG.info(_LI("initialize_connection success. Return data: %s."), properties)
        return {"driver_volume_type": "iscsi", "data": properties}