|
| 1 | +"""Attach the host 4k disk but QEMU is exposing a 512 to the Guest. |
| 2 | +Test the unaligned discard operation on the disk.""" |
| 3 | + |
| 4 | +import re |
| 5 | + |
| 6 | +from avocado.utils import process |
| 7 | +from virttest import env_process, error_context |
| 8 | +from virttest.utils_misc import get_linux_drive_path |
| 9 | + |
| 10 | + |
| 11 | +@error_context.context_aware |
| 12 | +def run(test, params, env): |
| 13 | + """ |
| 14 | + Qemu send unaligned discard test: |
| 15 | + 1) Load scsi_debug module with sector_size=4096 |
| 16 | + 2) Boot guest with scsi_debug emulated disk as extra data disk |
| 17 | + 3) Login the guest and execute blkdiscard commands on the data disk |
| 18 | + 4) The unaligned discard command succeeds on the virtio or scsi-hd disk |
| 19 | + Fails on the pass-through device |
| 20 | +
|
| 21 | + :param test: kvm test object |
| 22 | + :param params: Dictionary with the test parameters |
| 23 | + :param env: Dictionary with test environment. |
| 24 | + """ |
| 25 | + |
| 26 | + def _execute_io_in_guest(serial=None): |
| 27 | + drive = get_linux_drive_path(session, serial) |
| 28 | + if not drive: |
| 29 | + test.fail("Can not find disk by {}".format(serial)) |
| 30 | + |
| 31 | + io_cmd_num = params.get_numeric("guest_io_cmd_number") |
| 32 | + results_raw = params.get( |
| 33 | + "expected_results", params.get("expected_resultes", "") |
| 34 | + ).split() |
| 35 | + if len(results_raw) != io_cmd_num: |
| 36 | + test.cancel( |
| 37 | + "Mismatch: io_cmd_number={} but has {} items".format( |
| 38 | + io_cmd_num, len(results_raw) |
| 39 | + ) |
| 40 | + ) |
| 41 | + for i in range(io_cmd_num): |
| 42 | + cmd = params["guest_io_cmd{}".format(i)].format(drive) |
| 43 | + expected = int(results_raw[i]) |
| 44 | + rc, out = session.cmd_status_output(cmd) |
| 45 | + logger.debug( |
| 46 | + "guest_io_cmd%d: rc=%s, cmd=%r, out=%s", i, rc, cmd, out.strip() |
| 47 | + ) |
| 48 | + if rc != expected: |
| 49 | + test.fail( |
| 50 | + "Unexpected rc=%s:%s, cmd=%r, out=%s" |
| 51 | + % (rc, expected, cmd, out.strip()) |
| 52 | + ) |
| 53 | + |
| 54 | + def _get_scsi_debug_disk(): |
| 55 | + cmd = "lsscsi -g -w -s | grep scsi_debug || true" |
| 56 | + out = ( |
| 57 | + process.system_output(cmd, shell=True, ignore_status=True).decode().strip() |
| 58 | + ) |
| 59 | + logger.info("Host cmd output '%s'", out) |
| 60 | + if not out: |
| 61 | + test.log.warning("Can not find scsi_debug disk") |
| 62 | + return |
| 63 | + disk_info = [] |
| 64 | + for line in out.splitlines(): |
| 65 | + tokens = line.split() |
| 66 | + path = next((t for t in tokens if t.startswith("/dev/sd")), None) |
| 67 | + sg = next((t for t in tokens if t.startswith("/dev/sg")), None) |
| 68 | + size = next( |
| 69 | + (t for t in tokens if re.search(r"(?i)\d+(?:\.\d+)?[KMGTPE]B$", t)), |
| 70 | + None, |
| 71 | + ) |
| 72 | + wwn_tok = next((t for t in tokens if t.startswith("0x")), None) |
| 73 | + wwn = wwn_tok.split("0x", 1)[1] |
| 74 | + if not (path and sg): |
| 75 | + logger.warning("Unable to parse scsi_debug line: %s", line) |
| 76 | + continue |
| 77 | + disk_info.append( |
| 78 | + {"path": path, "sg": sg, "wwn": wwn, "size": size, "all": line} |
| 79 | + ) |
| 80 | + if not disk_info: |
| 81 | + test.fail("Parsed no scsi_debug devices from lsscsi output") |
| 82 | + return disk_info |
| 83 | + |
| 84 | + logger = test.log |
| 85 | + vm = None |
| 86 | + disk_wwn = None |
| 87 | + if params.get("get_scsi_device") == "yes": |
| 88 | + scsi_debug_devs = _get_scsi_debug_disk() |
| 89 | + if scsi_debug_devs: |
| 90 | + dev = scsi_debug_devs[0] |
| 91 | + disk_wwn = dev["wwn"] |
| 92 | + print(disk_wwn) |
| 93 | + if params["drive_format_stg1"] == "scsi-generic": |
| 94 | + params["image_name_stg1"] = dev["sg"] |
| 95 | + else: |
| 96 | + params["image_name_stg1"] = dev["path"] |
| 97 | + else: |
| 98 | + test.fail("Can not find scsi_debug devices") |
| 99 | + try: |
| 100 | + if params.get("not_preprocess", "no") == "yes": |
| 101 | + logger.debug("Ready boot VM : %s", params["images"]) |
| 102 | + env_process.process( |
| 103 | + test, |
| 104 | + params, |
| 105 | + env, |
| 106 | + env_process.preprocess_image, |
| 107 | + env_process.preprocess_vm, |
| 108 | + ) |
| 109 | + |
| 110 | + logger.info("Get the main VM") |
| 111 | + vm = env.get_vm(params["main_vm"]) |
| 112 | + vm.verify_alive() |
| 113 | + timeout = params.get_numeric("timeout", 300) |
| 114 | + session = vm.wait_for_login(timeout=timeout) |
| 115 | + serial = params.get("serial_stg1") |
| 116 | + _execute_io_in_guest(serial if serial else disk_wwn) |
| 117 | + |
| 118 | + logger.info("Ready to destroy vm") |
| 119 | + vm.destroy() |
| 120 | + finally: |
| 121 | + if vm and vm.is_alive(): |
| 122 | + vm.destroy(gracefully=False) |
0 commit comments