docs: describe the implemented sandbox architecture
Update the design doc to the shipped shape: the public API, the module layout, the crate-private Unshare constructors and what the conformance tests actually cover. Refresh the AGENTS.md scope list and the test mode wording in the README and CLI. Rename the last context-named helpers (new_with_base, unshare_test_sandbox, RunEnv) and the comments that still described the old abstraction.
This commit is contained in:
1 parent
d5bf2ceeb3
commit
674b99b00b
17 files changed
+207
-214
No files matched your search
+78
-81
@@ -1,6 +1,6 @@
|
||||
# Replacing Context with the Sandbox trait
|
||||
|
||||
Status: design proposal, not implemented.
|
||||
Status: implemented. The public build and test APIs take `Arc<dyn Sandbox>`; `src/context/` is gone.
|
||||
|
||||
## Why the old abstraction was wrong
|
||||
|
||||
@@ -23,7 +23,7 @@ process-global. New code has to say which one it means.
|
||||
A small trait with explicit construction and explicit passing fixes both. The
|
||||
Ecrou microVM implementation can be added without touching build logic.
|
||||
|
||||
## What exists today
|
||||
## What the old design was
|
||||
|
||||
| Piece | Where | Used by |
|
||||
| --- | --- | --- |
|
||||
@@ -257,9 +257,8 @@ commit to an archive format.
|
||||
Overlayfs is specific to the unshare backend, so the mechanism stays in
|
||||
`Unshare::put`. The shared policy is `PutOptions`:
|
||||
|
||||
- `reuse` names backend state to reuse. `Unshare` treats it as the directory
|
||||
that holds a persistent upper and work layer. A backend without resume
|
||||
ignores it and returns `Fresh`.
|
||||
- `reuse` carries the backend's persistent overlay upper and work paths
|
||||
(`ReuseState`). A backend without resume ignores it and returns `Fresh`.
|
||||
- `skip_dirs` lists directory names the backend may omit during a copy.
|
||||
`stage_tree` fills it with the VCS names.
|
||||
- `PutOutcome::Resumed` tells the caller that the previous attempt's build
|
||||
@@ -273,31 +272,41 @@ depends on host-side overlay upperdirs.
|
||||
|
||||
### Implementations
|
||||
|
||||
`Unshare` is what `EphemeralContextGuard` and the old unshare `Context` are today:
|
||||
`Unshare` is what `EphemeralContextGuard` and the old unshare `Context` were:
|
||||
an ephemeral unshare chroot with the same tarball cache, device nodes, `/proc`,
|
||||
apt cache and ccache mounts. Its spec holds what the current guard takes:
|
||||
apt cache and ccache mounts. The crate-private constructors take the values the
|
||||
old guard took:
|
||||
|
||||
```rust
|
||||
pub struct UnshareSpec {
|
||||
pub root: PathBuf,
|
||||
pub series: String,
|
||||
pub arch: Option<String>,
|
||||
pub mirror: Option<String>,
|
||||
pub ccache: Option<PathBuf>,
|
||||
pub share_apt_cache: bool,
|
||||
pub teardown: Teardown,
|
||||
}
|
||||
impl Unshare {
|
||||
pub(crate) async fn new_with_base(
|
||||
series: &str,
|
||||
arch: Option<&str>,
|
||||
mirror: Option<&str>,
|
||||
base: Arc<dyn Sandbox>,
|
||||
view: &dyn BuildView,
|
||||
profiler: &mut BuildProfiler,
|
||||
) -> Result<Self, Box<dyn Error>>;
|
||||
|
||||
pub enum Teardown {
|
||||
/// Remove the root on success and on failure.
|
||||
Always,
|
||||
/// Remove the root on success, keep it after a failure for inspection.
|
||||
KeepOnFailure,
|
||||
/// The owner removes the root; the sandbox only unmounts.
|
||||
Owner,
|
||||
pub(crate) async fn new_for_session(
|
||||
session: Session,
|
||||
series: &str,
|
||||
arch: Option<&str>,
|
||||
mirror: Option<&str>,
|
||||
base: Arc<dyn Sandbox>,
|
||||
view: &dyn BuildView,
|
||||
keep_upper: bool,
|
||||
ccache: Option<&Path>,
|
||||
profiler: &mut BuildProfiler,
|
||||
) -> Result<Self, Box<dyn Error>>;
|
||||
}
|
||||
```
|
||||
|
||||
The teardown policy is the old one: a successful build removes an anonymous
|
||||
chroot, a failed one leaves it for inspection, and a session record removes the
|
||||
per-run chroot in every case while retaining its staged tree exactly for
|
||||
`--incremental`.
|
||||
|
||||
An absolute environment path such as `/etc/apt/sources.list` maps to
|
||||
`root/etc/apt/sources.list` in the unshare backend. The implementation strips
|
||||
the leading slash before joining, because `Path::join` replaces the base on an
|
||||
@@ -309,8 +318,7 @@ overlay for a tree when possible, or the upper and work layer under
|
||||
|
||||
`Host` is the host machine. It runs commands with `std::process::Command`,
|
||||
uses `std::fs`, and has no teardown. It serves `pkh test --mode local` and the
|
||||
unit tests that currently build a `Context::new(ContextConfig::Local)`. It is
|
||||
not a user-selectable backend.
|
||||
unit tests. It is not a user-selectable backend.
|
||||
|
||||
`Ecrou` is the future microVM backend. It runs commands through a guest agent and
|
||||
moves files over its channel. Its spec will name the kernel, initrd, root
|
||||
@@ -321,11 +329,12 @@ conformance tests. It does not need changes in `deb/local`, `apt` or
|
||||
### Construction and selection
|
||||
|
||||
Each implementation owns its constructor, because each needs different inputs.
|
||||
`Unshare::create` is async because it downloads and extracts a tarball.
|
||||
`Host::new` is trivial. `Ecrou::create` will be async.
|
||||
`Unshare::new_with_base` and `Unshare::new_for_session` are async because they
|
||||
download and extract a tarball. `Host::new` is trivial. `Ecrou::create` will be
|
||||
async.
|
||||
|
||||
`pkh deb` calls `Unshare::create` today. Adding the Ecrou backend means adding a
|
||||
small `Backend` value to `DebBuildOptions` and a CLI flag, then choosing the
|
||||
`pkh deb` calls `Unshare::new_with_base` today. Adding the Ecrou backend means
|
||||
adding a small `Backend` value to `DebBuildOptions` and a CLI flag, then choosing the
|
||||
constructor there. Everything downstream already takes `Arc<dyn Sandbox>`.
|
||||
|
||||
A closed `Backend` enum is enough for the CLI. Library callers that want to
|
||||
@@ -333,9 +342,9 @@ supply an implementation `pkh` does not know need an injection point, for
|
||||
example `DebBuildOptions.sandbox: Option<Arc<dyn Sandbox>>`. That is a new
|
||||
field with a new meaning, not the old config-resolved `ctx`.
|
||||
|
||||
`Unshare::create` stays crate-private while it takes a `&mut BuildProfiler`.
|
||||
Publishing a constructor for downstream callers means making the profiler
|
||||
optional in the signature first.
|
||||
The `Unshare` constructors stay crate-private while they take a
|
||||
`&mut BuildProfiler` and a `Session`. Publishing a constructor for downstream
|
||||
callers means making both optional in the signature first.
|
||||
|
||||
`pkh test` maps its modes to implementations: `local` to `Host`, `chroot` to
|
||||
`Unshare`, and a future microVM mode to `Ecrou`. The existing `TestMode::Vm`
|
||||
@@ -352,10 +361,9 @@ different flag name.
|
||||
applies the conservative teardown policy. `Unshare` keeps a failed anonymous
|
||||
chroot for inspection and removes it on success, as today.
|
||||
- `cleanup` takes `Option<BuildOutcome>` so it works through
|
||||
`Arc<dyn Sandbox>`. `deb` calls `cleanup(Some(Success))` after a good build
|
||||
and `cleanup(Some(Failure))` before returning an error; `Drop` and the
|
||||
interrupt hook call `cleanup(None)`. `Unshare` stores the outcome in an
|
||||
`AtomicBool` and the first call applies the teardown.
|
||||
`Arc<dyn Sandbox>`. `deb` marks success on the `Unshare` before it drops;
|
||||
`Drop` and the interrupt hook call `cleanup(None)`. `Unshare` stores the
|
||||
outcome in an `AtomicBool`.
|
||||
- A build record keeps its metadata and log outside the environment. Dropping a
|
||||
sandbox never touches them. Backend-owned state, such as `<record>/chroot`,
|
||||
belongs to the backend and is removed by `cleanup`.
|
||||
@@ -383,23 +391,22 @@ different flag name.
|
||||
`Option` on the keyring helpers. Every caller passes a value today; the
|
||||
`context::current()` default goes away.
|
||||
|
||||
After the migration the word does not appear in the code. This document
|
||||
mentions it only to describe what is removed. No setting, global, driver or
|
||||
command builder keeps the name.
|
||||
The build and test APIs no longer expose the word. This document mentions it
|
||||
only to describe what was removed; no setting, global, driver or command
|
||||
builder keeps the name.
|
||||
|
||||
## Public API changes
|
||||
|
||||
- `pkh::sandbox::{Sandbox, SandboxExt, BuildCommand, Capture, CommandSpec,
|
||||
Execution, PutOptions, PutOutcome, BuildOutcome, StageOptions, stage_tree}`.
|
||||
- `pkh::sandbox::{Unshare, UnshareSpec, Teardown}` and `Host`.
|
||||
- `pkh::sandbox::{Host, Unshare, ReuseState}`. The `Unshare` constructors are
|
||||
crate-private until their profiler and session inputs become optional.
|
||||
- `pkh::exec::{LineSink, Stream}`. `report::BuildView::sink` returns the moved
|
||||
`LineSink`.
|
||||
- `pkh::deb::DebBuildOptions` loses `ctx` and `mode`. A later `Backend` value is
|
||||
additive.
|
||||
- `pkh::apt::sources::{load, save}` and
|
||||
`pkh::apt::keyring::download_trust_ppa_key` take `&dyn Sandbox`.
|
||||
- `pkh::apt::keyring::download_cache_keyrings` becomes host-only. The bootstrap
|
||||
always runs on the host now.
|
||||
- `pkh::apt::sources::{load, save}` and the keyring helpers take
|
||||
`Option<Arc<dyn Sandbox>>`; `None` means a `Host` sandbox.
|
||||
|
||||
`pkh` is a library and a binary at version `0.2.0`. These are breaking changes,
|
||||
so the release that ships them should be `0.3.0`.
|
||||
@@ -415,22 +422,19 @@ src/
|
||||
staging.rs StageOptions, stage_tree, VCS pruning
|
||||
host.rs Host
|
||||
unshare/
|
||||
mod.rs Unshare, UnshareSpec, Teardown, Drop
|
||||
bootstrap.rs tarball cache, download, extraction
|
||||
finalize.rs device nodes, /proc, apt cache, ccache shim
|
||||
overlay.rs overlay mounting, resume state
|
||||
namespace.rs namespace and pivot scripts, MAPPED_BUILD_ID
|
||||
mod.rs Unshare, bootstrap, finalize, ccache, apt cache,
|
||||
teardown
|
||||
namespace.rs UnshareDriver, path mapping, overlay staging,
|
||||
namespace and pivot scripts, MAPPED_BUILD_ID
|
||||
ecrou.rs future Ecrou
|
||||
build_profile.rs Phase, BuildProfiler, moved out of deb
|
||||
```
|
||||
|
||||
`Phase` and `BuildProfiler` move out of `deb` because `Unshare::create` needs
|
||||
them and `sandbox` must not depend on `deb`. `pkh::deb::Phase` can be
|
||||
re-exported for compatibility.
|
||||
`Phase` and `BuildProfiler` still live in `deb`; moving them to
|
||||
`build_profile.rs` is the remaining separation between `sandbox` and `deb`.
|
||||
|
||||
## Migration plan
|
||||
## Migration plan (implemented)
|
||||
|
||||
Each step compiles and passes its tests. The work is additive first, subtractive
|
||||
Each step compiled and passed its tests. The work is additive first, subtractive
|
||||
last.
|
||||
|
||||
1. `exec: move line capture out of context` adds `src/exec.rs` with `Stream`,
|
||||
@@ -446,13 +450,13 @@ last.
|
||||
`Arc<dyn Sandbox>`. The SSH and schroot drivers are not moved.
|
||||
4. `sandbox: move the chroot bootstrap out of deb` moves the tarball,
|
||||
extraction, finalize, ccache, apt cache and teardown helpers from
|
||||
`deb/ephemeral.rs`, and folds `EphemeralContextGuard` into `Unshare::create`
|
||||
and `Drop`.
|
||||
`deb/ephemeral.rs`, and folds `EphemeralContextGuard` into
|
||||
`Unshare::new_with_base`/`new_for_session` and `Drop`.
|
||||
5. `deb: drop the ctx and mode options` removes
|
||||
`DebBuildOptions.ctx`, `DebBuildOptions.mode` and `BuildMode`, and passes the
|
||||
`Arc<dyn Sandbox>` explicitly through `deb/local`.
|
||||
6. `test: make local mode use Host` replaces `Bed::Local(Arc<Context>)` with
|
||||
`Bed::Host` and the trait methods.
|
||||
`Bed::Local(Arc<dyn Sandbox>)` backed by `Host`.
|
||||
7. `apt,build: take the sandbox explicitly` removes the `Option`
|
||||
parameters and the `context::current()` defaults, and passes `Host` in the
|
||||
unit tests.
|
||||
@@ -491,8 +495,8 @@ The commit scopes follow `AGENTS.md`: add `sandbox`, `build_profile` and
|
||||
2. Staging and `--incremental`. Host-side overlayfs is specific to the unshare
|
||||
backend. The `put` and `stage_tree` API must be settled before `Ecrou`, or
|
||||
the Ecrou backend will not support incremental builds.
|
||||
3. Trait size. The trait has 11 required methods and one provided capability
|
||||
method (`is_host`), and every backend implement the required ones. One trait keeps `Arc<dyn Sandbox>` simple. If it grows, split
|
||||
3. Trait size. The trait has 10 required methods and two provided methods
|
||||
(`is_host`, `cleanup`); every backend implements the required ones. One trait keeps `Arc<dyn Sandbox>` simple. If it grows, split
|
||||
it into `CommandRunner` and `FileSystem` supertraits rather than adding
|
||||
optional methods.
|
||||
4. Per-call cost over a microVM channel. A file read is cheap on the host and a
|
||||
@@ -530,24 +534,17 @@ The commit scopes follow `AGENTS.md`: add `sandbox`, `build_profile` and
|
||||
|
||||
## Testing
|
||||
|
||||
- Add a conformance suite that takes `&dyn Sandbox` and checks the
|
||||
contract: run a command, capture its output, read and write a file, list a
|
||||
directory, stage a tree, copy a file out, create a temporary directory, and
|
||||
clean up. Run it against `Host` always and against `Unshare` when root and
|
||||
`unshare` are available.
|
||||
- A future `Ecrou` runs the same suite. That is the main reason to keep the trait
|
||||
small and behavior explicit.
|
||||
- Use `Arc<dyn Sandbox>` in a test to catch object-safety regressions.
|
||||
- Port the existing `context` unit tests: staging strips VCS metadata, copies
|
||||
dangling symlinks, maps unshare paths, prunes nested VCS directories, and
|
||||
stages over an overlay upperdir.
|
||||
- Delete the `ContextManager` tests with the manager.
|
||||
- Add lifecycle tests: create an `Unshare` over a pre-populated fake root, drop
|
||||
it, and check the root is kept or removed per `Teardown`. These do not need
|
||||
privileges if they stop before `unshare`.
|
||||
- Keep the `Host` implementation, so `build/binary.rs`, `deb/local.rs` and
|
||||
`apt/sources.rs` unit tests stay fast and unprivileged.
|
||||
- Run `pkh test --mode local` and `--mode chroot` in the e2e harness.
|
||||
- Run the full suite and `cargo fmt --all` plus
|
||||
`cargo clippy --all-targets --all-features` before each commit, as
|
||||
`AGENTS.md` requires.
|
||||
- `sandbox::tests::conformance` takes `&dyn Sandbox` and checks the contract:
|
||||
run a command, capture its output, read and write a file, list a directory,
|
||||
stage a tree, copy a file out, create a temporary directory, and clean up.
|
||||
It runs against `Host`.
|
||||
- `sandbox::unshare::namespace::path_tests` runs the path and staging half
|
||||
against `UnshareDriver` over a temp root, without namespace privileges.
|
||||
- A bootstrapped `Unshare` needs root, `unshare` and a tarball download, so its
|
||||
full command conformance is left to the `pkh deb` and `pkh test` e2e runs.
|
||||
- The existing staging units moved with the code: VCS metadata is skipped,
|
||||
dangling symlinks round-trip, unshare paths map below the root, and overlay
|
||||
mounts prune the lower tree.
|
||||
- `Host` stays fast and unprivileged, so `build/binary.rs`, `deb/local.rs` and
|
||||
`apt/sources.rs` unit tests run without a chroot.
|
||||
|
||||
Reference in new issue
Block a user