virtiofs: add --mount-mode {unshare,direct} #10
No files matched your search
+16
-6
@@ -4,10 +4,12 @@
|
|||||||
//! at build time) handles VM bootstrap; /boco/exec runs the container's
|
//! at build time) handles VM bootstrap; /boco/exec runs the container's
|
||||||
//! CMD/ENTRYPOINT. Runtime overrides are delivered as `boco.cmd=<base64>`
|
//! CMD/ENTRYPOINT. Runtime overrides are delivered as `boco.cmd=<base64>`
|
||||||
//! on the kernel cmdline. Host directories are shared into the guest via
|
//! on the kernel cmdline. Host directories are shared into the guest via
|
||||||
//! virtio-fs: one virtiofsd daemon (under `podman unshare` for rootless
|
//! virtio-fs: one virtiofsd daemon per `--mount`, plus a `-chardev socket`
|
||||||
//! UID mapping) per `--mount`, plus a `-chardev socket` + `-device
|
//! + `-device vhost-user-fs-pci` pair on the QEMU side. virtiofsd runs
|
||||||
//! vhost-user-fs-pci` pair on the QEMU side. The guest mounts each share
|
//! under `podman unshare` (rootless UID mapping) by default, or directly
|
||||||
//! with `mount -t virtiofs <tag> <dest>` (see `boco.mount=` cmdline).
|
//! as the invoking user with `--mount-mode direct` (1:1 UID mapping).
|
||||||
|
//! The guest mounts each share with `mount -t virtiofs <tag> <dest>`
|
||||||
|
//! (see `boco.mount=` cmdline).
|
||||||
|
|
||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result, bail};
|
||||||
use base64::Engine;
|
use base64::Engine;
|
||||||
@@ -17,7 +19,7 @@ use std::{fmt::Write as _, process::Command};
|
|||||||
use crate::{
|
use crate::{
|
||||||
forward::PortForward,
|
forward::PortForward,
|
||||||
registry::{registry_base_dir, registry_dir},
|
registry::{registry_base_dir, registry_dir},
|
||||||
virtiofs::{self, VirtiofsShare},
|
virtiofs::{self, MountMode, VirtiofsShare},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
@@ -44,6 +46,13 @@ pub struct BootCmd {
|
|||||||
/// relative to the image's WORKINGDIR. Can be repeated.
|
/// relative to the image's WORKINGDIR. Can be repeated.
|
||||||
#[clap(long)]
|
#[clap(long)]
|
||||||
mount: Vec<String>,
|
mount: Vec<String>,
|
||||||
|
|
||||||
|
/// How virtiofsd is launched: `unshare` runs it inside
|
||||||
|
/// `podman unshare` (rootless UID mapping, guest UIDs map into the
|
||||||
|
/// subuid range); `direct` runs it as the invoking user (guest UIDs
|
||||||
|
/// map 1:1 to host UIDs — needed for nested virtiofs shares).
|
||||||
|
#[clap(long, value_enum, default_value = "unshare")]
|
||||||
|
mount_mode: MountMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn boot(
|
pub(crate) fn boot(
|
||||||
@@ -53,6 +62,7 @@ pub(crate) fn boot(
|
|||||||
forward,
|
forward,
|
||||||
cmd,
|
cmd,
|
||||||
mount,
|
mount,
|
||||||
|
mount_mode,
|
||||||
}: BootCmd,
|
}: BootCmd,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let reg_dir = registry_dir(&name)?;
|
let reg_dir = registry_dir(&name)?;
|
||||||
@@ -108,7 +118,7 @@ pub(crate) fn boot(
|
|||||||
Some((h, g)) => (h, g.to_string()),
|
Some((h, g)) => (h, g.to_string()),
|
||||||
None => (spec.as_str(), spec.to_string()),
|
None => (spec.as_str(), spec.to_string()),
|
||||||
};
|
};
|
||||||
let share = virtiofs::spawn_share(i, host_path, guest_path)?;
|
let share = virtiofs::spawn_share(i, host_path, guest_path, mount_mode)?;
|
||||||
let tag = share.tag.clone();
|
let tag = share.tag.clone();
|
||||||
let chardev = format!("char{i}");
|
let chardev = format!("char{i}");
|
||||||
let socket = share
|
let socket = share
|
||||||
|
|||||||
+59
-15
@@ -6,14 +6,22 @@
|
|||||||
//! hands the socket path to QEMU via `-chardev socket,path=...`, and
|
//! hands the socket path to QEMU via `-chardev socket,path=...`, and
|
||||||
//! cleans up when the VM exits.
|
//! cleans up when the VM exits.
|
||||||
//!
|
//!
|
||||||
//! To serve files owned by arbitrary guest UIDs (e.g. when an image
|
//! Launch modes ([`MountMode`]):
|
||||||
//! uses the `USER` directive) without running as root, virtiofsd is
|
//!
|
||||||
//! launched under `podman unshare` in a user namespace with
|
//! * `unshare` (default): virtiofsd runs under `podman unshare` in a
|
||||||
//! `--sandbox chroot`. This reuses rootless podman's existing
|
//! user namespace with `--sandbox chroot`. This serves files owned
|
||||||
|
//! by arbitrary guest UIDs (e.g. when an image uses the `USER`
|
||||||
|
//! directive) without running as root, reusing rootless podman's
|
||||||
//! subuid/subgid mapping: guest UID 0 maps to the invoking user, and
|
//! subuid/subgid mapping: guest UID 0 maps to the invoking user, and
|
||||||
//! other guest UIDs map into the subuid range, so each guest UID
|
//! other guest UIDs map into the subuid range, so each guest UID
|
||||||
//! becomes a distinct host UID.
|
//! becomes a distinct host UID.
|
||||||
//!
|
//!
|
||||||
|
//! * `direct`: virtiofsd runs as the invoking user with `--sandbox
|
||||||
|
//! none`. Guest UIDs map 1:1 to host UIDs. Use this when the
|
||||||
|
//! shared directory lives on a filesystem that rejects subuid-range
|
||||||
|
//! UIDs (e.g. nested virtiofs), or when the guest should act as the
|
||||||
|
//! invoking user rather than the subuid range.
|
||||||
|
//!
|
||||||
//! Lifecycle: boco spawns all virtiofsd children before launching QEMU,
|
//! Lifecycle: boco spawns all virtiofsd children before launching QEMU,
|
||||||
//! waits for each listening socket to appear, then drops the
|
//! waits for each listening socket to appear, then drops the
|
||||||
//! [`VirtiofsShare`] guard after QEMU returns — `Drop` sends SIGTERM,
|
//! [`VirtiofsShare`] guard after QEMU returns — `Drop` sends SIGTERM,
|
||||||
@@ -28,10 +36,26 @@ use std::thread;
|
|||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use anyhow::{Context, Result, anyhow, bail};
|
use anyhow::{Context, Result, anyhow, bail};
|
||||||
|
use clap::ValueEnum;
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
use crate::command::cmd;
|
use crate::command::cmd;
|
||||||
|
|
||||||
|
/// How virtiofsd is launched for a share.
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
|
||||||
|
pub enum MountMode {
|
||||||
|
/// Run virtiofsd inside `podman unshare` (rootless UID mapping:
|
||||||
|
/// guest UID 0 maps to the invoking user, other guest UIDs map
|
||||||
|
/// into the subuid range).
|
||||||
|
Unshare,
|
||||||
|
/// Run virtiofsd directly as the invoking user. Guest UIDs map 1:1
|
||||||
|
/// to host UIDs. Needed when the shared directory itself lives on
|
||||||
|
/// a virtiofs/fuse mount that rejects subuid-range UIDs (e.g.
|
||||||
|
/// nested boco), where writes through the subuid mapping fail with
|
||||||
|
/// EINVAL.
|
||||||
|
Direct,
|
||||||
|
}
|
||||||
|
|
||||||
/// Locations searched for the virtiofsd binary, in order.
|
/// Locations searched for the virtiofsd binary, in order.
|
||||||
const VIRTIOFSD_CANDIDATES: &[&str] = &[
|
const VIRTIOFSD_CANDIDATES: &[&str] = &[
|
||||||
"/usr/libexec/virtiofsd",
|
"/usr/libexec/virtiofsd",
|
||||||
@@ -114,7 +138,12 @@ fn wait_for_socket(socket: &Path) -> Result<()> {
|
|||||||
///
|
///
|
||||||
/// `index` becomes the suffix of the tag (`boco{index}`) and is used to
|
/// `index` becomes the suffix of the tag (`boco{index}`) and is used to
|
||||||
/// derive a unique socket filename.
|
/// derive a unique socket filename.
|
||||||
pub fn spawn_share(index: usize, host_path: &str, guest_path: String) -> Result<VirtiofsShare> {
|
pub fn spawn_share(
|
||||||
|
index: usize,
|
||||||
|
host_path: &str,
|
||||||
|
guest_path: String,
|
||||||
|
mode: MountMode,
|
||||||
|
) -> Result<VirtiofsShare> {
|
||||||
let virtiofsd = find_virtiofsd()?;
|
let virtiofsd = find_virtiofsd()?;
|
||||||
let canonical = resolve_host_dir(host_path)?;
|
let canonical = resolve_host_dir(host_path)?;
|
||||||
let host_str = canonical
|
let host_str = canonical
|
||||||
@@ -128,17 +157,29 @@ pub fn spawn_share(index: usize, host_path: &str, guest_path: String) -> Result<
|
|||||||
let tag = format!("boco{index}"); // short tag to fit on cmdline
|
let tag = format!("boco{index}"); // short tag to fit on cmdline
|
||||||
let socket = dir.path().join(format!("{tag}.sock"));
|
let socket = dir.path().join(format!("{tag}.sock"));
|
||||||
|
|
||||||
// TODO: Make rootless mode optional
|
// In `unshare` mode, run virtiofsd under `podman unshare` so it can
|
||||||
|
// serve files owned by arbitrary guest UIDs without root, mapping
|
||||||
// Run virtiofsd under `podman unshare` so it can serve files owned
|
// them into the host user's subuid range. `--sandbox chroot` is
|
||||||
// by arbitrary guest UIDs without root, mapping them into the host
|
// required because the outer `podman unshare` already entered a user
|
||||||
// user's subuid range. `--sandbox chroot` is required because the
|
// namespace (nested user namespaces are not allowed); chroot still
|
||||||
// outer `podman unshare` already entered a user namespace (nested
|
// prevents symlink escapes from the shared dir.
|
||||||
// user namespaces are not allowed); chroot still prevents symlink
|
//
|
||||||
// escapes from the shared dir.
|
// In `direct` mode, run virtiofsd as the invoking user with 1:1 UID
|
||||||
|
// mapping. This cannot use the chroot sandbox (unprivileged
|
||||||
|
// chroot is not permitted), so it falls back to `--sandbox none`.
|
||||||
|
let mut command = match mode {
|
||||||
|
MountMode::Unshare => {
|
||||||
let mut command = Command::new("podman");
|
let mut command = Command::new("podman");
|
||||||
|
command.args(["unshare", "--", virtiofsd_str, "--socket-path"]);
|
||||||
|
command
|
||||||
|
}
|
||||||
|
MountMode::Direct => {
|
||||||
|
let mut command = Command::new(virtiofsd_str);
|
||||||
|
command.arg("--socket-path");
|
||||||
|
command
|
||||||
|
}
|
||||||
|
};
|
||||||
command
|
command
|
||||||
.args(["unshare", "--", virtiofsd_str, "--socket-path"])
|
|
||||||
.arg(&socket)
|
.arg(&socket)
|
||||||
.args([
|
.args([
|
||||||
"--shared-dir",
|
"--shared-dir",
|
||||||
@@ -146,7 +187,10 @@ pub fn spawn_share(index: usize, host_path: &str, guest_path: String) -> Result<
|
|||||||
"--tag",
|
"--tag",
|
||||||
&tag,
|
&tag,
|
||||||
"--sandbox",
|
"--sandbox",
|
||||||
"chroot",
|
match mode {
|
||||||
|
MountMode::Unshare => "chroot",
|
||||||
|
MountMode::Direct => "none",
|
||||||
|
},
|
||||||
"--cache",
|
"--cache",
|
||||||
"auto",
|
"auto",
|
||||||
])
|
])
|
||||||
|
|||||||
Reference in new issue
Block a user