def _get_single_tpg(iqn): """ Returns TPG object for given iqn, assuming that each target has a single TPG. """ tpg = rtslib.TPG(rtslib.Target(rtslib.FabricModule('iscsi'), iqn), 1) return tpg
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
def create_tpg(iqn): """ Creates new TPG on iscsi target with given iqn. """ logger.info("trying to create new tpg for %s" % iqn) try: rtslib.TPG(rtslib.Target(rtslib.FabricModule('iscsi'), iqn), _next_free_tpg_index(iqn)) except: raise return None
def _createTarget(self, iqn, imgPath, rollback): '''Using LIO Python binding to configure iSCSI target. LIO can export various types of backend storage object as LUN, and support many fabric modules like iSCSI, FCoE. The backstores/fileio/image hierachy and iscsi/target/tpg/lun hierarchy are managed separately. Their lifecycles are independent. Create the backstore hierachy and the iSCSI hierachy, then attach the image file to the lun. For more infomation, see http://www.linux-iscsi.org/wiki/ISCSI . ''' fio = rtslib.FileIOStorageObject(os.path.basename(imgPath), imgPath, os.path.getsize(imgPath)) rollback.prependDefer(fio.delete) iscsiMod = rtslib.FabricModule('iscsi') tgt = rtslib.Target(iscsiMod, iqn, mode='create') # Target.delete() will delete all # TPGs, ACLs, LUNs, Portals recursively rollback.prependDefer(tgt.delete) # TPG is a group of network portals tpg = rtslib.TPG(tgt, None, mode='create') rtslib.LUN(tpg, None, fio) # Enable demo mode, grant all initiators to access all LUNs in the TPG tpg.set_attribute('generate_node_acls', '1') tpg.set_attribute('cache_dynamic_acls', '1') # Do not use any authentication methods tpg.set_attribute('authentication', '0') # Allow writing to LUNs in demo mode tpg.set_attribute('demo_mode_write_protect', '0') # Activate the TPG otherwise it is not able to access the LUNs in it tpg.enable = True # Bind to '127.0.0.1' so it's OK to use demo mode just for testing rtslib.NetworkPortal(tpg, self.address, 3260, mode='create')
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
#!/usr/bin/env python import rtslib iscsi = rtslib.FabricModule("iscsi") f = rtslib.FileIOStorageObject("test1", "/tmp/test.img", 20000000000) f2 = rtslib.FileIOStorageObject("test2", "/tmp/test2.img", 20000000000) target = rtslib.Target(iscsi) tpg = rtslib.TPG(target, 1) tpg.enable = True tpg.set_attribute("authentication", False) tpg.set_attribute("demo_mode_write_protect", False) portal = rtslib.NetworkPortal(tpg, "0.0.0.0", 3260) lun = rtslib.LUN(tpg, 0, f) lun2 = rtslib.LUN(tpg, 1, f2) tpg.set_attribute("cache_dynamic_acls", True) tpg.set_attribute("generate_node_acls", True) print target.wwn