def export_target(self): """ Export target in localhost for emulated iscsi """ selinux_mode = None # create image disk if not os.path.isfile(self.emulated_image): process.system(self.create_cmd) else: emulated_image_size = os.path.getsize(self.emulated_image) // 1024 if emulated_image_size != self.emulated_expect_size: # No need to remvoe, rebuild is fine process.system(self.create_cmd) # confirm if the target exists and create iSCSI target cmd = "targetcli ls /iscsi 1" output = decode_to_text(process.system_output(cmd)) if not re.findall("%s$" % self.target, output, re.M): logging.debug("Need to export target in host") # Set selinux to permissive mode to make sure # iscsi target export successfully if utils_selinux.is_enforcing(): selinux_mode = utils_selinux.get_status() utils_selinux.set_status("permissive") # In fact, We've got two options here # # 1) Create a block backstore that usually provides the best # performance. We can use a block device like /dev/sdb or # a logical volume previously created, # (lvcreate -name lv_iscsi -size 1G vg) # 2) Create a fileio backstore, # which enables the local file system cache. # # This class Only works for emulated iscsi device, # So fileio backstore is enough and safe. # Create a fileio backstore device_cmd = ("targetcli /backstores/fileio/ create %s %s" % (self.device, self.emulated_image)) output = decode_to_text(process.system_output(device_cmd)) if "Created fileio" not in output: raise exceptions.TestFail("Failed to create fileio %s. (%s)" % (self.device, output)) # Create an IQN with a target named target_name target_cmd = "targetcli /iscsi/ create %s" % self.target output = decode_to_text(process.system_output(target_cmd)) if "Created target" not in output: raise exceptions.TestFail("Failed to create target %s. (%s)" % (self.target, output)) check_portal = "targetcli /iscsi/%s/tpg1/portals ls" % self.target portal_info = decode_to_text(process.system_output(check_portal)) if "0.0.0.0:3260" not in portal_info: # Create portal # 0.0.0.0 means binding to INADDR_ANY # and using default IP port 3260 portal_cmd = ("targetcli /iscsi/%s/tpg1/portals/ create %s" % (self.target, "0.0.0.0")) output = decode_to_text(process.system_output(portal_cmd)) if "Created network portal" not in output: raise exceptions.TestFail("Failed to create portal. (%s)" % output) if ("ipv6" == utils_net.IPAddress(self.portal_ip).version and self.portal_ip not in portal_info): # Ipv6 portal address can't be created by default, # create ipv6 portal if needed. portal_cmd = ("targetcli /iscsi/%s/tpg1/portals/ create %s" % (self.target, self.portal_ip)) output = decode_to_text(process.system_output(portal_cmd)) if "Created network portal" not in output: raise exceptions.TestFail("Failed to create portal. (%s)" % output) # Create lun lun_cmd = "targetcli /iscsi/%s/tpg1/luns/ " % self.target dev_cmd = "create /backstores/fileio/%s" % self.device output = decode_to_text(process.system_output(lun_cmd + dev_cmd)) luns = re.findall(r"Created LUN (\d+).", output) if not luns: raise exceptions.TestFail("Failed to create lun. (%s)" % output) self.luns = luns[0] # Set firewall if it's enabled output = decode_to_text( process.system_output("firewall-cmd --state", ignore_status=True)) if re.findall("^running", output, re.M): # firewall is running process.system("firewall-cmd --permanent --add-port=3260/tcp") process.system("firewall-cmd --reload") # Restore selinux if selinux_mode is not None: utils_selinux.set_status(selinux_mode) self.export_flag = True else: logging.info("Target %s has already existed!" % self.target) if self.chap_flag: # Set CHAP authentication on the exported target self.set_chap_auth_target() # Set CHAP authentication for initiator to login target if self.portal_visible(): self.set_chap_auth_initiator() else: # To enable that so-called "demo mode" TPG operation, # disable all authentication for the corresponding Endpoint. # which means grant access to all initiators, # so that they can access all LUNs in the TPG # without further authentication. auth_cmd = "targetcli /iscsi/%s/tpg1/ " % self.target attr_cmd = ("set attribute %s %s %s %s" % ("authentication=0", "demo_mode_write_protect=0", "generate_node_acls=1", "cache_dynamic_acls=1")) output = decode_to_text(process.system_output(auth_cmd + attr_cmd)) logging.info("Define access rights: %s" % output) # Discovery the target self.portal_visible() # Save configuration process.system("targetcli / saveconfig") # Restart iSCSI service restart_iscsid()
def get_expected_listen_ips(params, networks, expected_result): """ Predict listen IPs from parameters. """ def random_ip(version='ipv4'): """ Randomly select a valid ip with target version. """ ips = [ip for ip in utils_net.get_all_ips() if ip.version == version] return random.choice(ips) spice_listen_type = params.get("spice_listen_type", "not_set") if spice_listen_type == 'none': expected_spice_ips = [] elif spice_listen_type == 'network': net_type = params.get("spice_network_type", "vnet") spice_network = networks[net_type] expected_spice_ips = [utils_net.IPAddress(spice_network.ip)] else: if spice_listen_type == 'address': spice_listen = params.get("spice_listen_address", 'not_set') else: spice_listen = params.get("spice_listen", "not_set") if spice_listen == 'not_set': expected_spice_ips = [utils_net.IPAddress('127.0.0.1')] elif spice_listen == 'valid_ipv4': expected_spice_ips = [random_ip('ipv4')] elif spice_listen == 'valid_ipv6': expected_spice_ips = [random_ip('ipv6')] else: listen_ip = utils_net.IPAddress(spice_listen) if listen_ip == utils_net.IPAddress('0.0.0.0'): expected_spice_ips = [ip for ip in utils_net.get_all_ips() if ip.version == 'ipv4'] elif listen_ip == utils_net.IPAddress('::'): expected_spice_ips = [ip for ip in utils_net.get_all_ips()] else: expected_spice_ips = [listen_ip] for ip in expected_spice_ips: logging.debug("Expected SPICE IP: %s", ip) expected_result['spice_ips'] = expected_spice_ips vnc_listen_type = params.get("vnc_listen_type", "not_set") vnc_listen = params.get("vnc_listen", "not_set") if vnc_listen_type == 'none': expected_vnc_ips = [] elif vnc_listen_type == 'network': net_type = params.get("vnc_network_type", "vnet") vnc_network = networks[net_type] expected_vnc_ips = [utils_net.IPAddress(vnc_network.ip)] else: if vnc_listen_type == 'address': vnc_listen = params.get("vnc_listen_address", 'not_set') else: vnc_listen = params.get("vnc_listen", "not_set") if vnc_listen == 'not_set': expected_vnc_ips = [utils_net.IPAddress('127.0.0.1')] elif vnc_listen == 'valid_ipv4': expected_vnc_ips = [random_ip('ipv4')] elif vnc_listen == 'valid_ipv6': expected_vnc_ips = [random_ip('ipv6')] else: listen_ip = utils_net.IPAddress(vnc_listen) if listen_ip == utils_net.IPAddress('0.0.0.0'): expected_vnc_ips = [ip for ip in utils_net.get_all_ips() if ip.version == 'ipv4'] elif listen_ip == utils_net.IPAddress('::'): expected_vnc_ips = [ip for ip in utils_net.get_all_ips()] else: expected_vnc_ips = [listen_ip] for ip in expected_vnc_ips: logging.debug("Expected VNC IP: %s", ip) expected_result['vnc_ips'] = expected_vnc_ips