def postprocess_remote_storage(): """ Logout from target. """ image_name = params.get("images").split()[0] base_dir = params.get("images_base_dir", data_dir.get_data_dir()) iscsidevice = qemu_storage.Iscsidev(params, base_dir, image_name) iscsidevice.cleanup()
def preprocess_remote_storage(): """ Prepare remote ISCSI storage for block image, and login session for iscsi device. """ image_name = params.get("images").split()[0] base_dir = params.get("images_base_dir", data_dir.get_data_dir()) iscsidevice = qemu_storage.Iscsidev(params, base_dir, image_name) iscsidevice.setup()
def clean(self): params = self.parser_test_args() if params.get("target_image_type") == "iscsi": image = qemu_storage.Iscsidev(params, self.data_dir, "") # cleanup iscsi disk to ensure it works for other test utils.run("dd if=/dev/zero of=%s bs=1M count=512" % self.target_image) image.cleanup() elif params.get("target_image_type") == "nfs": image = nfs.Nfs(params) image.cleanup() super(DriveMirror, self).clean()
def prepare_disk(path, disk_format): """ Prepare the disk for a given disk format. """ disk = {} # Check if we test with a non-existed disk. if os.path.split(path)[-1].startswith("notexist."): disk.update({"format": disk_format, "source": path}) elif disk_format == "scsi": scsi_option = params.get("virt_disk_device_scsi_option", "") disk_source = libvirt.create_scsi_disk(scsi_option) if disk_source: disk.update({"format": "scsi", "source": disk_source}) else: raise error.TestNAError("Get scsi disk failed") elif disk_format in ["iso", "floppy"]: disk_path = libvirt.create_local_disk(disk_format, path) disk.update({"format": disk_format, "source": disk_path}) elif disk_format == "nfs": nfs_disk_type = params.get("nfs_disk_type", None) disk.update(setup_nfs_disk(os.path.split(path)[-1], nfs_disk_type)) elif disk_format == "iscsi": # Create iscsi device if needed. disk_dev = qemu_storage.Iscsidev(params, os.path.dirname(path), "iscsi") device_source = disk_dev.setup() logging.debug("iscsi dev name: %s", device_source) # Format the disk and make file system. open("/tmp/fdisk-cmd", "w").write("n\np\n\n\n\nw\n") output = utils.run("fdisk %s < /tmp/fdisk-cmd" % device_source).stdout.strip() logging.debug("fdisk output: %s", output) output = utils.run("mkfs.ext3 %s1" % device_source).stdout.strip() logging.debug("mkfs output: %s", output) device_source += "1" disk.update({ "format": disk_format, "disk_dev": disk_dev, "source": device_source }) elif disk_format in ["raw", "qcow2"]: disk_size = params.get("virt_disk_device_size", "1") device_source = libvirt.create_local_disk("file", path, disk_size, disk_format=disk_format) disk.update({"format": disk_format, "source": device_source}) return disk
def clean(self): super(DriveMirror, self).clean() params = self.parser_test_args() if params.get("image_type") == "iscsi": params["host_setup_flag"] = int(params["host_setup_flag"]) qemu_img = utils_misc.get_qemu_img_binary(self.params) # Reformat it to avoid impact other test cmd = "%s create -f %s %s %s" % (qemu_img, params["image_format"], self.target_image, params["image_size"]) utils.system(cmd) image = qemu_storage.Iscsidev(params, self.data_dir, params["target_image"]) image.cleanup() elif params.get("image_type") == "nfs": image = nfs.Nfs(params) image.cleanup()
def __init__(self, test, params): self.td = None self.cpu_num = int(params.get("cpu_num", "1")) self.vm_name = params.get("main_vm") self.vm_new_name = params.get("vm_new_name") self.cgroup_name = params.get("cgroup_name") self.cgroup_dir = params.get("cgroup_dir") self.new_image_file = params.get("new_image_file") if self.new_image_file: self.new_image_file = os.path.join(test.virtdir, self.new_image_file) self.time_out = int(params.get("time_out", "600")) self.cpu_status = utils_misc.get_cpu_status(self.cpu_num) self.twice_execute = "yes" == params.get("twice_execute", "no") self.kill_first = "yes" == params.get("kill_first", "no") if params.get("abnormal_type") in ["disk_lack", ""]: self.selinux_enforcing = utils_selinux.is_enforcing() if self.selinux_enforcing: utils_selinux.set_status("permissive") self.fs_type = params.get("fs_type", "ext4") xml_file = vm_xml.VMXML.new_from_inactive_dumpxml(self.vm_name) disk_node = xml_file.get_disk_all()['vda'] source_file = disk_node.find('source').get('file') self.image_size = utils_misc.get_image_info(source_file)['dsize'] # Set the size to be image_size iscsi_size = "%sM" % (self.image_size / 1024 / 1024) params['image_size'] = iscsi_size self.iscsi_dev = qemu_storage.Iscsidev(params, test.virtdir, "iscsi") try: device_source = self.iscsi_dev.setup() except (exceptions.TestError, ValueError) as detail: self.iscsi_dev.cleanup() self.test.cancel("Cannot get iscsi device on this" " host:%s\n" % detail) libvirt.mk_label(device_source) libvirt.mk_part(device_source, iscsi_size) self.mount_dir = os.path.join(test.virtdir, params.get('mount_dir')) if not os.path.exists(self.mount_dir): os.mkdir(self.mount_dir) params['mount_dir'] = self.mount_dir self.partition = device_source + "1" libvirt.mkfs(self.partition, self.fs_type) utils_misc.mount(self.partition, self.mount_dir, self.fs_type) self.new_image_file = os.path.join(self.mount_dir, "new_file")
def get_target_image(self): params = self.parser_test_args() t_params = {} t_params["image_name"] = params["target_image"] t_params["image_format"] = params["target_format"] target_image = storage.get_image_filename(t_params, self.data_dir) if params.get("target_image_type") == "nfs": image = nfs.Nfs(params) image.setup() # sleep 30s to wait nfs ready, it's requried by some rhel6 host time.sleep(30) elif params.get("target_image_type") == "iscsi": image = qemu_storage.Iscsidev(params, self.data_dir, "") target_image = image.setup() if (params["create_mode"] == "existing" and not os.path.exists(target_image)): image = qemu_storage.QemuImg(t_params, self.data_dir, "") image.create(t_params) return target_image
def get_target_image(self): params = self.parser_test_args() target_image = storage.get_image_filename(params, self.data_dir) if params.get("image_type") == "nfs": image = nfs.Nfs(params) image.setup() utils_misc.wait_for(lambda: os.path.ismount(image.mount_dir), timeout=30) elif params.get("image_type") == "iscsi": image = qemu_storage.Iscsidev(params, self.data_dir, params["target_image"]) return image.setup() if (params["create_mode"] == "existing" and not os.path.exists(target_image)): image = qemu_storage.QemuImg(params, self.data_dir, params["target_image"]) image.create(params) return target_image
vm_name = params.get("main_vm") vm = env.get_vm(vm_name) if vm.is_alive(): vm.destroy(gracefully=False) # Back up xml file. backup_xml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) if source_path: device_source = os.path.join(test.virtdir, device_source_name) else: device_source = device_source_name # Create virtual device file. if test_block_dev: try: iscsi_dev = qemu_storage.Iscsidev(params, test.virtdir, "iscsi") device_source = iscsi_dev.setup() logging.debug("iscsi dev name: %s" % device_source) except error.TestError: # We should skip this case raise error.TestNAError("Can not get iscsi device name in host") else: create_device_file(device_source) if vm.is_alive(): vm.destroy(gracefully=False) # if we are testing audit, we need to start audit servcie first. if test_audit: auditd_service = Factory.create_service("auditd") if not auditd_service.status():
def run(test, params, env): """ Test virsh {at|de}tach-disk command for lxc. The command can attach new disk/detach disk. 1.Prepare test environment,destroy or suspend a VM. 2.Perform virsh attach/detach-disk operation. 3.Recover test environment. 4.Confirm the test result. """ vm_ref = params.get("at_dt_disk_vm_ref", "name") at_options = params.get("at_dt_disk_at_options", "") dt_options = params.get("at_dt_disk_dt_options", "") pre_vm_state = params.get("at_dt_disk_pre_vm_state", "running") status_error = "yes" == params.get("status_error", 'no') no_attach = params.get("at_dt_disk_no_attach", 'no') # Get test command. test_cmd = params.get("at_dt_disk_test_cmd", "attach-disk") # Disk specific attributes. device_source = params.get("at_dt_disk_device_source", "/dev/sdc1") device_target = params.get("at_dt_disk_device_target", "vdd") test_twice = "yes" == params.get("at_dt_disk_test_twice", "no") test_audit = "yes" == params.get("at_dt_disk_check_audit", "no") serial = params.get("at_dt_disk_serial", "") address = params.get("at_dt_disk_address", "") address2 = params.get("at_dt_disk_address2", "") if serial: at_options += (" --serial %s" % serial) if address2: at_options_twice = at_options + (" --address %s" % address2) if address: at_options += (" --address %s" % address) vm_name = params.get("main_vm") vm = env.get_vm(vm_name) if vm.is_alive(): vm.destroy(gracefully=False) # Back up xml file. backup_xml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) # Create virtual device file if user doesn't prepare a partition. test_block_dev = False if device_source.count("ENTER"): try: iscsi_dev = qemu_storage.Iscsidev(params, test.virtdir, "iscsi") device_source = iscsi_dev.setup() logging.debug("iscsi dev name: %s" % device_source) test_block_dev = True except error.TestError: # We should skip this case raise error.TestNAError("Can not get iscsi device name in host") if vm.is_alive(): vm.destroy(gracefully=False) # if we are testing audit, we need to start audit servcie first. if test_audit: auditd_service = Factory.create_service("auditd") if not auditd_service.status(): auditd_service.start() logging.info("Auditd service status: %s" % auditd_service.status()) # If we are testing detach-disk, we need to attach certain device first. if test_cmd == "detach-disk" and no_attach != "yes": s_attach = virsh.attach_disk(vm_name, device_source, device_target, "--config").exit_status if s_attach != 0: logging.error("Attaching device failed before testing detach-disk") if test_twice: device_target2 = params.get("at_dt_disk_device_target2", device_target) s_attach = virsh.attach_disk(vm_name, device_source, device_target2, "--config").exit_status if s_attach != 0: logging.error("Attaching device failed before testing " "detach-disk test_twice") vm.start() # Turn VM into certain state. if pre_vm_state == "paused": logging.info("Suspending %s..." % vm_name) if vm.is_alive(): vm.pause() elif pre_vm_state == "shut off": logging.info("Shuting down %s..." % vm_name) if vm.is_alive(): vm.destroy(gracefully=False) # Get disk count before test. disk_count_before_cmd = vm_xml.VMXML.get_disk_count(vm_name) # Test. domid = vm.get_id() domuuid = vm.get_uuid() # Confirm how to reference a VM. if vm_ref == "name": vm_ref = vm_name elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "uuid": vm_ref = domuuid else: vm_ref = "" if test_cmd == "attach-disk": status = virsh.attach_disk(vm_ref, device_source, device_target, at_options, debug=True).exit_status elif test_cmd == "detach-disk": status = virsh.detach_disk(vm_ref, device_target, dt_options, debug=True).exit_status if test_twice: device_target2 = params.get("at_dt_disk_device_target2", device_target) if test_cmd == "attach-disk": if address2: at_options = at_options_twice status = virsh.attach_disk(vm_ref, device_source, device_target2, at_options, debug=True).exit_status elif test_cmd == "detach-disk": status = virsh.detach_disk(vm_ref, device_target2, dt_options, debug=True).exit_status # Resume guest after command. On newer libvirt this is fixed as it has # been a bug. The change in xml file is done after the guest is resumed. if pre_vm_state == "paused": vm.resume() # Check audit log check_audit_after_cmd = True if test_audit: grep_audit = ('grep "%s" /var/log/audit/audit.log' % test_cmd.split("-")[0]) cmd = (grep_audit + ' | ' + 'grep "%s" | tail -n1 | grep "res=success"' % device_source) if utils.run(cmd).exit_status: logging.error("Audit check failed") check_audit_after_cmd = False # Check disk count after command. check_count_after_cmd = True disk_count_after_cmd = vm_xml.VMXML.get_disk_count(vm_name) if test_cmd == "attach-disk": if disk_count_after_cmd == disk_count_before_cmd: check_count_after_cmd = False elif test_cmd == "detach-disk": if disk_count_after_cmd < disk_count_before_cmd: check_count_after_cmd = False # Recover VM state. if pre_vm_state == "shut off": vm.start() # Check disk type after attach. check_disk_type = True try: check_disk_type = vm_xml.VMXML.check_disk_type(vm_name, device_source, "block") except xcepts.LibvirtXMLError: # No disk found check_disk_type = False # Check disk serial after attach. check_disk_serial = True if serial: disk_serial = vm_xml.VMXML.get_disk_serial(vm_name, device_target) if serial != disk_serial: check_disk_serial = False # Check disk address after attach. check_disk_address = True if address: disk_address = vm_xml.VMXML.get_disk_address(vm_name, device_target) if utils_test.canonicalize_disk_address(address) !=\ utils_test.canonicalize_disk_address(disk_address): check_disk_address = False # Check multifunction address after attach. check_disk_address2 = True if address2: disk_address2 = vm_xml.VMXML.get_disk_address(vm_name, device_target2) if utils_test.canonicalize_disk_address(address2) !=\ utils_test.canonicalize_disk_address(disk_address2): check_disk_address2 = False # Destroy VM. vm.destroy(gracefully=False) # Check disk count after VM shutdown (with --config). check_count_after_shutdown = True disk_count_after_shutdown = vm_xml.VMXML.get_disk_count(vm_name) if test_cmd == "attach-disk": if disk_count_after_shutdown == disk_count_before_cmd: check_count_after_shutdown = False elif test_cmd == "detach-disk": if disk_count_after_shutdown < disk_count_before_cmd: check_count_after_shutdown = False # Recover VM. if vm.is_alive(): vm.destroy(gracefully=False) backup_xml.sync() if test_block_dev: iscsi_dev.cleanup() # Check results. if status_error: if not status: raise error.TestFail("virsh %s exit with unexpected value." % test_cmd) else: if status: raise error.TestFail("virsh %s failed." % test_cmd) if test_cmd == "attach-disk": if at_options.count("config"): if not check_count_after_shutdown: raise error.TestFail("Cannot see config attached device " "in xml file after VM shutdown.") if not check_disk_serial: raise error.TestFail("Serial set failed after attach") if not check_disk_address: raise error.TestFail("Address set failed after attach") if not check_disk_address2: raise error.TestFail("Address(multifunction) set failed" " after attach") else: if not check_count_after_cmd: raise error.TestFail("Cannot see device in xml file" " after attach.") if not check_disk_type: raise error.TestFail("Check disk type failed after" " attach.") if not check_audit_after_cmd: raise error.TestFail("Audit hotplug failure after attach") if at_options.count("persistent"): if not check_count_after_shutdown: raise error.TestFail("Cannot see device attached " "with persistent after " "VM shutdown.") else: if check_count_after_shutdown: raise error.TestFail("See non-config attached device " "in xml file after VM shutdown.") elif test_cmd == "detach-disk": if dt_options.count("config"): if check_count_after_shutdown: raise error.TestFail("See config detached device in " "xml file after VM shutdown.") else: if check_count_after_cmd: raise error.TestFail("See device in xml file " "after detach.") if not check_audit_after_cmd: raise error.TestFail("Audit hotunplug failure " "after detach") if dt_options.count("persistent"): if check_count_after_shutdown: raise error.TestFail("See device deattached " "with persistent after " "VM shutdown.") else: if not check_count_after_shutdown: raise error.TestFail("See non-config detached " "device in xml file after " "VM shutdown.") else: raise error.TestError("Unknown command %s." % test_cmd)