Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions nvx-rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1548,9 +1548,29 @@ the caller's mappings.
`Root::map`: a caller's host mappings, through the same mapper. A share, a
child, the export, or a mapping that fails latches the root.

`Root::teardown`: the shares and mappings in reverse order, then the mounts of
an aggregate and of the export, the later first, the overlay, the layers from
the top down, and the upper, with every failed sync or unmount reported. An
`Root::mount_runtime`: the runtime's file systems, which an init mounts once
for its VM, before the root's first live share or mapping, after it makes every
mount of the root's tree `nosuid,nodev`: procfs at `/proc`, sysfs at `/sys`,
read-only, a tmpfs at `/dev` with an execution's device nodes and links, devpts
at `/dev/pts`, and tmpfs at `/dev/shm` and `/run`, with an execution's options,
then the time status's directory, when the caller names one, bound read-only at
`/run/nvx/time`. It refuses a mount point that leads through a symbolic link or
where something is mounted already, and, before it changes anything, a call
while the root holds the runtime's mounts, whose `/dev` and `/dev/pts` a second
restriction would make `nodev`, or a live share or mapping, since the `/run`
tmpfs would hide one beneath `/run` from the teardown. A call that fails
unmounts what it mounted. A later share or mapping beneath `/run` is on that
tmpfs, which the root's mapper owns, as a guest's mapper owns its `/tmp`.
Each attachment is checked at its named target before it is recorded. A runtime
mount whose attachment fails and which cannot be detached keeps its mount
descriptor, so rollback and teardown can retry cleanup without trusting its
original path.

`Root::teardown`: the shares and mappings in reverse order, a runtime mount whose
attachment failed and which could not be detached, then the mounts of an
aggregate, of the export, and of the runtime, the later first, the overlay,
the layers from the top down, and the upper, with every failed sync or unmount
reported. An
ext4 scratch's file system is synced before its unmount, since a mount namespace
that copied the root's mounts keeps them, unsynced, after this one unmounts
them, and a scratch whose sync fails stays mounted.
Expand Down Expand Up @@ -2826,6 +2846,17 @@ NVX_BUNDLE=skip cargo test --locked --all-features --lib -- \
--ignored --exact guest::root::tests::a_thread_with_private_tables_finds_its_mount_points_and_options
```

Ignored tests of `Root::mount_runtime` mount the runtime's file systems on a
temporary tmpfs root, in a thread's private mount namespace, and check their
options, device nodes, and time status, a call's undo, the teardown, and the
order of the runtime and the mappings beneath `/run`. They need root and Linux
6.8 or later; run them as root with:

```bash
NVX_BUNDLE=skip cargo test --locked --all-features --lib -- \
--ignored guest::root::tests::privileged
```

`tests/openvmm_vectors.rs` is an ignored test that compares the copies of
OpenVMM's vectors in `tests/vectors/openvmm/` with the files of the OpenVMM
submodule, which the `nvx-rs` job cannot check out, and the versions of
Expand Down
12 changes: 12 additions & 0 deletions nvx-rs/public-api/linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,8 @@ field nvx::guest::root::Error::Recovery::device (hidden)
field nvx::guest::root::Error::Recovery::number (hidden)
variant nvx::guest::root::Error::Reserved (hidden)
field nvx::guest::root::Error::Reserved::path (hidden)
variant nvx::guest::root::Error::Restrict (hidden)
field nvx::guest::root::Error::Restrict::error (hidden)
variant nvx::guest::root::Error::Runtime (hidden)
field nvx::guest::root::Error::Runtime::path (hidden)
variant nvx::guest::root::Error::Share (hidden)
Expand Down Expand Up @@ -1440,6 +1442,8 @@ field nvx::guest::root::Mount::Layer::number (hidden)
variant nvx::guest::root::Mount::Mapping (hidden)
field nvx::guest::root::Mount::Mapping::target (hidden)
variant nvx::guest::root::Mount::Overlay (hidden)
variant nvx::guest::root::Mount::Runtime (hidden)
field nvx::guest::root::Mount::Runtime::0 (hidden)
variant nvx::guest::root::Mount::Scratch (hidden)
variant nvx::guest::root::Mount::Share (hidden)
field nvx::guest::root::Mount::Share::tag (hidden)
Expand Down Expand Up @@ -1469,6 +1473,14 @@ method nvx::guest::root::Root::path (hidden)
method nvx::guest::root::Root::runtime (hidden)
method nvx::guest::root::Root::teardown (hidden)
method nvx::guest::root::Root::upper (hidden)
enum nvx::guest::root::RuntimeMounts (hidden)
variant nvx::guest::root::RuntimeMounts::Dev (hidden)
variant nvx::guest::root::RuntimeMounts::DevPts (hidden)
variant nvx::guest::root::RuntimeMounts::DevShm (hidden)
variant nvx::guest::root::RuntimeMounts::Proc (hidden)
variant nvx::guest::root::RuntimeMounts::Run (hidden)
variant nvx::guest::root::RuntimeMounts::Sys (hidden)
variant nvx::guest::root::RuntimeMounts::TimeStatus (hidden)
struct nvx::guest::root::ShareChild (hidden)
field nvx::guest::root::ShareChild::access (hidden)
field nvx::guest::root::ShareChild::kind (hidden)
Expand Down
32 changes: 28 additions & 4 deletions nvx-rs/src/guest/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ use std::io::{self, Read};
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};

use super::mappings::mapper::{AttachFailure, bind_retained_with};
use super::mappings::{
self, Access, Kernel, Kind, Mapped, Mapping, MappingError, SourceRoot, Step, SystemKernel,
TargetRoot, TeardownError,
Expand Down Expand Up @@ -155,8 +156,9 @@ const UNCHANGED_ID: u32 = u32::MAX;
const MACHINE_ID_MODE: u32 = 0o444;
/// Where [`Entry::bind_runtime`] binds a generated machine ID in the root.
const MACHINE_ID_TARGET: &str = "/etc/machine-id";
/// Where [`Entry::bind_runtime`] binds the time status in the root.
const TIME_STATUS_TARGET: &str = "/run/nvx/time";
/// Where [`Entry::bind_runtime`] binds the time status in the root, as a layered root's
/// [`mount_runtime`](super::root::Root::mount_runtime) does.
pub(crate) const TIME_STATUS_TARGET: &str = "/run/nvx/time";
/// Where [`Entry::bind_runtime`] binds the resolver file in the root.
const RESOLVER_TARGET: &str = "/etc/resolv.conf";

Expand Down Expand Up @@ -876,13 +878,35 @@ fn bind_file(
/// Binds the file or directory at `path`, which must be of `kind`, read-only at `target` beneath
/// `root`, with `kernel`, through the walk and attach of [`mappings::bind`] from a source root at
/// `path`'s parent directory, and returns the bind.
fn bind_runtime_file(
pub(crate) fn bind_runtime_file(
kernel: &mut dyn Kernel,
root: &TargetRoot,
path: &Path,
target: &str,
kind: Kind,
) -> Result<Mapped, MappingError> {
let (source, mapping) = runtime_mapping(path, target, kind)?;
mappings::bind_with(kernel, root, &source, &mapping)
}

/// Binds as [`bind_runtime_file`] does, retaining a failed bind that its caller must detach.
pub(crate) fn bind_runtime_file_retained(
kernel: &mut dyn Kernel,
root: &TargetRoot,
path: &Path,
target: &str,
kind: Kind,
) -> Result<Mapped, AttachFailure> {
let (source, mapping) = runtime_mapping(path, target, kind)?;
bind_retained_with(kernel, root, &source, &mapping)
}

/// Prepares the source root and read-only mapping for a runtime bind.
fn runtime_mapping(
path: &Path,
target: &str,
kind: Kind,
) -> Result<(SourceRoot, Mapping), MappingError> {
let source_error = |error: i32| MappingError::System {
step: Step::Source,
error,
Expand All @@ -895,7 +919,7 @@ fn bind_runtime_file(
let mapping = Mapping::new(name, target, Access::ReadOnly)
.map_err(|_| source_error(Errno::EINVAL as i32))?
.with_kind(kind);
mappings::bind_with(kernel, root, &source, &mapping)
Ok((source, mapping))
}

/// Returns the directory that holds the runtime file at `path`, the current directory for a
Expand Down
17 changes: 13 additions & 4 deletions nvx-rs/src/guest/mappings/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,16 @@ pub(crate) fn bind_with(
source: &SourceRoot,
mapping: &Mapping,
) -> Result<Mapped, MappingError> {
bind_retained_with(kernel, root, source, mapping).map_err(|failure| failure.error)
}

/// Implements [`bind_with`], retaining a moved mount that could not be detached for its caller.
pub(crate) fn bind_retained_with(
kernel: &mut dyn Kernel,
root: &TargetRoot,
source: &SourceRoot,
mapping: &Mapping,
) -> Result<Mapped, AttachFailure> {
attach(
kernel,
root,
Expand All @@ -813,7 +823,6 @@ pub(crate) fn bind_with(
target: mapping.target().to_path_buf(),
mount,
})
.map_err(|failure| failure.error)
}

/// Unmounts `binds`, which [`bind_with`] made beneath `root`, in reverse order, with `kernel`, as a
Expand Down Expand Up @@ -852,11 +861,11 @@ enum Source<'a> {
/// Why an attach failed, with the descriptor of the mapping's mount if a rename moved it and
/// detaching it failed too, so that it stays mounted wherever it went.
#[derive(Debug)]
struct AttachFailure {
pub(crate) struct AttachFailure {
/// Why it failed.
error: MappingError,
pub(crate) error: MappingError,
/// The descriptor of the mapping's mount, if it stays mounted.
stray: Option<FileDescriptor>,
pub(crate) stray: Option<FileDescriptor>,
}

impl From<MappingError> for AttachFailure {
Expand Down
Loading
Loading