Skip to content

Commit 89587a5

Browse files
Merge pull request containers#21638 from ashley-cui/buildtag
Build tag out QEMU for Darwin
2 parents 2c45047 + a9401de commit 89587a5

21 files changed

+122
-88
lines changed

pkg/machine/define/usb.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package define
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
type USBConfig struct {
10+
Bus string
11+
DevNumber string
12+
Vendor int
13+
Product int
14+
}
15+
16+
func ParseUSBs(usbs []string) ([]USBConfig, error) {
17+
configs := []USBConfig{}
18+
for _, str := range usbs {
19+
if str == "" {
20+
// Ignore --usb="" as it can be used to reset USBConfigs
21+
continue
22+
}
23+
24+
vals := strings.Split(str, ",")
25+
if len(vals) != 2 {
26+
return configs, fmt.Errorf("usb: fail to parse: missing ',': %s", str)
27+
}
28+
29+
left := strings.Split(vals[0], "=")
30+
if len(left) != 2 {
31+
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str)
32+
}
33+
34+
right := strings.Split(vals[1], "=")
35+
if len(right) != 2 {
36+
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str)
37+
}
38+
39+
option := left[0] + "_" + right[0]
40+
41+
switch option {
42+
case "bus_devnum", "devnum_bus":
43+
bus, devnumber := left[1], right[1]
44+
if right[0] == "bus" {
45+
bus, devnumber = devnumber, bus
46+
}
47+
48+
configs = append(configs, USBConfig{
49+
Bus: bus,
50+
DevNumber: devnumber,
51+
})
52+
case "vendor_product", "product_vendor":
53+
vendorStr, productStr := left[1], right[1]
54+
if right[0] == "vendor" {
55+
vendorStr, productStr = productStr, vendorStr
56+
}
57+
58+
vendor, err := strconv.ParseInt(vendorStr, 16, 0)
59+
if err != nil {
60+
return configs, fmt.Errorf("usb: fail to convert vendor of %s: %s", str, err)
61+
}
62+
63+
product, err := strconv.ParseInt(productStr, 16, 0)
64+
if err != nil {
65+
return configs, fmt.Errorf("usb: fail to convert product of %s: %s", str, err)
66+
}
67+
68+
configs = append(configs, USBConfig{
69+
Vendor: int(vendor),
70+
Product: int(product),
71+
})
72+
default:
73+
return configs, fmt.Errorf("usb: fail to parse: %s", str)
74+
}
75+
}
76+
return configs, nil
77+
}

pkg/machine/qemu/command/command.go

+3-72
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !darwin
2+
13
package command
24

35
import (
@@ -6,7 +8,6 @@ import (
68
"io/fs"
79
"os"
810
"strconv"
9-
"strings"
1011
"time"
1112

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

6061
// SetNetwork adds a network device to the machine
61-
func (q *QemuCmd) SetUSBHostPassthrough(usbs []USBConfig) {
62+
func (q *QemuCmd) SetUSBHostPassthrough(usbs []define.USBConfig) {
6263
if len(usbs) == 0 {
6364
return
6465
}
@@ -115,76 +116,6 @@ func (q *QemuCmd) Build() []string {
115116
return *q
116117
}
117118

118-
type USBConfig struct {
119-
Bus string
120-
DevNumber string
121-
Vendor int
122-
Product int
123-
}
124-
125-
func ParseUSBs(usbs []string) ([]USBConfig, error) {
126-
configs := []USBConfig{}
127-
for _, str := range usbs {
128-
if str == "" {
129-
// Ignore --usb="" as it can be used to reset USBConfigs
130-
continue
131-
}
132-
133-
vals := strings.Split(str, ",")
134-
if len(vals) != 2 {
135-
return configs, fmt.Errorf("usb: fail to parse: missing ',': %s", str)
136-
}
137-
138-
left := strings.Split(vals[0], "=")
139-
if len(left) != 2 {
140-
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str)
141-
}
142-
143-
right := strings.Split(vals[1], "=")
144-
if len(right) != 2 {
145-
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str)
146-
}
147-
148-
option := left[0] + "_" + right[0]
149-
150-
switch option {
151-
case "bus_devnum", "devnum_bus":
152-
bus, devnumber := left[1], right[1]
153-
if right[0] == "bus" {
154-
bus, devnumber = devnumber, bus
155-
}
156-
157-
configs = append(configs, USBConfig{
158-
Bus: bus,
159-
DevNumber: devnumber,
160-
})
161-
case "vendor_product", "product_vendor":
162-
vendorStr, productStr := left[1], right[1]
163-
if right[0] == "vendor" {
164-
vendorStr, productStr = productStr, vendorStr
165-
}
166-
167-
vendor, err := strconv.ParseInt(vendorStr, 16, 0)
168-
if err != nil {
169-
return configs, fmt.Errorf("usb: fail to convert vendor of %s: %s", str, err)
170-
}
171-
172-
product, err := strconv.ParseInt(productStr, 16, 0)
173-
if err != nil {
174-
return configs, fmt.Errorf("usb: fail to convert product of %s: %s", str, err)
175-
}
176-
177-
configs = append(configs, USBConfig{
178-
Vendor: int(vendor),
179-
Product: int(product),
180-
})
181-
default:
182-
return configs, fmt.Errorf("usb: fail to parse: %s", str)
183-
}
184-
}
185-
return configs, nil
186-
}
187-
188119
type Monitor struct {
189120
// Address portion of the qmp monitor (/tmp/tmp.sock)
190121
Address define.VMFile

pkg/machine/qemu/command/command_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !darwin
2+
13
package command
24

35
import (

pkg/machine/qemu/command/helpers.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !darwin
2+
13
package command
24

35
import (

pkg/machine/qemu/command/qemu_command_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build (amd64 && !windows) || (arm64 && !windows)
1+
//go:build ((amd64 && !windows) || (arm64 && !windows)) && !darwin
22

33
package command
44

pkg/machine/qemu/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !darwin
2+
13
package qemu
24

35
import (

pkg/machine/qemu/config_test.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1+
//go:build !darwin
2+
13
package qemu
24

35
import (
46
"reflect"
57
"testing"
68

7-
"github.com/containers/podman/v5/pkg/machine/qemu/command"
9+
"github.com/containers/podman/v5/pkg/machine/define"
810
)
911

1012
func TestUSBParsing(t *testing.T) {
1113
tests := []struct {
1214
name string
1315
args []string
14-
result []command.USBConfig
16+
result []define.USBConfig
1517
wantErr bool
1618
}{
1719
{
1820
name: "Good vendor and product",
1921
args: []string{"vendor=13d3,product=5406", "vendor=08ec,product=0016"},
20-
result: []command.USBConfig{
22+
result: []define.USBConfig{
2123
{
2224
Vendor: 5075,
2325
Product: 21510,
@@ -32,7 +34,7 @@ func TestUSBParsing(t *testing.T) {
3234
{
3335
name: "Good bus and device number",
3436
args: []string{"bus=1,devnum=4", "bus=1,devnum=3"},
35-
result: []command.USBConfig{
37+
result: []define.USBConfig{
3638
{
3739
Bus: "1",
3840
DevNumber: "4",
@@ -47,26 +49,26 @@ func TestUSBParsing(t *testing.T) {
4749
{
4850
name: "Bad vendor and product, not hexa",
4951
args: []string{"vendor=13dk,product=5406"},
50-
result: []command.USBConfig{},
52+
result: []define.USBConfig{},
5153
wantErr: true,
5254
},
5355
{
5456
name: "Bad vendor and product, bad separator",
5557
args: []string{"vendor=13d3:product=5406"},
56-
result: []command.USBConfig{},
58+
result: []define.USBConfig{},
5759
wantErr: true,
5860
},
5961
{
6062
name: "Bad vendor and product, missing equal",
6163
args: []string{"vendor=13d3:product-5406"},
62-
result: []command.USBConfig{},
64+
result: []define.USBConfig{},
6365
wantErr: true,
6466
},
6567
}
6668

6769
for _, test := range tests {
6870
t.Run(test.name, func(t *testing.T) {
69-
got, err := command.ParseUSBs(test.args)
71+
got, err := define.ParseUSBs(test.args)
7072
if (err != nil) != test.wantErr {
7173
t.Errorf("parseUUBs error = %v, wantErr %v", err, test.wantErr)
7274
return

pkg/machine/qemu/machine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build amd64 || arm64
1+
//go:build !darwin
22

33
package qemu
44

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
//go:build !amd64 && !arm64
1+
//go:build !amd64 && !arm64 && darwin
22

33
package qemu

pkg/machine/qemu/machine_windows.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows
2+
13
package qemu
24

35
import (

pkg/machine/qemu/options_freebsd.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build freebsd
2+
13
package qemu
24

35
import (

pkg/machine/qemu/options_freebsd_amd64.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build freebsd && amd64
2+
13
package qemu
24

35
var (

pkg/machine/qemu/options_freebsd_arm64.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build freebsd && arm64
2+
13
package qemu
24

35
var (

pkg/machine/qemu/options_linux_amd64.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build linux && amd64
2+
13
package qemu
24

35
var (

pkg/machine/qemu/options_linux_arm64.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build linux && arm64
2+
13
package qemu
24

35
import (

pkg/machine/qemu/options_windows.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows
2+
13
package qemu
24

35
import (

pkg/machine/qemu/options_windows_amd64.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows && amd64
2+
13
package qemu
24

35
var (

pkg/machine/qemu/options_windows_arm64.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows && arm64
2+
13
package qemu
24

35
var (

pkg/machine/qemu/stubber.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !darwin
2+
13
package qemu
24

35
import (
@@ -277,7 +279,7 @@ func (q *QEMUStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define.
277279
}
278280

279281
if opts.USBs != nil {
280-
usbs, err := command.ParseUSBs(*opts.USBs)
282+
usbs, err := define.ParseUSBs(*opts.USBs)
281283
if err != nil {
282284
return err
283285
}

pkg/machine/vmconfigs/config.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
77
"github.com/containers/podman/v5/pkg/machine/define"
88
"github.com/containers/podman/v5/pkg/machine/ignition"
9-
"github.com/containers/podman/v5/pkg/machine/qemu/command"
109
"github.com/containers/storage/pkg/lockfile"
1110
)
1211

@@ -128,7 +127,7 @@ type ResourceConfig struct {
128127
// Memory in megabytes assigned to the vm
129128
Memory uint64
130129
// Usbs
131-
USBs []command.USBConfig
130+
USBs []define.USBConfig
132131
}
133132

134133
// SSHConfig contains remote access information for SSH

pkg/machine/vmconfigs/machine.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/containers/podman/v5/pkg/machine/connection"
1515
"github.com/containers/podman/v5/pkg/machine/define"
1616
"github.com/containers/podman/v5/pkg/machine/lock"
17-
"github.com/containers/podman/v5/pkg/machine/qemu/command"
1817
"github.com/containers/podman/v5/utils"
1918
"github.com/sirupsen/logrus"
2019
)
@@ -63,7 +62,7 @@ func NewMachineConfig(opts define.InitOptions, dirs *define.MachineDirs, sshIden
6362
return nil, fmt.Errorf("USB host passthrough not supported for %s machines", vmtype)
6463
}
6564

66-
usbs, err := command.ParseUSBs(opts.USBs)
65+
usbs, err := define.ParseUSBs(opts.USBs)
6766
if err != nil {
6867
return nil, err
6968
}

0 commit comments

Comments
 (0)