From b0fa05704ae226a4a465d89314762f9184dad49e Mon Sep 17 00:00:00 2001 From: Joshua Foster Date: Mon, 13 Jul 2020 20:25:56 -0400 Subject: [PATCH] change usb controllers to a list. add ability to set as a usb3 Closes #8874 --- builder/vsphere/driver/vm.go | 20 ++++++++++++----- builder/vsphere/iso/config.hcl2spec.go | 4 ++-- builder/vsphere/iso/step_create.go | 22 +++++++++++++++++-- builder/vsphere/iso/step_create.hcl2spec.go | 4 ++-- .../vsphere/iso/CreateConfig-not-required.mdx | 2 +- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/builder/vsphere/driver/vm.go b/builder/vsphere/driver/vm.go index ccc7020eb..0437fac44 100644 --- a/builder/vsphere/driver/vm.go +++ b/builder/vsphere/driver/vm.go @@ -75,7 +75,7 @@ type CreateConfig struct { Datastore string GuestOS string // example: otherGuest NICs []NIC - USBController bool + USBController []string Version uint // example: 10 Storage []Disk } @@ -175,11 +175,21 @@ func (d *Driver) CreateVM(config *CreateConfig) (*VirtualMachine, error) { return nil, err } - if config.USBController { - t := true - usb := &types.VirtualUSBController{ - EhciEnabled: &t, + t := true + for _, usbType := range config.USBController { + var usb types.BaseVirtualDevice + switch usbType { + // handle "true" and "1" for backwards compatibility + case "usb", "true", "1": + usb = &types.VirtualUSBController{ + EhciEnabled: &t, + } + case "xhci": + usb = new(types.VirtualUSBXHCIController) + default: + continue } + devices = append(devices, usb) } diff --git a/builder/vsphere/iso/config.hcl2spec.go b/builder/vsphere/iso/config.hcl2spec.go index 8bf03e722..316116b6c 100644 --- a/builder/vsphere/iso/config.hcl2spec.go +++ b/builder/vsphere/iso/config.hcl2spec.go @@ -31,7 +31,7 @@ type FlatConfig struct { DiskControllerType []string `mapstructure:"disk_controller_type" cty:"disk_controller_type" hcl:"disk_controller_type"` Storage []FlatDiskConfig `mapstructure:"storage" cty:"storage" hcl:"storage"` NICs []FlatNIC `mapstructure:"network_adapters" cty:"network_adapters" hcl:"network_adapters"` - USBController *bool `mapstructure:"usb_controller" cty:"usb_controller" hcl:"usb_controller"` + USBController []string `mapstructure:"usb_controller" cty:"usb_controller" hcl:"usb_controller"` Notes *string `mapstructure:"notes" cty:"notes" hcl:"notes"` VMName *string `mapstructure:"vm_name" cty:"vm_name" hcl:"vm_name"` Folder *string `mapstructure:"folder" cty:"folder" hcl:"folder"` @@ -165,7 +165,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "disk_controller_type": &hcldec.AttrSpec{Name: "disk_controller_type", Type: cty.List(cty.String), Required: false}, "storage": &hcldec.BlockListSpec{TypeName: "storage", Nested: hcldec.ObjectSpec((*FlatDiskConfig)(nil).HCL2Spec())}, "network_adapters": &hcldec.BlockListSpec{TypeName: "network_adapters", Nested: hcldec.ObjectSpec((*FlatNIC)(nil).HCL2Spec())}, - "usb_controller": &hcldec.AttrSpec{Name: "usb_controller", Type: cty.Bool, Required: false}, + "usb_controller": &hcldec.AttrSpec{Name: "usb_controller", Type: cty.List(cty.String), Required: false}, "notes": &hcldec.AttrSpec{Name: "notes", Type: cty.String, Required: false}, "vm_name": &hcldec.AttrSpec{Name: "vm_name", Type: cty.String, Required: false}, "folder": &hcldec.AttrSpec{Name: "folder", Type: cty.String, Required: false}, diff --git a/builder/vsphere/iso/step_create.go b/builder/vsphere/iso/step_create.go index 11e3f40ee..e1013c31d 100644 --- a/builder/vsphere/iso/step_create.go +++ b/builder/vsphere/iso/step_create.go @@ -107,8 +107,8 @@ type CreateConfig struct { Storage []DiskConfig `mapstructure:"storage"` // Network adapters NICs []NIC `mapstructure:"network_adapters"` - // Create USB controller for virtual machine. Defaults to `false`. - USBController bool `mapstructure:"usb_controller"` + // Create USB controllers for the virtual machine. "usb" for a usb 2.0 controller. "xhci" for a usb 3.0 controller. There can only be at most one of each. + USBController []string `mapstructure:"usb_controller"` // VM notes. Notes string `mapstructure:"notes"` } @@ -136,6 +136,24 @@ func (c *CreateConfig) Prepare() []error { c.GuestOSType = "otherGuest" } + usbCount := 0 + xhciCount := 0 + + for i, s := range c.USBController { + switch s { + // 1 and true for backwards compatibility + case "usb", "1", "true": + usbCount++ + case "xhci": + xhciCount++ + default: + errs = append(errs, fmt.Errorf("usb_controller[%d] references an unknown usb controller", i)) + } + } + if usbCount > 1 || xhciCount > 1 { + errs = append(errs, fmt.Errorf("there can only be one usb controller and one xhci controller")) + } + return errs } diff --git a/builder/vsphere/iso/step_create.hcl2spec.go b/builder/vsphere/iso/step_create.hcl2spec.go index bc0ceaa7f..dadf605d5 100644 --- a/builder/vsphere/iso/step_create.hcl2spec.go +++ b/builder/vsphere/iso/step_create.hcl2spec.go @@ -14,7 +14,7 @@ type FlatCreateConfig struct { DiskControllerType []string `mapstructure:"disk_controller_type" cty:"disk_controller_type" hcl:"disk_controller_type"` Storage []FlatDiskConfig `mapstructure:"storage" cty:"storage" hcl:"storage"` NICs []FlatNIC `mapstructure:"network_adapters" cty:"network_adapters" hcl:"network_adapters"` - USBController *bool `mapstructure:"usb_controller" cty:"usb_controller" hcl:"usb_controller"` + USBController []string `mapstructure:"usb_controller" cty:"usb_controller" hcl:"usb_controller"` Notes *string `mapstructure:"notes" cty:"notes" hcl:"notes"` } @@ -35,7 +35,7 @@ func (*FlatCreateConfig) HCL2Spec() map[string]hcldec.Spec { "disk_controller_type": &hcldec.AttrSpec{Name: "disk_controller_type", Type: cty.List(cty.String), Required: false}, "storage": &hcldec.BlockListSpec{TypeName: "storage", Nested: hcldec.ObjectSpec((*FlatDiskConfig)(nil).HCL2Spec())}, "network_adapters": &hcldec.BlockListSpec{TypeName: "network_adapters", Nested: hcldec.ObjectSpec((*FlatNIC)(nil).HCL2Spec())}, - "usb_controller": &hcldec.AttrSpec{Name: "usb_controller", Type: cty.Bool, Required: false}, + "usb_controller": &hcldec.AttrSpec{Name: "usb_controller", Type: cty.List(cty.String), Required: false}, "notes": &hcldec.AttrSpec{Name: "notes", Type: cty.String, Required: false}, } return s diff --git a/website/pages/partials/builder/vsphere/iso/CreateConfig-not-required.mdx b/website/pages/partials/builder/vsphere/iso/CreateConfig-not-required.mdx index fa5f997f9..7ff4577ee 100644 --- a/website/pages/partials/builder/vsphere/iso/CreateConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/iso/CreateConfig-not-required.mdx @@ -15,6 +15,6 @@ - `network_adapters` ([]NIC) - Network adapters -- `usb_controller` (bool) - Create USB controller for virtual machine. Defaults to `false`. +- `usb_controller` ([]string) - Create USB controllers for the virtual machine. "usb" for a usb 2.0 controller. "xhci" for a usb 3.0 controller. There can only be at most one of each. - `notes` (string) - VM notes.