Skip to content

feat(krun): expand DiskBuilder with id, cache, direct_io, sync#51

Merged
appcypher merged 3 commits intokrunfrom
appcypher/disk-builder-volume-knobs
Apr 26, 2026
Merged

feat(krun): expand DiskBuilder with id, cache, direct_io, sync#51
appcypher merged 3 commits intokrunfrom
appcypher/disk-builder-volume-knobs

Conversation

@appcypher
Copy link
Copy Markdown
Member

@appcypher appcypher commented Apr 23, 2026

Summary

  • Expand DiskBuilder with id(), cache(), direct_io(), and sync() methods, surfacing per-disk knobs that libkrun's krun_add_disk3 C API already supports but the Rust wrapper had hardcoded.
  • Add CacheMode { Writeback, Unsafe } and SyncMode { None, Relaxed, Full } enums (re-exported at the crate root) with From impls into devices::virtio::{CacheType, block::SyncMode}.
  • Fix a latent naming bug: format!("vd{}", (b'a' + i) as char) rolled over into invalid characters (vd{, vd|, ...) past the 26th disk. Replaced with a vd_suffix() helper implementing the kernel's bijective base-26 disk_name() scheme (vda..vdz, vdaa..vdzz, vdaaa...).
  • Motivation: microsandbox2 currently uses disk images only as rootfs. This PR unlocks attaching additional disks as volumes at arbitrary guest mount paths, with stable caller-chosen block IDs so the guest can address them via /dev/disk/by-id/virtio-<id> independent of attach order.
  • Non-breaking: defaults (Writeback cache, direct_io=false, Full sync) preserve the previous hardcoded behavior, so existing .disk(|d| d.path().format().read_only()) callers are unaffected.

Test Plan

  • cargo build -p msb_krun (no features) passes
  • cargo build -p msb_krun --features blk passes
  • cargo build -p msb_krun --features blk,net passes
  • cargo test -p msb_krun --features blk passes — 7 unit + 22 doctests (includes four new tests: vd_suffix_matches_kernel_scheme, disk_builder_preserves_insertion_order, disk_builder_auto_id_then_custom, disk_builder_per_disk_settings_dont_leak)
  • cargo clippy -p msb_krun --features blk -- -D warnings clean
  • Downstream smoke

Surface the per-disk knobs that libkrun's krun_add_disk3 C API already
supports but the Rust wrapper had hardcoded. This lets callers attach
extra disks as volumes (not just a rootfs) with stable identifiers and
per-disk safety/perf settings.

- Add CacheMode { Writeback, Unsafe } and SyncMode { None, Relaxed, Full }
  enums with From impls to devices::virtio::{CacheType, block::SyncMode}.
- Add DiskBuilder::id() for caller-chosen block_ids, enabling guest
  addressing via /dev/disk/by-id/virtio-<id> independent of attach order.
- Add DiskBuilder::cache(), direct_io(), sync() alongside the existing
  path/format/read_only methods.
- Replace the naive format!("vd{}", (b'a' + i) as char) device-id
  fallback, which rolled over into invalid characters past vdz, with a
  bijective base-26 vd_suffix() helper matching block/genhd.c's
  disk_name() scheme (vda..vdz, vdaa..vdzz, vdaaa...).
- Re-export CacheMode and SyncMode from the crate root.

Defaults (Writeback cache, direct_io=false, Full sync) preserve the
previous hardcoded behavior, so existing .disk(|d| d.path().format()
.read_only()) callers are unaffected.
When the caller passes a non-empty id via DiskBuilder::id() it now
shows up in the guest as the virtio-blk serial (visible at
/sys/block/<dev>/serial and, with udev, /dev/disk/by-id/virtio-<id>).
Previously the id was stored as Block.id but disk_image_id was always
generated from the host file's st_rdev + st_ino, so the configured id
never reached the guest.

Falls back to the rdev+inode default when id is empty, preserving
existing behaviour for callers that do not set an id.

Padding/truncation matches the existing default path: zero-padded to
VIRTIO_BLK_ID_BYTES (20), with longer ids truncated.
@appcypher appcypher merged commit a5b01a2 into krun Apr 26, 2026
8 checks passed
@appcypher appcypher deleted the appcypher/disk-builder-volume-knobs branch April 26, 2026 19:05
appcypher added a commit that referenced this pull request Apr 26, 2026
Bump all msb_krun_* workspace crates and their internal path-dependency
version refs from 0.1.10 to 0.1.11, and refresh both lockfiles. The
libkrun C-API crate (1.17.3) and the rust_vm example are unchanged.

Changes since 0.1.10:

- feat(krun): expand DiskBuilder with id, cache, direct_io, sync (#51)
- msb_krun_cpuid: fix compilation on Rust 1.93 by wrapping CPUID calls
  in unsafe blocks (#50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant