terraform-provider-vsphere icon indicating copy to clipboard operation
terraform-provider-vsphere copied to clipboard

Creating r/virtual_machine with multiple PCI passthrough devices is not supported

Open spacegospod opened this issue 7 months ago • 0 comments

Community Guidelines

  • [X] I have read and agree to the HashiCorp Community Guidelines .
  • [X] Vote on this issue by adding a 👍 reaction to the original issue initial description to help the maintainers prioritize.
  • [X] Do not leave "+1" or other comments that do not add relevant information or questions.
  • [X] If you are interested in working on this issue or have submitted a pull request, please leave a comment.

Terraform

1.5.7

Terraform Provider

2.8.2

VMware vSphere

8.0.2

Description

I identified this problem while working on #1688

A PCI passthrough device can be added to a virtual machine only during reconfiguration. Attempting to create a virtual machine with such devices fails.

This happens because same codepath is followed both when creating and reconfiguring a virtual machine, at least as far as PCI devices are concerned.

The root cause of the problem is in the following method

https://github.com/hashicorp/terraform-provider-vsphere/blob/main/vsphere/internal/virtualdevice/virtual_machine_device_subresource.go#L974

func (c *pciApplyConfig) modifyVirtualPciDevices(devList *schema.Set, op types.VirtualDeviceConfigSpecOperation) error {
	log.Printf("VirtualMachine: Creating PCI passthrough device specs %v", op)
	for _, addDev := range devList.List() {
		log.Printf("[DEBUG] modifyVirtualPciDevices: Appending %v spec for %s", op, addDev.(string))
		pciDev, err := c.getHostPciDevice(addDev.(string))
		if err != nil {
			return err
		}
		dev := &types.VirtualPCIPassthrough{
			VirtualDevice: types.VirtualDevice{
				DynamicData: types.DynamicData{},
				Backing: &types.VirtualPCIPassthroughDeviceBackingInfo{
					VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{},
					Id:                             pciDev.Id,
					SystemId:                       c.SystemID,
					VendorId:                       pciDev.VendorId,
				},
				Key: c.VirtualDevice.NewKey(),
			},
		}
		vm, err := virtualmachine.FromUUID(c.Client, c.ResourceData.Id())

This function attempts to retrieve the virtual machine by its identifier which is impossible before the machine has been created. The VM reference is necessary so that this method can later on loop over its devices and prepare the reconfiguration changes.

We need to define a dedicated procedure for adding PCI devices to a VM creation specification. This will require changes to virtual_machine_device_subresource.go as well as refactoring in resource_vsphere_virtual_machine.go

Affected Resources or Data Sources

resource/vsphere_virtual_machine

Terraform Configuration

resource "vsphere_virtual_machine" "vm" {
  ...
  pci_device_id = ["any:pci:device:id"]
}

Debug Output

N/A

Panic Output

No response

Expected Behavior

A virtual machine should be created

Actual Behavior

The execution fails before the creation task is triggered

Steps to Reproduce

Attempt to create a virtual machine with at least one value for pci_device_id

Environment Details

No response

Screenshots

No response

References

No response

spacegospod avatar Jul 08 '24 08:07 spacegospod