Skip to content

Commit

Permalink
fix(qemu): use proper machine type and cpu model based on host arch (#…
Browse files Browse the repository at this point in the history
…1627)

* fix(qemu): use proper machine type and cpu model based on host arch

---------

Signed-off-by: Luca Di Maio <[email protected]>
  • Loading branch information
89luca89 authored Nov 8, 2024
1 parent fd9b5c9 commit c072b03
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions pkg/container/qemu_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,14 @@ func createMicroVM(ctx context.Context, cfg *Config) error {

// we need to fallback to -machine virt, if not machine has been specified
if !microvm {
baseargs = append(baseargs, "-machine", "virt")
if cfg.Arch.ToAPK() != apko_types.ParseArchitecture(runtime.GOARCH).ToAPK() {
baseargs = append(baseargs, "-machine", "virt,virtualization=true")
} else if _, err := os.Stat("/dev/kvm"); err == nil {
// aarch64 supports virt machine type, let's use that if we're on it, else
// if we're on x86 arch, but without microvm machine type, let's go to q35
if cfg.Arch.ToAPK() == "aarch64" {
baseargs = append(baseargs, "-machine", "virt")
} else if cfg.Arch.ToAPK() == "x86_64" {
baseargs = append(baseargs, "-machine", "q35")
} else {
return fmt.Errorf("unknown architecture: %s", cfg.Arch.ToAPK())
}
}

Expand Down Expand Up @@ -370,6 +373,18 @@ func createMicroVM(ctx context.Context, cfg *Config) error {
baseargs = append(baseargs, "-cpu", cfg.CPUModel)
} else {
baseargs = append(baseargs, "-cpu", "host")
// we cant use `-cpu host` when architecture does not match between host
// and guest
if cfg.Arch.ToAPK() != apko_types.ParseArchitecture(runtime.GOARCH).ToAPK() {
// let's use a recent default for those
if cfg.Arch.ToAPK() == "aarch64" {
baseargs = append(baseargs, "-cpu", "cortex-a76")
} else if cfg.Arch.ToAPK() == "x86_64" {
baseargs = append(baseargs, "-cpu", "Haswell-v4")
} else {
return fmt.Errorf("unknown architecture: %s", cfg.Arch.ToAPK())
}
}
}

baseargs = append(baseargs, "-daemonize")
Expand Down

0 comments on commit c072b03

Please sign in to comment.