Skip to content

Commit

Permalink
Replace WMIC with powershell cmdlet
Browse files Browse the repository at this point in the history
From Win11-24H2, Windows remove WMIC, so replace it
with some powershell cmdlet

Signed-off-by: Leidong Wang <[email protected]>
  • Loading branch information
leidwang committed Dec 24, 2024
1 parent 460d4fb commit ea85065
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 31 deletions.
5 changes: 3 additions & 2 deletions provider/block_devices_plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion provider/throttle_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion provider/win_driver_installer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 27 additions & 9 deletions provider/win_driver_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions qemu/deps/win_driver_install/win_driver_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion qemu/tests/balloon_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion qemu/tests/qemu_guest_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 7 additions & 13 deletions qemu/tests/single_driver_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 "
Expand Down
2 changes: 1 addition & 1 deletion qemu/tests/virtio_win_installer_version_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit ea85065

Please sign in to comment.