From ea85065a55ce31b7ca0494528d6ac54080ade230 Mon Sep 17 00:00:00 2001 From: Leidong Wang Date: Tue, 17 Dec 2024 15:40:59 +0800 Subject: [PATCH] Replace WMIC with powershell cmdlet From Win11-24H2, Windows remove WMIC, so replace it with some powershell cmdlet Signed-off-by: Leidong Wang --- provider/block_devices_plug.py | 5 +-- provider/throttle_utils.py | 6 +++- provider/win_driver_installer_test.py | 2 +- provider/win_driver_utils.py | 36 ++++++++++++++----- .../win_driver_install/win_driver_install.py | 5 +-- qemu/tests/balloon_check.py | 2 +- qemu/tests/qemu_guest_agent.py | 2 +- qemu/tests/single_driver_install.py | 20 ++++------- .../virtio_win_installer_version_check.py | 2 +- 9 files changed, 49 insertions(+), 31 deletions(-) diff --git a/provider/block_devices_plug.py b/provider/block_devices_plug.py index 731cf338b7..36a41dd54e 100644 --- a/provider/block_devices_plug.py +++ b/provider/block_devices_plug.py @@ -64,8 +64,9 @@ def wrapper(self, *args, **kwargs): step=1.5, ): disks_info_win = ( - "wmic logicaldisk get drivetype,name,description " - "& wmic diskdrive list brief /format:list" + 'powershell -command "Get-CimInstance Win32_LogicalDisk | ' + "Select-Object DriveType, Name, Description;" + 'Get-CimInstance Win32_DiskDrive | Select-Object *"' ) disks_info_linux = "lsblk -a" _session = self.vm.wait_for_login(timeout=360) diff --git a/provider/throttle_utils.py b/provider/throttle_utils.py index cf977dafa3..5492ff2129 100644 --- a/provider/throttle_utils.py +++ b/provider/throttle_utils.py @@ -200,7 +200,11 @@ def _get_drive_path(session, params, image): extra_params = image_params["blk_extra_params"] serial = re.search(r"(serial|wwn)=(\w+)", extra_params, re.M).group(2) if os_type == "windows": - cmd = "wmic diskdrive where SerialNumber='%s' get Index,Name" + cmd = ( + 'powshell "Get-CimInstance Win32_DiskDrive' + " | Where-Object {$_.SerialNumber -eq %s}" + ' | Select-Object Index, Name"' + ) disks = session.cmd_output(cmd % serial) info = disks.splitlines() if len(info) > 1: diff --git a/provider/win_driver_installer_test.py b/provider/win_driver_installer_test.py index 9b033a6e64..b6a99612f1 100644 --- a/provider/win_driver_installer_test.py +++ b/provider/win_driver_installer_test.py @@ -65,7 +65,7 @@ def install_gagent(session, test, qemu_ga_pkg, gagent_install_cmd, gagent_pkg_in :param gagent_pkg_info_cmd: guest agent pkg info check command. """ LOG_JOB.info("Install 'qemu-guest-agent' package in guest.") - vol_virtio_key = "VolumeName like '%virtio-win%'" + vol_virtio_key = "VolumeName='virtio-win*'" vol_virtio = utils_misc.get_win_disk_vol(session, vol_virtio_key) qemu_ga_pkg_path = r"%s:\%s\%s" % (vol_virtio, "guest-agent", qemu_ga_pkg) gagent_install_cmd = gagent_install_cmd % qemu_ga_pkg_path diff --git a/provider/win_driver_utils.py b/provider/win_driver_utils.py index 7c6ce03a0b..d7f70d04f4 100644 --- a/provider/win_driver_utils.py +++ b/provider/win_driver_utils.py @@ -12,7 +12,7 @@ import aexpect from virttest import data_dir, error_context, utils_misc, utils_test from virttest.utils_version import VersionInterval -from virttest.utils_windows import system, virtio_win, wmic +from virttest.utils_windows import system, virtio_win LOG_JOB = logging.getLogger("avocado.test") @@ -60,14 +60,32 @@ def _pnpdrv_info(session, name_pattern, props=None): - """Get the driver props eg: InfName""" - cmd = wmic.make_query( - "path win32_pnpsigneddriver", - "DeviceName like '%s'" % name_pattern, - props=props, - get_swch=wmic.FMT_TYPE_LIST, + """ + Get the driver props eg: InfName + + :param session: The guest session object. + :param name_pattern: String with the device name + :param props: Dict witj the properties list + """ + cmd = ( + 'powershell "Get-CimInstance Win32_PnPSignedDriver' + " | Where-Object { $_.DeviceName -like '%s' }" + ' | Select-Object %s | Format-List *"' ) - return wmic.parse_list(session.cmd(cmd, timeout=QUERY_TIMEOUT)) + cmd = cmd % (name_pattern, ",".join(props)) + output = session.cmd_output(cmd, timeout=QUERY_TIMEOUT) + out = [] + for para in re.split("(?:\r?\n){2,}", output.strip()): + keys, vals = [], [] + for line in para.splitlines(): + key, val = line.split(":", 1) + keys.append(key.strip()) + vals.append(val.strip()) + if len(keys) == 1: + out.append(vals[0]) + else: + out.append(dict(zip(keys, vals))) + return out def uninstall_driver(session, test, devcon_path, driver_name, device_name, device_hwid): @@ -294,7 +312,7 @@ def copy_file_to_samepath(session, test, params): "Copy autoit scripts and virtio-win-guest-tools.exe " "to the same path." ) dst_path = r"C:\\" - vol_virtio_key = "VolumeName like '%virtio-win%'" + vol_virtio_key = "VolumeName='virtio-win*'" vol_virtio = utils_misc.get_win_disk_vol(session, vol_virtio_key) installer_path = r"%s:\%s" % (vol_virtio, "virtio-win-guest-tools.exe") diff --git a/qemu/deps/win_driver_install/win_driver_install.py b/qemu/deps/win_driver_install/win_driver_install.py index ecbdf5b06a..57738fb122 100644 --- a/qemu/deps/win_driver_install/win_driver_install.py +++ b/qemu/deps/win_driver_install/win_driver_install.py @@ -122,8 +122,9 @@ def get_current_driver_ver(device_name): """ key = r"\d*\.\d*\.\d*\.\d*" get_driver_ver_cmd = ( - "wmic path win32_pnpsigneddriver where" - " Devicename='%s' get driverversion" % device_name + 'powershell "Get-CimInstance Win32_PnPSignedDriver' + " | Where-Object {$_.DeviceName -eq %s}" + ' | Select-Object DriverVersion"' ) driver_version = os.popen(get_driver_ver_cmd).read() if not driver_version.strip(): diff --git a/qemu/tests/balloon_check.py b/qemu/tests/balloon_check.py index 0f8861d1c6..b05af99294 100644 --- a/qemu/tests/balloon_check.py +++ b/qemu/tests/balloon_check.py @@ -495,7 +495,7 @@ def get_disk_vol(self, session): :param session: VM session. """ - key = "VolumeName like 'virtio-win%'" + key = "VolumeName='virtio-win*'" try: return utils_misc.get_win_disk_vol(session, condition=key) except Exception: diff --git a/qemu/tests/qemu_guest_agent.py b/qemu/tests/qemu_guest_agent.py index 4209715c6b..8c46af7916 100644 --- a/qemu/tests/qemu_guest_agent.py +++ b/qemu/tests/qemu_guest_agent.py @@ -4383,7 +4383,7 @@ def get_qga_pkg_path(self, qemu_ga_pkg, test, session, params, vm): vm.copy_files_to(gagent_host_path, self.gagent_guest_dir) qemu_ga_pkg_path = r"%s\%s" % (self.gagent_guest_dir, qemu_ga_pkg) elif self.gagent_src_type == "virtio-win": - vol_virtio_key = "VolumeName like '%virtio-win%'" + vol_virtio_key = "VolumeName='%virtio-win*'" vol_virtio = utils_misc.get_win_disk_vol(session, vol_virtio_key) qemu_ga_pkg_path = r"%s:\%s\%s" % (vol_virtio, "guest-agent", qemu_ga_pkg) else: diff --git a/qemu/tests/single_driver_install.py b/qemu/tests/single_driver_install.py index bb74dc161f..fdf40ad393 100644 --- a/qemu/tests/single_driver_install.py +++ b/qemu/tests/single_driver_install.py @@ -4,7 +4,9 @@ from aexpect import ShellTimeoutError from virttest import error_context, utils_misc, utils_net from virttest.utils_test.qemu import windrv_verify_running -from virttest.utils_windows import virtio_win, wmic +from virttest.utils_windows import virtio_win + +from provider import win_driver_utils LOG_JOB = logging.getLogger("avocado.test") @@ -31,16 +33,6 @@ def _add_cert(session, cert_path, store): session.cmd(add_cmd, timeout=OPERATION_TIMEOUT) -def _pnpdrv_info(session, name_pattern, props=None): - cmd = wmic.make_query( - "path win32_pnpsigneddriver", - "DeviceName like '%s'" % name_pattern, - props=props, - get_swch=wmic.FMT_TYPE_LIST, - ) - return wmic.parse_list(session.cmd(cmd, timeout=QUERY_TIMEOUT)) - - def send_key(vm, key): # Send key to guest for i in key: @@ -122,7 +114,9 @@ def run(test, params, env): if params.get("need_uninstall", "no") == "yes": error_context.context("Uninstalling previous installed driver", test.log.info) - for inf_name in _pnpdrv_info(session, device_name, ["InfName"]): + for inf_name in win_driver_utils._pnpdrv_info( + session, device_name, ["InfName"] + ): pnp_cmd = "pnputil /delete-driver %s /uninstall /force" uninst_store_cmd = params.get("uninst_store_cmd", pnp_cmd) % inf_name status, output = session.cmd_status_output(uninst_store_cmd, inst_timeout) @@ -197,7 +191,7 @@ def run(test, params, env): session = vm.reboot(session) windrv_verify_running(session, test, driver_verifier) - ver_list = _pnpdrv_info(session, device_name, ["DriverVersion"]) + ver_list = win_driver_utils._pnpdrv_info(session, device_name, ["DriverVersion"]) if expected_ver not in ver_list: test.fail( "The expected driver version is '%s', but " diff --git a/qemu/tests/virtio_win_installer_version_check.py b/qemu/tests/virtio_win_installer_version_check.py index 177e8f11e6..37574b11ac 100644 --- a/qemu/tests/virtio_win_installer_version_check.py +++ b/qemu/tests/virtio_win_installer_version_check.py @@ -44,7 +44,7 @@ def run(test, params, env): # virtio-win-1.9.xx iso_label_ver = re.findall(ver_pattern, iso_label_name, re.I)[0] - vol_virtio_key = "VolumeName like '%virtio-win%'" + vol_virtio_key = "VolumeName='virtio-win*'" vol_virtio = utils_misc.get_win_disk_vol(session, vol_virtio_key) installer_ver = session.cmd_output(params["installer_chk_cmd"] % vol_virtio).strip() if not pkg_ver == iso_ver == iso_label_ver == installer_ver: