Skip to content

Replace WMIC with powershell cmd #4034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

leidwang
Copy link
Contributor

@leidwang leidwang commented Nov 29, 2024

From Win11-24H2, WMIC is an optional feature for Windows, And it will be fully removed in the future.So replace the related cmd

ID: 3268

@leidwang leidwang force-pushed the wmic_replacement branch 2 times, most recently from 97073f0 to 194ab37 Compare November 29, 2024 12:59
@leidwang leidwang force-pushed the wmic_replacement branch 2 times, most recently from 1c9d9ea to 7c4813a Compare December 24, 2024 03:11
@leidwang leidwang marked this pull request as ready for review December 31, 2024 01:43
@leidwang leidwang force-pushed the wmic_replacement branch 6 times, most recently from 9f86791 to d1bcda5 Compare January 8, 2025 01:55
@leidwang
Copy link
Contributor Author

leidwang commented Jan 8, 2025

Hi @xiagao @heywji Would you please help test this MR with our acceptance test, thanks.

@heywji
Copy link
Contributor

heywji commented Jan 15, 2025

Test 271 acceptance with win2022,win11,win1032 and win2016 guests, this patch worked.

@leidwang
Copy link
Contributor Author

Hi @XueqiangWei @nanliu-r @fbq815 Would you please help test this MR, it is mainly for wmic repalcement.Thanks.

@xiagao
Copy link
Contributor

xiagao commented Jan 16, 2025

Acceptance test looks good.
LGTM.

@mcasquer
Copy link
Contributor

balloon_hotplug test cases now passed in Win2025

 (1/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.q35: STARTED
 (1/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.q35: PASS (50028.55 s)
 (2/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_reboot.q35: STARTED
 (2/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_reboot.q35: PASS (939.88 s)
 (3/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_system_reset.q35: STARTED
 (3/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_system_reset.q35: PASS (871.67 s)
 (4/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_shutdown_after_unplug.q35: STARTED
 (4/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_shutdown_after_unplug.q35: PASS (497.14 s)
 (5/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_shutdown_after_plug.q35: STARTED
 (5/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_shutdown_after_plug.q35: PASS (473.43 s)
 (6/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_migrate.migrate_after_hotplug.q35: STARTED
 (6/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_migrate.migrate_after_hotplug.q35: PASS (601.20 s)
 (7/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_migrate.migrate_after_unplug.q35: STARTED
 (7/7) Host_RHEL.m9.u6.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.Win2025.x86_64.io-github-autotest-qemu.balloon_hotplug.with_migrate.migrate_after_unplug.q35: PASS (694.86 s)
RESULTS    : PASS 7 | ERROR 0 | FAIL 0 | SKIP 0 | WARN 0 | INTERRUPT 0 | CANCEL 0

Copy link
Contributor

@mcasquer mcasquer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@leidwang
Copy link
Contributor Author

Hi @yanan-fu @YongxueHong Would you please help review this MR?Thanks.

return out if out else []
c_name, c_value = cond.split("=")
cmd = (
'powershell -command "Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object {$_.%s -like %s}'
Copy link
Contributor

@mcasquer mcasquer Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The disk needs to be under single quotes, same you've done in utils_netperf.py and replace the existing ones by double quotes

Suggested change
'powershell -command "Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object {$_.%s -like %s}'
"powershell -command \"Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object {$_.%s -like '%s'}"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mcasquer Thank you for your suggestion, actually, c_value already has quotes, you can check line37.

return mapping
return {}
cmd = (
'powershell -command "Get-CimInstance -ClassName Win32_Diskdrive | Where-Object {$_.SerialNumber -eq %s}'
Copy link
Contributor

@mcasquer mcasquer Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Suggested change
'powershell -command "Get-CimInstance -ClassName Win32_Diskdrive | Where-Object {$_.SerialNumber -eq %s}'
"powershell -command \"Get-CimInstance -ClassName Win32_Diskdrive | Where-Object {$_.SerialNumber -eq '%s'}"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let me confirm it and update the code.

results.append(vals[0])
else:
results.append(dict(zip(keys, vals)))
return results if results else []
Copy link
Contributor

@mcasquer mcasquer Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leidwang
Another comment here, this implementation seems to no longer return a map, so this will probably break all the calls to this function.

Comment on lines +1685 to +1691
octets = subnet_mask.split(".")

prefix_length = 0
for octet in octets:
prefix_length += bin(int(octet)).count("1")

return prefix_length
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
octets = subnet_mask.split(".")
prefix_length = 0
for octet in octets:
prefix_length += bin(int(octet)).count("1")
return prefix_length
return sum(bin(int(octet)).count("1") for octet in subnet_mask.split("."))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @PaulYuuu
Would you like to help to review it?
Thanks. CC @nickzhq

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that the netmask in set_guest_ip_addr can be CIDR AND dot-decimal notation, only when users give a dot-decimal notation, we have to convert it to a bitmask.

However, the new function is a duplicate of convert_netmask in utils_net module, and set_guest_ip_addr also used it.

nic_ifname = get_linux_ifname(session, mac)
if session.cmd_status("which ip") != 0:
info_cmd = "ifconfig -a; ethtool -S %s" % nic_ifname
cmd = "ifconfig %s %s netmask %s" % (nic_ifname, ip_addr, netmask)
else:
if "." in netmask:
netmask = convert_netmask(netmask)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, the current convert_netmask can only handle dot-decimal notation, as we already import ipaddress, an enhancement here is to use it to handle all types of netmask.

>>> import ipaddress
>>> ip_addr = "192.168.1.1"
>>> netmask = "24" # or "255.255.255.0"
>>> ipaddr = ipaddress.IPv4Network(f"{ip_addr}/{netmask}", strict=False)
>>> ipaddr.netmask
IPv4Address('255.255.255.0')
>>> ipaddr.prefixlen
24

And then you can use it for linux/windows

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @YongxueHong @PaulYuuu , let me update my code.

@@ -3941,19 +3968,22 @@ def update_mac_ip_address(vm, timeout=240):


def get_windows_nic_attribute(
session, key, value, target, timeout=240, global_switch="nic"
session, key, value, target, timeout=240, global_switch="NetworkAdapter"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leidwang leidwang force-pushed the wmic_replacement branch 2 times, most recently from fb627bc to e8aaf97 Compare February 26, 2025 01:10

:return: volume ID.
"""
cmd = "wmic logicaldisk where (%s) get DeviceID" % condition
c_name, c_value = condition.split("=")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leidwang this logic seems to be incorrect. In the balloon_hotplug variants I am hitting the following:

[stdlog] 2025-03-03 15:09:30,148 avocado.test stacktrace       L0050 ERROR|   File "/usr/local/lib/python3.9/site-packages/avocado_framework_plugin_vt-109.0-py3.9.egg/virttest/utils_misc.py", line 2482, in get_win_disk_vol
[stdlog] 2025-03-03 15:09:30,148 avocado.test stacktrace       L0050 ERROR|     c_name, c_value = condition.split("=")
[stdlog] 2025-03-03 15:09:30,148 avocado.test stacktrace       L0050 ERROR| ValueError: not enough values to unpack (expected 2, got 1)

Which leads to:
avocado.core.exceptions.TestError: Could not get virtio-win disk vol!

Copy link
Contributor

@mcasquer mcasquer Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for balloon_service and balloon_memhp variants

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the full debug.log to issue3268, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leidwang sorry for the late reply, debug.log updated !

@@ -488,7 +488,7 @@ def get_guest_total_mem(cls, vm):
:return: physical memory report by guest OS in MB
"""
if vm.params.get("os_type") == "windows":
cmd = "wmic ComputerSystem get TotalPhysicalMemory"
cmd = 'powershell -command "(Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory"'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leidwang also hit a timeout with the following command, either executing it on the test loop or individually:
ERROR: Timeout expired while waiting for shell command to complete: 'powershell -command "(Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory"' (output: '')

@mcasquer
Copy link
Contributor

Hello @leidwang is there any plan to update this patch ? I am still hitting wmic command issues in Win11 recent test runs 😕

@leidwang
Copy link
Contributor Author

leidwang commented Apr 25, 2025

Hello @leidwang is there any plan to update this patch ? I am still hitting wmic command issues in Win11 recent test runs 😕

Hi @mcasquer I will update this MR recently, sorry for the delay.Currently, if you hit the issue that wmic not installed, you can try to run unattended_install test case again, it should be helpful.

Just want to explain more about why we will hit this issue. Win11-24H2 and Win2025 remove wmic, and does not provide installer for wmic, so we have to install it from MSFT, which then requires downloading something from the MSFT server,, so the wmic installation sometimes fails.

@mcasquer
Copy link
Contributor

Hello @leidwang is there any plan to update this patch ? I am still hitting wmic command issues in Win11 recent test runs 😕

Hi @mcasquer I will update this MR recently, sorry for the delay.Currently, if you hit the issue that wmic not installed, you can try to run unattended_install test case again, it should be helpful.

Just want to explain more about why we will hit this issue. Win11-24H2 and Win2025 remove wmic, and does not provide installer for wmic, so we have to install it from MSFT, which then requires downloading something from the MSFT server,, so the wmic installation sometimes fails.

@leidwang ok, I get the idea, but if I trigger an automated test loop I cannot run the unattended_install twice, which significantly affects automation purpose...

@leidwang
Copy link
Contributor Author

Hello @leidwang is there any plan to update this patch ? I am still hitting wmic command issues in Win11 recent test runs 😕

Hi @mcasquer I will update this MR recently, sorry for the delay.Currently, if you hit the issue that wmic not installed, you can try to run unattended_install test case again, it should be helpful.
Just want to explain more about why we will hit this issue. Win11-24H2 and Win2025 remove wmic, and does not provide installer for wmic, so we have to install it from MSFT, which then requires downloading something from the MSFT server,, so the wmic installation sometimes fails.

@leidwang ok, I get the idea, but if I trigger an automated test loop I cannot run the unattended_install twice, which significantly affects automation purpose...

Yes, we have to replace all wmic related command in our framework and test cases, then this issue can be fully fixed.

From Win11-24H2, WMIC is an optional feature for Windows,
And it will be fully removed in the future.So replace the
related cmd

Signed-off-by: Leidong Wang <[email protected]>
@qcheng-redhat
Copy link
Contributor

Tested with vTPM test cases. LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants