Skip to content

Commit 1d23493

Browse files
committed
Verify that cpu type exists in qemu
Newer CPU like cortex-a53 might not exist in old QEMU. Make sure to pass a -machine, if needed by qemu help. Signed-off-by: Anders F Björklund <[email protected]>
1 parent e27305f commit 1d23493

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

pkg/qemu/qemu.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,16 @@ type features struct {
290290
// e.g. "Supported machines are:\nakita...\n...virt-6.2...\n...virt-7.0...\n...\n"
291291
// Not machine-readable, but checking strings.Contains() should be fine.
292292
MachineHelp []byte
293+
// CPUHelp is the output of `qemu-system-x86_64 -cpu help`
294+
// e.g. "Available CPUs:\n...\nx86 base...\nx86 host...\n...\n"
295+
// Not machine-readable, but checking strings.Contains() should be fine.
296+
CPUHelp []byte
293297

294298
// VersionGEQ7 is true when the QEMU version seems v7.0.0 or later
295299
VersionGEQ7 bool
296300
}
297301

298-
func inspectFeatures(exe string) (*features, error) {
302+
func inspectFeatures(exe string, machine string) (*features, error) {
299303
var (
300304
f features
301305
stdout bytes.Buffer
@@ -338,6 +342,19 @@ func inspectFeatures(exe string) (*features, error) {
338342
}
339343
f.VersionGEQ7 = strings.Contains(string(f.MachineHelp), "-7.0")
340344

345+
// Avoid error: "No machine specified, and there is no default"
346+
cmd = exec.Command(exe, "-cpu", "help", "-machine", machine)
347+
cmd.Stdout = &stdout
348+
cmd.Stderr = &stderr
349+
if err := cmd.Run(); err != nil {
350+
logrus.Warnf("failed to run %v: stdout=%q, stderr=%q", cmd.Args, stdout.String(), stderr.String())
351+
} else {
352+
f.CPUHelp = stdout.Bytes()
353+
if len(f.CPUHelp) == 0 {
354+
f.CPUHelp = stderr.Bytes()
355+
}
356+
}
357+
341358
return &f, nil
342359
}
343360

@@ -442,14 +459,22 @@ func adjustMemBytesDarwinARM64HVF(memBytes int64, accel string, features *featur
442459
return memBytes
443460
}
444461

462+
// qemuMachine returns string to use for -machine
463+
func qemuMachine(arch limayaml.Arch) string {
464+
if arch == limayaml.X8664 {
465+
return "q35"
466+
}
467+
return "virt"
468+
}
469+
445470
func Cmdline(cfg Config) (string, []string, error) {
446471
y := cfg.LimaYAML
447472
exe, args, err := getExe(*y.Arch)
448473
if err != nil {
449474
return "", nil, err
450475
}
451476

452-
features, err := inspectFeatures(exe)
477+
features, err := inspectFeatures(exe, qemuMachine(*y.Arch))
453478
if err != nil {
454479
return "", nil, err
455480
}
@@ -495,6 +520,9 @@ func Cmdline(cfg Config) (string, []string, error) {
495520
}
496521
}
497522
}
523+
if !strings.Contains(string(features.CPUHelp), strings.Split(cpu, ",")[0]) {
524+
return "", nil, fmt.Errorf("cpu %q is not supported by %s", cpu, exe)
525+
}
498526
args = appendArgsIfNoConflict(args, "-cpu", cpu)
499527

500528
// Machine

0 commit comments

Comments
 (0)