def setup_or_cleanup_iscsi(is_setup, is_login=True, emulated_image="emulated_iscsi", image_size="1G"): """ Set up(and login iscsi target) or clean up iscsi service on localhost. :param is_setup: Boolean value, true for setup, false for cleanup :param is_login: Boolean value, true for login, false for not login :param emulated_image: name of iscsi device :param image_size: emulated image's size :return: iscsi device name or iscsi target """ try: utils_misc.find_command("tgtadm") utils_misc.find_command("iscsiadm") except ValueError: raise error.TestNAError("Missing command 'tgtadm' and/or 'iscsiadm'.") tmpdir = os.path.join(data_dir.get_root_dir(), 'tmp') emulated_path = os.path.join(tmpdir, emulated_image) emulated_target = "iqn.2001-01.com.virttest:%s.target" % emulated_image iscsi_params = {"emulated_image": emulated_path, "target": emulated_target, "image_size": image_size, "iscsi_thread_id": "virt"} _iscsi = iscsi.Iscsi(iscsi_params) if is_setup: sv_status = None if utils_misc.selinux_enforcing(): sv_status = utils_selinux.get_status() utils_selinux.set_status("permissive") _iscsi.export_target() if sv_status is not None: utils_selinux.set_status(sv_status) if is_login: _iscsi.login() # The device doesn't necessarily appear instantaneously, so give # about 5 seconds for it to appear before giving up iscsi_device = utils_misc.wait_for(_iscsi.get_device_name, 5, 0, 1, "Searching iscsi device name.") if iscsi_device: logging.debug("iscsi device: %s", iscsi_device) return iscsi_device if not iscsi_device: logging.error("Not find iscsi device.") # Cleanup and return "" - caller needs to handle that # _iscsi.export_target() will have set the emulated_id and # export_flag already on success... _iscsi.cleanup() utils.run("rm -f %s" % emulated_path) else: return emulated_target else: _iscsi.export_flag = True _iscsi.emulated_id = _iscsi.get_target_id() _iscsi.cleanup() utils.run("rm -f %s" % emulated_path) return ""
def __init__(self, params, root_dir, tag): """ Init the default value for image object. :param params: Dictionary containing the test parameters. :param root_dir: Base directory for relative filenames. :param tag: Image tag defined in parameter images """ Rawdev.__init__(self, params, root_dir, tag) self.emulated_file_remove = False self.emulated_image = params.get("emulated_image") if self.emulated_image: if params.get("emulated_file_remove", "no") == "yes": self.emulated_file_remove = True params["iscsi_thread_id"] = self.image_name self.iscsidevice = iscsi.Iscsi(params, root_dir=root_dir) self.device_id = params.get("device_id") self.iscsi_init_timeout = int(params.get("iscsi_init_timeout", 10))
def login_iscsi(emulated_image, image_size): """ Login the iscsi target, and return the device """ utils.run("setenforce 0") emulated_image_path = os.path.join(test.tmpdir, emulated_image) emulated_target = "iqn.2001-01.com.autotest:%s.target" % emulated_image iscsi_params = {"emulated_image": emulated_image_path, "target": emulated_target, "image_size": image_size} _iscsi = iscsi.Iscsi(iscsi_params) _iscsi.export_target() _iscsi.login() iscsi_device = _iscsi.get_device_name() logging.debug("iscsi device: %s", iscsi_device) utils.run("setenforce 1") return iscsi_device