Skip to content

Commit f06e45c

Browse files
vm: Expose memory, CPU topology, host affinity, and HA settings
Closes #2 Get-XoVm now returns these additional friendly properties: - AffinityHost: preferred host UUID for VM placement - MemorySize: memory allocation in bytes (from memory.size) - CoresPerSocket: CPU topology (cores per socket) - AutoPoweron: whether the VM auto-starts on host boot - HA: high availability restart priority Set-XoVm now accepts these new parameters: - -AffinityHost: set the preferred host (with UUID validation) - -Memory: set memory size in bytes - -CPUs: set vCPU count - -CoresPerSocket: set cores per socket (CPU topology) - -AutoPoweron: set auto power-on behavior - -HA: set HA priority ("", "restart", "best-effort") The fields affinityHost, coresPerSocket, and high_availability are added to the API query so they are fetched from Xen Orchestra. Example — set each running VM's affinity to its current host: Get-XoVm -PowerState Running | ForEach-Object { Set-XoVm -VmUuid $_.VmUuid -AffinityHost $_.HostUuid } Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 76f5242 commit f06e45c

File tree

1 file changed

+88
-9
lines changed

1 file changed

+88
-9
lines changed

src/vm.ps1

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3-
$script:XO_VM_FIELDS = "uuid,name_label,name_description,power_state,addresses,tags,memory,VIFs,snapshots,current_operations,auto_poweron,os_version,startTime,VCPUs_at_startup,CPUs,VCPUs_number,`$VBDs"
3+
$script:XO_VM_FIELDS = "uuid,name_label,name_description,power_state,addresses,tags,memory,VIFs,snapshots,current_operations,auto_poweron,os_version,startTime,VCPUs_at_startup,CPUs,VCPUs_number,affinityHost,coresPerSocket,high_availability,`$VBDs"
44
$script:XO_VM_TEMPLATE_FIELDS = $script:XO_VM_FIELDS + ",isDefaultTemplate"
55

66
function ConvertTo-XoVmObject {
@@ -21,13 +21,18 @@ function ConvertTo-XoVmObject {
2121

2222
process {
2323
$props = @{
24-
VmUuid = $InputObject.uuid
25-
Name = $InputObject.name_label
26-
Description = $InputObject.name_description
27-
PowerState = $InputObject.power_state
28-
OsVersion = $InputObject.os_version
29-
Parent = $InputObject.parent
30-
HostUuid = $InputObject.$container
24+
VmUuid = $InputObject.uuid
25+
Name = $InputObject.name_label
26+
Description = $InputObject.name_description
27+
PowerState = $InputObject.power_state
28+
OsVersion = $InputObject.os_version
29+
Parent = $InputObject.parent
30+
HostUuid = $InputObject.$container
31+
AffinityHost = $InputObject.affinityHost
32+
MemorySize = $InputObject.memory.size
33+
CoresPerSocket = $InputObject.coresPerSocket
34+
AutoPoweron = $InputObject.auto_poweron
35+
HA = $InputObject.high_availability
3136
}
3237

3338
if ($InputObject.CPUs.number) {
@@ -199,6 +204,42 @@ function Get-XoVm {
199204
}
200205

201206
function Set-XoVm {
207+
<#
208+
.SYNOPSIS
209+
Update VM settings.
210+
.DESCRIPTION
211+
Modifies properties of a VM such as name, description, tags, memory,
212+
CPU count, CPU topology, host affinity, and auto power-on.
213+
.PARAMETER VmUuid
214+
The UUID of the VM to update.
215+
.PARAMETER Name
216+
Set the VM name.
217+
.PARAMETER Description
218+
Set the VM description.
219+
.PARAMETER Tags
220+
Set the VM tags.
221+
.PARAMETER AffinityHost
222+
Set the host affinity (preferred host UUID for the VM).
223+
.PARAMETER Memory
224+
Set the memory size in bytes.
225+
.PARAMETER CPUs
226+
Set the number of virtual CPUs.
227+
.PARAMETER CoresPerSocket
228+
Set the number of cores per socket (affects CPU topology).
229+
.PARAMETER AutoPoweron
230+
Set whether the VM should auto-start when the host boots.
231+
.PARAMETER HA
232+
Set the high availability restart priority. Valid values: empty string (disabled), "restart", "best-effort".
233+
.EXAMPLE
234+
Set-XoVm -VmUuid "12345678-abcd-1234-abcd-1234567890ab" -Memory 4294967296
235+
Sets the VM memory to 4 GB.
236+
.EXAMPLE
237+
Get-XoVm -PowerState Running | ForEach-Object { Set-XoVm -VmUuid $_.VmUuid -AffinityHost $_.HostUuid }
238+
Sets each running VM's host affinity to its current host.
239+
.EXAMPLE
240+
Set-XoVm -VmUuid "12345678-abcd-1234-abcd-1234567890ab" -CPUs 4 -CoresPerSocket 2
241+
Sets the VM to 4 vCPUs with 2 cores per socket (2 sockets).
242+
#>
202243
[CmdletBinding()]
203244
param(
204245
[Parameter(Mandatory, ValueFromPipelineByPropertyName, Position = 0)]
@@ -212,7 +253,27 @@ function Set-XoVm {
212253
[string]$Description,
213254

214255
[Parameter()]
215-
[string[]]$Tags
256+
[string[]]$Tags,
257+
258+
[Parameter(ValueFromPipelineByPropertyName)]
259+
[ValidatePattern("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")]
260+
[string]$AffinityHost,
261+
262+
[Parameter()]
263+
[long]$Memory,
264+
265+
[Parameter()]
266+
[int]$CPUs,
267+
268+
[Parameter()]
269+
[int]$CoresPerSocket,
270+
271+
[Parameter()]
272+
[bool]$AutoPoweron,
273+
274+
[Parameter()]
275+
[ValidateSet("", "restart", "best-effort")]
276+
[string]$HA
216277
)
217278

218279
$params = @{}
@@ -226,6 +287,24 @@ function Set-XoVm {
226287
if ($PSBoundParameters.ContainsKey("Tags")) {
227288
$params["tags"] = $Tags
228289
}
290+
if ($PSBoundParameters.ContainsKey("AffinityHost")) {
291+
$params["affinityHost"] = $AffinityHost
292+
}
293+
if ($PSBoundParameters.ContainsKey("Memory")) {
294+
$params["memory"] = $Memory
295+
}
296+
if ($PSBoundParameters.ContainsKey("CPUs")) {
297+
$params["CPUs"] = $CPUs
298+
}
299+
if ($PSBoundParameters.ContainsKey("CoresPerSocket")) {
300+
$params["coresPerSocket"] = $CoresPerSocket
301+
}
302+
if ($PSBoundParameters.ContainsKey("AutoPoweron")) {
303+
$params["auto_poweron"] = $AutoPoweron
304+
}
305+
if ($PSBoundParameters.ContainsKey("HA")) {
306+
$params["high_availability"] = $HA
307+
}
229308

230309
if ($params.Count -gt 0) {
231310
$body = [System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json $params))

0 commit comments

Comments
 (0)