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
import (
@ -6,7 +8,6 @@ import (
"io/fs"
"os"
"strconv"
"strings"
"time"
"github.com/containers/podman/v5/pkg/machine/define"
@ -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
}
@ -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

View File

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

View File

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

View File

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

View File

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

View File

@ -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,
@ -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",
@ -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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,5 @@
//go:build !darwin
package qemu
import (
@ -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
}

View File

@ -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"
)
@ -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

View File

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