Skip to content

Commit

Permalink
Merge pull request containers#21638 from ashley-cui/buildtag
Browse files Browse the repository at this point in the history
Build tag out QEMU for Darwin
  • Loading branch information
openshift-merge-bot[bot] authored Feb 19, 2024
2 parents 2c45047 + a9401de commit 89587a5
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 88 deletions.
77 changes: 77 additions & 0 deletions pkg/machine/define/usb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package define

import (
"fmt"
"strconv"
"strings"
)

type USBConfig struct {
Bus string
DevNumber string
Vendor int
Product int
}

func ParseUSBs(usbs []string) ([]USBConfig, error) {
configs := []USBConfig{}
for _, str := range usbs {
if str == "" {
// Ignore --usb="" as it can be used to reset USBConfigs
continue
}

vals := strings.Split(str, ",")
if len(vals) != 2 {
return configs, fmt.Errorf("usb: fail to parse: missing ',': %s", str)
}

left := strings.Split(vals[0], "=")
if len(left) != 2 {
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str)
}

right := strings.Split(vals[1], "=")
if len(right) != 2 {
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str)
}

option := left[0] + "_" + right[0]

switch option {
case "bus_devnum", "devnum_bus":
bus, devnumber := left[1], right[1]
if right[0] == "bus" {
bus, devnumber = devnumber, bus
}

configs = append(configs, USBConfig{
Bus: bus,
DevNumber: devnumber,
})
case "vendor_product", "product_vendor":
vendorStr, productStr := left[1], right[1]
if right[0] == "vendor" {
vendorStr, productStr = productStr, vendorStr
}

vendor, err := strconv.ParseInt(vendorStr, 16, 0)
if err != nil {
return configs, fmt.Errorf("usb: fail to convert vendor of %s: %s", str, err)
}

product, err := strconv.ParseInt(productStr, 16, 0)
if err != nil {
return configs, fmt.Errorf("usb: fail to convert product of %s: %s", str, err)
}

configs = append(configs, USBConfig{
Vendor: int(vendor),
Product: int(product),
})
default:
return configs, fmt.Errorf("usb: fail to parse: %s", str)
}
}
return configs, nil
}
75 changes: 3 additions & 72 deletions pkg/machine/qemu/command/command.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !darwin

package command

import (
Expand All @@ -6,7 +8,6 @@ import (
"io/fs"
"os"
"strconv"
"strings"
"time"

"github.com/containers/podman/v5/pkg/machine/define"
Expand Down Expand Up @@ -58,7 +59,7 @@ func (q *QemuCmd) SetNetwork() {
}

// SetNetwork adds a network device to the machine
func (q *QemuCmd) SetUSBHostPassthrough(usbs []USBConfig) {
func (q *QemuCmd) SetUSBHostPassthrough(usbs []define.USBConfig) {
if len(usbs) == 0 {
return
}
Expand Down Expand Up @@ -115,76 +116,6 @@ func (q *QemuCmd) Build() []string {
return *q
}

type USBConfig struct {
Bus string
DevNumber string
Vendor int
Product int
}

func ParseUSBs(usbs []string) ([]USBConfig, error) {
configs := []USBConfig{}
for _, str := range usbs {
if str == "" {
// Ignore --usb="" as it can be used to reset USBConfigs
continue
}

vals := strings.Split(str, ",")
if len(vals) != 2 {
return configs, fmt.Errorf("usb: fail to parse: missing ',': %s", str)
}

left := strings.Split(vals[0], "=")
if len(left) != 2 {
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str)
}

right := strings.Split(vals[1], "=")
if len(right) != 2 {
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str)
}

option := left[0] + "_" + right[0]

switch option {
case "bus_devnum", "devnum_bus":
bus, devnumber := left[1], right[1]
if right[0] == "bus" {
bus, devnumber = devnumber, bus
}

configs = append(configs, USBConfig{
Bus: bus,
DevNumber: devnumber,
})
case "vendor_product", "product_vendor":
vendorStr, productStr := left[1], right[1]
if right[0] == "vendor" {
vendorStr, productStr = productStr, vendorStr
}

vendor, err := strconv.ParseInt(vendorStr, 16, 0)
if err != nil {
return configs, fmt.Errorf("usb: fail to convert vendor of %s: %s", str, err)
}

product, err := strconv.ParseInt(productStr, 16, 0)
if err != nil {
return configs, fmt.Errorf("usb: fail to convert product of %s: %s", str, err)
}

configs = append(configs, USBConfig{
Vendor: int(vendor),
Product: int(product),
})
default:
return configs, fmt.Errorf("usb: fail to parse: %s", str)
}
}
return configs, nil
}

type Monitor struct {
// Address portion of the qmp monitor (/tmp/tmp.sock)
Address define.VMFile
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/command/command_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !darwin

package command

import (
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/command/helpers.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !darwin

package command

import (
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/qemu/command/qemu_command_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build (amd64 && !windows) || (arm64 && !windows)
//go:build ((amd64 && !windows) || (arm64 && !windows)) && !darwin

package command

Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !darwin

package qemu

import (
Expand Down
18 changes: 10 additions & 8 deletions pkg/machine/qemu/config_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
//go:build !darwin

package qemu

import (
"reflect"
"testing"

"github.com/containers/podman/v5/pkg/machine/qemu/command"
"github.com/containers/podman/v5/pkg/machine/define"
)

func TestUSBParsing(t *testing.T) {
tests := []struct {
name string
args []string
result []command.USBConfig
result []define.USBConfig
wantErr bool
}{
{
name: "Good vendor and product",
args: []string{"vendor=13d3,product=5406", "vendor=08ec,product=0016"},
result: []command.USBConfig{
result: []define.USBConfig{
{
Vendor: 5075,
Product: 21510,
Expand All @@ -32,7 +34,7 @@ func TestUSBParsing(t *testing.T) {
{
name: "Good bus and device number",
args: []string{"bus=1,devnum=4", "bus=1,devnum=3"},
result: []command.USBConfig{
result: []define.USBConfig{
{
Bus: "1",
DevNumber: "4",
Expand All @@ -47,26 +49,26 @@ func TestUSBParsing(t *testing.T) {
{
name: "Bad vendor and product, not hexa",
args: []string{"vendor=13dk,product=5406"},
result: []command.USBConfig{},
result: []define.USBConfig{},
wantErr: true,
},
{
name: "Bad vendor and product, bad separator",
args: []string{"vendor=13d3:product=5406"},
result: []command.USBConfig{},
result: []define.USBConfig{},
wantErr: true,
},
{
name: "Bad vendor and product, missing equal",
args: []string{"vendor=13d3:product-5406"},
result: []command.USBConfig{},
result: []define.USBConfig{},
wantErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := command.ParseUSBs(test.args)
got, err := define.ParseUSBs(test.args)
if (err != nil) != test.wantErr {
t.Errorf("parseUUBs error = %v, wantErr %v", err, test.wantErr)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/qemu/machine.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build amd64 || arm64
//go:build !darwin

package qemu

Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/qemu/machine_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//go:build !amd64 && !arm64
//go:build !amd64 && !arm64 && darwin

package qemu
2 changes: 2 additions & 0 deletions pkg/machine/qemu/machine_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build windows

package qemu

import (
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/options_freebsd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build freebsd

package qemu

import (
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/options_freebsd_amd64.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build freebsd && amd64

package qemu

var (
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/options_freebsd_arm64.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build freebsd && arm64

package qemu

var (
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/options_linux_amd64.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux && amd64

package qemu

var (
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/options_linux_arm64.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux && arm64

package qemu

import (
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/options_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build windows

package qemu

import (
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/options_windows_amd64.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build windows && amd64

package qemu

var (
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/options_windows_arm64.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build windows && arm64

package qemu

var (
Expand Down
4 changes: 3 additions & 1 deletion pkg/machine/qemu/stubber.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !darwin

package qemu

import (
Expand Down Expand Up @@ -277,7 +279,7 @@ func (q *QEMUStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define.
}

if opts.USBs != nil {
usbs, err := command.ParseUSBs(*opts.USBs)
usbs, err := define.ParseUSBs(*opts.USBs)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/machine/vmconfigs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/ignition"
"github.com/containers/podman/v5/pkg/machine/qemu/command"
"github.com/containers/storage/pkg/lockfile"
)

Expand Down Expand Up @@ -128,7 +127,7 @@ type ResourceConfig struct {
// Memory in megabytes assigned to the vm
Memory uint64
// Usbs
USBs []command.USBConfig
USBs []define.USBConfig
}

// SSHConfig contains remote access information for SSH
Expand Down
3 changes: 1 addition & 2 deletions pkg/machine/vmconfigs/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/containers/podman/v5/pkg/machine/connection"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/lock"
"github.com/containers/podman/v5/pkg/machine/qemu/command"
"github.com/containers/podman/v5/utils"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -63,7 +62,7 @@ func NewMachineConfig(opts define.InitOptions, dirs *define.MachineDirs, sshIden
return nil, fmt.Errorf("USB host passthrough not supported for %s machines", vmtype)
}

usbs, err := command.ParseUSBs(opts.USBs)
usbs, err := define.ParseUSBs(opts.USBs)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 89587a5

Please sign in to comment.