deb,test: drop explicit context selection

DebBuildOptions no longer carries a Context or a BuildMode. Every build
starts from a local base, resolves a record and bootstraps an Unshare.
The testbed's local mode uses Host instead of the process-global context,
and apt falls back to Host when no sandbox is passed.

Remove the context swap from Unshare: callers now hold the sandbox they
use, so no global current context is needed.
This commit is contained in:
vhaudiquet committed 2026-11-24 18:35:17 +01:00
1 parent 483e532baf
commit 7c689e8c39
12 files changed
+177 -208

No files matched your search

+63 -116
View File
@@ -1,30 +1,20 @@
mod cross;
/// Ephemeral (per-build) unshare contexts, including the process-global
/// cleanup-hook registry drained by the SIGINT handler
pub(crate) mod ephemeral;
pub(crate) mod local;
/// Resumable build sessions: manifest, discovery, `pkh deb list` and the
/// host-tree snapshot/sync that keeps build artifacts alive across attempts
pub mod session;
use crate::context::{self, Context};
use crate::context::{Context, ContextConfig};
use crate::logfmt::{
AptInstallClassifier, AptUpdateClassifier, Classifier, GenericClassifier, MakeClassifier,
MmdebstrapClassifier, QuiltClassifier,
};
use crate::report::{BuildTarget, BuildView, TargetKind};
use crate::sandbox::{PutOutcome, ReuseState, Sandbox, SandboxExt, StageOptions};
use crate::sandbox::{PutOutcome, ReuseState, Sandbox, SandboxExt, StageOptions, Unshare};
use std::error::Error;
use std::path::{Path, PathBuf};
use std::sync::Arc;
/// Build mode for the binary build
#[derive(PartialEq)]
pub enum BuildMode {
/// Local build, directly on the context
Local,
}
/// Phases of a binary build, announced to the [`BuildView`]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Phase {
@@ -222,8 +212,6 @@ pub struct DebBuildOptions<'a> {
/// Cross-compile for the target architecture instead of using
/// qemu-binfmt.
pub cross: bool,
/// Build mode; defaults to [`BuildMode::Local`].
pub mode: Option<BuildMode>,
/// PPAs to add for build-dependencies (`user/ppa_name`).
pub ppa: Vec<String>,
/// Packages to inject into the build environment before build-dep
@@ -232,8 +220,6 @@ pub struct DebBuildOptions<'a> {
/// Parallel build jobs; defaults to the core count available in the
/// build context.
pub jobs: Option<usize>,
/// Explicit build context; defaults to the current context.
pub ctx: Option<Arc<Context>>,
/// Incremental request (`pkh deb --incremental [<id>]`): which
/// record to fork as the base of this build's staged tree, and
/// whether this build's own staged tree is retained. Defaults to a
@@ -334,11 +320,9 @@ impl Default for DebBuildOptions<'_> {
mirror: None,
cwd: None,
cross: false,
mode: None,
ppa: Vec::new(),
inject: Vec::new(),
jobs: None,
ctx: None,
incremental: Incremental::Off,
ccache: true,
view: &QUIET,
@@ -379,11 +363,9 @@ async fn build_binary_package_impl(
ref mirror,
ref cwd,
cross,
ref mode,
ref ppa,
ref inject,
ref jobs,
ref ctx,
ref incremental,
ccache,
view,
@@ -457,21 +439,15 @@ async fn build_binary_package_impl(
let current_arch = crate::get_current_arch();
let arch = arch.as_deref().unwrap_or(&current_arch);
// Make sure we select a specific mode, either using user-requested
// or by using default for user-supplied parameters
let default_mode = BuildMode::Local;
let mode = mode.as_ref().unwrap_or(&default_mode);
// Create an ephemeral unshare context for all Local builds
// Use qemu_binfmt when target architecture differs from host and cross is not requested
let chroot_arch = if mode == &BuildMode::Local && arch != current_arch && !cross {
// Every build starts from a local base and bootstraps an unshare
// chroot. Use qemu_binfmt when the target architecture differs from the
// host and cross is not requested.
let chroot_arch = if arch != current_arch && !cross {
Some(arch)
} else {
None
};
// Use provided context or get current
let base_ctx = ctx.clone().unwrap_or_else(context::current);
let base_ctx = Arc::new(Context::new(ContextConfig::Local)?);
// Identify the target in the live view once the changelog is parsed, so
// even the chroot download output is attributed and tee'd
@@ -488,13 +464,9 @@ async fn build_binary_package_impl(
// this scope so it outlives the guarded section below and is only dropped
// once the live view has been cleared.
//
// Build records are a local-only feature: with a local base context
// every build runs in its own record under the sessions directory
// Every build runs in its own record under the sessions directory
// (forked from a previous one with `--incremental`, thrown away
// otherwise); every other base context keeps the historical anonymous
// temp chroot.
let session_enabled = *mode == BuildMode::Local
&& matches!(base_ctx.config, crate::context::ContextConfig::Local);
// otherwise).
let host_tree = cwd.canonicalize().unwrap_or_else(|_| cwd.to_path_buf());
let identity = session::SessionIdentity {
package: package.clone(),
@@ -503,29 +475,21 @@ async fn build_binary_package_impl(
cross,
};
let mut keep_upper = false;
let session = if session_enabled {
match resolve_record(
incremental,
&host_tree,
&identity,
&version,
series,
chroot_arch,
mirror.as_deref(),
view,
) {
Ok((record, keep)) => {
keep_upper = keep;
Some(record)
}
Err(e) => {
view.finish_failure();
return Err(e);
}
let (session, keep_upper) = match resolve_record(
incremental,
&host_tree,
&identity,
&version,
series,
chroot_arch,
mirror.as_deref(),
view,
) {
Ok((record, keep)) => (Some(record), keep),
Err(e) => {
view.finish_failure();
return Err(e);
}
} else {
None
};
// Tee every captured build line into the record's log: the wrapper
@@ -561,15 +525,15 @@ async fn build_binary_package_impl(
};
// The shared ccache of this target — skipped with --no-ccache.
let ccache_dir = (session_enabled && ccache).then(|| session::ccache_dir(&identity));
let ccache_dir = ccache.then(|| session::ccache_dir(&identity));
let mirror_arg = mirror.as_deref();
// The per-phase wall-clock profile of this run: fed by every
// enter_phase call from the chroot bootstrap to the artifact
// retrieval, recorded on the record at the end of the run.
let mut profiler = BuildProfiler::new();
let mut guard = if let Some(s) = session.clone() {
Some(
ephemeral::EphemeralContextGuard::new_for_session(
Some(Arc::new(
Unshare::new_for_session(
s,
series,
chroot_arch,
@@ -581,10 +545,10 @@ async fn build_binary_package_impl(
&mut profiler,
)
.await?,
)
} else if *mode == BuildMode::Local {
Some(
ephemeral::EphemeralContextGuard::new_with_context(
))
} else {
Some(Arc::new(
Unshare::new_with_context(
series,
chroot_arch,
mirror_arg,
@@ -593,9 +557,7 @@ async fn build_binary_package_impl(
&mut profiler,
)
.await?,
)
} else {
None
))
};
// Determine the build context explicitly: for Local builds it is the
@@ -604,7 +566,7 @@ async fn build_binary_package_impl(
// re-pointed at their own chroot); otherwise the base context is used
// directly.
let build_ctx: Arc<dyn Sandbox> = match guard.as_ref() {
Some(g) => g.context(),
Some(g) => Arc::clone(g) as Arc<dyn Sandbox>,
None => base_ctx.clone(),
};
@@ -614,7 +576,7 @@ async fn build_binary_package_impl(
let result = async {
// Prepare build directory: the stable /tmp/pkh-build for session
// builds (see EphemeralContextGuard::build_root — the overlay
// builds (see Unshare::build_root — the overlay
// upperdir mirrors paths relative to the merge point), a fresh
// timestamped temp dir otherwise.
let build_root = match guard.as_ref() {
@@ -723,38 +685,33 @@ async fn build_binary_package_impl(
&build_ctx,
)?;
// Run the build using target build mode. It returns the exact set of
// artifacts produced by this build (binary packages registered in
// debian/files plus the generated .buildinfo/.changes), as paths
// inside the build context.
let remote_files: Vec<PathBuf> = match mode {
BuildMode::Local => {
local::build(
&package,
&version,
arch,
series,
pocket.as_deref(),
mirror.as_deref(),
&package_dir,
cross,
ppa,
inject,
build_ctx.clone(),
view,
*jobs,
session.as_ref(),
build_resume,
ccache,
// Rootless builds: only a context pkh controls (the
// ephemeral chroot) can carry the on-demand build user.
guard.is_some().then_some(local::BUILD_USER),
&mut timings,
&mut profiler,
)
.await?
}
};
// Run the build. It returns the exact set of artifacts produced by
// this build (binary packages registered in debian/files plus the
// generated .buildinfo/.changes), as paths inside the sandbox.
let remote_files: Vec<PathBuf> = local::build(
&package,
&version,
arch,
series,
pocket.as_deref(),
mirror.as_deref(),
&package_dir,
cross,
ppa,
inject,
build_ctx.clone(),
view,
*jobs,
session.as_ref(),
build_resume,
ccache,
// Rootless builds: only a context pkh controls (the
// ephemeral chroot) can carry the on-demand build user.
Some(local::BUILD_USER),
&mut timings,
&mut profiler,
)
.await?;
// Retrieve the produced artifacts (binary packages plus the upload
// metadata) to the parent directory: the .debs land next to the
@@ -1408,9 +1365,9 @@ mod tests {
.expect("Cannot pull package");
log::info!("Successfully pulled package {}", package);
// Create a fresh local context for this test
let ctx = Arc::new(Context::new(crate::context::ContextConfig::Local).unwrap());
let ctx_sandbox: Arc<dyn Sandbox> = ctx.clone();
// Resolve the package directory through a local sandbox.
let ctx_sandbox: Arc<dyn Sandbox> =
Arc::new(Context::new(crate::context::ContextConfig::Local).unwrap());
// Change directory to the package directory
let cwd = crate::deb::find_package_directory(
@@ -1429,7 +1386,6 @@ mod tests {
series: Some(series.to_string()),
cross,
cwd: Some(cwd.to_path_buf()),
ctx: Some(ctx),
..Default::default()
})
.await
@@ -1633,14 +1589,11 @@ mod tests {
let temp_dir = tempfile::tempdir().unwrap();
let pkg_dir = create_indep_cross_test_source(temp_dir.path());
let ctx = Arc::new(Context::new(crate::context::ContextConfig::Local).unwrap());
crate::deb::build_binary_package(DebBuildOptions {
arch: Some("arm64".to_string()),
series: Some("noble".to_string()),
cwd: Some(pkg_dir),
cross: true,
ctx: Some(ctx),
..Default::default()
})
.await
@@ -1718,14 +1671,11 @@ mod tests {
std::fs::write(pkg_dir.join("debian/source/format"), "3.0 (native)\n").unwrap();
let ctx = Arc::new(Context::new(crate::context::ContextConfig::Local).unwrap());
let err = crate::deb::build_binary_package(DebBuildOptions {
arch: Some("arm64".to_string()),
series: Some("noble".to_string()),
cwd: Some(pkg_dir),
cross: true,
ctx: Some(ctx),
..Default::default()
})
.await
@@ -1789,12 +1739,9 @@ mod tests {
std::fs::write(pkg_dir.join("debian/source/format"), "3.0 (native)\n").unwrap();
let ctx = Arc::new(Context::new(crate::context::ContextConfig::Local).unwrap());
crate::deb::build_binary_package(DebBuildOptions {
series: Some("noble".to_string()),
cwd: Some(pkg_dir),
ctx: Some(ctx),
..Default::default()
})
.await