Skip to content

Commit 14a3842

Browse files
committed
qemu: allow overriding -cpu, -machine, .. as $QEMU_SYSTEM_X86_64
e.g. QEMU_SYSTEM_X86_64="qemu-system-x86_64 -cpu host" Signed-off-by: Akihiro Suda <[email protected]>
1 parent 75443f8 commit 14a3842

File tree

2 files changed

+102
-9
lines changed

2 files changed

+102
-9
lines changed

pkg/qemu/qemu.go

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,50 @@ func EnsureDisk(cfg Config) error {
8383
return nil
8484
}
8585

86+
func argValue(args []string, key string) (string, bool) {
87+
if !strings.HasPrefix(key, "-") {
88+
panic(errors.Errorf("got unexpected key %q", key))
89+
}
90+
for i, s := range args {
91+
if s == key {
92+
if i == len(args)-1 {
93+
return "", true
94+
}
95+
value := args[i+1]
96+
if strings.HasPrefix(value, "-") {
97+
return "", true
98+
}
99+
return value, true
100+
}
101+
}
102+
return "", false
103+
}
104+
105+
// appendArgsIfNoConflict can be used for: -cpu, -machine, -m, -boot ...
106+
// appendArgsIfNoConflict cannot be used for: -drive, -cdrom, ...
107+
func appendArgsIfNoConflict(args []string, k, v string) []string {
108+
if !strings.HasPrefix(k, "-") {
109+
panic(errors.Errorf("got unexpected key %q", k))
110+
}
111+
switch k {
112+
case "-drive", "-cdrom", "-chardev", "-blockdev", "-netdev", "-device":
113+
panic(errors.Errorf("appendArgsIfNoConflict() must not be called with k=%q", k))
114+
}
115+
116+
if v == "" {
117+
if _, ok := argValue(args, k); ok {
118+
return args
119+
}
120+
return append(args, k)
121+
}
122+
123+
if origV, ok := argValue(args, k); ok {
124+
logrus.Warnf("Not adding QEMU argument %q %q, as it conflicts with %q %q", k, v, k, origV)
125+
return args
126+
}
127+
return append(args, k, v)
128+
}
129+
86130
func Cmdline(cfg Config) (string, []string, error) {
87131
y := cfg.LimaYAML
88132
exe, args, err := getExe(y.Arch)
@@ -96,23 +140,23 @@ func Cmdline(cfg Config) (string, []string, error) {
96140
case limayaml.X8664:
97141
// NOTE: "-cpu host" seems to cause kernel panic
98142
// (MacBookPro 2020, Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz, macOS 11.3, Ubuntu 21.04)
99-
args = append(args, "-cpu", "Haswell-v4")
100-
args = append(args, "-machine", "q35,accel="+accel)
143+
args = appendArgsIfNoConflict(args, "-cpu", "Haswell-v4")
144+
args = appendArgsIfNoConflict(args, "-machine", "q35,accel="+accel)
101145
case limayaml.AARCH64:
102-
args = append(args, "-cpu", "cortex-a72")
103-
args = append(args, "-machine", "virt,accel="+accel+",highmem=off")
146+
args = appendArgsIfNoConflict(args, "-cpu", "cortex-a72")
147+
args = appendArgsIfNoConflict(args, "-machine", "virt,accel="+accel+",highmem=off")
104148
}
105149

106150
// SMP
107-
args = append(args, "-smp",
151+
args = appendArgsIfNoConflict(args, "-smp",
108152
fmt.Sprintf("%d,sockets=1,cores=%d,threads=1", y.CPUs, y.CPUs))
109153

110154
// Memory
111155
memBytes, err := units.RAMInBytes(y.Memory)
112156
if err != nil {
113157
return "", nil, err
114158
}
115-
args = append(args, "-m", strconv.Itoa(int(memBytes>>20)))
159+
args = appendArgsIfNoConflict(args, "-m", strconv.Itoa(int(memBytes>>20)))
116160

117161
// Firmware
118162
if !y.Firmware.LegacyBIOS {
@@ -132,10 +176,10 @@ func Cmdline(cfg Config) (string, []string, error) {
132176
return "", nil, err
133177
}
134178
if isBaseDiskCDROM {
135-
args = append(args, "-boot", "order=d,splash-time=0,menu=on")
179+
args = appendArgsIfNoConflict(args, "-boot", "order=d,splash-time=0,menu=on")
136180
args = append(args, "-drive", fmt.Sprintf("file=%s,media=cdrom,readonly=on", baseDisk))
137181
} else {
138-
args = append(args, "-boot", "order=c,splash-time=0,menu=on")
182+
args = appendArgsIfNoConflict(args, "-boot", "order=c,splash-time=0,menu=on")
139183
}
140184
if diskSize, _ := units.RAMInBytes(cfg.LimaYAML.Disk); diskSize > 0 {
141185
args = append(args, "-drive", fmt.Sprintf("file=%s,if=virtio", diffDisk))
@@ -156,7 +200,7 @@ func Cmdline(cfg Config) (string, []string, error) {
156200

157201
// Graphics
158202
if y.Video.Display != "" {
159-
args = append(args, "-display", y.Video.Display)
203+
args = appendArgsIfNoConflict(args, "-display", y.Video.Display)
160204
}
161205
switch y.Arch {
162206
case limayaml.X8664:

pkg/qemu/qemu_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package qemu
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestArgValue(t *testing.T) {
10+
type testCase struct {
11+
key string
12+
expectedValue string
13+
expectedOK bool
14+
}
15+
args := []string{"-cpu", "foo", "-no-reboot", "-m", "2G", "-s"}
16+
testCases := []testCase{
17+
{
18+
key: "-cpu",
19+
expectedValue: "foo",
20+
expectedOK: true,
21+
},
22+
{
23+
key: "-no-reboot",
24+
expectedValue: "",
25+
expectedOK: true,
26+
},
27+
{
28+
key: "-m",
29+
expectedValue: "2G",
30+
expectedOK: true,
31+
},
32+
{
33+
key: "-machine",
34+
expectedValue: "",
35+
expectedOK: false,
36+
},
37+
{
38+
key: "-s",
39+
expectedValue: "",
40+
expectedOK: true,
41+
},
42+
}
43+
44+
for _, tc := range testCases {
45+
v, ok := argValue(args, tc.key)
46+
assert.Equal(t, tc.expectedValue, v)
47+
assert.Equal(t, tc.expectedOK, ok)
48+
}
49+
}

0 commit comments

Comments
 (0)