Skip to content

Commit

Permalink
Merge pull request #371 from Tinyblargon/rework-qemu-usb
Browse files Browse the repository at this point in the history
Rework: qemu usb
  • Loading branch information
Tinyblargon authored Nov 3, 2024
2 parents b38644d + d771101 commit 5b6e96a
Show file tree
Hide file tree
Showing 9 changed files with 1,203 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ proxmox-api-go
.vscode
.env

coverage.html
coverage.out
*coverage.html
*coverage.out
66 changes: 15 additions & 51 deletions proxmox/config_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type ConfigQemu struct {
QemuPCIDevices QemuDevices `json:"hostpci,omitempty"` // TODO should be a struct
QemuPxe bool `json:"pxe,omitempty"`
QemuUnusedDisks QemuDevices `json:"unused,omitempty"` // TODO should be a struct
QemuUsbs QemuDevices `json:"usb,omitempty"` // TODO should be a struct
USBs QemuUSBs `json:"usbs,omitempty"`
QemuVga QemuDevice `json:"vga,omitempty"` // TODO should be a struct
RNGDrive QemuDevice `json:"rng0,omitempty"` // TODO should be a struct
Scsihw string `json:"scsihw,omitempty"` // TODO should be custom type with enum
Expand Down Expand Up @@ -126,9 +126,6 @@ func (config *ConfigQemu) defaults() {
if config.QemuUnusedDisks == nil {
config.QemuUnusedDisks = QemuDevices{}
}
if config.QemuUsbs == nil {
config.QemuUsbs = QemuDevices{}
}
if config.QemuVga == nil {
config.QemuVga = QemuDevice{}
}
Expand Down Expand Up @@ -268,8 +265,9 @@ func (config ConfigQemu) mapToAPI(currentConfig ConfigQemu, version Version) (re
params["vga"] = strings.Join(vgaParam, ",")
}

// Create usb interfaces
config.CreateQemuUsbsParams(params)
if config.USBs != nil {
itemsToDelete += config.USBs.mapToAPI(currentConfig.USBs, params)
}

config.CreateQemuPCIsParams(params)

Expand Down Expand Up @@ -431,40 +429,7 @@ func (ConfigQemu) mapToStruct(vmr *VmRef, params map[string]interface{}) (*Confi
config.Networks = QemuNetworkInterfaces{}.mapToSDK(params)
config.Serials = SerialInterfaces{}.mapToSDK(params)

// Add usbs
usbNames := []string{}

for k := range params {
if usbName := rxUsbName.FindStringSubmatch(k); len(usbName) > 0 {
usbNames = append(usbNames, usbName[0])
}
}

if len(usbNames) > 0 {
config.QemuUsbs = QemuDevices{}
for _, usbName := range usbNames {
usbConfStr := params[usbName]
usbConfList := strings.Split(usbConfStr.(string), ",")
id := rxDeviceID.FindStringSubmatch(usbName)
usbID, _ := strconv.Atoi(id[0])
_, host := ParseSubConf(usbConfList[0], "=")

usbConfMap := QemuDevice{
"id": usbID,
"host": host,
}

usbConfMap.readDeviceConfig(usbConfList[1:])
if usbConfMap["usb3"] == 1 {
usbConfMap["usb3"] = true
}

// And device config to usbs map.
if len(usbConfMap) > 0 {
config.QemuUsbs[usbID] = usbConfMap
}
}
}
config.USBs = QemuUSBs{}.mapToSDK(params)

// hostpci
hostPCInames := []string{}
Expand Down Expand Up @@ -714,6 +679,11 @@ func (config ConfigQemu) Validate(current *ConfigQemu, version Version) (err err
return
}
}
if config.USBs != nil {
if err = config.USBs.Validate(nil); err != nil {
return
}
}
} else { // Update
if config.CPU != nil {
if err = config.CPU.Validate(current.CPU, version); err != nil {
Expand All @@ -735,6 +705,11 @@ func (config ConfigQemu) Validate(current *ConfigQemu, version Version) (err err
return
}
}
if config.USBs != nil {
if err = config.USBs.Validate(current.USBs); err != nil {
return
}
}
}
// Shared
if config.Agent != nil {
Expand Down Expand Up @@ -826,7 +801,6 @@ var (
rxUnusedDiskName = regexp.MustCompile(`^(unused)\d+`)
rxNicName = regexp.MustCompile(`net\d+`)
rxMpName = regexp.MustCompile(`mp\d+`)
rxUsbName = regexp.MustCompile(`usb\d+`)
rxPCIName = regexp.MustCompile(`hostpci\d+`)
)

Expand Down Expand Up @@ -1160,16 +1134,6 @@ func (c ConfigQemu) CreateQemuPCIsParams(params map[string]interface{}) {
}
}

// Create parameters for usb interface
func (c ConfigQemu) CreateQemuUsbsParams(params map[string]interface{}) {
for usbID, usbConfMap := range c.QemuUsbs {
qemuUsbName := "usb" + strconv.Itoa(usbID)

// Add back to Qemu prams.
params[qemuUsbName] = FormatUsbParam(usbConfMap)
}
}

// Create parameters for serial interface
func (c ConfigQemu) CreateQemuMachineParam(
params map[string]interface{},
Expand Down
10 changes: 5 additions & 5 deletions proxmox/config_qemu_cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,13 @@ func (CloudInitNetworkConfig) mapToSDK(param string) (config CloudInitNetworkCon
}
if v, isSet := params["ip6"]; isSet {
ipv6Set = true
if v == "dhcp" {
switch v {
case "dhcp":
ipv6.DHCP = true
} else if v == "auto" {
case "auto":
ipv6.SLAAC = true
} else {
tmp := IPv6CIDR(v)
ipv6.Address = &tmp
default:
ipv6.Address = util.Pointer(IPv6CIDR(v))
}
}
if v, isSet := params["gw6"]; isSet {
Expand Down
Loading

0 comments on commit 5b6e96a

Please sign in to comment.