Merge pull request #21638 from ashley-cui/buildtag

Build tag out QEMU for Darwin
This commit is contained in:
openshift-merge-bot[bot]
2024-02-19 13:31:58 +00:00
committed by GitHub
21 changed files with 122 additions and 88 deletions

77
pkg/machine/define/usb.go Normal file
View File

@ -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
}

View File

@ -1,3 +1,5 @@
//go:build !darwin
package command package command
import ( import (
@ -6,7 +8,6 @@ import (
"io/fs" "io/fs"
"os" "os"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/containers/podman/v5/pkg/machine/define" "github.com/containers/podman/v5/pkg/machine/define"
@ -58,7 +59,7 @@ func (q *QemuCmd) SetNetwork() {
} }
// SetNetwork adds a network device to the machine // 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 { if len(usbs) == 0 {
return return
} }
@ -115,76 +116,6 @@ func (q *QemuCmd) Build() []string {
return *q 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 { type Monitor struct {
// Address portion of the qmp monitor (/tmp/tmp.sock) // Address portion of the qmp monitor (/tmp/tmp.sock)
Address define.VMFile Address define.VMFile

View File

@ -1,3 +1,5 @@
//go:build !darwin
package command package command
import ( import (

View File

@ -1,3 +1,5 @@
//go:build !darwin
package command package command
import ( import (

View File

@ -1,4 +1,4 @@
//go:build (amd64 && !windows) || (arm64 && !windows) //go:build ((amd64 && !windows) || (arm64 && !windows)) && !darwin
package command package command

View File

@ -1,3 +1,5 @@
//go:build !darwin
package qemu package qemu
import ( import (

View File

@ -1,23 +1,25 @@
//go:build !darwin
package qemu package qemu
import ( import (
"reflect" "reflect"
"testing" "testing"
"github.com/containers/podman/v5/pkg/machine/qemu/command" "github.com/containers/podman/v5/pkg/machine/define"
) )
func TestUSBParsing(t *testing.T) { func TestUSBParsing(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
args []string args []string
result []command.USBConfig result []define.USBConfig
wantErr bool wantErr bool
}{ }{
{ {
name: "Good vendor and product", name: "Good vendor and product",
args: []string{"vendor=13d3,product=5406", "vendor=08ec,product=0016"}, args: []string{"vendor=13d3,product=5406", "vendor=08ec,product=0016"},
result: []command.USBConfig{ result: []define.USBConfig{
{ {
Vendor: 5075, Vendor: 5075,
Product: 21510, Product: 21510,
@ -32,7 +34,7 @@ func TestUSBParsing(t *testing.T) {
{ {
name: "Good bus and device number", name: "Good bus and device number",
args: []string{"bus=1,devnum=4", "bus=1,devnum=3"}, args: []string{"bus=1,devnum=4", "bus=1,devnum=3"},
result: []command.USBConfig{ result: []define.USBConfig{
{ {
Bus: "1", Bus: "1",
DevNumber: "4", DevNumber: "4",
@ -47,26 +49,26 @@ func TestUSBParsing(t *testing.T) {
{ {
name: "Bad vendor and product, not hexa", name: "Bad vendor and product, not hexa",
args: []string{"vendor=13dk,product=5406"}, args: []string{"vendor=13dk,product=5406"},
result: []command.USBConfig{}, result: []define.USBConfig{},
wantErr: true, wantErr: true,
}, },
{ {
name: "Bad vendor and product, bad separator", name: "Bad vendor and product, bad separator",
args: []string{"vendor=13d3:product=5406"}, args: []string{"vendor=13d3:product=5406"},
result: []command.USBConfig{}, result: []define.USBConfig{},
wantErr: true, wantErr: true,
}, },
{ {
name: "Bad vendor and product, missing equal", name: "Bad vendor and product, missing equal",
args: []string{"vendor=13d3:product-5406"}, args: []string{"vendor=13d3:product-5406"},
result: []command.USBConfig{}, result: []define.USBConfig{},
wantErr: true, wantErr: true,
}, },
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { 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 { if (err != nil) != test.wantErr {
t.Errorf("parseUUBs error = %v, wantErr %v", err, test.wantErr) t.Errorf("parseUUBs error = %v, wantErr %v", err, test.wantErr)
return return

View File

@ -1,4 +1,4 @@
//go:build amd64 || arm64 //go:build !darwin
package qemu package qemu

View File

@ -1,3 +1,3 @@
//go:build !amd64 && !arm64 //go:build !amd64 && !arm64 && darwin
package qemu package qemu

View File

@ -1,3 +1,5 @@
//go:build windows
package qemu package qemu
import ( import (

View File

@ -1,3 +1,5 @@
//go:build freebsd
package qemu package qemu
import ( import (

View File

@ -1,3 +1,5 @@
//go:build freebsd && amd64
package qemu package qemu
var ( var (

View File

@ -1,3 +1,5 @@
//go:build freebsd && arm64
package qemu package qemu
var ( var (

View File

@ -1,3 +1,5 @@
//go:build linux && amd64
package qemu package qemu
var ( var (

View File

@ -1,3 +1,5 @@
//go:build linux && arm64
package qemu package qemu
import ( import (

View File

@ -1,3 +1,5 @@
//go:build windows
package qemu package qemu
import ( import (

View File

@ -1,3 +1,5 @@
//go:build windows && amd64
package qemu package qemu
var ( var (

View File

@ -1,3 +1,5 @@
//go:build windows && arm64
package qemu package qemu
var ( var (

View File

@ -1,3 +1,5 @@
//go:build !darwin
package qemu package qemu
import ( import (
@ -277,7 +279,7 @@ func (q *QEMUStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define.
} }
if opts.USBs != nil { if opts.USBs != nil {
usbs, err := command.ParseUSBs(*opts.USBs) usbs, err := define.ParseUSBs(*opts.USBs)
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,7 +6,6 @@ import (
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types" gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/containers/podman/v5/pkg/machine/define" "github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/ignition" "github.com/containers/podman/v5/pkg/machine/ignition"
"github.com/containers/podman/v5/pkg/machine/qemu/command"
"github.com/containers/storage/pkg/lockfile" "github.com/containers/storage/pkg/lockfile"
) )
@ -128,7 +127,7 @@ type ResourceConfig struct {
// Memory in megabytes assigned to the vm // Memory in megabytes assigned to the vm
Memory uint64 Memory uint64
// Usbs // Usbs
USBs []command.USBConfig USBs []define.USBConfig
} }
// SSHConfig contains remote access information for SSH // SSHConfig contains remote access information for SSH

View File

@ -14,7 +14,6 @@ import (
"github.com/containers/podman/v5/pkg/machine/connection" "github.com/containers/podman/v5/pkg/machine/connection"
"github.com/containers/podman/v5/pkg/machine/define" "github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/lock" "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/containers/podman/v5/utils"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -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) 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 { if err != nil {
return nil, err return nil, err
} }