qemu: fix machine types for various platforms
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import platform
|
||||
import random
|
||||
from collections.abc import Generator
|
||||
from contextlib import contextmanager
|
||||
@@ -85,6 +86,44 @@ class QemuCommand:
|
||||
vsock_cid: int | None = None
|
||||
|
||||
|
||||
def get_machine_options() -> str:
|
||||
"""Get appropriate QEMU machine options for host architecture."""
|
||||
arch = platform.machine().lower()
|
||||
system = platform.system().lower()
|
||||
|
||||
# Determine accelerator based on OS
|
||||
if system == "darwin":
|
||||
# macOS uses Hypervisor.framework
|
||||
accel = "hvf"
|
||||
else:
|
||||
# Linux and others use KVM
|
||||
accel = "kvm"
|
||||
|
||||
if arch in ("x86_64", "amd64", "i386", "i686"):
|
||||
# For x86_64, use q35 for modern PCIe support
|
||||
return f"q35,memory-backend=mem,accel={accel}"
|
||||
if arch in ("aarch64", "arm64"):
|
||||
# Use virt machine type for ARM64
|
||||
if system == "darwin":
|
||||
# macOS ARM uses GIC version 2
|
||||
return f"virt,gic-version=2,memory-backend=mem,accel={accel}"
|
||||
# Linux ARM uses max GIC version
|
||||
return f"virt,gic-version=max,memory-backend=mem,accel={accel}"
|
||||
if arch == "armv7l":
|
||||
# 32-bit ARM
|
||||
return f"virt,memory-backend=mem,accel={accel}"
|
||||
if arch in ("riscv64", "riscv32"):
|
||||
# RISC-V architectures
|
||||
return f"virt,memory-backend=mem,accel={accel}"
|
||||
if arch in ("powerpc64le", "powerpc64", "ppc64le", "ppc64"):
|
||||
# PowerPC architectures
|
||||
return f"powernv,memory-backend=mem,accel={accel}"
|
||||
|
||||
# No fallback - raise an error for unsupported architectures
|
||||
msg = f"Unsupported architecture: {arch} on {system}. Supported architectures are: x86_64, aarch64, armv7l, riscv64, riscv32, powerpc64"
|
||||
raise ClanError(msg)
|
||||
|
||||
|
||||
def qemu_command(
|
||||
vm: VmConfig,
|
||||
nixos_config: dict[str, str],
|
||||
@@ -116,13 +155,14 @@ def qemu_command(
|
||||
if not vm.waypipe.enable:
|
||||
kernel_cmdline.append("console=tty0")
|
||||
hostfwd = ",".join(f"hostfwd=tcp::{h}-:{g}" for h, g in portmap.items())
|
||||
machine_options = get_machine_options()
|
||||
# fmt: off
|
||||
command = [
|
||||
"qemu-kvm",
|
||||
"-name", vm.machine_name,
|
||||
"-m", f'{nixos_config["memorySize"]}M',
|
||||
"-object", f"memory-backend-memfd,id=mem,size={nixos_config['memorySize']}M",
|
||||
"-machine", "pc,memory-backend=mem,accel=kvm",
|
||||
"-machine", machine_options,
|
||||
"-smp", str(nixos_config["cores"]),
|
||||
"-cpu", "max",
|
||||
"-enable-kvm",
|
||||
|
||||
Reference in New Issue
Block a user