change usb controllers to a list. add ability to set as a usb3

Closes #8874
This commit is contained in:
Joshua Foster 2020-07-13 20:25:56 -04:00
parent 1400662db7
commit b0fa05704a
5 changed files with 40 additions and 12 deletions

View File

@ -75,7 +75,7 @@ type CreateConfig struct {
Datastore string Datastore string
GuestOS string // example: otherGuest GuestOS string // example: otherGuest
NICs []NIC NICs []NIC
USBController bool USBController []string
Version uint // example: 10 Version uint // example: 10
Storage []Disk Storage []Disk
} }
@ -175,11 +175,21 @@ func (d *Driver) CreateVM(config *CreateConfig) (*VirtualMachine, error) {
return nil, err return nil, err
} }
if config.USBController {
t := true t := true
usb := &types.VirtualUSBController{ 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, EhciEnabled: &t,
} }
case "xhci":
usb = new(types.VirtualUSBXHCIController)
default:
continue
}
devices = append(devices, usb) devices = append(devices, usb)
} }

View File

@ -31,7 +31,7 @@ type FlatConfig struct {
DiskControllerType []string `mapstructure:"disk_controller_type" cty:"disk_controller_type" hcl:"disk_controller_type"` DiskControllerType []string `mapstructure:"disk_controller_type" cty:"disk_controller_type" hcl:"disk_controller_type"`
Storage []FlatDiskConfig `mapstructure:"storage" cty:"storage" hcl:"storage"` Storage []FlatDiskConfig `mapstructure:"storage" cty:"storage" hcl:"storage"`
NICs []FlatNIC `mapstructure:"network_adapters" cty:"network_adapters" hcl:"network_adapters"` 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"` Notes *string `mapstructure:"notes" cty:"notes" hcl:"notes"`
VMName *string `mapstructure:"vm_name" cty:"vm_name" hcl:"vm_name"` VMName *string `mapstructure:"vm_name" cty:"vm_name" hcl:"vm_name"`
Folder *string `mapstructure:"folder" cty:"folder" hcl:"folder"` 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}, "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())}, "storage": &hcldec.BlockListSpec{TypeName: "storage", Nested: hcldec.ObjectSpec((*FlatDiskConfig)(nil).HCL2Spec())},
"network_adapters": &hcldec.BlockListSpec{TypeName: "network_adapters", Nested: hcldec.ObjectSpec((*FlatNIC)(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}, "notes": &hcldec.AttrSpec{Name: "notes", Type: cty.String, Required: false},
"vm_name": &hcldec.AttrSpec{Name: "vm_name", 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}, "folder": &hcldec.AttrSpec{Name: "folder", Type: cty.String, Required: false},

View File

@ -107,8 +107,8 @@ type CreateConfig struct {
Storage []DiskConfig `mapstructure:"storage"` Storage []DiskConfig `mapstructure:"storage"`
// Network adapters // Network adapters
NICs []NIC `mapstructure:"network_adapters"` NICs []NIC `mapstructure:"network_adapters"`
// Create USB controller for virtual machine. Defaults to `false`. // 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 bool `mapstructure:"usb_controller"` USBController []string `mapstructure:"usb_controller"`
// VM notes. // VM notes.
Notes string `mapstructure:"notes"` Notes string `mapstructure:"notes"`
} }
@ -136,6 +136,24 @@ func (c *CreateConfig) Prepare() []error {
c.GuestOSType = "otherGuest" 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 return errs
} }

View File

@ -14,7 +14,7 @@ type FlatCreateConfig struct {
DiskControllerType []string `mapstructure:"disk_controller_type" cty:"disk_controller_type" hcl:"disk_controller_type"` DiskControllerType []string `mapstructure:"disk_controller_type" cty:"disk_controller_type" hcl:"disk_controller_type"`
Storage []FlatDiskConfig `mapstructure:"storage" cty:"storage" hcl:"storage"` Storage []FlatDiskConfig `mapstructure:"storage" cty:"storage" hcl:"storage"`
NICs []FlatNIC `mapstructure:"network_adapters" cty:"network_adapters" hcl:"network_adapters"` 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"` 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}, "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())}, "storage": &hcldec.BlockListSpec{TypeName: "storage", Nested: hcldec.ObjectSpec((*FlatDiskConfig)(nil).HCL2Spec())},
"network_adapters": &hcldec.BlockListSpec{TypeName: "network_adapters", Nested: hcldec.ObjectSpec((*FlatNIC)(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}, "notes": &hcldec.AttrSpec{Name: "notes", Type: cty.String, Required: false},
} }
return s return s

View File

@ -15,6 +15,6 @@
- `network_adapters` ([]NIC) - Network adapters - `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. - `notes` (string) - VM notes.