petri: consolidate hyper-v vm creation with wmi#3113
petri: consolidate hyper-v vm creation with wmi#3113tjones60 wants to merge 2 commits intomicrosoft:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the Hyper-V Petri backend VM creation path to reduce the number of PowerShell/WMI calls by consolidating creation-time configuration into a single WMI DefineSystem call (via a new New-CustomVM helper), aiming to speed up tests given the single-threaded WMI provider.
Changes:
- Introduces
New-CustomVM(WMI/DefineSystem-based) and routes VM creation through a consolidated argument struct (HyperVNewCustomVMArgs). - Refactors VMGS resource accessors and updates OpenHCL VMGS encryption-policy checks to use the new helpers.
- Adds compatibility plumbing to detect missing VSSD properties and fall back to OpenHCL command-line parameters when needed.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
petri/src/vm/openvmm/construct.rs |
Updates VMGS encryption-policy check to use the new encryption_policy() helper. |
petri/src/vm/mod.rs |
Refactors PetriVmgsResource accessors (vmgs(), disk(), encryption_policy()) and adds Firmware::openhcl_firmware(). |
petri/src/vm/hyperv/vm.rs |
Changes Hyper-V VM creation to take a single HyperVNewCustomVMArgs struct and call the consolidated creation path. |
petri/src/vm/hyperv/powershell.rs |
Adds new enums/flags and HyperVNewCustomVMArgs + run_new_customvm, including compatibility handling for missing VSSD properties. |
petri/src/vm/hyperv/mod.rs |
Switches backend VM creation flow to build HyperVNewCustomVMArgs from config and rely on consolidated creation. |
petri/src/vm/hyperv/hyperv.psm1 |
Adds New-CustomVM that uses WMI DefineSystem and performs initial VM settings in one call. |
petri/Cargo.toml |
Adds bitfield-struct dependency for the new flags type. |
Cargo.lock |
Records the new dependency in the lockfile. |
bc080b6 to
2d98efc
Compare
| $psd = Get-CimClass -Namespace $ROOT_HYPER_V_NAMESPACE -ClassName "Msvm_ProcessorSettingData" | New-CimInstance -ClientOnly -Property @{ | ||
| VirtualQuantity = $VpCount | ||
| ApicMode = $ApicMode | ||
| HwThreadsPerCore = $HwThreadsPerCore | ||
| MaxProcessorsPerNumaNode = $MaxProcessorsPerNumaNode | ||
| } | ||
|
|
There was a problem hiding this comment.
MaxProcessorsPerNumaNode is always written into Msvm_ProcessorSettingData, even when the caller did not intend to override it. With the current default of 1, this can produce an invalid or overly constrained topology relative to VirtualQuantity/VpCount. Gate this assignment behind a $null check (and make the parameter nullable) so the default Hyper-V behavior is preserved unless overridden.
| $psd = Get-CimClass -Namespace $ROOT_HYPER_V_NAMESPACE -ClassName "Msvm_ProcessorSettingData" | New-CimInstance -ClientOnly -Property @{ | |
| VirtualQuantity = $VpCount | |
| ApicMode = $ApicMode | |
| HwThreadsPerCore = $HwThreadsPerCore | |
| MaxProcessorsPerNumaNode = $MaxProcessorsPerNumaNode | |
| } | |
| $psdProperties = @{ | |
| VirtualQuantity = $VpCount | |
| ApicMode = $ApicMode | |
| HwThreadsPerCore = $HwThreadsPerCore | |
| } | |
| if ($null -ne $MaxProcessorsPerNumaNode) { | |
| $psdProperties["MaxProcessorsPerNumaNode"] = $MaxProcessorsPerNumaNode | |
| } | |
| $psd = Get-CimClass -Namespace $ROOT_HYPER_V_NAMESPACE -ClassName "Msvm_ProcessorSettingData" | New-CimInstance -ClientOnly -Property $psdProperties |
|
|
||
| let hyperv_args = { | ||
| let mut args = HyperVNewCustomVMArgs::from_config(&config, &properties)?; | ||
| args.firmware_file = igvm_file.clone(); |
There was a problem hiding this comment.
args.firmware_file points at temp_dir/.../igvm.bin and is passed into New-CustomVM before the IGVM is copied into that location and before ACLs are applied (the copy happens later in the OpenHCL block). VM creation can fail if DefineSystem validates/probes FirmwareFile during creation. Consider copying the IGVM into temp_dir (and applying an ACL readable by Hyper-V, e.g. the Virtual Machines group) before calling HyperVVM::new(), or avoid setting FirmwareFile until after the copy as the old flow did.
| args.firmware_file = igvm_file.clone(); |
Consolidate several PowerShell commands called during the creation of a Hyper-V VM into one using the
DefineSystemWMI method. This should help speed up tests, especially since the WMI provider is single threaded.