예제 #1
0
    def create_iscsi_target(self, name, vol_id, tid, lun, path,
                            chap_auth=None):
        scst_group = "%s%s" % (self.initiator_iqn, self.target_name)
        vol_name = path.split("/")[3]
        try:
            (out, _err) = self.scst_execute('-noprompt',
                                            '-set_drv_attr',
                                            self.target_driver,
                                            '-attributes',
                                            'enabled=1')
            LOG.debug('StdOut from set driver attribute: %s', out)
        except putils.ProcessExecutionError as e:
            LOG.error(_LE("Failed to set attribute for enable target driver "
                          "%s"), e)
            raise exception.ISCSITargetHelperCommandFailed(
                error_message="Failed to enable SCST Target driver.")

        if self._get_target(name) is None:
            try:
                (out, _err) = self.scst_execute('-add_target', name,
                                                '-driver', self.target_driver)
                LOG.debug("StdOut from scstadmin create target: %s", out)
            except putils.ProcessExecutionError as e:
                LOG.error(_LE("Failed to create iscsi target for volume "
                          "id:%(vol_id)s: %(e)s"), {'vol_id': name, 'e': e})
                raise exception.ISCSITargetCreateFailed(volume_id=vol_name)
            try:
                (out, _err) = self.scst_execute('-enable_target', name,
                                                '-driver', self.target_driver)
                LOG.debug("StdOut from scstadmin enable target: %s", out)
            except putils.ProcessExecutionError as e:
                LOG.error(_LE("Failed to set 'enable' attribute for "
                              "SCST target %s"), e)
                raise exception.ISCSITargetHelperCommandFailed(
                    error_mesage="Failed to enable SCST Target.")
            if chap_auth and self.target_name:
                try:
                    chap_string = self._iscsi_authentication('IncomingUser='******'-noprompt',
                                                    '-set_tgt_attr', name,
                                                    '-driver',
                                                    self.target_driver,
                                                    '-attributes',
                                                    chap_string)
                    LOG.debug("StdOut from scstadmin set target attribute:"
                              " %s.", out)
                except putils.ProcessExecutionError:
                    msg = _("Failed to set attribute 'Incoming user' for "
                            "SCST target.")
                    LOG.exception(msg)
                    raise exception.ISCSITargetHelperCommandFailed(
                        error_mesage=msg)

        if self.target_name:
            if self._get_group() is None:
                try:
                    (out, _err) = self.scst_execute('-add_group', scst_group,
                                                    '-driver',
                                                    self.target_driver,
                                                    '-target', name)
                    LOG.debug("StdOut from scstadmin create group: %s", out)
                except putils.ProcessExecutionError as e:
                    LOG.error(_LE("Failed to create group to SCST target "
                                  "%s"), e)
                    raise exception.ISCSITargetHelperCommandFailed(
                        error_message="Failed to create group to SCST target.")
            try:
                (out, _err) = self.scst_execute('-add_init',
                                                self.initiator_iqn,
                                                '-driver', self.target_driver,
                                                '-target', name,
                                                '-group', scst_group)
                LOG.debug("StdOut from scstadmin add initiator: %s", out)
            except putils.ProcessExecutionError as e:
                LOG.error(_LE("Failed to add initiator to group "
                          " for SCST target %s"), e)
                raise exception.ISCSITargetHelperCommandFailed(
                    error_message="Failed to add Initiator to group for "
                                  "SCST target.")

        tid = self._get_target(name)
        if self.target_name is None:
            disk_id = "disk%s" % tid
        else:
            disk_id = "%s%s" % (lun, vol_id.split('-')[-1])

        try:
            self.scst_execute('-open_dev', disk_id,
                              '-handler', 'vdisk_fileio',
                              '-attributes', 'filename=%s' % path)
        except putils.ProcessExecutionError as e:
            LOG.error(_LE("Failed to add device to handler %s"), e)
            raise exception.ISCSITargetHelperCommandFailed(
                error_message="Failed to add device to SCST handler.")

        try:
            if self.target_name:
                self.scst_execute('-add_lun', lun,
                                  '-driver', self.target_driver,
                                  '-target', name,
                                  '-device', disk_id,
                                  '-group', scst_group)
            else:
                self.scst_execute('-add_lun', lun,
                                  '-driver', self.target_driver,
                                  '-target', name,
                                  '-device', disk_id)
        except putils.ProcessExecutionError as e:
            LOG.error(_LE("Failed to add lun to SCST target "
                      "id:%(vol_id)s: %(e)s"), {'vol_id': name, 'e': e})
            raise exception.ISCSITargetHelperCommandFailed(
                error_message="Failed to add LUN to SCST Target for "
                              "volume " + vol_name)

            # SCST uses /etc/scst.conf as the default configuration when it
            # starts
        try:
            self.scst_execute('-write_config', '/etc/scst.conf')
        except putils.ProcessExecutionError as e:
            LOG.error(_LE("Failed to write in /etc/scst.conf."))
            raise exception.ISCSITargetHelperCommandFailed(
                error_message="Failed to write in /etc/scst.conf.")

        return tid
예제 #2
0
파일: cxt.py 프로젝트: abusse/cinder
    def create_iscsi_target(self, name, tid, lun, path,
                            chap_auth=None, **kwargs):

        (out, err) = utils.execute('iscsictl',
                                   '-c',
                                   'target=ALL',
                                   run_as_root=True)
        LOG.debug("Targets prior to update: %s", out)
        volumes_dir = self._get_volumes_dir()
        fileutils.ensure_tree(volumes_dir)

        vol_id = name.split(':')[1]

        if netutils.is_valid_ipv4(self.configuration.iscsi_ip_address):
            portal = "%s:%s" % (self.configuration.iscsi_ip_address,
                                self.configuration.iscsi_port)
        else:
            # ipv6 addresses use [ip]:port format, ipv4 use ip:port
            portal = "[%s]:%s" % (self.configuration.iscsi_ip_address,
                                  self.configuration.iscsi_port)

        if chap_auth is None:
            volume_conf = self.TARGET_FMT % (name, path, portal)
        else:
            volume_conf = self.TARGET_FMT_WITH_CHAP % (name,
                                                       path, portal,
                                                       '"%s":"%s"' % chap_auth)
        LOG.debug('Creating iscsi_target for: %s', vol_id)
        volume_path = os.path.join(volumes_dir, vol_id)

        if os.path.exists(volume_path):
            LOG.warning(_LW('Persistence file already exists for volume, '
                            'found file at: %s'), volume_path)
        f = open(volume_path, 'w+')
        f.write(volume_conf)
        f.close()
        LOG.debug('Created volume path %(vp)s,\n'
                  'content: %(vc)s',
                  {'vp': volume_path, 'vc': volume_conf})

        old_persist_file = None
        old_name = kwargs.get('old_name', None)
        if old_name:
            LOG.debug('Detected old persistence file for volume '
                      '%{vol}s at %{old_name}s',
                      {'vol': vol_id, 'old_name': old_name})
            old_persist_file = os.path.join(volumes_dir, old_name)

        try:
            # With the persistent tgts we create them
            # by creating the entry in the persist file
            # and then doing an update to get the target
            # created.
            (out, err) = utils.execute('iscsictl', '-S', 'target=%s' % name,
                                       '-f', volume_path,
                                       '-x', self.config,
                                       run_as_root=True)
        except putils.ProcessExecutionError as e:
            LOG.error(_LE("Failed to create iscsi target for volume "
                          "id:%(vol_id)s: %(e)s"),
                      {'vol_id': vol_id, 'e': e})

            # Don't forget to remove the persistent file we created
            os.unlink(volume_path)
            raise exception.ISCSITargetCreateFailed(volume_id=vol_id)
        finally:
            LOG.debug("StdOut from iscsictl -S: %s", out)
            LOG.debug("StdErr from iscsictl -S: %s", err)

        # Grab targets list for debug
        (out, err) = utils.execute('iscsictl',
                                   '-c',
                                   'target=ALL',
                                   run_as_root=True)
        LOG.debug("Targets after update: %s", out)

        iqn = '%s%s' % (self.iscsi_target_prefix, vol_id)
        tid = self._get_target(iqn)
        if tid is None:
            LOG.error(_LE("Failed to create iscsi target for volume "
                          "id:%(vol_id)s. Please verify your configuration "
                          "in %(volumes_dir)'"), {
                      'vol_id': vol_id,
                      'volumes_dir': volumes_dir, })
            raise exception.NotFound()

        if old_persist_file is not None and os.path.exists(old_persist_file):
            os.unlink(old_persist_file)

        return tid
예제 #3
0
    def create_iscsi_target(self,
                            name,
                            tid,
                            lun,
                            path,
                            chap_auth=None,
                            **kwargs):

        # Note(jdg) tid and lun aren't used by TgtAdm but remain for
        # compatibility

        # NOTE(jdg): Remove this when we get to the bottom of bug: #1398078
        # for now, since we intermittently hit target already exists we're
        # adding some debug info to try and pinpoint what's going on
        (out, err) = utils.execute('tgtadm',
                                   '--lld',
                                   'iscsi',
                                   '--op',
                                   'show',
                                   '--mode',
                                   'target',
                                   run_as_root=True)
        LOG.debug("Targets prior to update: %s", out)
        fileutils.ensure_tree(self.volumes_dir)

        vol_id = name.split(':')[1]
        write_cache = self.configuration.get('iscsi_write_cache', 'on')
        driver = self.iscsi_protocol
        chap_str = ''

        if chap_auth is not None:
            chap_str = 'incominguser %s %s' % chap_auth

        target_flags = self.configuration.get('iscsi_target_flags', '')
        if target_flags:
            target_flags = 'bsoflags ' + target_flags

        volume_conf = self.VOLUME_CONF % {
            'name': name,
            'path': path,
            'driver': driver,
            'chap_auth': chap_str,
            'target_flags': target_flags,
            'write_cache': write_cache
        }

        LOG.debug('Creating iscsi_target for Volume ID: %s', vol_id)
        volumes_dir = self.volumes_dir
        volume_path = os.path.join(volumes_dir, vol_id)

        if os.path.exists(volume_path):
            LOG.warning(
                _LW('Persistence file already exists for volume, '
                    'found file at: %s'), volume_path)
        f = open(volume_path, 'w+')
        f.write(volume_conf)
        f.close()
        LOG.debug(('Created volume path %(vp)s,\n'
                   'content: %(vc)s'), {
                       'vp': volume_path,
                       'vc': volume_conf
                   })

        old_persist_file = None
        old_name = kwargs.get('old_name', None)
        if old_name is not None:
            LOG.debug(
                'Detected old persistence file for volume '
                '%{vol}s at %{old_name}s', {
                    'vol': vol_id,
                    'old_name': old_name
                })
            old_persist_file = os.path.join(volumes_dir, old_name)

        try:
            # With the persistent tgts we create them
            # by creating the entry in the persist file
            # and then doing an update to get the target
            # created.

            self._do_tgt_update(name)
        except putils.ProcessExecutionError as e:
            if "target already exists" in e.stderr:
                # Adding the additional Warning message below for a clear
                # ER marker (Ref bug: #1398078).
                LOG.warning(
                    _LW('Could not create target because '
                        'it already exists for volume: %s'), vol_id)
                LOG.debug('Exception was: %s', e)

            else:
                LOG.error(
                    _LE("Failed to create iscsi target for Volume "
                        "ID: %(vol_id)s: %(e)s"), {
                            'vol_id': vol_id,
                            'e': e
                        })

            # Don't forget to remove the persistent file we created
            os.unlink(volume_path)
            raise exception.ISCSITargetCreateFailed(volume_id=vol_id)

        # Grab targets list for debug
        # Consider adding a check for lun 0 and 1 for tgtadm
        # before considering this as valid
        (out, err) = utils.execute('tgtadm',
                                   '--lld',
                                   'iscsi',
                                   '--op',
                                   'show',
                                   '--mode',
                                   'target',
                                   run_as_root=True)
        LOG.debug("Targets after update: %s", out)

        iqn = '%s%s' % (self.iscsi_target_prefix, vol_id)
        tid = self._get_target(iqn)
        if tid is None:
            LOG.error(
                _LE("Failed to create iscsi target for Volume "
                    "ID: %(vol_id)s. Please ensure your tgtd config "
                    "file contains 'include %(volumes_dir)s/*'"), {
                        'vol_id': vol_id,
                        'volumes_dir': volumes_dir,
                    })
            raise exception.NotFound()

        # NOTE(jdg): Sometimes we have some issues with the backing lun
        # not being created, believe this is due to a device busy
        # or something related, so we're going to add some code
        # here that verifies the backing lun (lun 1) was created
        # and we'll try and recreate it if it's not there
        if not self._verify_backing_lun(iqn, tid):
            try:
                self._recreate_backing_lun(iqn, tid, name, path)
            except putils.ProcessExecutionError:
                os.unlink(volume_path)
                raise exception.ISCSITargetCreateFailed(volume_id=vol_id)

            # Finally check once more and if no go, fail and punt
            if not self._verify_backing_lun(iqn, tid):
                os.unlink(volume_path)
                raise exception.ISCSITargetCreateFailed(volume_id=vol_id)

        if old_persist_file is not None and os.path.exists(old_persist_file):
            os.unlink(old_persist_file)

        return tid
예제 #4
0
파일: iscsi.py 프로젝트: cdwertmann/cinder
    def create_iscsi_target(self,
                            name,
                            tid,
                            lun,
                            path,
                            chap_auth=None,
                            **kwargs):
        # Note(jdg) tid and lun aren't used by TgtAdm but remain for
        # compatibility

        utils.ensure_tree(FLAGS.volumes_dir)

        vol_id = name.split(':')[1]
        if chap_auth is None:
            volume_conf = """
                <target %s>
                    backing-store %s
                </target>
            """ % (name, path)
        else:
            volume_conf = """
                <target %s>
                    backing-store %s
                    %s
                </target>
            """ % (name, path, chap_auth)

        LOG.info(_('Creating iscsi_target for: %s') % vol_id)
        volumes_dir = FLAGS.volumes_dir
        volume_path = os.path.join(volumes_dir, vol_id)

        f = open(volume_path, 'w+')
        f.write(volume_conf)
        f.close()

        old_persist_file = None
        old_name = kwargs.get('old_name', None)
        if old_name is not None:
            old_persist_file = os.path.join(volumes_dir, old_name)

        try:
            (out, err) = self._execute('tgt-admin',
                                       '--update',
                                       name,
                                       run_as_root=True)
        except exception.ProcessExecutionError as e:
            LOG.error(
                _("Failed to create iscsi target for volume "
                  "id:%(vol_id)s: %(e)s") % {
                      'vol_id': vol_id,
                      'e': str(e)
                  })

            #Don't forget to remove the persistent file we created
            os.unlink(volume_path)
            raise exception.ISCSITargetCreateFailed(volume_id=vol_id)

        iqn = '%s%s' % (FLAGS.iscsi_target_prefix, vol_id)
        tid = self._get_target(iqn)
        if tid is None:
            LOG.error(
                _("Failed to create iscsi target for volume "
                  "id:%(vol_id)s. Please ensure your tgtd config file "
                  "contains 'include %(volumes_dir)s/*'") % locals())
            raise exception.NotFound()

        if old_persist_file is not None and os.path.exists(old_persist_file):
            os.unlink(old_persist_file)

        return tid
예제 #5
0
    def create_iscsi_target(self, name, tid, lun, path,
                            chap_auth=None, **kwargs):
        # Note(jdg) tid and lun aren't used by TgtAdm but remain for
        # compatibility
        fileutils.ensure_tree(self.volumes_dir)

        vol_id = name.split(':')[1]
        if chap_auth is None:
            volume_conf = self.VOLUME_CONF % (name, path)
        else:
            volume_conf = self.VOLUME_CONF_WITH_CHAP_AUTH % (name,
                                                             path, chap_auth)

        LOG.info(_('Creating iscsi_target for: %s') % vol_id)
        volumes_dir = self.volumes_dir
        volume_path = os.path.join(volumes_dir, vol_id)

        f = open(volume_path, 'w+')
        f.write(volume_conf)
        f.close()
        LOG.debug(('Created volume path %(vp)s,\n'
                   'content: %(vc)s')
                  % {'vp': volume_path, 'vc': volume_conf})

        old_persist_file = None
        old_name = kwargs.get('old_name', None)
        if old_name is not None:
            old_persist_file = os.path.join(volumes_dir, old_name)

        try:
            # with the persistent tgts we create them
            # by creating the entry in the persist file
            # and then doing an update to get the target
            # created.
            (out, err) = self._execute('tgt-admin', '--update', name,
                                       run_as_root=True)
            LOG.debug("StdOut from tgt-admin --update: %s", out)
            LOG.debug("StdErr from tgt-admin --update: %s", err)

            # Grab targets list for debug
            # Consider adding a check for lun 0 and 1 for tgtadm
            # before considering this as valid
            (out, err) = self._execute('tgtadm',
                                       '--lld',
                                       'iscsi',
                                       '--op',
                                       'show',
                                       '--mode',
                                       'target',
                                       run_as_root=True)
            LOG.debug("Targets after update: %s" % out)
        except putils.ProcessExecutionError as e:
            LOG.warning(_("Failed to create iscsi target for volume "
                        "id:%(vol_id)s: %(e)s")
                        % {'vol_id': vol_id, 'e': e})

            #Don't forget to remove the persistent file we created
            os.unlink(volume_path)
            raise exception.ISCSITargetCreateFailed(volume_id=vol_id)

        iqn = '%s%s' % (self.iscsi_target_prefix, vol_id)
        tid = self._get_target(iqn)
        if tid is None:
            LOG.error(_("Failed to create iscsi target for volume "
                        "id:%(vol_id)s. Please ensure your tgtd config file "
                        "contains 'include %(volumes_dir)s/*'") % {
                      'vol_id': vol_id,
                      'volumes_dir': volumes_dir, })
            raise exception.NotFound()

        # NOTE(jdg): Sometimes we have some issues with the backing lun
        # not being created, believe this is due to a device busy
        # or something related, so we're going to add some code
        # here that verifies the backing lun (lun 1) was created
        # and we'll try and recreate it if it's not there
        if not self._verify_backing_lun(iqn, tid):
            try:
                self._recreate_backing_lun(iqn, tid, name, path)
            except putils.ProcessExecutionError:
                os.unlink(volume_path)
                raise exception.ISCSITargetCreateFailed(volume_id=vol_id)

            # Finally check once more and if no go, fail and punt
            if not self._verify_backing_lun(iqn, tid):
                os.unlink(volume_path)
                raise exception.ISCSITargetCreateFailed(volume_id=vol_id)

        if old_persist_file is not None and os.path.exists(old_persist_file):
            os.unlink(old_persist_file)

        return tid