Skip to content

Commit

Permalink
Share /dev/shm as a separate mount with DAX
Browse files Browse the repository at this point in the history
Create a new virtiofs filesystem and mount it separately with DAX
unconditionally enabled.

Signed-off-by: Asahi Lina <[email protected]>
  • Loading branch information
asahilina committed Oct 28, 2024
1 parent fddc28d commit 541fd95
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
22 changes: 18 additions & 4 deletions crates/muvm/src/bin/muvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use std::path::Path;

use anyhow::{anyhow, Context, Result};
use krun_sys::{
krun_add_disk, krun_add_vsock_port, krun_create_ctx, krun_set_env, krun_set_gpu_options2,
krun_set_log_level, krun_set_passt_fd, krun_set_root, krun_set_vm_config, krun_set_workdir,
krun_start_enter, VIRGLRENDERER_DRM, VIRGLRENDERER_THREAD_SYNC,
VIRGLRENDERER_USE_ASYNC_FENCE_CB, VIRGLRENDERER_USE_EGL,
krun_add_disk, krun_add_virtiofs2, krun_add_vsock_port, krun_create_ctx, krun_set_env,
krun_set_gpu_options2, krun_set_log_level, krun_set_passt_fd, krun_set_root,
krun_set_vm_config, krun_set_workdir, krun_start_enter, VIRGLRENDERER_DRM,
VIRGLRENDERER_THREAD_SYNC, VIRGLRENDERER_USE_ASYNC_FENCE_CB, VIRGLRENDERER_USE_EGL,
};
use log::debug;
use muvm::cli_options::options;
Expand Down Expand Up @@ -231,6 +231,20 @@ fn main() -> Result<()> {
let err = Errno::from_raw_os_error(-err);
return Err(err).context("Failed to configure root path");
}

// SAFETY: `c_path` and `c_path` are pointers to C-string literals.
let err = unsafe {
krun_add_virtiofs2(
ctx_id,
c"devshm".as_ptr(),
c"/dev/shm/".as_ptr(),
1u64 << 29, // 512MiB should be enough for /dev/shm
)
};
if err < 0 {
let err = Errno::from_raw_os_error(-err);
return Err(err).context("Failed to configure /dev/shm filesystem");
}
}

{
Expand Down
15 changes: 14 additions & 1 deletion crates/muvm/src/guest/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::path::Path;
use anyhow::{Context, Result};
use rustix::fs::{mkdir, symlink, Mode, CWD};
use rustix::mount::{
mount2, mount_bind, move_mount, open_tree, MountFlags, MoveMountFlags, OpenTreeFlags,
mount2, mount_bind, move_mount, open_tree, unmount, MountFlags, MoveMountFlags, OpenTreeFlags,
UnmountFlags,
};

fn make_tmpfs(dir: &str) -> Result<()> {
Expand Down Expand Up @@ -160,5 +161,17 @@ pub fn mount_filesystems() -> Result<()> {
make_tmpfs("/tmp/.X11-unix")?;
}

// Mount /dev/shm separately with DAX enabled, to allow cross-domain shared memory
// /dev/shm is mounted by libkrunfw, so unmount it first
unmount("/dev/shm", UnmountFlags::empty()).context("Failed to unmount /dev/shm")?;
mount2(
Some("devshm"),
"/dev/shm",
Some("virtiofs"),
MountFlags::NOEXEC | MountFlags::NOSUID,
Some(c"dax"),
)
.context("Failed to mount `/dev/shm`")?;

Ok(())
}

0 comments on commit 541fd95

Please sign in to comment.