Support host:guest mount syntax with WORKINGDIR resolution
CI / build (pull_request) Successful in 12s

--mount now accepts <host-path>:<guest-path> (Docker-style). When the
guest path is omitted, the host path is used. Non-absolute guest paths
are resolved against the image WORKINGDIR, which is written to
/slim/workdir at build time and read by slim-init.sh at boot.

Changes:
- qemu.rs: Parse host:guest spec, pass guest path (not host path) on
  the kernel cmdline as slim.mount=<tag>:<base64(guest_path)>
- inject.rs: Accept working_dir param, write /slim/workdir into rootfs
- build.rs: Pass config.working_dir to inject()
- slim-init.sh: Read /slim/workdir, resolve relative guest paths
  against it before mounting
- test.sh: Test host:guest absolute paths (multi-mount) and relative
  guest path resolved against WORKINGDIR

Addresses PR #9 review comment from @hulthe.
This commit is contained in:
2026-09-10 12:12:07 +02:00
parent 8454c8e407
commit f2b68004ae
5 changed files with 107 additions and 27 deletions
+20 -13
View File
@@ -8,7 +8,7 @@
use anyhow::{Context, Result, bail};
use base64::Engine;
use clap::Args;
use std::{fmt::Write as _, path::PathBuf, process::Command};
use std::{fmt::Write as _, process::Command};
use crate::{
forward::PortForward,
@@ -33,10 +33,12 @@ pub struct RunCmd {
#[clap(long)]
cmd: Option<String>,
/// Share a host directory into the VM via 9p. The directory appears at
/// the same path inside the VM. Can be repeated for multiple mounts.
/// Share a host directory into the VM via 9p. Format:
/// `<host-path>` or `<host-path>:<guest-path>`. When the guest path is
/// omitted, the host path is used. Non-absolute guest paths are
/// relative to the image's WORKINGDIR. Can be repeated.
#[clap(long)]
mount: Vec<PathBuf>,
mount: Vec<String>,
}
pub(crate) fn run(
@@ -88,25 +90,30 @@ pub(crate) fn run(
}
// Build 9p shares for each --mount. Tags are short (slim0, slim1, …)
// because 9p mount_tag has a ~31-byte limit. The full destination path
// is passed on the kernel cmdline as slim.mount=<tag>:<base64(path)>.
// because 9p mount_tag has a ~31-byte limit. The guest destination path
// is passed on the kernel cmdline as slim.mount=<tag>:<base64(guest_path)>.
// If no guest path is specified, the host path is used as the guest path.
let mut virtfs_args: Vec<String> = Vec::new();
for (i, host_path) in mount.iter().enumerate() {
let canonical = host_path
for (i, spec) in mount.iter().enumerate() {
let (host_path, guest_path) = match spec.split_once(':') {
Some((h, g)) => (h, g),
None => (spec.as_str(), spec.as_str()),
};
let canonical = std::path::Path::new(host_path)
.canonicalize()
.with_context(|| format!("Cannot resolve mount path '{}'", host_path.display()))?;
let path_str = canonical
.with_context(|| format!("Cannot resolve mount path '{host_path}'"))?;
let host_str = canonical
.to_str()
.context("Mount path is not valid UTF-8")?;
let tag = format!("slim{i}");
let dest_b64 = base64::engine::general_purpose::STANDARD.encode(path_str.as_bytes());
let dest_b64 = base64::engine::general_purpose::STANDARD.encode(guest_path.as_bytes());
// QEMU's QemuOpts splits on commas — escape literal commas in the
// path as ",," per QEMU convention.
let path_escaped = path_str.replace(',', ",,");
let host_escaped = host_str.replace(',', ",,");
virtfs_args.push("-virtfs".into());
virtfs_args.push(format!(
"local,path={path_escaped},mount_tag={tag},security_model=mapped-xattr"
"local,path={host_escaped},mount_tag={tag},security_model=mapped-xattr"
));
cmdline.push(format!("slim.mount={tag}:{dest_b64}"));
}