|
| 1 | +from virttest import data_dir, env_process, error_context |
| 2 | + |
| 3 | + |
| 4 | +@error_context.context_aware |
| 5 | +def run(test, params, env): |
| 6 | + """ |
| 7 | + Please make sure the guest installed with signed driver |
| 8 | + Verify Secure MOR control feature using Device Guard tool in Windows guest: |
| 9 | +
|
| 10 | + 1) Boot up a guest. |
| 11 | + 2) Check if Secure Boot is enable. |
| 12 | + 3) Download DG_Readiness_Tool and copy to guest. |
| 13 | + 4) Enable Device Guard and check the output. |
| 14 | + 5) Reboot guest. |
| 15 | + 6) Check the result of Device Guard. |
| 16 | + 7) Disable Device Guard and shutdown guest. |
| 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 | + def set_powershell_execute_policy(): |
| 24 | + """ |
| 25 | + Set PowerShell execution policy using the provided session. |
| 26 | + It is used when creating a new session. |
| 27 | +
|
| 28 | + :param cmd: The PowerShell command to set execution policy. |
| 29 | + """ |
| 30 | + error_context.context("Setting PowerShell execution policy.") |
| 31 | + status, output = session.cmd_status_output(executionPolicy_command) |
| 32 | + if status != 0: |
| 33 | + test.fail("Failed to set PowerShell execution policy: %s" % output) |
| 34 | + |
| 35 | + def check_secure_boot_enabled(): |
| 36 | + """ |
| 37 | + Checks if Secure Boot is enabled in the guest. |
| 38 | + """ |
| 39 | + error_context.context("Checking if Secure Boot is enabled in the guest") |
| 40 | + output = session.cmd_output(check_cmd) |
| 41 | + if "false" in output.lower(): |
| 42 | + test.fail("Secure Boot is not enabled: %s" % output) |
| 43 | + |
| 44 | + def copy_dg_readiness_tool(): |
| 45 | + """ |
| 46 | + Copies the Device Guard Readiness tool from the host to the guest VM. |
| 47 | + """ |
| 48 | + dgreadiness_host_path = data_dir.get_deps_dir("dgreadiness") |
| 49 | + dst_path = params["dst_path"] |
| 50 | + test.log.info("Copy Device Guard tool to guest.") |
| 51 | + s, o = session.cmd_status_output("mkdir %s" % dst_path) |
| 52 | + if s and "already exists" not in o: |
| 53 | + test.error( |
| 54 | + "Could not create Device Guard directory in " |
| 55 | + "VM '%s', detail: '%s'" % (vm.name, o) |
| 56 | + ) |
| 57 | + vm.copy_files_to(dgreadiness_host_path, dst_path) |
| 58 | + |
| 59 | + def check_vbs_ready(): |
| 60 | + """ |
| 61 | + Check the status of Virtualization-Based Security (VBS) using the provided |
| 62 | + session. |
| 63 | +
|
| 64 | + :return: True if VBS is enabled, False otherwise. |
| 65 | + """ |
| 66 | + status, output = session.cmd_status_output(ready_command) |
| 67 | + if status != 0: |
| 68 | + test.fail("Failed to check VBS status: %s" % output) |
| 69 | + if vbs_ready_info in output: |
| 70 | + test.log.info("VBS is already enabled, and guest boot up successfully") |
| 71 | + return True |
| 72 | + else: |
| 73 | + test.log.info( |
| 74 | + "VBS is not enabled or the expected info was not found in the output" |
| 75 | + ) |
| 76 | + return False |
| 77 | + |
| 78 | + def run_device_guard_tool(cmd, expect_info): |
| 79 | + """ |
| 80 | + Executes the Device Guard Readiness Tool command in the guest to enable |
| 81 | + or disable Virtualization-Based Security (VBS). |
| 82 | +
|
| 83 | + :param cmd: The command to enable or disable VBS. |
| 84 | + """ |
| 85 | + error_context.context("running device guard readiness tool with %s" % cmd) |
| 86 | + output = session.cmd_output(cmd, 360) |
| 87 | + if expect_info not in output: |
| 88 | + test.fail("Failed to enable VBS: %s" % output) |
| 89 | + |
| 90 | + def install_wsl2_and_rhel(): |
| 91 | + """ |
| 92 | + Install WSL2 and start RHEL distribution in Windows VM. |
| 93 | + This function is called after VBS verification (step 5). |
| 94 | + """ |
| 95 | + error_context.context("Installing WSL2 and RHEL distribution") |
| 96 | + |
| 97 | + # Enable WSL feature |
| 98 | + test.log.info("Enabling WSL feature...") |
| 99 | + status, output = session.cmd_status_output(wsl_enable_cmd, timeout=300) |
| 100 | + if status != 0: |
| 101 | + test.fail("Failed to enable WSL feature: %s" % output) |
| 102 | + |
| 103 | + # Enable Virtual Machine Platform |
| 104 | + test.log.info("Enabling Virtual Machine Platform...") |
| 105 | + status, output = session.cmd_status_output(vm_platform_cmd, timeout=300) |
| 106 | + if status != 0: |
| 107 | + test.fail("Failed to enable VM Platform: %s" % output) |
| 108 | + |
| 109 | + # Reboot to apply WSL2 features |
| 110 | + test.log.info("Rebooting to apply WSL2 features...") |
| 111 | + vm.reboot(timeout=login_timeout) |
| 112 | + new_session = vm.wait_for_serial_login(timeout=login_timeout) |
| 113 | + set_powershell_execute_policy() |
| 114 | + new_session.cmd(dgreadiness_path_command) |
| 115 | + |
| 116 | + # Set WSL2 as default |
| 117 | + test.log.info("Setting WSL2 as default version...") |
| 118 | + status, output = new_session.cmd_status_output(wsl_set_default_cmd, timeout=60) |
| 119 | + if status != 0: |
| 120 | + test.fail("Failed to set WSL2 default: %s" % output) |
| 121 | + |
| 122 | + # Install RHEL distribution |
| 123 | + test.log.info("Installing RHEL distribution...") |
| 124 | + status, output = new_session.cmd_status_output(rhel_install_cmd, timeout=600) |
| 125 | + if status != 0: |
| 126 | + test.fail("Failed to install RHEL: %s" % output) |
| 127 | + |
| 128 | + # Verify WSL2 and RHEL installation |
| 129 | + test.log.info("Verifying WSL2 and RHEL...") |
| 130 | + status, output = new_session.cmd_status_output(wsl_list_cmd, timeout=60) |
| 131 | + if status != 0: |
| 132 | + test.fail("Failed to list WSL distributions: %s" % output) |
| 133 | + if rhel_distro_name not in output: |
| 134 | + test.fail("RHEL distribution not found: %s" % output) |
| 135 | + |
| 136 | + # Test RHEL functionality |
| 137 | + test.log.info("Testing RHEL in WSL2...") |
| 138 | + status, output = new_session.cmd_status_output(rhel_test_cmd, timeout=120) |
| 139 | + if status != 0: |
| 140 | + test.fail("RHEL test failed: %s" % output) |
| 141 | + test.log.info("WSL2 with RHEL installed and verified successfully") |
| 142 | + return new_session |
| 143 | + |
| 144 | + login_timeout = int(params.get("login_timeout", 360)) |
| 145 | + params["ovmf_vars_filename"] = "OVMF_VARS.secboot.fd" |
| 146 | + params["clone_master"] = "yes" |
| 147 | + params["master_images_clone"] = "image1" |
| 148 | + params["remove_image_image1"] = "yes" |
| 149 | + params["start_vm"] = "yes" |
| 150 | + env_process.preprocess_vm(test, params, env, params["main_vm"]) |
| 151 | + vm = env.get_vm(params["main_vm"]) |
| 152 | + session = vm.wait_for_serial_login(timeout=login_timeout) |
| 153 | + |
| 154 | + check_cmd = params["check_secure_boot_enabled_cmd"] |
| 155 | + dgreadiness_path_command = params["dgreadiness_path_cmd"] |
| 156 | + executionPolicy_command = params["set_ps_policy_cmd"] |
| 157 | + enable_command = params["vbs_enable_cmd"] |
| 158 | + disable_command = params["vbs_disable_cmd"] |
| 159 | + ready_command = params["vbs_ready_cmd"] |
| 160 | + vbs_ready_info = params["vbs_ready_info"] |
| 161 | + vbs_enable_info = params["vbs_enable_info"] |
| 162 | + vbs_disable_info = params["vbs_disable_info"] |
| 163 | + wsl_enable_cmd = params["wsl_enable_cmd"] |
| 164 | + vm_platform_cmd = params["vm_platform_cmd"] |
| 165 | + wsl_set_default_cmd = params["wsl_set_default_cmd"] |
| 166 | + rhel_install_cmd = params["rhel_install_cmd"] |
| 167 | + wsl_list_cmd = params["wsl_list_cmd"] |
| 168 | + rhel_distro_name = params["rhel_distro_name"] |
| 169 | + rhel_test_cmd = params["rhel_test_cmd"] |
| 170 | + |
| 171 | + try: |
| 172 | + check_secure_boot_enabled() |
| 173 | + copy_dg_readiness_tool() |
| 174 | + set_powershell_execute_policy() |
| 175 | + session.cmd(dgreadiness_path_command) |
| 176 | + if not check_vbs_ready(): |
| 177 | + run_device_guard_tool(enable_command, vbs_enable_info) |
| 178 | + vm.reboot(timeout=login_timeout) |
| 179 | + session = vm.wait_for_serial_login(timeout=login_timeout) |
| 180 | + session.cmd(dgreadiness_path_command) |
| 181 | + set_powershell_execute_policy() |
| 182 | + if not check_vbs_ready(): |
| 183 | + test.fail("VBS is not enabled after reboot.") |
| 184 | + |
| 185 | + session = install_wsl2_and_rhel() |
| 186 | + run_device_guard_tool(disable_command, vbs_disable_info) |
| 187 | + except Exception as e: |
| 188 | + test.fail(f"Test failed: {e}") |
| 189 | + else: |
| 190 | + test.log.info("Test completed successfully.") |
| 191 | + finally: |
| 192 | + if vm.is_alive(): |
| 193 | + vm.destroy() |
| 194 | + if session: |
| 195 | + session.close() |
0 commit comments