Skip to content

Commit 262b4e4

Browse files
committed
bcvk-qemu: Add MachineType for architecture-appropriate defaults
Per previous commit we're still carrying direct qemu code, and we really do want to use the modern default machine types. This came up as part of deduplicating our qemu logic with that generated for custom bootc testing. This mirrors the machine types used by bcvk's libvirt integration (see crates/kit/src/arch.rs) for consistency between direct QEMU and libvirt code paths. Assisted-by: OpenCode (claude-opus-4-5@20251101) Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 217e275 commit 262b4e4

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

crates/bcvk-qemu/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ pub use credentials::{
5353
};
5454

5555
pub use qemu::{
56-
BootMode, DiskFormat, DisplayMode, NetworkMode, QemuConfig, ResourceLimits, RunningQemu,
57-
VirtioBlkDevice, VirtioSerialOut, VirtiofsMount, VHOST_VSOCK,
56+
BootMode, DiskFormat, DisplayMode, MachineType, NetworkMode, QemuConfig, ResourceLimits,
57+
RunningQemu, VirtioBlkDevice, VirtioSerialOut, VirtiofsMount, VHOST_VSOCK,
5858
};
5959

6060
pub use virtiofsd::{spawn_virtiofsd_async, validate_virtiofsd_config, VirtiofsConfig};

crates/bcvk-qemu/src/qemu.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,38 @@ pub struct ResourceLimits {
118118
pub nice_level: Option<i8>,
119119
}
120120

121+
/// QEMU machine type selection.
122+
#[derive(Debug, Clone, Default)]
123+
pub enum MachineType {
124+
/// Auto-detect based on host architecture (recommended).
125+
#[default]
126+
Auto,
127+
/// Use a specific machine type string (e.g., "q35", "pc-q35-9.2").
128+
Explicit(String),
129+
}
130+
131+
impl MachineType {
132+
/// Resolve to a QEMU `-machine` argument for the current host.
133+
///
134+
/// Returns `None` for unknown architectures, letting QEMU use its default.
135+
pub fn resolve(&self) -> Option<&str> {
136+
// xref: https://github.com/coreos/coreos-assembler/blob/main/mantle/platform/qemu.go
137+
match self {
138+
Self::Auto => match std::env::consts::ARCH {
139+
"x86_64" => Some("q35"),
140+
// gic-version=max selects the best available GIC for the host
141+
"aarch64" => Some("virt,gic-version=max"),
142+
"s390x" => Some("s390-ccw-virtio"),
143+
// kvm-type=HV ensures bare metal KVM, not user mode
144+
// ic-mode=xics for interrupt controller
145+
"powerpc64" => Some("pseries,kvm-type=HV,ic-mode=xics"),
146+
_ => None,
147+
},
148+
Self::Explicit(name) => Some(name.as_str()),
149+
}
150+
}
151+
}
152+
121153
/// VM boot configuration.
122154
#[derive(Debug)]
123155
pub enum BootMode {
@@ -157,6 +189,8 @@ pub struct QemuConfig {
157189
pub memory_mb: u32,
158190
/// Number of vCPUs (1-256).
159191
pub vcpus: u32,
192+
/// Machine type (default: auto-detect based on host architecture).
193+
pub machine_type: MachineType,
160194
boot_mode: Option<BootMode>,
161195
/// Main VirtioFS configuration for root filesystem (handled separately from additional mounts).
162196
pub main_virtiofs_config: Option<VirtiofsConfig>,
@@ -488,6 +522,11 @@ fn spawn(
488522
});
489523
}
490524

525+
// Set machine type (auto-detected or explicit)
526+
if let Some(machine) = config.machine_type.resolve() {
527+
cmd.args(["-machine", machine]);
528+
}
529+
491530
cmd.args([
492531
"-m",
493532
&memory_arg,

0 commit comments

Comments
 (0)