Files
Reims-Go/patches/reims-ctl-qcow2-l2-cache.patch
T

53 lines
2.1 KiB
Diff

diff --git a/crates/reims-ctl/src/qemu.rs b/crates/reims-ctl/src/qemu.rs
index 66c7952..67101dc 100644
--- a/crates/reims-ctl/src/qemu.rs
+++ b/crates/reims-ctl/src/qemu.rs
@@ -195,6 +195,33 @@ pub fn opt(s: &str) -> String {
s.replace(',', ",,")
}
+/// Compute the qcow2 L2 cache size in MiB needed to cover the full virtual disk
+/// (`virtual_size / cluster_size * 8` bytes), clamped to `..=32 MiB`, returning `Some(mib)` only
+/// when the image needs more than QEMU's 8 MiB non-Linux default.
+fn qcow2_l2_cache_mib(path: &Path) -> Option<u64> {
+ use std::io::Read;
+ let mut hdr = [0u8; 32];
+ let mut f = std::fs::File::open(path).ok()?;
+ f.read_exact(&mut hdr).ok()?;
+ if &hdr[0..4] != b"QFI\xfb" {
+ return None;
+ }
+ let version = u32::from_be_bytes(hdr[4..8].try_into().ok()?);
+ if version < 2 || version > 3 {
+ return None;
+ }
+ let cluster_bits = u32::from_be_bytes(hdr[20..24].try_into().ok()?);
+ if !(9..=21).contains(&cluster_bits) {
+ return None;
+ }
+ let virtual_size = u64::from_be_bytes(hdr[24..32].try_into().ok()?);
+ let cluster_size = 1u64 << cluster_bits;
+ let clusters = virtual_size.div_ceil(cluster_size);
+ let l2_bytes = clusters.saturating_mul(8);
+ let mib = l2_bytes.div_ceil(1024 * 1024).min(32);
+ (mib > 8).then_some(mib)
+}
+
fn optp(p: &Path) -> String {
opt(&p.to_string_lossy())
}
@@ -378,6 +405,13 @@ fn q35(vm: &Vm, ctx: &Ctx, a: &mut Vec<String>) -> Result<()> {
c.disk_format.as_str(),
optp(&vm.disk())
);
+ // QEMU caps default qcow2 L2 cache at 8 MiB on non-Linux hosts (covering only 64 GiB at 64 KiB
+ // clusters). Size l2-cache-size to cover the image's virtual capacity (capped at 32 MiB).
+ if matches!(c.disk_format, crate::vm::ImageFormat::Qcow2) {
+ if let Some(mib) = qcow2_l2_cache_mib(&vm.disk()) {
+ disk.push_str(&format!(",l2-cache-size={mib}M"));
+ }
+ }
// An install's flushes, which the module's doc measures, are dropped while the installer is
// attached.
if q.installer.is_some() {