Skip to content

feat: add support for storage policies#697

Open
pascalinthecloud wants to merge 3 commits intovmware:mainfrom
pascalinthecloud:feat/#18-handling-of-storage-policies
Open

feat: add support for storage policies#697
pascalinthecloud wants to merge 3 commits intovmware:mainfrom
pascalinthecloud:feat/#18-handling-of-storage-policies

Conversation

@pascalinthecloud
Copy link
Copy Markdown

@pascalinthecloud pascalinthecloud commented Apr 26, 2026

Summary

Adds per-disk storage policy support to the vsphere-iso builder (and the shared DiskConfig used by vsphere-clone).

A new optional storage_policy field on each storage {} block accepts a vSphere storage policy name. At build time the plugin resolves the name to a profile UUID via the Policy-Based Management (PBM) API and attaches a VirtualMachineDefinedProfileSpec to the disk's device config spec. The VM home files (.vmx, .nvram, etc.) are also placed under the same policy via VirtualMachineConfigSpec.VmProfile, which the vSphere API requires whenever any disk carries a per-disk profile. If the field is left empty the behaviour is unchanged — the datastore's default policy applies.

The PBM client is initialised lazily on first use from the existing VIM25 session, so no new connection or credential is needed.

Example usage:

disk_controller_type = ["pvscsi"]

storage {
  disk_size             = 20480
  disk_thin_provisioned = true
  storage_policy        = "Gold Storage Policy"
}

Type

  • fix: Bug Fix
  • feat: Feature or Enhancement
  • docs: Documentation
  • refactor: Refactoring
  • chore: Build, Dependencies, Workflows, etc.
  • other: Other (Please describe.)

Breaking Changes?

  • Yes, there are breaking changes.
  • No, there are no breaking changes.

The new field is optional. Existing templates without storage_policy are unaffected.

Tests

  • Tests have been added or updated.
  • Tests have been completed.

New unit tests:

File Test What it covers
driver/disk_test.go TestAddStorageDevices_WithStoragePolicy VirtualMachineDefinedProfileSpec is attached to the correct disk's VirtualDeviceConfigSpec; absent on disks without a policy
driver/vm_test.go TestVirtualMachineDriver_CreateVM_WithStoragePolicy CreateVM succeeds end-to-end via the govmomi simulator when a disk carries a StoragePolicyID, exercising the VmProfile branch of the VM config spec
iso/step_create_test.go TestStepCreateVM_Run_WithStoragePolicy Policy name is resolved to a UUID and forwarded as StoragePolicyID on the driver disk
iso/step_create_test.go TestStepCreateVM_Run_StoragePolicyNotFound Step halts with a clear error when the policy name does not exist on vCenter

Output:

ok  github.com/vmware/packer-plugin-vsphere/builder/vsphere/driver  4.158s
ok  github.com/vmware/packer-plugin-vsphere/builder/vsphere/iso     1.771s
ok  github.com/vmware/packer-plugin-vsphere/builder/vsphere/common  1.815s
ok  github.com/vmware/packer-plugin-vsphere/builder/vsphere/clone   0.612s

The feature was also validated against a live vCenter with a real storage policy.

Documentation

  • Documentation has been added or updated.

docs-partials/builder/vsphere/common/DiskConfig-not-required.mdx was updated with the new storage_policy field. This partial is shared between vsphere-iso and vsphere-clone docs.

Issue References

Closes #18

Release Note

- Added `storage_policy` field to `storage {}` blocks in the `vsphere-iso` and `vsphere-clone` builders, enabling per-disk vSphere storage policy assignment. (#18)

Additional Information

Implementation notes:

  • The PBM client (govmomi/pbm) is already an indirect dependency via govmomi — no new module dependency is introduced.
  • Policy name resolution happens at build time (the Run step), not during Prepare, so the error message includes the vCenter-side policy name and is actionable.
  • VirtualMachineConfigSpec.VmProfile is set to the first disk's policy when any disk carries a policy. This is required by the vSphere API and mirrors the behaviour of other tools (Terraform provider, govc). If disks have mixed policies the VM home uses the first disk's policy; a dedicated VM-home policy field can be added in a follow-up.

@tenthirtyam
Copy link
Copy Markdown
Collaborator

CI is failing. Be sure to run "make generate" and push to your fork. Also, please sign your commits per the contributing guidelines. Thanks!

@tenthirtyam tenthirtyam added the stage/awaiting-reply Stage: Awaiting Reply label Apr 26, 2026
@pascalinthecloud pascalinthecloud force-pushed the feat/#18-handling-of-storage-policies branch from a5ad88b to a1eb4e3 Compare April 27, 2026 07:07
@pascalinthecloud
Copy link
Copy Markdown
Author

Hey,
I ran "make generate" and signed off my commits. :) Should be fine now.

@tenthirtyam tenthirtyam removed the stage/awaiting-reply Stage: Awaiting Reply label Apr 27, 2026
@tenthirtyam tenthirtyam modified the milestones: v2.y.z, v2.2.0 Apr 27, 2026
@tenthirtyam tenthirtyam changed the title feat(storage): add support for storage policies in disk configuration… feat(storage): add support for storage policies Apr 27, 2026
@tenthirtyam tenthirtyam changed the title feat(storage): add support for storage policies feat: add support for storage policies Apr 27, 2026
@tenthirtyam tenthirtyam force-pushed the feat/#18-handling-of-storage-policies branch from a1eb4e3 to fa65dfc Compare April 27, 2026 20:16
@tenthirtyam
Copy link
Copy Markdown
Collaborator

Hi @pascalinthecloud 👋🏻 - I'm hoping to be able to review and test e2e later this week.

Ryan

@tenthirtyam tenthirtyam self-requested a review April 28, 2026 18:28
pascalinthecloud and others added 3 commits May 4, 2026 13:37
… and VM creation

Signed-off-by: Pascal T. <pascal@toepke.dev>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Pascal T. <pascal@toepke.dev>
@tenthirtyam tenthirtyam force-pushed the feat/#18-handling-of-storage-policies branch from fa65dfc to ac915b9 Compare May 4, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Documentation needs-review Needs Review size/l Relative Sizing: Large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vsphere-iso: Add support to set a VM storage policy while configuring hard disks

2 participants