|
| 1 | +import os |
| 2 | + |
| 3 | +from avocado.utils import cpu |
| 4 | +from virttest import data_dir as virttest_data_dir |
| 5 | +from virttest import env_process, error_context |
| 6 | +from virttest.utils_misc import verify_dmesg |
| 7 | + |
| 8 | + |
| 9 | +@error_context.context_aware |
| 10 | +def run(test, params, env): |
| 11 | + """ |
| 12 | + Snp direct kernel boot test: |
| 13 | + 1. Check host snp capability |
| 14 | + 2. Boot snp VM with direct kernel boot |
| 15 | + 3. Verify snp enabled in guest |
| 16 | + 4. Verify attestation |
| 17 | +
|
| 18 | + :param test: QEMU test object |
| 19 | + :param params: Dictionary with the test parameters |
| 20 | + :param env: Dictionary with test environment. |
| 21 | + """ |
| 22 | + |
| 23 | + error_context.context("Start sev-snp test", test.log.info) |
| 24 | + timeout = params.get_numeric("login_timeout", 240) |
| 25 | + |
| 26 | + snp_module_path = params["snp_module_path"] |
| 27 | + if os.path.exists(snp_module_path): |
| 28 | + with open(snp_module_path) as f: |
| 29 | + output = f.read().strip() |
| 30 | + if output not in params.objects("module_status"): |
| 31 | + test.cancel("Host sev-snp support check fail.") |
| 32 | + else: |
| 33 | + test.cancel("Host sev-snp support check fail.") |
| 34 | + |
| 35 | + family_id = cpu.get_family() |
| 36 | + model_id = cpu.get_model() |
| 37 | + dict_cpu = {"251": "milan", "2517": "genoa", "2617": "turin"} |
| 38 | + key = str(family_id) + str(model_id) |
| 39 | + host_cpu_model = dict_cpu.get(key, "unknown") |
| 40 | + |
| 41 | + params["start_vm"] = "yes" |
| 42 | + guest_name = params.get("guest_name") |
| 43 | + params["kernel"] = f"images/{guest_name}/vmlinuz" |
| 44 | + params["initrd"] = f"images/{guest_name}/initrd.img" |
| 45 | + |
| 46 | + vm_name = params["main_vm"] |
| 47 | + env_process.preprocess_vm(test, params, env, vm_name) |
| 48 | + vm = env.get_vm(vm_name) |
| 49 | + vm.verify_alive() |
| 50 | + session = vm.wait_for_login(timeout=timeout) |
| 51 | + verify_dmesg() |
| 52 | + guest_check_cmd = params["snp_guest_check"] |
| 53 | + try: |
| 54 | + session.cmd_output(guest_check_cmd, timeout=240) |
| 55 | + except Exception as e: |
| 56 | + test.fail("Guest snp verify fail: %s" % str(e)) |
| 57 | + else: |
| 58 | + # Verify attestation |
| 59 | + error_context.context("Start to do attestation", test.log.info) |
| 60 | + guest_dir = params["guest_dir"] |
| 61 | + host_script = params["host_script"] |
| 62 | + guest_cmd = params["guest_cmd"] |
| 63 | + deps_dir = virttest_data_dir.get_deps_dir() |
| 64 | + host_file = os.path.join(deps_dir, host_script) |
| 65 | + try: |
| 66 | + vm.copy_files_to(host_file, guest_dir) |
| 67 | + session.cmd_output(params["guest_tool_install"], timeout=240) |
| 68 | + session.cmd_output("chmod 755 %s" % guest_cmd) |
| 69 | + except Exception as e: |
| 70 | + test.fail("Guest test preparation fail: %s" % str(e)) |
| 71 | + guest_cmd = guest_cmd + " " + host_cpu_model |
| 72 | + s = session.cmd_status(guest_cmd, timeout=360) |
| 73 | + if s: |
| 74 | + test.fail("Guest script error") |
| 75 | + finally: |
| 76 | + session.close() |
| 77 | + vm.destroy() |
0 commit comments