Skip to content

Commit

Permalink
feat(vm): support PCI device resource mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
zharalim committed Aug 18, 2023
1 parent e02c52b commit b1f88f0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
3 changes: 2 additions & 1 deletion docs/resources/virtual_environment_vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ output "ubuntu_vm_public_key" {
- `hostpci` - (Optional) A host PCI device mapping (multiple blocks supported).
- `device` - (Required) The PCI device name for Proxmox, in form
of `hostpciX` where `X` is a sequential number from 0 to 3.
- `id` - (Required) The PCI device ID.
- `id` - (Optional) The PCI device ID. Use either this or `mapping`.
- `mapping` - (Optional) The resource mapping name of the device, for example gpu. Use either this or `id`.
- `mdev` - (Optional) The mediated device ID to use.
- `pcie` - (Optional) Tells Proxmox to use a PCIe or PCI port. Some
guests/device combination require PCIe rather than PCI. PCIe is only
Expand Down
11 changes: 11 additions & 0 deletions example/resource_virtual_environment_vm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ resource "proxmox_virtual_environment_vm" "example" {
}
}

#hostpci {
# device = "hostpci0"
# id = "0000:00:1f.0"
# pcie = true
#}

#hostpci {
# device = "hostpci1"
# mapping = "gpu"
# pcie = true
#}
}

output "resource_proxmox_virtual_environment_vm_example_id" {
Expand Down
25 changes: 20 additions & 5 deletions proxmox/nodes/vms/vms_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ type CustomNUMADevices []CustomNUMADevice

// CustomPCIDevice handles QEMU host PCI device mapping parameters.
type CustomPCIDevice struct {
DeviceIDs []string `json:"host" url:"host,semicolon"`
DeviceIDs *[]string `json:"host,omitempty" url:"host,omitempty,semicolon"`
Mapping *string `json:"mapping,omitempty" url:"mapping,omitempty"`
MDev *string `json:"mdev,omitempty" url:"mdev,omitempty"`
PCIExpress *types.CustomBool `json:"pcie,omitempty" url:"pcie,omitempty,int"`
ROMBAR *types.CustomBool `json:"rombar,omitempty" url:"rombar,omitempty,int"`
Expand Down Expand Up @@ -912,8 +913,18 @@ func (r CustomNUMADevices) EncodeValues(key string, v *url.Values) error {

// EncodeValues converts a CustomPCIDevice struct to a URL vlaue.
func (r CustomPCIDevice) EncodeValues(key string, v *url.Values) error {
values := []string{
fmt.Sprintf("host=%s", strings.Join(r.DeviceIDs, ";")),
values := []string{}

if r.DeviceIDs == nil && r.Mapping == nil {
return fmt.Errorf("either device ID or resource mapping must be set")
}

if r.DeviceIDs != nil {
values = append(values, fmt.Sprintf("host=%s", strings.Join(*r.DeviceIDs, ";")))
}

if r.Mapping != nil {
values = append(values, fmt.Sprintf("mapping=%s", *r.Mapping))
}

if r.MDev != nil {
Expand Down Expand Up @@ -1606,11 +1617,15 @@ func (r *CustomPCIDevice) UnmarshalJSON(b []byte) error {
for _, p := range pairs {
v := strings.Split(strings.TrimSpace(p), "=")
if len(v) == 1 {
r.DeviceIDs = strings.Split(v[0], ";")
dIds := strings.Split(v[0], ";")
r.DeviceIDs = &dIds
} else if len(v) == 2 {
switch v[0] {
case "host":
r.DeviceIDs = strings.Split(v[1], ";")
dIds := strings.Split(v[0], ";")
r.DeviceIDs = &dIds
case "mapping":
r.Mapping = &v[1]
case "mdev":
r.MDev = &v[1]
case "pcie":
Expand Down
16 changes: 14 additions & 2 deletions proxmox/nodes/vms/vms_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestCustomPCIDevice_UnmarshalJSON(t *testing.T) {
name: "id only pci device",
line: `"0000:81:00.2"`,
want: &CustomPCIDevice{
DeviceIDs: []string{"0000:81:00.2"},
DeviceIDs: &[]string{"0000:81:00.2"},
MDev: nil,
PCIExpress: types.BoolPtr(false),
ROMBAR: types.BoolPtr(true),
Expand All @@ -90,7 +90,19 @@ func TestCustomPCIDevice_UnmarshalJSON(t *testing.T) {
name: "pci device with more details",
line: `"host=81:00.4,pcie=0,rombar=1,x-vga=0"`,
want: &CustomPCIDevice{
DeviceIDs: []string{"81:00.4"},
DeviceIDs: &[]string{"81:00.4"},
MDev: nil,
PCIExpress: types.BoolPtr(false),
ROMBAR: types.BoolPtr(true),
ROMFile: nil,
XVGA: types.BoolPtr(false),
},
},
{
name: "pci device with mapping",
line: `"mapping=mappeddevice,pcie=0,rombar=1,x-vga=0"`,
want: &CustomPCIDevice{
Mapping: types.StrPtr("mappeddevice"),
MDev: nil,
PCIExpress: types.BoolPtr(false),
ROMBAR: types.BoolPtr(true),
Expand Down
32 changes: 27 additions & 5 deletions proxmoxtf/resource/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ const (
mkResourceVirtualEnvironmentVMHostPCI = "hostpci"
mkResourceVirtualEnvironmentVMHostPCIDevice = "device"
mkResourceVirtualEnvironmentVMHostPCIDeviceID = "id"
mkResourceVirtualEnvironmentVMHostPCIDeviceMapping = "mapping"
mkResourceVirtualEnvironmentVMHostPCIDeviceMDev = "mdev"
mkResourceVirtualEnvironmentVMHostPCIDevicePCIE = "pcie"
mkResourceVirtualEnvironmentVMHostPCIDeviceROMBAR = "rombar"
Expand Down Expand Up @@ -1004,8 +1005,13 @@ func VM() *schema.Resource {
},
mkResourceVirtualEnvironmentVMHostPCIDeviceID: {
Type: schema.TypeString,
Description: "The PCI ID of the device, for example 0000:00:1f.0 (or 0000:00:1f.0;0000:00:1f.1 for multiple device functions, or 0000:00:1f for all functions)",
Required: true,
Description: "The PCI ID of the device, for example 0000:00:1f.0 (or 0000:00:1f.0;0000:00:1f.1 for multiple device functions, or 0000:00:1f for all functions). Use either this or mapping.",
Optional: true,
},
mkResourceVirtualEnvironmentVMHostPCIDeviceMapping: {
Type: schema.TypeString,
Description: "The resource mapping name of the device, for example gpu. Use either this or id.",
Optional: true,
},
mkResourceVirtualEnvironmentVMHostPCIDeviceMDev: {
Type: schema.TypeString,
Expand Down Expand Up @@ -3115,15 +3121,17 @@ func vmGetHostPCIDeviceObjects(d *schema.ResourceData) vms.CustomPCIDevices {
)
romfile, _ := block[mkResourceVirtualEnvironmentVMHostPCIDeviceROMFile].(string)
xvga := types.CustomBool(block[mkResourceVirtualEnvironmentVMHostPCIDeviceXVGA].(bool))
mapping, _ := block[mkResourceVirtualEnvironmentVMHostPCIDeviceMapping].(string)

device := vms.CustomPCIDevice{
DeviceIDs: strings.Split(ids, ";"),
PCIExpress: &pcie,
ROMBAR: &rombar,
XVGA: &xvga,
Mapping: &mapping,
}
if ids != "" {
device.DeviceIDs = strings.Split(ids, ";")
dIds := strings.Split(ids, ";")
device.DeviceIDs = &dIds
}

if mdev != "" {
Expand All @@ -3134,6 +3142,10 @@ func vmGetHostPCIDeviceObjects(d *schema.ResourceData) vms.CustomPCIDevices {
device.ROMFile = &romfile
}

if mapping != "" {
device.Mapping = &mapping
}

pciDeviceObjects[i] = device
}

Expand Down Expand Up @@ -3898,7 +3910,11 @@ func vmReadCustom(
pci := map[string]interface{}{}

pci[mkResourceVirtualEnvironmentVMHostPCIDevice] = pi
pci[mkResourceVirtualEnvironmentVMHostPCIDeviceID] = strings.Join(pp.DeviceIDs, ";")
if pp.DeviceIDs != nil {
pci[mkResourceVirtualEnvironmentVMHostPCIDeviceID] = strings.Join(*pp.DeviceIDs, ";")
} else {
pci[mkResourceVirtualEnvironmentVMHostPCIDeviceID] = ""
}

if pp.MDev != nil {
pci[mkResourceVirtualEnvironmentVMHostPCIDeviceMDev] = *pp.MDev
Expand Down Expand Up @@ -3930,6 +3946,12 @@ func vmReadCustom(
pci[mkResourceVirtualEnvironmentVMHostPCIDeviceXVGA] = false
}

if pp.Mapping != nil {
pci[mkResourceVirtualEnvironmentVMHostPCIDeviceMapping] = *pp.Mapping
} else {
pci[mkResourceVirtualEnvironmentVMHostPCIDeviceMapping] = ""
}

pciMap[pi] = pci
}

Expand Down

0 comments on commit b1f88f0

Please sign in to comment.