mirror of
https://github.com/GoodOlClint/PSProxmoxVE.git
synced 2026-08-28 07:56:57 +00:00
feat: disk controller/IO options on New-PveVm + surface all Get-PveVmConfig keys
New-PveVm (F089): - Add -DiskBus (virtio/scsi/sata/ide, default virtio), -ScsiHardware (scsihw), -DiskIoThread, -DiskAio, -DiskSsd, -DiskDiscard, -DiskCache so a tuned disk (e.g. virtio-scsi-single + scsi0,iothread=1,aio=native,ssd=1,discard=on) can be created in one call instead of diskless + a hand-built Set-PveVmConfig string. - Disk spec built via BuildDiskSpec; ValidateDiskOptions runs before ShouldProcess and rejects ssd on virtio and iothread on sata/ide or scsi-without-virtio-scsi-single with clear errors, instead of letting PVE fail at VM start. Get-PveVmConfig (F090): - PveVmConfig was a fixed allow-list, silently dropping keys like scsihw, efidisk0, tpmstate0, hostpci0. Add typed scsihw/efidisk0/tpmstate0 plus a [JsonExtensionData] catch-all exposed as AdditionalProperties (native types via JsonHelper.ToNative, per D013 — no JToken leakage). Makes the disk tuning above verifiable by reading the config back. Closes #65. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -173,5 +173,57 @@ namespace PSProxmoxVE.Core.Tests.Models
|
||||
Assert.Equal("8.8.8.8", config.Nameserver);
|
||||
Assert.Equal("example.com", config.Searchdomain);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PveVmConfig_Deserialize_SurfacesScsiHardware()
|
||||
{
|
||||
var json = TestHelper.LoadFixture("pve9_vm_config.json");
|
||||
var data = JObject.Parse(json)["data"];
|
||||
Assert.NotNull(data);
|
||||
var config = data.ToObject<PveVmConfig>();
|
||||
Assert.NotNull(config);
|
||||
Assert.Equal("virtio-scsi-single", config.ScsiHardware);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PveVmConfig_Deserialize_SurfacesEfiDiskAndTpm()
|
||||
{
|
||||
var json = @"{ ""efidisk0"": ""local-lvm:vm-100-disk-1,efitype=4m,size=528K"",
|
||||
""tpmstate0"": ""local-lvm:vm-100-disk-2,size=4M,version=v2.0"" }";
|
||||
var config = JObject.Parse(json).ToObject<PveVmConfig>();
|
||||
Assert.NotNull(config);
|
||||
Assert.Contains("efitype=4m", config!.EfiDisk0);
|
||||
Assert.Contains("version=v2.0", config.TpmState0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PveVmConfig_UnmappedKeys_LandInAdditionalProperties_AsNativeTypes()
|
||||
{
|
||||
// hostpci0 and numa0 are not typed properties; they must not be dropped.
|
||||
var json = @"{ ""cores"": 2,
|
||||
""hostpci0"": ""0000:01:00.0,pcie=1"",
|
||||
""numa0"": ""cpus=0-1,memory=2048"" }";
|
||||
var config = JObject.Parse(json).ToObject<PveVmConfig>();
|
||||
Assert.NotNull(config);
|
||||
|
||||
Assert.Equal(2, config!.Cores); // typed property still works
|
||||
Assert.True(config.AdditionalProperties.ContainsKey("hostpci0"));
|
||||
Assert.Equal("0000:01:00.0,pcie=1", config.AdditionalProperties["hostpci0"]);
|
||||
// Value must be a native type (string), never a Newtonsoft JToken (D013).
|
||||
Assert.IsType<string>(config.AdditionalProperties["hostpci0"]);
|
||||
Assert.DoesNotContain("Newtonsoft", config.AdditionalProperties["hostpci0"]!.GetType().FullName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PveVmConfig_TypedKeys_DoNotLeakIntoAdditionalProperties()
|
||||
{
|
||||
var json = TestHelper.LoadFixture("pve9_vm_config.json");
|
||||
var data = JObject.Parse(json)["data"];
|
||||
var config = data!.ToObject<PveVmConfig>();
|
||||
Assert.NotNull(config);
|
||||
// scsihw and cores are typed → they must not also appear in the catch-all.
|
||||
Assert.False(config!.AdditionalProperties.ContainsKey("scsihw"));
|
||||
Assert.False(config.AdditionalProperties.ContainsKey("cores"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,4 +168,75 @@ Describe 'New-PveVm' {
|
||||
Should -Not -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Disk controller / IO parameter metadata' {
|
||||
BeforeAll { $script:Cmd = Get-Command 'New-PveVm' }
|
||||
|
||||
It 'Should have <_> parameter' -ForEach @(
|
||||
'DiskBus', 'ScsiHardware', 'DiskIoThread', 'DiskAio', 'DiskSsd', 'DiskDiscard', 'DiskCache'
|
||||
) {
|
||||
$script:Cmd.Parameters.ContainsKey($_) | Should -BeTrue
|
||||
}
|
||||
|
||||
It 'DiskBus should have a ValidateSet of virtio, scsi, sata, ide' {
|
||||
$vs = $script:Cmd.Parameters['DiskBus'].Attributes |
|
||||
Where-Object { $_ -is [System.Management.Automation.ValidateSetAttribute] } |
|
||||
Select-Object -First 1
|
||||
$vs.ValidValues | Should -Contain 'virtio'
|
||||
$vs.ValidValues | Should -Contain 'scsi'
|
||||
$vs.ValidValues | Should -Contain 'sata'
|
||||
$vs.ValidValues | Should -Contain 'ide'
|
||||
}
|
||||
|
||||
It 'DiskIoThread and DiskSsd should be switch parameters' {
|
||||
$script:Cmd.Parameters['DiskIoThread'].ParameterType | Should -Be ([System.Management.Automation.SwitchParameter])
|
||||
$script:Cmd.Parameters['DiskSsd'].ParameterType | Should -Be ([System.Management.Automation.SwitchParameter])
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Disk option validation' {
|
||||
# Validation runs before ShouldProcess, so -WhatIf exercises it offline.
|
||||
|
||||
It 'Should reject -DiskSsd on the virtio bus' {
|
||||
{ New-PveVm -Node 'pve-node1' -DiskStorage 'local-lvm' -DiskSize '32' -DiskSsd -WhatIf -ErrorAction Stop } |
|
||||
Should -Throw '*virtio bus*'
|
||||
}
|
||||
|
||||
It 'Should reject -DiskSsd on the default (virtio) bus when bus omitted' {
|
||||
{ New-PveVm -Node 'pve-node1' -DiskStorage 'local-lvm' -DiskSize '32' -DiskSsd -WhatIf -ErrorAction Stop } |
|
||||
Should -Throw '*virtio bus*'
|
||||
}
|
||||
|
||||
It 'Should accept -DiskSsd on the scsi bus' {
|
||||
{ New-PveVm -Node 'pve-node1' -DiskStorage 'local-lvm' -DiskSize '32' -DiskBus scsi -DiskSsd -WhatIf -ErrorAction Stop } |
|
||||
Should -Not -Throw
|
||||
}
|
||||
|
||||
It 'Should reject -DiskIoThread on the sata bus' {
|
||||
{ New-PveVm -Node 'pve-node1' -DiskStorage 'local-lvm' -DiskSize '32' -DiskBus sata -DiskIoThread -WhatIf -ErrorAction Stop } |
|
||||
Should -Throw '*requires -DiskBus virtio or scsi*'
|
||||
}
|
||||
|
||||
It 'Should reject -DiskIoThread on scsi without -ScsiHardware virtio-scsi-single' {
|
||||
{ New-PveVm -Node 'pve-node1' -DiskStorage 'local-lvm' -DiskSize '32' -DiskBus scsi -DiskIoThread -WhatIf -ErrorAction Stop } |
|
||||
Should -Throw '*virtio-scsi-single*'
|
||||
}
|
||||
|
||||
It 'Should accept -DiskIoThread on scsi with -ScsiHardware virtio-scsi-single' {
|
||||
{ New-PveVm -Node 'pve-node1' -DiskStorage 'local-lvm' -DiskSize '32' -DiskBus scsi -ScsiHardware 'virtio-scsi-single' -DiskIoThread -WhatIf -ErrorAction Stop } |
|
||||
Should -Not -Throw
|
||||
}
|
||||
|
||||
It 'Should accept -DiskIoThread on the default virtio bus' {
|
||||
{ New-PveVm -Node 'pve-node1' -DiskStorage 'local-lvm' -DiskSize '32' -DiskIoThread -WhatIf -ErrorAction Stop } |
|
||||
Should -Not -Throw
|
||||
}
|
||||
|
||||
It 'Should accept a fully tuned scsi disk spec' {
|
||||
{ New-PveVm -Node 'pve-node1' -DiskStorage 'local-lvm' -DiskSize '60' -DiskBus scsi `
|
||||
-ScsiHardware 'virtio-scsi-single' -DiskIoThread -DiskAio native -DiskSsd -DiskDiscard `
|
||||
-DiskCache none -WhatIf -ErrorAction Stop } |
|
||||
Should -Not -Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user