From 1252c39fa858b923871bdeaa9c08ef21a061654c Mon Sep 17 00:00:00 2001 From: Sergio Lopez Date: Mon, 9 Feb 2026 12:02:44 +0100 Subject: [PATCH] start/macos: extend mount script to chdir workdir If workdir is pointing to one of the mounted volumes (something that's quite usual), it would appear empty because we switched to it in libkrun's init/init.c, before /.krunvm-mount.sh is executed. Extend the script to also switch to workdir after mounting the volumes. Signed-off-by: Sergio Lopez --- src/commands/start.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/commands/start.rs b/src/commands/start.rs index 6c1db67..5c56c97 100644 --- a/src/commands/start.rs +++ b/src/commands/start.rs @@ -166,7 +166,7 @@ unsafe fn exec_vm( #[cfg(target_os = "macos")] let virtiofs_mounts = map_volumes(ctx, vmcfg, rootfs); #[cfg(target_os = "macos")] - let mount_wrapper = build_mount_wrapper(rootfs, cmd, &args, &virtiofs_mounts); + let mount_wrapper = build_mount_wrapper(rootfs, cmd, &vmcfg.workdir, &args, &virtiofs_mounts); let mut ports = Vec::new(); for (host_port, guest_port) in vmcfg.mapped_ports.iter() { @@ -273,6 +273,7 @@ unsafe fn exec_vm( fn build_mount_wrapper( rootfs: &str, cmd: Option<&str>, + workdir: &str, args: &[CString], mounts: &[(String, String)], ) -> Option<(CString, Vec)> { @@ -280,7 +281,7 @@ fn build_mount_wrapper( return None; } - let helper_path = write_mount_script(rootfs, mounts); + let helper_path = write_mount_script(rootfs, workdir, mounts); let mut exec_args: Vec = Vec::new(); let command = cmd.unwrap_or("/bin/sh"); @@ -292,7 +293,7 @@ fn build_mount_wrapper( } #[cfg(target_os = "macos")] -fn write_mount_script(rootfs: &str, mounts: &[(String, String)]) -> String { +fn write_mount_script(rootfs: &str, workdir: &str, mounts: &[(String, String)]) -> String { let host_path = format!("{}/.krunvm-mount.sh", rootfs); let guest_path = "/.krunvm-mount.sh".to_string(); @@ -306,6 +307,9 @@ fn write_mount_script(rootfs: &str, mounts: &[(String, String)]) -> String { for (tag, guest_path) in mounts { writeln!(file, "mount -t virtiofs {} {}", tag, guest_path).unwrap(); } + if !workdir.is_empty() { + writeln!(file, "cd {}", workdir).unwrap(); + } writeln!(file, "exec \"$@\"").unwrap(); let perms = fs::Permissions::from_mode(0o755);