示例#1
0
def create(backing_device,
           name,
           userid,
           password,
           iser_enabled,
           initiator_iqns=None):
    try:
        rtsroot = rtslib.root.RTSRoot()
    except rtslib.utils.RTSLibError:
        print(_('Ensure that configfs is mounted at /sys/kernel/config.'))
        raise

    # Look to see if BlockStorageObject already exists
    for x in rtsroot.storage_objects:
        if x.name == name:
            # Already exists, use this one
            return

    so_new = rtslib.BlockStorageObject(name=name, dev=backing_device)

    target_new = rtslib.Target(rtslib.FabricModule('iscsi'), name, 'create')

    tpg_new = rtslib.TPG(target_new, mode='create')
    tpg_new.set_attribute('authentication', '1')

    lun_new = rtslib.LUN(tpg_new, storage_object=so_new)

    if initiator_iqns:
        initiator_iqns = initiator_iqns.strip(' ')
        for i in initiator_iqns.split(','):
            acl_new = rtslib.NodeACL(tpg_new, i, mode='create')
            acl_new.chap_userid = userid
            acl_new.chap_password = password

            rtslib.MappedLUN(acl_new, lun_new.lun, lun_new.lun)

    tpg_new.enable = 1

    try:
        portal = rtslib.NetworkPortal(tpg_new, '0.0.0.0', 3260, mode='any')
    except rtslib.utils.RTSLibError:
        print(
            _('Error creating NetworkPortal: ensure port 3260 '
              'is not in use by another service.'))
        raise

    try:
        if iser_enabled == 'True':
            portal._set_iser(1)
    except rtslib.utils.RTSLibError:
        print(
            _('Error enabling iSER for NetworkPortal: please ensure that '
              'RDMA is supported on your iSCSI port.'))
        raise

    try:
        rtslib.NetworkPortal(tpg_new, '::0', 3260, mode='any')
    except rtslib.utils.RTSLibError:
        # TODO(emh): Binding to IPv6 fails sometimes -- let pass for now.
        pass
示例#2
0
def add_initiator(target_iqn, initiator_iqn, userid, password):
    target = _lookup_target(target_iqn, initiator_iqn)
    tpg = target.tpgs.next()  # get the first one
    for acl in tpg.node_acls:
        # See if this ACL configuration already exists
        if acl['node_wwn'] == initiator_iqn:
            # No further action required
            return

    acl_new = rtslib.NodeACL(tpg, initiator_iqn, mode='create')
    acl_new.chap_userid = userid
    acl_new.chap_password = password

    rtslib.MappedLUN(acl_new, 0, tpg_lun=0)
示例#3
0
def create_mapped_lun(iqn, iqn_initiator, lun):
    """
    Map a lun to given acl.
    """
    logger.info("trying to map LUN %s for %s" % (lun, iqn))
    if lun in (curlun['name'] for curlun in current_attached_luns(iqn)):
        if {
                'lun': _get_lun_id(iqn, lun)
        } in current_mapped_luns(iqn, iqn_initiator):
            logger.debug("Mapped_LUN %s still exists" % lun)
        else:
            try:
                rtslib.MappedLUN(_get_single_tpg(iqn).node_acl(iqn_initiator,
                                                               mode='lookup'),
                                 _next_free_mapped_lun_index(
                                     iqn, iqn_initiator),
                                 tpg_lun=_get_lun_id(iqn, lun))
            except:
                raise
    else:
        logger.debug("LUN %s does not exists" % lun)

    return None
示例#4
0
#!/usr/bin/env python

import rtslib

i = rtslib.FabricModule("iscsi")
targets = list(i.targets)
t = targets[0]
tpg = list(t.tpgs)[0]
lun = list(tpg.luns)[0]
nodeacl = rtslib.NodeACL(tpg, sys.argv[1])
mlun = rtslib.MappedLUN(nodeacl, 0, lun)
示例#5
0
def create(backing_device,
           name,
           userid,
           password,
           iser_enabled,
           initiator_iqns=None,
           portals_ips=None,
           portals_port=3260):
    # List of IPS that will not raise an error when they fail binding.
    # Originally we will fail on all binding errors.
    ips_allow_fail = ()

    try:
        rtsroot = rtslib.root.RTSRoot()
    except rtslib.utils.RTSLibError:
        print(_('Ensure that configfs is mounted at /sys/kernel/config.'))
        raise

    # Look to see if BlockStorageObject already exists
    for x in rtsroot.storage_objects:
        if x.name == name:
            # Already exists, use this one
            return

    so_new = rtslib.BlockStorageObject(name=name, dev=backing_device)

    target_new = rtslib.Target(rtslib.FabricModule('iscsi'), name, 'create')

    tpg_new = rtslib.TPG(target_new, mode='create')
    tpg_new.set_attribute('authentication', '1')

    lun_new = rtslib.LUN(tpg_new, storage_object=so_new)

    if initiator_iqns:
        initiator_iqns = initiator_iqns.strip(' ')
        for i in initiator_iqns.split(','):
            acl_new = rtslib.NodeACL(tpg_new, i, mode='create')
            acl_new.chap_userid = userid
            acl_new.chap_password = password

            rtslib.MappedLUN(acl_new, lun_new.lun, lun_new.lun)

    tpg_new.enable = 1

    # If no ips are given we'll bind to all IPv4 and v6
    if not portals_ips:
        portals_ips = ('0.0.0.0', '::0')
        # TODO(emh): Binding to IPv6 fails sometimes -- let pass for now.
        ips_allow_fail = ('::0', )

    for ip in portals_ips:
        try:
            portal = rtslib.NetworkPortal(tpg_new,
                                          ip,
                                          portals_port,
                                          mode='any')
        except rtslib.utils.RTSLibError:
            raise_exc = ip not in ips_allow_fail
            msg_type = 'Error' if raise_exc else 'Warning'
            print(
                _('%(msg_type)s: creating NetworkPortal: ensure port '
                  '%(port)d on ip %(ip)s is not in use by another service.') %
                {
                    'msg_type': msg_type,
                    'port': portals_port,
                    'ip': ip
                })
            if raise_exc:
                raise
        else:
            try:
                if iser_enabled == 'True':
                    portal.iser = True
            except rtslib.utils.RTSLibError:
                print(
                    _('Error enabling iSER for NetworkPortal: please ensure '
                      'that RDMA is supported on your iSCSI port %(port)d '
                      'on ip %(ip)s.') % {
                          'port': portals_port,
                          'ip': ip
                      })
                raise