diff --git a/.packit.yaml b/.packit.yaml index 16dd716d26..2169865980 100644 --- a/.packit.yaml +++ b/.packit.yaml @@ -102,6 +102,28 @@ jobs: context: running_env: "packit" + # Run package-mode migration coverage on a single guest alongside the main + # tier-1 integration plan. + - job: tests + trigger: pull_request + identifier: package-mode-tier-1 + require: + label: + present: + - ci/tier-1 + targets: + - centos-stream-10-x86_64 + - centos-stream-10-aarch64 + - fedora-latest-stable-x86_64 + - fedora-latest-stable-aarch64 + tmt_plan: /tmt/plans/package-mode + tf_extra_params: + environments: + - tmt: + context: + running_env: "packit" + package_source: "artifacts" + # ci/merge: full target set - job: tests trigger: pull_request @@ -124,6 +146,28 @@ jobs: context: running_env: "packit" + - job: tests + trigger: pull_request + identifier: package-mode-full + require: + label: + present: + - ci/merge + targets: + - centos-stream-9-x86_64 + - centos-stream-9-aarch64 + - centos-stream-10-x86_64 + - centos-stream-10-aarch64 + - fedora-all-x86_64 + - fedora-all-aarch64 + tmt_plan: /tmt/plans/package-mode + tf_extra_params: + environments: + - tmt: + context: + running_env: "packit" + package_source: "artifacts" + - job: propose_downstream trigger: release dist_git_branches: diff --git a/Justfile b/Justfile index c23f160d33..a9d7ca2318 100644 --- a/Justfile +++ b/Justfile @@ -50,7 +50,8 @@ erofs_version := env("BOOTC_erofs_version", "v1") # Baseconfigs to inject into the image for testing (e.g. "etc-transient" or "root-transient") baseconfigs := env("BOOTC_baseconfigs", "") # Base container image to build from -base := env("BOOTC_base", "quay.io/centos-bootc/centos-bootc:stream10") +_default_base := "quay.io/centos-bootc/centos-bootc:stream10" +base := env("BOOTC_base", _default_base) # Buildroot base image buildroot_base := env("BOOTC_buildroot_base", "quay.io/centos/centos:stream10") # Optional: path to extra source (e.g. composefs-rs) for local development @@ -229,6 +230,108 @@ build-ukiboot-update-test-image: build-ukiboot-test-image test-ukiboot: build-ukiboot-update-test-image cargo xtask run-tmt --env=BOOTC_variant=composefs --filesystem=ext4 --boot-type=aboot --seal-state=unsealed --upgrade-image={{ukiboot_img}}:v2 {{ukiboot_img}}:v1 aboot +# Run the package-mode migration plan against the current checkout image. +# The VM cannot share controller Podman storage, so transfer it as an OCI archive. +[group('testing')] +test-tmt-package-mode image="centos-stream-10": + #!/bin/bash + set -euo pipefail + source_tree=$(git rev-parse --show-toplevel) + lbi_images="{{lbi_images}}" + case "{{image}}" in + fedora-44) + image_base=quay.io/fedora/fedora-bootc:44 + ;; + centos-stream-10) + image_base=quay.io/centos-bootc/centos-bootc:stream10 + ;; + *) + echo "Unsupported package-mode test image: {{image}}" >&2 + exit 1 + ;; + esac + # Respect an explicit base override from the caller, while keeping the + # image-specific default selected above (e.g. Fedora for fedora-44). + if [[ "{{base}}" != "{{_default_base}}" ]]; then + image_base="{{base}}" + fi + test_tree= + cleanup() { + local status=$? + trap - EXIT + if [[ -n "$test_tree" ]]; then + rm -rf -- "$test_tree" || true + fi + exit "$status" + } + trap cleanup EXIT + build_status=0 + BOOTC_base="$image_base" just {{_forward_settings}} base="$image_base" variant=ostree build || build_status=$? + if [[ "$build_status" -ne 0 ]]; then + exit "$build_status" + fi + + # TMT 1.69 copies the whole git root during fmf discovery. Use a small + # snapshot so ignored build outputs and local worktrees are never copied. + trap 'exit 129' HUP + trap 'exit 130' INT + trap 'exit 143' TERM + test_tree=$(mktemp -d -- "${TMPDIR:-/tmp}/bootc-tmt-package-mode.XXXXXX") + # Podman-generated build secrets use shared temp paths; leave cleanup to Podman. + # Stale artifacts are not deleted here, only excluded from the test snapshot. + rsync -a --delete \ + --exclude='/.git' \ + --exclude='/target' \ + --exclude='/.worktrees' \ + --exclude='/podman-build-secret-*' \ + --exclude='.package-mode-local.*' \ + "$source_tree/" "$test_tree/" + git -C "$test_tree" init -q + + archive_dir=$(mktemp -d -- "$test_tree/tmt/plans/.package-mode-local.XXXXXX") + archive_path="$archive_dir/bootc.oci" + archive_relative="${archive_dir#"$test_tree/"}/bootc.oci" + environment_file="$archive_dir/package-mode.env" + if [[ -e "$archive_path" ]]; then + echo "Refusing to overwrite existing archive: $archive_path" >&2 + exit 1 + fi + podman save --format oci-archive --output "$archive_path" {{base_img}} + # Keep TMT's lexical controller path under HOME without sudo or symlinks. + # The guest test stages its full /home run directory into preserved /var/home. + workdir_root="$HOME/.cache/bootc-tmt-workdirs" + mkdir -p -- "$workdir_root" + # The test consumes comma-separated refs, and tmt 1.69 supports these in + # a dotenv environment file without splitting the value on whitespace. + printf 'PACKAGE_MODE_LBI_IMAGES=%s\n' "${lbi_images// /,}" > "$environment_file" + tmt --root "$test_tree" \ + --context=running_env=packit \ + --context=package_source=local \ + --context="test_disk_image={{image}}" \ + run --workdir-root "$workdir_root" \ + --environment-file "$environment_file" \ + --environment "PACKAGE_MODE_IMAGE_NAME={{base_img}}" \ + --environment "PACKAGE_MODE_IMAGE_ARCHIVE=$archive_relative" \ + --environment "PACKAGE_MODE_TMT_WORKDIR_ROOT=$workdir_root" \ + plans --name '^/tmt/plans/package-mode$' + +# Check that a composefs caller still gets an OSTree package-mode test image, +# without losing its configured base image or image tag. +[group('testing')] +test-tmt-package-mode-dry-run: + #!/usr/bin/env bash + set -euo pipefail + output=$(BOOTC_variant=composefs BOOTC_image=localhost/package-mode-test:latest just --dry-run base=localhost/package-mode-base:latest test-tmt-package-mode 2>&1) + build_command=$(grep 'just .* variant=ostree build' <<< "$output") + if [[ ! "$build_command" =~ base=localhost/package-mode-base:latest.*variant=composefs.*variant=ostree\ build ]]; then + echo "package-mode dry-run did not force OSTree while preserving settings:" >&2 + echo "$build_command" >&2 + exit 1 + fi + grep -Fq 'podman save --format oci-archive --output "$archive_path" localhost/package-mode-test:latest' <<< "$output" + grep -Fq -- "--exclude='/podman-build-secret-*'" <<< "$output" + grep -Fq 'trap cleanup EXIT' <<< "$output" + # Split out from `test-container` because, unlike the container integration tests, # unit tests don't depend on variant/filesystem/bootloader/boot_type/seal_state, so # CI runs this once per OS (in the `package` job) instead of once per test-integration diff --git a/crates/lib/src/cli.rs b/crates/lib/src/cli.rs index de0174d17c..37bdec1cad 100644 --- a/crates/lib/src/cli.rs +++ b/crates/lib/src/cli.rs @@ -380,6 +380,9 @@ pub(crate) enum InstallOpts { /// the running host root filesystem. Currently, the host root filesystem's `/boot` partition /// will be wiped, but the content of the existing root will otherwise be retained, and will /// need to be cleaned up if desired when rebooted into the new root. + /// + /// When migrating from a package-mode system, use `--experimental --preserve-var` to copy + /// `/var` data into the new deployment before rebooting. This feature is experimental. ToExistingRoot(crate::install::InstallToExistingRootOpts), /// Nondestructively create a fresh installation state inside an existing bootc system. /// @@ -2550,10 +2553,14 @@ async fn run_from_opt(opt: Opt) -> Result { InstallOpts::Mount(opts) => crate::mount::mount(opts).await, #[cfg(feature = "install-to-disk")] InstallOpts::ToDisk(opts) => crate::install::install_to_disk(opts).await, - InstallOpts::ToFilesystem(opts) => { - crate::install::install_to_filesystem(opts, false, crate::install::Cleanup::Skip) - .await - } + InstallOpts::ToFilesystem(opts) => crate::install::install_to_filesystem( + opts, + false, + crate::install::Cleanup::Skip, + false, + ) + .await + .map(|_| ()), InstallOpts::ToExistingRoot(opts) => { crate::install::install_to_existing_root(opts).await } diff --git a/crates/lib/src/install.rs b/crates/lib/src/install.rs index 8d712a6c69..08587893a5 100644 --- a/crates/lib/src/install.rs +++ b/crates/lib/src/install.rs @@ -144,6 +144,7 @@ mod aleph; pub(crate) mod baseline; pub(crate) mod completion; pub(crate) mod config; +pub(crate) mod migrate; mod osbuild; pub(crate) mod osconfig; @@ -646,6 +647,41 @@ pub(crate) struct InstallToExistingRootOpts { #[clap(flatten)] pub(crate) composefs_opts: InstallComposefsOpts, + + /// Acknowledge that this feature is experimental. + #[clap(long)] + pub(crate) experimental: bool, + + /// Experimental. Requires `--experimental`; preserve the running system's `/var` data into the + /// new bootc deployment. + /// + /// Non-excluded content is copied with reflinks where individual files + /// support them, falling back to ordinary copies as needed. Reflinks are + /// created per file and are not a snapshot; stop stateful services or use + /// a consistent snapshot before copying live database data. + /// + /// `/var/tmp`, `/var/cache`, `/var/log/journal`, and `/var/lib/containers` + /// are always excluded. Repeat `--preserve-var-skip` to add paths relative + /// to `/var`. Nested mountpoints are represented by empty directories. + /// + /// Do not combine this with `--replace=wipe`: wiping the target would erase + /// the source `/var` before it can be copied. An abrupt termination may + /// leave hidden `.bootc-preserve-var.*` staging directories in the new + /// `/var`; they are not automatically removed later. Verify an individual + /// entry is an abandoned migration artifact before manually removing it. + /// Staged publication is atomic for visibility, but is not guaranteed + /// durable across sudden power loss. + #[clap(long, requires = "experimental")] + pub(crate) preserve_var: bool, + + /// Relative paths below `/var` to leave out while preserving `/var`. + /// Requires `--preserve-var`. + #[clap( + long = "preserve-var-skip", + value_name = "PATH", + requires = "preserve_var" + )] + pub(crate) preserve_var_skip: Vec, } #[derive(Debug, clap::Parser, PartialEq, Eq)] @@ -2087,7 +2123,7 @@ async fn install_with_sysroot( boot_uuid: &str, bound_images: BoundImages, has_ostree: bool, -) -> Result<()> { +) -> Result { let ostree = storage.get_ostree()?; let c_storage = storage.get_ensure_imgstore()?; @@ -2154,7 +2190,7 @@ async fn install_with_sysroot( } } - Ok(()) + Ok(camino::Utf8PathBuf::from(deployment_path.to_string())) } #[derive(Debug)] @@ -2165,36 +2201,45 @@ pub enum BoundImages { } impl BoundImages { - pub(crate) async fn from_state(state: &State) -> Result { - let bound_images = match state.config_opts.bound_images { - BoundImagesOpt::Skip => BoundImages::Skip, - others => { - let queried_images = crate::boundimage::query_bound_images(&state.container_root)?; - match others { - BoundImagesOpt::Stored => { - // Verify each bound image is present in the container storage - let mut r = Vec::with_capacity(queried_images.len()); - for image in queried_images { - let resolved = ResolvedBoundImage::from_image(&image).await?; - tracing::debug!("Resolved {}: {}", resolved.image, resolved.digest); - r.push(resolved) - } - BoundImages::Resolved(r) - } - BoundImagesOpt::Pull => { - // No need to resolve the images, we will pull them into the target later - BoundImages::Unresolved(queried_images) - } - BoundImagesOpt::Skip => anyhow::bail!("unreachable error"), + async fn from_state(state: &State) -> Result { + Self::from_config(state.config_opts.bound_images, &state.container_root).await + } + + async fn from_config(bound_images_opt: BoundImagesOpt, root: &Dir) -> Result { + if bound_images_opt == BoundImagesOpt::Skip { + return Ok(BoundImages::Skip); + } + + let queried_images = crate::boundimage::query_bound_images(root)?; + Self::from_queried(bound_images_opt, queried_images).await + } + + async fn from_queried( + bound_images_opt: BoundImagesOpt, + queried_images: Vec, + ) -> Result { + match bound_images_opt { + BoundImagesOpt::Stored => { + let mut resolved_images = Vec::with_capacity(queried_images.len()); + for image in queried_images { + let resolved = ResolvedBoundImage::from_image(&image).await?; + tracing::debug!("Resolved {}: {}", resolved.image, resolved.digest); + resolved_images.push(resolved); } + Ok(BoundImages::Resolved(resolved_images)) } - }; - - Ok(bound_images) + BoundImagesOpt::Pull => Ok(BoundImages::Unresolved(queried_images)), + BoundImagesOpt::Skip => Ok(BoundImages::Skip), + } } } -async fn ostree_install(state: &State, rootfs: &RootSetup, cleanup: Cleanup) -> Result<()> { +async fn ostree_install( + state: &State, + rootfs: &RootSetup, + cleanup: Cleanup, + pre_resolved_bound_images: Option, +) -> Result { // We verify this upfront because it's currently required by bootupd let boot_uuid = rootfs .get_boot_uuid()? @@ -2202,14 +2247,17 @@ async fn ostree_install(state: &State, rootfs: &RootSetup, cleanup: Cleanup) -> .ok_or_else(|| anyhow!("No uuid for boot/root"))?; tracing::debug!("boot uuid={boot_uuid}"); - let bound_images = BoundImages::from_state(state).await?; + let bound_images = match pre_resolved_bound_images { + Some(bound_images) => bound_images, + None => BoundImages::from_state(state).await?, + }; // Initialize the ostree sysroot (repo, stateroot, etc.) - { + let deployment_path = { let (sysroot, has_ostree) = initialize_ostree_root(state, rootfs).await?; - install_with_sysroot( + let deployment_path = install_with_sysroot( state, rootfs, &sysroot, @@ -2232,19 +2280,21 @@ async fn ostree_install(state: &State, rootfs: &RootSetup, cleanup: Cleanup) -> // We must drop the sysroot here in order to close any open file // descriptors. + deployment_path }; // Run this on every install as the penultimate step install_finalize(&rootfs.physical_root_path).await?; - Ok(()) + Ok(deployment_path) } async fn install_to_filesystem_impl( state: &State, rootfs: &mut RootSetup, cleanup: Cleanup, -) -> Result<()> { + pre_resolved_bound_images: Option, +) -> Result> { if matches!(state.selinux_state, SELinuxFinalState::ForceTargetDisabled) { rootfs.kargs.extend(&Cmdline::from("selinux=0")); } @@ -2266,7 +2316,7 @@ async fn install_to_filesystem_impl( } } - if state.composefs_options.composefs_backend { + let deployment_path = if state.composefs_options.composefs_backend { let fetch_ref = state.source.composefs_fetch_reference(); let (manifest, _) = get_container_manifest_and_config(&fetch_ref).await?; // A capable filesystem gets a strict provisional repository. The @@ -2378,12 +2428,14 @@ async fn install_to_filesystem_impl( ) .context("SELinux labeling of composefs objects")?; } - // Ensure the image storage is SELinux-labeled. This must happen // after all image pulls are complete. storage.ensure_imgstore_labeled()?; + + None } else { - ostree_install(state, rootfs, cleanup).await?; + let deployment_path = + ostree_install(state, rootfs, cleanup, pre_resolved_bound_images).await?; // For s390x, we set zipl as the bootloader // this needs to be done after the ostree commit is deployed, @@ -2402,7 +2454,8 @@ async fn install_to_filesystem_impl( .run_capture_stderr() .context("Setting bootloader config to zipl")?; } - } + Some(deployment_path) + }; // As the very last step before filesystem finalization, do a full SELinux // relabel of the physical root filesystem. Any files that are already @@ -2424,7 +2477,7 @@ async fn install_to_filesystem_impl( } } - Ok(()) + Ok(deployment_path) } fn installation_complete() { @@ -2506,7 +2559,7 @@ pub(crate) async fn install_to_disk(mut opts: InstallToDiskOpts) -> Result<()> { (rootfs, loopback_dev) }; - install_to_filesystem_impl(&state, &mut rootfs, Cleanup::Skip).await?; + install_to_filesystem_impl(&state, &mut rootfs, Cleanup::Skip, None).await?; // Drop all data about the root except the bits we need to ensure any file descriptors etc. are closed. let (root_path, luksdev) = rootfs.into_storage(); @@ -2782,7 +2835,10 @@ pub(crate) async fn install_to_filesystem( opts: InstallToFilesystemOpts, targeting_host_root: bool, cleanup: Cleanup, -) -> Result<()> { + require_ostree_backend: bool, +) -> Result> { + validate_preserve_var_replace(require_ostree_backend, opts.filesystem_opts.replace)?; + // Log the installation operation to systemd journal const INSTALL_FILESYSTEM_JOURNAL_ID: &str = "9a8b7c6d5e4f3a2b1c0d9e8f7a6b5c4d3"; let source_image = opts @@ -2806,21 +2862,8 @@ pub(crate) async fn install_to_filesystem( // And the last bit of state here is the fsopts, which we also destructure now. let mut fsopts = opts.filesystem_opts; - // If we're doing an alongside install, automatically set up the host rootfs - // mount if it wasn't done already. - if targeting_host_root - && fsopts.root_path.as_str() == ALONGSIDE_ROOT_MOUNT - && !fsopts.root_path.try_exists()? - { - tracing::debug!("Mounting host / to {ALONGSIDE_ROOT_MOUNT}"); - std::fs::create_dir(ALONGSIDE_ROOT_MOUNT)?; - bootc_mount::bind_mount_from_pidns( - bootc_mount::PID1, - "/".into(), - ALONGSIDE_ROOT_MOUNT.into(), - true, - ) - .context("Mounting host / to {ALONGSIDE_ROOT_MOUNT}")?; + if targeting_host_root { + mount_alongside_root_if_needed(&fsopts.root_path)?; } let target_root_path = fsopts.root_path.clone(); @@ -2894,6 +2937,21 @@ pub(crate) async fn install_to_filesystem( ) .await?; + // The image can select the composefs backend during preparation, so check + // compatibility after that choice is resolved but before target mutation. + validate_migration_backend( + require_ostree_backend, + state.composefs_options.composefs_backend, + )?; + + // Resolve stored bound images before replacement or boot-directory cleanup + // can mutate the existing root. Pull mode remains unresolved until deploy. + let pre_resolved_bound_images = if targeting_host_root && require_ostree_backend { + Some(BoundImages::from_state(&state).await?) + } else { + None + }; + // Check to see if this happens to be the real host root if !fsopts.acknowledge_destructive { warn_on_host_root(&target_rootfs_fd)?; @@ -2903,8 +2961,10 @@ pub(crate) async fn install_to_filesystem( Some(ReplaceMode::Wipe) => { let rootfs_fd = rootfs_fd.try_clone()?; println!("Wiping contents of root"); - tokio::task::spawn_blocking(move || remove_all_in_dir_no_xdev(&rootfs_fd, true)) - .await??; + tokio::task::spawn_blocking(move || { + wipe_install_target(&rootfs_fd, require_ostree_backend) + }) + .await??; } Some(ReplaceMode::Alongside) => { clean_boot_directories(&target_rootfs_fd, &target_root_path, is_already_ostree)? @@ -3056,17 +3116,96 @@ pub(crate) async fn install_to_filesystem( skip_finalize, }; - install_to_filesystem_impl(&state, &mut rootfs, cleanup).await?; + let deployment_path = + install_to_filesystem_impl(&state, &mut rootfs, cleanup, pre_resolved_bound_images).await?; // Drop all data about the root except the path to ensure any file descriptors etc. are closed. drop(rootfs); installation_complete(); + Ok(deployment_path) +} + +/// Set up the host root mount for the default alongside target if it has not +/// already been provided by the caller (e.g. with `-v /:/target`). +fn mount_alongside_root_if_needed(root_path: &Utf8Path) -> Result<()> { + if root_path.as_str() == ALONGSIDE_ROOT_MOUNT && !root_path.try_exists()? { + tracing::debug!("Mounting host / to {ALONGSIDE_ROOT_MOUNT}"); + std::fs::create_dir(ALONGSIDE_ROOT_MOUNT)?; + bootc_mount::bind_mount_from_pidns( + bootc_mount::PID1, + "/".into(), + ALONGSIDE_ROOT_MOUNT.into(), + true, + ) + .context("Mounting host / to {ALONGSIDE_ROOT_MOUNT}")?; + } + Ok(()) +} + +fn validate_migration_backend(require_ostree_backend: bool, composefs_backend: bool) -> Result<()> { + anyhow::ensure!( + !require_ostree_backend || !composefs_backend, + "--preserve-var requires the ostree backend" + ); + Ok(()) +} + +/// Resolve skip paths only for preserve-var installs. The loader is deferred +/// so an unrelated to-existing-root install never validates these options. +fn resolve_preserve_var_skip( + preserve_var: bool, + mut cli_skip: Vec, + load_config_skip: impl FnOnce() -> Result>>, +) -> Result> { + if !preserve_var { + return Ok(Vec::new()); + } + if let Some(config_skip) = load_config_skip()? { + cli_skip.extend(config_skip); + } + migrate::validate_exclusions(&cli_skip) +} + +fn validate_preserve_var_replace(preserve_var: bool, replace: Option) -> Result<()> { + anyhow::ensure!( + !(preserve_var && matches!(replace, Some(ReplaceMode::Wipe))), + "--preserve-var cannot be used with --replace=wipe: wiping the target would erase the source /var before it can be copied" + ); + Ok(()) +} + +fn wipe_install_target(rootfs: &Dir, preserve_var: bool) -> Result<()> { + validate_preserve_var_replace(preserve_var, Some(ReplaceMode::Wipe))?; + remove_all_in_dir_no_xdev(rootfs, true)?; + Ok(()) +} + +/// Resolve the physical sysroot when `root_path` points at an OSTree deployment. +fn physical_sysroot_path(root_path: &std::path::Path) -> std::path::PathBuf { + let possible_sysroot = root_path.join("sysroot"); + if possible_sysroot.join("ostree").exists() { + possible_sysroot + } else { + root_path.to_owned() + } +} + +fn write_destructive_cleanup_marker(root_path: &std::path::Path) -> Result<()> { + let physical_root = physical_sysroot_path(root_path); + let sysroot = Dir::open_ambient_dir(&physical_root, cap_std::ambient_authority()) + .with_context(|| format!("Opening physical sysroot {physical_root:?}"))?; + tracing::debug!("Writing {DESTRUCTIVE_CLEANUP}"); + sysroot.atomic_write(DESTRUCTIVE_CLEANUP, b"")?; Ok(()) } pub(crate) async fn install_to_existing_root(opts: InstallToExistingRootOpts) -> Result<()> { + // Reject this before configuration loading, mounting, preparation, or any + // target changes: a wipe would destroy the running source /var. + validate_preserve_var_replace(opts.preserve_var, opts.replace)?; + // Log the existing root installation operation to systemd journal const INSTALL_EXISTING_ROOT_JOURNAL_ID: &str = "7c6d5e4f3a2b1c0d9e8f7a6b5c4d3e2f1"; let source_image = opts @@ -3096,7 +3235,33 @@ pub(crate) async fn install_to_existing_root(opts: InstallToExistingRootOpts) -> false => Cleanup::Skip, }; - let opts = InstallToFilesystemOpts { + let preserve_var = opts.preserve_var; + let defer_cleanup = opts.cleanup && preserve_var; + let root_path = std::path::PathBuf::from(opts.root_path.as_str()); + let preserve_var_skip = + resolve_preserve_var_skip(preserve_var, opts.preserve_var_skip, || { + Ok(config::load_config()?.and_then(|config| config.preserve_var_skip)) + })?; + let require_ostree_backend = preserve_var; + validate_migration_backend( + require_ostree_backend, + opts.composefs_opts.composefs_backend, + )?; + + // Inspect source /var before installation can remove or replace the old + // system's bootable state. The actual copy runs afterward and can still + // fail, for example from concurrent changes or insufficient space. + let var_migration = if preserve_var { + mount_alongside_root_if_needed(&opts.root_path)?; + Some(migrate::VarMigrationPlan::prepare( + &root_path.join("var"), + &preserve_var_skip, + )?) + } else { + None + }; + + let fs_opts = InstallToFilesystemOpts { filesystem_opts: InstallTargetFilesystemOpts { root_path: opts.root_path, root_mount_spec: None, @@ -3111,7 +3276,42 @@ pub(crate) async fn install_to_existing_root(opts: InstallToExistingRootOpts) -> composefs_opts: opts.composefs_opts, }; - install_to_filesystem(opts, true, cleanup).await + let deployment_path = install_to_filesystem( + fs_opts, + true, + if defer_cleanup { + Cleanup::Skip + } else { + cleanup + }, + require_ostree_backend, + ) + .await?; + + if let Some(var_migration) = var_migration { + println!(); + println!("Preserving /var..."); + let deployment_path = deployment_path + .as_ref() + .context("Install did not produce an ostree deployment")?; + let physical_root = physical_sysroot_path(&root_path); + let deploy_dir = physical_root.join(deployment_path); + let new_var = migrate::deployment_var_path(&deploy_dir)?; + let sysroot = Dir::open_ambient_dir(&physical_root, cap_std::ambient_authority()) + .with_context(|| format!("Opening physical sysroot {physical_root:?}"))?; + let deployment_dir = sysroot + .open_dir(deployment_path) + .with_context(|| format!("Opening installed deployment {deployment_path:?}"))?; + let target_policy = crate::lsm::new_sepolicy_at(&deployment_dir)?; + migrate::preserve_var(&var_migration, &new_var, target_policy.as_ref()) + .context("Post-install /var preservation")?; + } + + if defer_cleanup { + write_destructive_cleanup_marker(&root_path)?; + } + + Ok(()) } /// Read the /boot entry from /etc/fstab, if it exists @@ -3293,6 +3493,7 @@ pub(crate) async fn install_finalize(target: &Utf8Path) -> Result<()> { #[cfg(test)] mod tests { use super::*; + use clap::Parser; #[test] fn test_composefs_opts_validate() { @@ -3643,4 +3844,169 @@ UUID=boot-uuid /boot ext4 defaults 0 0 Ok(()) } + + #[test] + fn test_migration_backend_compatibility() { + let cases = [ + (false, false, true), + (false, true, true), + (true, false, true), + (true, true, false), + ]; + for (require_ostree_backend, composefs_backend, valid) in cases { + assert_eq!( + validate_migration_backend(require_ostree_backend, composefs_backend).is_ok(), + valid, + "require_ostree_backend={require_ostree_backend}, composefs_backend={composefs_backend}" + ); + } + } + + #[test] + fn preserve_var_requires_experimental_and_skip_requires_preserve_var() { + assert!(InstallToExistingRootOpts::try_parse_from(["bootc"]).is_ok()); + assert!( + InstallToExistingRootOpts::try_parse_from(["bootc", "--preserve-var-skip", "lib/rpm"]) + .is_err() + ); + assert!(InstallToExistingRootOpts::try_parse_from(["bootc", "--preserve-var"]).is_err()); + assert!( + InstallToExistingRootOpts::try_parse_from([ + "bootc", + "--preserve-var", + "--experimental" + ]) + .is_ok() + ); + assert!( + InstallToExistingRootOpts::try_parse_from([ + "bootc", + "--preserve-var", + "--preserve-var-skip", + "lib/rpm" + ]) + .is_err() + ); + assert!( + InstallToExistingRootOpts::try_parse_from([ + "bootc", + "--experimental", + "--preserve-var-skip", + "lib/rpm" + ]) + .is_err() + ); + assert!( + InstallToExistingRootOpts::try_parse_from([ + "bootc", + "--experimental", + "--preserve-var", + "--preserve-var-skip", + "lib/rpm" + ]) + .is_ok() + ); + } + + #[test] + fn preserve_var_skip_config_is_ignored_unless_preserving_var() -> Result<()> { + let mut config_loaded = false; + let ordinary = resolve_preserve_var_skip(false, Vec::new(), || { + config_loaded = true; + Ok(Some(vec!["../invalid".to_string()])) + })?; + assert!(ordinary.is_empty()); + assert!( + !config_loaded, + "ordinary installs must not load skip config" + ); + + let error = resolve_preserve_var_skip(true, Vec::new(), || { + Ok(Some(vec!["../invalid".to_string()])) + }) + .unwrap_err(); + assert!(format!("{error:#}").contains("invalid /var exclusion")); + + assert_eq!( + resolve_preserve_var_skip(true, vec!["lib/cli".to_string()], || { + Ok(Some(vec!["lib/config".to_string()])) + })?, + ["lib/cli", "lib/config"] + ); + Ok(()) + } + + #[test] + fn preserve_var_rejects_wipe_before_target_contents_can_be_removed() -> Result<()> { + let target = tempfile::tempdir()?; + let sentinel = target.path().join("var/state.db"); + std::fs::create_dir_all(sentinel.parent().unwrap())?; + std::fs::write(&sentinel, b"must remain untouched")?; + + let rootfs = Dir::open_ambient_dir(target.path(), cap_std::ambient_authority())?; + let error = wipe_install_target(&rootfs, true).unwrap_err(); + + assert!(error.to_string().contains("--preserve-var")); + assert_eq!(std::fs::read(&sentinel)?, b"must remain untouched"); + Ok(()) + } + + #[tokio::test] + async fn test_bound_images_pull_mode_stays_unresolved() { + let queried_images = vec![BoundImage { + image: "quay.io/example/app:latest".to_string(), + auth_file: None, + }]; + + let bound_images = BoundImages::from_queried(BoundImagesOpt::Pull, queried_images) + .await + .unwrap(); + let BoundImages::Unresolved(images) = bound_images else { + panic!("pull mode should defer bound image resolution until deployment"); + }; + assert_eq!(images.len(), 1); + assert_eq!(images[0].image, "quay.io/example/app:latest"); + } + + #[tokio::test] + async fn test_bound_images_skip_does_not_query_malformed_directory() -> Result<()> { + let root = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; + root.create_dir_all(crate::boundimage::BOUND_IMAGE_DIR)?; + root.write( + format!("{}/broken.image", crate::boundimage::BOUND_IMAGE_DIR), + b"not a symlink or valid bound-image file", + )?; + + let bound_images = BoundImages::from_config(BoundImagesOpt::Skip, &root).await?; + assert!(matches!(bound_images, BoundImages::Skip)); + Ok(()) + } + + #[test] + fn test_destructive_cleanup_marker_uses_physical_sysroot() -> Result<()> { + let td = tempfile::tempdir()?; + let deployment_root = td.path(); + let physical_sysroot = deployment_root.join("sysroot"); + std::fs::create_dir_all(deployment_root.join("etc"))?; + std::fs::create_dir_all(physical_sysroot.join("ostree"))?; + std::fs::create_dir_all(physical_sysroot.join("etc"))?; + + write_destructive_cleanup_marker(deployment_root)?; + + assert!(physical_sysroot.join(DESTRUCTIVE_CLEANUP).is_file()); + assert!(!deployment_root.join(DESTRUCTIVE_CLEANUP).exists()); + Ok(()) + } + + #[test] + fn test_destructive_cleanup_marker_uses_root_without_nested_sysroot() -> Result<()> { + let td = tempfile::tempdir()?; + let root = td.path(); + std::fs::create_dir_all(root.join("etc"))?; + + write_destructive_cleanup_marker(root)?; + + assert!(root.join(DESTRUCTIVE_CLEANUP).is_file()); + Ok(()) + } } diff --git a/crates/lib/src/install/config.rs b/crates/lib/src/install/config.rs index 55fdd42f98..8595c74e16 100644 --- a/crates/lib/src/install/config.rs +++ b/crates/lib/src/install/config.rs @@ -132,6 +132,8 @@ pub(crate) struct InstallConfiguration { /// Enforce that the containers-storage stack has a non-default /// (i.e. not `insecureAcceptAnything`) container image signature policy. pub(crate) enforce_container_sigpolicy: Option, + /// Additional paths below `/var` to skip when `--preserve-var` is enabled. + pub(crate) preserve_var_skip: Option>, } fn merge_basic(s: &mut Option, o: Option, _env: &EnvProperties) { @@ -226,6 +228,11 @@ impl Mergeable for InstallConfiguration { other.enforce_container_sigpolicy, env, ); + if let Some(other_skip) = other.preserve_var_skip { + self.preserve_var_skip + .get_or_insert_with(Default::default) + .extend(other_skip) + } if let Some(other_kargs) = other.kargs { self.kargs .get_or_insert_with(Default::default) @@ -878,6 +885,30 @@ skip-boot-uuid = false // skip_boot_uuid should be overridden to true assert_eq!(install.bootupd.unwrap().skip_boot_uuid.unwrap(), true); } + + #[test] + fn test_merge_preserve_var_skip_appends_paths() { + let env = EnvProperties { + sys_arch: "x86_64".to_string(), + }; + let mut install: InstallConfiguration = toml::from_str( + r#"preserve-var-skip = ["lib/first"] +"#, + ) + .unwrap(); + let other: InstallConfiguration = toml::from_str( + r#"preserve-var-skip = ["lib/second"] +"#, + ) + .unwrap(); + + install.merge(other, &env); + + assert_eq!( + install.preserve_var_skip.unwrap(), + ["lib/first", "lib/second"] + ); + } } #[test] diff --git a/crates/lib/src/install/migrate.rs b/crates/lib/src/install/migrate.rs new file mode 100644 index 0000000000..96dbbc834b --- /dev/null +++ b/crates/lib/src/install/migrate.rs @@ -0,0 +1,2883 @@ +//! # Package-mode to image-mode migration helpers +//! +//! This module implements the post-install state-preservation steps that make +//! `bootc install to-existing-root` useful for converting a live package-mode +//! (RPM/DEB) system to a bootc image-mode deployment without losing data. +//! +//! This migration is triggered by passing `--preserve-var` to +//! `bootc install to-existing-root`. It runs **after** the core install +//! (ostree deploy + bootupd) completes but **before** the first reboot, while +//! the package-mode environment is still the running OS. +//! +//! ## `/var` preservation (`--preserve-var`) +//! +//! After a plain `bootc install to-existing-root`, the new deployment's `/var` +//! is bound from `/ostree/deploy//var/` — an initially-empty +//! directory. The old package-mode `/var` is stranded at `/var` +//! (inside the container, the host root is mounted at `root_path`). +//! +//! Two strategies are tried in order: +//! +//! - **Strategy C — reflink copy** (btrfs / XFS with reflinks): `cp --reflink=auto` +//! clones files copy-on-write where possible and falls back to a regular copy +//! for individual files that cannot be cloned. Files are handled individually, +//! so this is not an atomic snapshot of the `/var` tree or of a live database. +//! +//! - **Strategy D — plain copy** (ext4 and other non-reflink filesystems): +//! `cp -a` copies each non-ephemeral subdirectory of `/var` into the new +//! deployment's stateroot `var/`. This is correct but slow for large `/var` +//! trees, and **unsafe for live databases** (see the known-limitation comment +//! on `preserve_var_copy_mounts`). +//! +//! Nested mountpoints are selected from one `/proc/self/mountinfo` snapshot +//! before the copy. This is a point-in-time, best-effort snapshot, not a +//! race-free guard: mounts added or removed beneath `/var` during copying can +//! race with the copy, just like concurrent file changes. +//! Each missing entry is copied to a unique sibling staging directory and +//! published atomically without replacing an existing destination. A failed +//! entry leaves no partial final entry and can be retried; there is no global +//! rollback, so entries completely published before a later failure remain. +//! +//! The built-in exclusions are `tmp`, `cache`, `log/journal`, and +//! `lib/containers`. Additional exclusions can be supplied through +//! configuration or repeated `--preserve-var-skip` options; `lib/rpm` is not +//! excluded unless explicitly configured. +//! +//! Staging directories use names beginning `.bootc-preserve-var.` beneath the +//! destination `/var`. They are cleaned up during normal success and error +//! handling, but an abrupt termination can leave them behind. They are not +//! automatically removed on a later run because a matching entry could be +//! unrelated. Before manually removing one, ensure no migration is running and +//! verify the individual entry is an artifact of an interrupted migration; do +//! not blindly remove every similarly named entry. +//! +//! Staged rename publication is atomic for namespace visibility, but this code +//! does not fsync copied files or directories. It therefore makes no guarantee +//! that a published entry survives sudden power loss or kernel failure. This is +//! per-entry publication, not a transaction: entries published before a later +//! error remain. +//! +//! Rollback preservation is intentionally independent from `/var` migration. + +mod mountinfo; + +use std::path::{Component, Path}; +use std::{ + collections::{BTreeSet, HashMap}, + ffi::OsStr, + os::fd::{AsFd, AsRawFd}, + path::PathBuf, + process::{Command, Stdio}, + sync::{ + Arc, + atomic::{AtomicU64, Ordering}, + }, +}; + +use crate::store::reflink::supports_reflink; +use anyhow::{Context, Result}; +use cap_std_ext::cap_std::ambient_authority; +use cap_std_ext::cap_std::fs::{Dir as CapStdDir, Metadata, MetadataExt}; +use cap_std_ext::cap_std::io_lifetimes::AsFilelike; +use cap_std_ext::cmdext::{CapStdExtCommandExt, CmdFds}; +#[cfg(test)] +use fn_error_context::context; + +/// Paths below `/var` which `--preserve-var` never copies. +const ALWAYS_EXCLUDED: &[&str] = &["tmp", "cache", "log/journal", "lib/containers"]; + +/// Source-side work identified before the install starts. Copying remains a +/// post-install operation and can still fail (for example, due to space or +/// concurrent source changes). +#[derive(Debug)] +pub(crate) struct VarMigrationPlan { + /// Canonical path of the running system's `/var`. + src_var: PathBuf, + /// Mounts below `src_var`, relative to it, which get empty placeholders. + nested_mounts: BTreeSet, + /// Built-in and requested exclusions, relative to `src_var`. + exclusions: Vec, +} + +impl VarMigrationPlan { + /// Resolve and inspect source `/var` before installation touches the target. + pub(crate) fn prepare(src_var: &Path, exclusions: &[String]) -> Result { + let canonical_src_var = std::fs::canonicalize(src_var) + .with_context(|| format!("Canonicalizing source /var {}", src_var.display()))?; + let source = CapStdDir::open_ambient_dir(&canonical_src_var, ambient_authority()) + .with_context(|| format!("Opening source {}", canonical_src_var.display()))?; + for entry in source + .entries() + .with_context(|| format!("Listing source {}", canonical_src_var.display()))? + { + entry.with_context(|| format!("Reading source {}", canonical_src_var.display()))?; + } + + let nested_mounts = mountinfo::nested_mountpoints_for(&canonical_src_var)?; + let exclusions = ALWAYS_EXCLUDED + .iter() + .map(|path| (*path).to_string()) + .chain(exclusions.iter().cloned()) + .collect::>(); + + validate_mountpoint_types(&canonical_src_var, &exclusions, &nested_mounts)?; + + Ok(Self { + src_var: canonical_src_var, + nested_mounts, + exclusions, + }) + } +} + +/// Only directories and regular files can receive safe placeholders. Do not +/// inspect mountpoints the filtered copy will never reach. +fn validate_mountpoint_types( + src_var: &Path, + exclusions: &[String], + nested_mounts: &BTreeSet, +) -> Result<()> { + for mount in nested_mounts { + let is_excluded = exclusions + .iter() + .any(|excluded| mount.starts_with(excluded)); + let is_below_another_mount = nested_mounts + .iter() + .any(|other| other != mount && mount.starts_with(other)); + if is_excluded || is_below_another_mount { + continue; + } + let metadata = std::fs::symlink_metadata(src_var.join(mount)) + .with_context(|| format!("Inspecting mountpoint /var/{}", mount.display()))?; + anyhow::ensure!( + metadata.is_dir() || metadata.is_file(), + "/var/{} is a mountpoint that is neither a directory nor a file; exclude it with --preserve-var-skip", + mount.display() + ); + } + Ok(()) +} + +/// Preserve the running `/var` into the new deployment's `var/` directory. +/// +/// `new_var` is the new deployment's `var/` directory, which may have +/// image-seeded content. +pub(crate) fn preserve_var( + plan: &VarMigrationPlan, + new_var: &Path, + policy: Option<&ostree_ext::ostree::SePolicy>, +) -> Result<()> { + let src_var = CapStdDir::open_ambient_dir(&plan.src_var, ambient_authority()) + .with_context(|| format!("Opening source {}", plan.src_var.display()))?; + let new_var = open_or_create_var_dir(new_var)?; + let labeler = policy.map(|policy| TargetPolicyLabeler { policy }); + let labeler = labeler + .as_ref() + .map(|labeler| labeler as &dyn StagedLabeler); + + let effective_exclusions = &plan.exclusions; + let nested_mounts = &plan.nested_mounts; + + let copy_result = if supports_reflink(&src_var, &new_var)? { + println!(" Filesystem supports reflinks — using copy-on-write clone (Strategy C)"); + preserve_var_reflink_mounts_labeled( + &src_var, + &new_var, + effective_exclusions, + nested_mounts, + labeler, + ) + } else { + println!(" Filesystem does not support reflinks — falling back to full copy (Strategy D)"); + preserve_var_copy_mounts_labeled( + &src_var, + &new_var, + effective_exclusions, + nested_mounts, + labeler, + ) + }; + copy_result?; + + Ok(()) +} + +/// Open the destination through its parent capability, creating just the +/// final `/var` directory if needed. The caller-supplied parent is the initial +/// ambient authority; all subsequent traversal is relative to opened handles. +fn open_or_create_var_dir(path: &Path) -> Result { + let parent = path.parent().context("Destination /var has no parent")?; + let name = path + .file_name() + .context("Destination /var has no final path component")?; + let parent = CapStdDir::open_ambient_dir(parent, ambient_authority()) + .with_context(|| format!("Opening destination parent {}", parent.display()))?; + match parent.create_dir(name) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + anyhow::ensure!( + parent.symlink_metadata(name)?.is_dir(), + "Destination {} exists and is not a directory", + path.display() + ); + } + Err(e) => return Err(e).with_context(|| format!("Creating {}", path.display())), + } + open_child_dir_nofollow(&parent, name) + .with_context(|| format!("Opening destination {}", path.display())) +} + +pub(crate) fn deployment_var_path(deploy_dir: &Path) -> Result { + let deploy_parent = deploy_dir + .parent() + .context("Deployment path has no parent")?; + let stateroot = deploy_parent + .parent() + .context("Deployment path has no stateroot")?; + Ok(stateroot.join("var")) +} + +/// Run GNU cp with directory handles inherited as fixed descriptors. Each +/// operand is `/proc/self/fd/N[/entry]`; cp's top-level paths therefore remain +/// rooted in the opened capabilities even if the original paths are renamed. +/// GNU cp still performs its own recursive traversal beneath each operand, so +/// this is not race-safe against a concurrent writer replacing entries while +/// the copy is in progress. +fn run_cp( + cp_program: &OsStr, + src_root: &CapStdDir, + src_names: &[&OsStr], + dst_root: &CapStdDir, + dst_name: Option<&OsStr>, + options: &[&str], + stdout: Stdio, + stderr: Stdio, +) -> Result { + let src_fd = Arc::new( + src_root + .as_filelike_view::() + .as_fd() + .try_clone_to_owned()?, + ); + let dst_fd = Arc::new( + dst_root + .as_filelike_view::() + .as_fd() + .try_clone_to_owned()?, + ); + let mut fds = CmdFds::new(); + fds.take_fd_n(src_fd, 3); + fds.take_fd_n(dst_fd, 4); + + let src_paths = src_names.iter().map(|name| { + let mut path = PathBuf::from("/proc/self/fd/3"); + path.push(name); + path + }); + let mut dst_path = PathBuf::from("/proc/self/fd/4"); + if let Some(name) = dst_name { + dst_path.push(name); + } + + let status = Command::new(cp_program) + .args(options) + .arg("--") + .args(src_paths) + .arg(dst_path) + .stdout(stdout) + .stderr(stderr) + .take_fds(fds) + .status() + .context("Running cp")?; + Ok(status) +} + +/// Strategy C: reflink-copy each top-level entry under `src_var` into `new_var`. +/// +/// Skips well-known ephemeral subdirectories. +#[cfg(test)] +#[context("Reflink-copying /var into new deployment (Strategy C)")] +fn preserve_var_reflink( + src_var: &CapStdDir, + new_var: &CapStdDir, + exclusions: &[String], +) -> Result<()> { + preserve_var_reflink_mounts(src_var, new_var, exclusions, &BTreeSet::new()) +} + +#[cfg(test)] +fn preserve_var_reflink_mounts( + src_var: &CapStdDir, + new_var: &CapStdDir, + exclusions: &[String], + mountpoints: &BTreeSet, +) -> Result<()> { + preserve_var_reflink_mounts_labeled(src_var, new_var, exclusions, mountpoints, None) +} + +fn preserve_var_reflink_mounts_labeled( + src_var: &CapStdDir, + new_var: &CapStdDir, + exclusions: &[String], + mountpoints: &BTreeSet, + labeler: Option<&dyn StagedLabeler>, +) -> Result<()> { + copy_var_filtered( + src_var, + new_var, + Path::new(""), + exclusions, + mountpoints, + true, + false, + labeler, + labeler.is_some(), + OsStr::new(GNU_CP), + ) + .context("Reflink-copying /var into new deployment (Strategy C)")?; + println!(" /var reflink copy complete."); + Ok(()) +} + +/// Copy a directory's ordinary children together with cp -a, recursing only +/// into directories whose descendants contain a mountpoint or exclusion. +/// Batching ordinary siblings in one cp invocation retains hardlinks between +/// those entries. Hardlinks crossing a mountpoint or exclusion split, or +/// crossing separate copies while merging into an existing destination +/// directory, may not remain linked. Untouched subtrees remain entirely under +/// cp -a's control. +fn copy_var_filtered( + src_dir: &CapStdDir, + dst_dir: &CapStdDir, + relative_dir: &Path, + exclusions: &[String], + mountpoints: &BTreeSet, + reflink: bool, + destination_created: bool, + labeler: Option<&dyn StagedLabeler>, + target_policy_available: bool, + cp_program: &OsStr, +) -> Result<()> { + // Existing directories are merged in place, so capture their own metadata + // before reading or copying children can alter their atime/mtime. The same + // wrapper restores source metadata on newly-created destination directories. + let source_metadata = if destination_created { + Some(snapshot_directory_metadata(src_dir).with_context(|| { + format!( + "Snapshotting source metadata for /var/{}", + relative_dir.display() + ) + })?) + } else { + None + }; + let destination_metadata = if destination_created { + None + } else { + Some(dst_dir.metadata(".").with_context(|| { + format!( + "Snapshotting existing destination timestamps for /var/{}", + relative_dir.display() + ) + })?) + }; + + let copy_result = copy_var_filtered_inner( + src_dir, + dst_dir, + relative_dir, + exclusions, + mountpoints, + reflink, + labeler, + target_policy_available, + cp_program, + ); + let restore_result = match (source_metadata.as_ref(), destination_metadata.as_ref()) { + (Some(metadata), _) => restore_directory_metadata(dst_dir, metadata), + (None, Some(metadata)) => restore_directory_timestamps(dst_dir, metadata), + (None, None) => Ok(()), + } + .with_context(|| { + format!( + "Restoring directory metadata for /var/{}", + relative_dir.display() + ) + }); + + match (copy_result, restore_result) { + (Ok(()), Ok(())) => Ok(()), + (Ok(()), Err(restore_error)) => Err(restore_error), + (Err(copy_error), Ok(())) => Err(copy_error), + (Err(copy_error), Err(restore_error)) => Err(copy_error.context(format!( + "Additionally failed to restore /var/{} metadata: {restore_error:#}", + relative_dir.display() + ))), + } +} + +fn copy_var_filtered_inner( + src_dir: &CapStdDir, + dst_dir: &CapStdDir, + relative_dir: &Path, + exclusions: &[String], + mountpoints: &BTreeSet, + reflink: bool, + labeler: Option<&dyn StagedLabeler>, + target_policy_available: bool, + cp_program: &OsStr, +) -> Result<()> { + let entries = src_dir + .read_dir(".") + .with_context(|| format!("Reading /var/{}", relative_dir.display()))?; + + let mut included_entries = Vec::new(); + for entry in entries { + let entry = entry.context("Reading entry in /var during migration")?; + let name = entry.file_name(); + let relative = relative_dir.join(&name); + + if exclusions + .iter() + .any(|excluded| Path::new(excluded) == relative) + { + println!(" Skipping {} (excluded)", relative.display()); + continue; + } + + if mountpoints.contains(&relative) { + println!( + " Creating empty mountpoint placeholder for {}", + relative.display() + ); + create_staged_placeholder( + src_dir, + dst_dir, + &name, + &relative, + labeler, + target_policy_available, + )?; + continue; + } + + let has_nested_mount = mountpoints + .iter() + .any(|mountpoint| mountpoint != &relative && mountpoint.starts_with(&relative)); + let has_nested_exclusion = exclusions.iter().any(|excluded| { + let excluded = Path::new(excluded); + excluded != relative && excluded.starts_with(&relative) + }); + let source_metadata = src_dir.symlink_metadata(&name)?; + if source_metadata.is_dir() { + let child_src = open_child_dir_nofollow(src_dir, &name) + .with_context(|| format!("Opening /var/{}", relative.display()))?; + // Use the opened directory as the source of truth for metadata. + // This keeps the directory metadata and xattrs tied to the same + // inode even if its parent entry is replaced after the open. + let existing_dst = open_existing_child_dir(dst_dir, &name)?; + if let Some(child_dst) = existing_dst { + copy_var_filtered( + &child_src, + &child_dst, + &relative, + exclusions, + mountpoints, + reflink, + false, + labeler, + target_policy_available, + cp_program, + )?; + } else if destination_entry_exists(dst_dir, &name)? { + // A destination of a different type is an existing entry, not + // permission to replace it with the source directory. + println!( + " Skipping destination collision at /var path {:?}", + relative + ); + continue; + } else if has_nested_mount || has_nested_exclusion { + copy_filtered_directory_staged( + &child_src, + dst_dir, + &name, + &relative, + exclusions, + mountpoints, + reflink, + labeler, + target_policy_available, + cp_program, + )?; + } else { + included_entries.push(name); + } + continue; + } + if destination_entry_exists(dst_dir, &name)? { + // Match cp --no-clobber without ever opening the existing object. + println!( + " Skipping destination collision at /var path {:?}", + relative + ); + continue; + } + included_entries.push(name); + } + + if !included_entries.is_empty() { + let names = included_entries + .iter() + .map(|name| name.as_os_str()) + .collect::>(); + copy_batch_staged( + cp_program, + src_dir, + &names, + dst_dir, + reflink, + relative_dir, + mountpoints, + labeler, + )?; + } + + Ok(()) +} + +static NEXT_STAGING_ID: AtomicU64 = AtomicU64::new(0); +// Bound collision retries so a pathological destination cannot make staging +// allocation loop forever. The process id plus monotonic id makes collisions +// exceptional; one hundred attempts leave room for stale entries while still +// failing promptly if the namespace is obstructed. +const STAGING_NAME_ATTEMPTS: u32 = 100; + +// Do not resolve privileged copy commands through a caller-controlled PATH. +// Lower-level helpers still accept an executable parameter for test injection. +const GNU_CP: &str = "/usr/bin/cp"; +const CP_REFLINK_OPTIONS: &[&str] = &["--reflink=auto", "-a"]; + +/// Make an empty, uniquely-named staging directory beneath the destination +/// capability. Publishing from the same parent keeps the final rename atomic. +fn create_staging_dir(parent: &CapStdDir) -> Result<(std::ffi::OsString, CapStdDir)> { + use rustix::fs::{Mode, mkdirat}; + + let parent_fd = parent.as_filelike_view::(); + for _ in 0..STAGING_NAME_ATTEMPTS { + let id = NEXT_STAGING_ID.fetch_add(1, Ordering::Relaxed); + let name = + std::ffi::OsString::from(format!(".bootc-preserve-var.{}.{}", std::process::id(), id)); + match mkdirat(parent_fd.as_fd(), &name, Mode::from_bits_truncate(0o700)) { + Ok(()) => match open_child_dir_nofollow(parent, &name) { + Ok(directory) => return Ok((name, directory)), + Err(error) => { + parent.remove_dir_all(&name).with_context(|| { + format!("Cleaning staging directory after open failure: {error}") + })?; + return Err(error).context("Opening /var migration staging directory"); + } + }, + Err(error) if error == rustix::io::Errno::EXIST => continue, + Err(error) => { + return Err(error).context("Creating /var migration staging directory"); + } + } + } + anyhow::bail!("Unable to allocate a unique /var migration staging directory") +} + +/// Ensure temporary staging entries are removed on both success and failure. +/// Any entries already published remain complete; there is no global rollback +/// of earlier successful publications. +fn with_staging_dir( + parent: &CapStdDir, + operation: impl FnOnce(&OsStr, &CapStdDir) -> Result, +) -> Result { + let (name, directory) = create_staging_dir(parent)?; + let result = operation(&name, &directory); + drop(directory); + let cleanup = parent + .remove_dir_all(&name) + .context("Removing /var migration staging data"); + match (result, cleanup) { + (Ok(value), Ok(())) => Ok(value), + (Err(error), Ok(())) => Err(error), + (Ok(_), Err(error)) => Err(error), + (Err(error), Err(cleanup_error)) => Err(error.context(format!( + "Additionally failed to clean migration staging data: {cleanup_error:#}" + ))), + } +} + +fn destination_entry_exists(parent: &CapStdDir, name: &OsStr) -> Result { + match parent.symlink_metadata(name) { + Ok(_) => Ok(true), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false), + Err(error) => Err(error.into()), + } +} + +fn publish_staged_entry( + staging: &CapStdDir, + destination: &CapStdDir, + name: &OsStr, + relative: &Path, +) -> Result { + use rustix::fs::{RenameFlags, renameat_with}; + + match renameat_with(staging, name, destination, name, RenameFlags::NOREPLACE) { + Ok(()) => Ok(true), + // A destination can appear after the initial no-clobber check. Merge + // staged directories into a same-type destination without replacing + // existing children; other collisions remain untouched. + Err(error) if error == rustix::io::Errno::EXIST => { + println!( + " Destination collision at /var path {:?}; keeping existing entries", + relative + ); + if staging.symlink_metadata(name)?.is_dir() { + if let Some(destination_dir) = open_existing_child_dir(destination, name)? { + let staging_dir = open_child_dir_nofollow(staging, name) + .context("Opening staged directory after publish collision")?; + merge_staged_directory(&staging_dir, &destination_dir, relative)?; + } + } + Ok(false) + } + Err(error) => Err(error.into()), + } +} + +/// The target used to apply a policy label to one staged inode. Regular files +/// and directories use an already-open fd; symlinks and other special files +/// use a no-follow path anchored at the opened parent directory. +enum StagedLabelTarget<'a> { + Fd(std::os::fd::BorrowedFd<'a>), + NoFollowPath(&'a CapStdDir, &'a OsStr), +} + +trait StagedLabeler { + fn lookup(&self, logical_path: &Path, mode: u32) -> Result>>; + fn apply(&self, logical_path: &Path, target: StagedLabelTarget<'_>, label: &[u8]) + -> Result<()>; +} + +struct TargetPolicyLabeler<'a> { + policy: &'a ostree_ext::ostree::SePolicy, +} + +impl StagedLabeler for TargetPolicyLabeler<'_> { + fn lookup(&self, logical_path: &Path, mode: u32) -> Result>> { + crate::lsm::optional_label_os(self.policy, logical_path, mode) + } + + fn apply( + &self, + _logical_path: &Path, + target: StagedLabelTarget<'_>, + label: &[u8], + ) -> Result<()> { + match target { + StagedLabelTarget::Fd(fd) => crate::lsm::set_security_selinux(fd, label), + StagedLabelTarget::NoFollowPath(parent, name) => { + let parent_fd = parent.as_filelike_view::(); + let mut path = PathBuf::from(format!("/proc/self/fd/{}", parent_fd.as_raw_fd())); + path.push(name); + rustix::fs::lsetxattr(path, SELINUX_XATTR, label, rustix::fs::XattrFlags::empty()) + .context("lsetxattr(security.selinux)")?; + Ok(()) + } + } + } +} + +const VAR_LOGICAL_ROOT: &str = "/var"; + +fn logical_var_path(relative: &Path) -> PathBuf { + Path::new(VAR_LOGICAL_ROOT).join(relative) +} + +type StagedLabels = HashMap>>; +type RegularFileLabels = HashMap<(libc::dev_t, libc::ino_t), (PathBuf, Vec)>; + +fn collect_label( + labeler: &dyn StagedLabeler, + logical_path: &Path, + mode: u32, + regular_file_id: Option<(libc::dev_t, libc::ino_t)>, + allow_missing_label: bool, + labels: &mut StagedLabels, + regular_file_labels: &mut RegularFileLabels, +) -> Result<()> { + let label = labeler.lookup(logical_path, mode)?; + let Some(label) = label else { + anyhow::ensure!( + allow_missing_label, + "No SELinux policy label for {logical_path:?}" + ); + labels.insert(logical_path.to_owned(), None); + return Ok(()); + }; + + if let Some(file_id) = regular_file_id { + if let Some((alias_path, alias_label)) = regular_file_labels.get(&file_id) { + anyhow::ensure!( + alias_label == &label, + "Hardlinked staged regular files {alias_path:?} and {logical_path:?} resolve to different SELinux labels" + ); + } else { + regular_file_labels.insert(file_id, (logical_path.to_owned(), label.clone())); + } + } + labels.insert(logical_path.to_owned(), Some(label)); + Ok(()) +} + +fn apply_staged_label( + labeler: &dyn StagedLabeler, + labels: &StagedLabels, + logical_path: &Path, + target: StagedLabelTarget<'_>, +) -> Result<()> { + let label = labels + .get(logical_path) + .with_context(|| format!("No collected SELinux label for {logical_path:?}"))?; + if let Some(label) = label { + labeler.apply(logical_path, target, label) + } else { + Ok(()) + } +} + +/// Relabel only staged entries. Logical lookup paths are composed from the +/// source-relative path, never from the private staging directory name. +fn relabel_staged_names( + labeler: &dyn StagedLabeler, + staging: &CapStdDir, + names: &[&OsStr], + relative_dir: &Path, + mountpoints: &BTreeSet, +) -> Result<()> { + let staging_device = staging.metadata(".")?.dev(); + let mut labels = StagedLabels::new(); + let mut regular_file_labels = RegularFileLabels::new(); + for name in names { + let relative_path = relative_dir.join(name); + collect_staged_entry_labels( + labeler, + staging, + name, + &logical_var_path(&relative_path), + mountpoints, + Some(staging_device), + false, + &mut labels, + &mut regular_file_labels, + )?; + } + for name in names { + let relative_path = relative_dir.join(name); + apply_staged_entry_labels( + labeler, + staging, + name, + &logical_var_path(&relative_path), + mountpoints, + Some(staging_device), + false, + &labels, + )?; + } + Ok(()) +} + +/// Look up labels for staged entries without applying them. Regular-file inode +/// identities are grouped across this staging tree so aliases must agree +/// before any labels are changed. +fn collect_staged_entry_labels( + labeler: &dyn StagedLabeler, + parent: &CapStdDir, + name: &OsStr, + logical_path: &Path, + mountpoints: &BTreeSet, + root_device: Option, + stop_at_entry: bool, + labels: &mut StagedLabels, + regular_file_labels: &mut RegularFileLabels, +) -> Result<()> { + use rustix::fs::{Mode, OFlags, openat}; + + let metadata = parent + .symlink_metadata(name) + .with_context(|| format!("Inspecting staged /var entry {logical_path:?} before relabel"))?; + if metadata.is_dir() { + let directory = open_child_dir_nofollow(parent, name) + .with_context(|| format!("Opening staged directory {logical_path:?}"))?; + let metadata = directory.metadata(".")?; + let device = root_device.unwrap_or_else(|| metadata.dev()); + let relative = logical_path + .strip_prefix(VAR_LOGICAL_ROOT) + .with_context(|| format!("Invalid logical migration path {logical_path:?}"))?; + let is_placeholder = mountpoints.contains(relative); + let is_empty_placeholder = if is_placeholder { + let mut entries = directory + .read_dir(".") + .with_context(|| format!("Reading staged mountpoint directory {logical_path:?}"))?; + entries + .next() + .transpose() + .with_context(|| format!("Reading staged mountpoint directory {logical_path:?}"))? + .is_none() + } else { + false + }; + collect_label( + labeler, + logical_path, + metadata.mode(), + None, + is_empty_placeholder, + labels, + regular_file_labels, + )?; + + if stop_at_entry || metadata.dev() != device || mountpoints.contains(relative) { + return Ok(()); + } + for entry in directory.read_dir(".")? { + let entry = entry?; + let child_name = entry.file_name(); + collect_staged_entry_labels( + labeler, + &directory, + &child_name, + &logical_path.join(&child_name), + mountpoints, + Some(device), + false, + labels, + regular_file_labels, + )?; + } + return Ok(()); + } + + if metadata.is_symlink() { + return collect_label( + labeler, + logical_path, + metadata.mode(), + None, + false, + labels, + regular_file_labels, + ); + } + + if metadata.is_file() { + let fd = openat( + parent.as_filelike_view::().as_fd(), + name, + OFlags::CLOEXEC | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::RDONLY, + Mode::empty(), + ) + .with_context(|| format!("Opening staged file {logical_path:?} without following links"))?; + let stat = rustix::fs::fstat(&fd)?; + anyhow::ensure!( + stat.st_mode & libc::S_IFMT == libc::S_IFREG, + "Staged entry {logical_path:?} changed type before relabel" + ); + return collect_label( + labeler, + logical_path, + stat.st_mode, + Some((stat.st_dev, stat.st_ino)), + stat.st_size == 0 + && logical_path + .strip_prefix(VAR_LOGICAL_ROOT) + .is_ok_and(|relative| mountpoints.contains(relative)), + labels, + regular_file_labels, + ); + } + + collect_label( + labeler, + logical_path, + metadata.mode(), + None, + false, + labels, + regular_file_labels, + ) +} + +/// Apply previously collected labels to staged entries without following +/// symlinks. This is intentionally a separate walk from label lookup so all +/// hardlink aliases can be checked before the first inode is relabeled. +fn apply_staged_entry_labels( + labeler: &dyn StagedLabeler, + parent: &CapStdDir, + name: &OsStr, + logical_path: &Path, + mountpoints: &BTreeSet, + root_device: Option, + stop_at_entry: bool, + labels: &StagedLabels, +) -> Result<()> { + use rustix::fs::{Mode, OFlags, openat}; + + let metadata = parent + .symlink_metadata(name) + .with_context(|| format!("Inspecting staged /var entry {logical_path:?} before relabel"))?; + if metadata.is_dir() { + let directory = open_child_dir_nofollow(parent, name) + .with_context(|| format!("Opening staged directory {logical_path:?}"))?; + let metadata = directory.metadata(".")?; + let device = root_device.unwrap_or_else(|| metadata.dev()); + let fd = directory.as_filelike_view::(); + apply_staged_label( + labeler, + labels, + logical_path, + StagedLabelTarget::Fd(fd.as_fd()), + )?; + + let relative = logical_path + .strip_prefix(VAR_LOGICAL_ROOT) + .with_context(|| format!("Invalid logical migration path {logical_path:?}"))?; + if stop_at_entry || metadata.dev() != device || mountpoints.contains(relative) { + return Ok(()); + } + for entry in directory.read_dir(".")? { + let entry = entry?; + let child_name = entry.file_name(); + apply_staged_entry_labels( + labeler, + &directory, + &child_name, + &logical_path.join(&child_name), + mountpoints, + Some(device), + false, + labels, + )?; + } + return Ok(()); + } + + if metadata.is_symlink() { + return apply_staged_label( + labeler, + labels, + logical_path, + StagedLabelTarget::NoFollowPath(parent, name), + ); + } + + if metadata.is_file() { + let fd = openat( + parent.as_filelike_view::().as_fd(), + name, + OFlags::CLOEXEC | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::RDONLY, + Mode::empty(), + ) + .with_context(|| format!("Opening staged file {logical_path:?} without following links"))?; + let stat = rustix::fs::fstat(&fd)?; + anyhow::ensure!( + stat.st_mode & libc::S_IFMT == libc::S_IFREG, + "Staged entry {logical_path:?} changed type before relabel" + ); + return apply_staged_label( + labeler, + labels, + logical_path, + StagedLabelTarget::Fd(fd.as_fd()), + ); + } + + apply_staged_label( + labeler, + labels, + logical_path, + StagedLabelTarget::NoFollowPath(parent, name), + ) +} + +fn relabel_staged_entry( + labeler: &dyn StagedLabeler, + parent: &CapStdDir, + name: &OsStr, + logical_path: &Path, + mountpoints: &BTreeSet, + root_device: Option, + stop_at_entry: bool, +) -> Result<()> { + let mut labels = StagedLabels::new(); + let mut regular_file_labels = RegularFileLabels::new(); + collect_staged_entry_labels( + labeler, + parent, + name, + logical_path, + mountpoints, + root_device, + stop_at_entry, + &mut labels, + &mut regular_file_labels, + )?; + apply_staged_entry_labels( + labeler, + parent, + name, + logical_path, + mountpoints, + root_device, + stop_at_entry, + &labels, + ) +} + +/// Move staged children into an existing destination directory without +/// replacing entries already present there. Keep the destination directory's +/// metadata unchanged; moving children updates its timestamps, so restore its +/// original metadata after the merge. +fn merge_staged_directory( + staging: &CapStdDir, + destination: &CapStdDir, + relative_dir: &Path, +) -> Result<()> { + let metadata = snapshot_directory_metadata(destination)?; + let names = staging + .read_dir(".")? + .map(|entry| entry.map(|entry| entry.file_name())) + .collect::>>()?; + + let merge_result = (|| { + for name in names { + publish_staged_entry(staging, destination, &name, &relative_dir.join(&name))?; + } + Ok(()) + })(); + let restore_result = restore_directory_metadata(destination, &metadata); + + match (merge_result, restore_result) { + (Ok(()), Ok(())) => Ok(()), + (Ok(()), Err(error)) => Err(error), + (Err(error), Ok(())) => Err(error), + (Err(error), Err(restore_error)) => Err(error.context(format!( + "Additionally failed to restore merged destination directory metadata: {restore_error:#}" + ))), + } +} + +fn copy_batch_staged( + cp_program: &OsStr, + source: &CapStdDir, + names: &[&OsStr], + destination: &CapStdDir, + reflink: bool, + relative_dir: &Path, + mountpoints: &BTreeSet, + labeler: Option<&dyn StagedLabeler>, +) -> Result<()> { + with_staging_dir(destination, |_, staging| { + let options = if reflink { + CP_REFLINK_OPTIONS + } else { + &["-a"][..] + }; + let status = run_cp( + cp_program, + source, + names, + staging, + None, + options, + Stdio::inherit(), + Stdio::inherit(), + )?; + anyhow::ensure!( + status.success(), + "cp failed while copying /var/{}", + relative_dir.display() + ); + + if let Some(labeler) = labeler { + relabel_staged_names(labeler, staging, names, relative_dir, mountpoints)?; + } + + for name in names { + publish_staged_entry(staging, destination, name, &relative_dir.join(name))?; + } + Ok(()) + }) +} + +fn copy_filtered_directory_staged( + source: &CapStdDir, + destination_parent: &CapStdDir, + name: &OsStr, + relative: &Path, + exclusions: &[String], + mountpoints: &BTreeSet, + reflink: bool, + labeler: Option<&dyn StagedLabeler>, + target_policy_available: bool, + cp_program: &OsStr, +) -> Result<()> { + with_staging_dir(destination_parent, |_, staging| { + staging.create_dir(name)?; + let destination = open_child_dir_nofollow(staging, name)?; + copy_var_filtered( + source, + &destination, + relative, + exclusions, + mountpoints, + reflink, + true, + None, + target_policy_available, + cp_program, + )?; + if let Some(labeler) = labeler { + relabel_staged_entry( + labeler, + staging, + name, + &logical_var_path(relative), + mountpoints, + Some(staging.metadata(".")?.dev()), + false, + )?; + } + publish_staged_entry(staging, destination_parent, name, relative)?; + Ok(()) + }) +} + +fn create_staged_placeholder( + src_parent: &CapStdDir, + dst_parent: &CapStdDir, + name: &OsStr, + relative: &Path, + labeler: Option<&dyn StagedLabeler>, + target_policy_available: bool, +) -> Result<()> { + with_staging_dir(dst_parent, |_, staging| { + create_mountpoint_placeholder(src_parent, staging, name, target_policy_available)?; + if let Some(labeler) = labeler { + relabel_staged_entry( + labeler, + staging, + name, + &logical_var_path(relative), + &BTreeSet::from([relative.to_owned()]), + Some(staging.metadata(".")?.dev()), + true, + )?; + } + if !publish_staged_entry(staging, dst_parent, name, relative)? { + validate_existing_placeholder( + dst_parent, + name, + relative, + src_parent.symlink_metadata(name)?, + )?; + } + Ok(()) + }) +} + +fn validate_existing_placeholder( + destination_parent: &CapStdDir, + name: &OsStr, + relative: &Path, + source_metadata: Metadata, +) -> Result<()> { + let destination_metadata = destination_parent.symlink_metadata(name)?; + let same_type = (source_metadata.is_dir() && destination_metadata.is_dir()) + || (source_metadata.is_file() && destination_metadata.is_file()); + anyhow::ensure!( + same_type, + "Mountpoint placeholder /var/{} already exists in the new deployment with a different type", + relative.display() + ); + let has_content = if destination_metadata.is_dir() { + open_child_dir_nofollow(destination_parent, name)? + .entries()? + .next() + .is_some() + } else { + destination_metadata.len() > 0 + }; + if has_content { + println!( + " Keeping the image's content at mountpoint /var/{}; the mount hides it at boot", + relative.display() + ); + } + Ok(()) +} + +fn create_mountpoint_placeholder( + src_parent: &CapStdDir, + dst_parent: &CapStdDir, + name: &OsStr, + target_policy_available: bool, +) -> Result<()> { + let metadata = src_parent.symlink_metadata(name)?; + if metadata.is_dir() { + let src_dir = open_child_dir_nofollow(src_parent, name) + .context("Opening mountpoint source directory")?; + let directory_metadata = src_dir + .metadata(".") + .context("Reading mountpoint source directory metadata")?; + dst_parent.create_dir(name).with_context(|| { + format!( + "Creating mountpoint directory placeholder {}", + name.to_string_lossy() + ) + })?; + let dst_dir = open_child_dir_nofollow(dst_parent, name) + .context("Opening mountpoint directory placeholder")?; + preserve_directory_metadata( + &src_dir, + &dst_dir, + &directory_metadata, + target_policy_available, + ) + .context("Preserving mountpoint directory metadata")?; + return Ok(()); + } + anyhow::ensure!( + metadata.is_file(), + "Cannot create safe placeholder for non-file mountpoint {}", + name.to_string_lossy() + ); + use cap_std_ext::cap_std::fs::OpenOptions; + let dst_file = dst_parent + .open_with(name, OpenOptions::new().write(true).create_new(true)) + .context("Creating empty mountpoint file placeholder")?; + preserve_metadata( + src_parent, + name, + &metadata, + dst_file.as_filelike_view::().as_fd(), + target_policy_available, + ) + .context("Preserving mountpoint file metadata") +} + +/// Reproduce metadata for a directory which was split into filtered children +/// instead of being copied by `cp -a` as one subtree. +fn preserve_directory_metadata( + src_dir: &CapStdDir, + dst_dir: &CapStdDir, + metadata: &Metadata, + target_policy_available: bool, +) -> Result<()> { + let mut snapshot = snapshot_directory_metadata_from_source(src_dir, metadata)?; + snapshot + .xattrs + .retain(|(name, _)| should_copy_placeholder_xattr(name, target_policy_available)); + restore_directory_metadata(dst_dir, &snapshot) +} + +struct DirectoryMetadataSnapshot { + metadata: Metadata, + xattrs: Vec<(std::ffi::OsString, Vec)>, +} + +fn snapshot_directory_metadata(directory: &CapStdDir) -> Result { + let metadata = directory.metadata(".")?; + snapshot_directory_metadata_from_source(directory, &metadata) +} + +fn snapshot_directory_metadata_from_source( + directory: &CapStdDir, + metadata: &Metadata, +) -> Result { + use cap_std_ext::dirext::CapStdExtDirExt; + + let mut xattrs = Vec::new(); + for attribute in directory.listxattrs(".")?.iter() { + if let Some(value) = directory.getxattr(".", attribute)? { + xattrs.push((attribute.to_os_string(), value)); + } + } + Ok(DirectoryMetadataSnapshot { + metadata: metadata.clone(), + xattrs, + }) +} + +fn restore_directory_metadata( + directory: &CapStdDir, + snapshot: &DirectoryMetadataSnapshot, +) -> Result<()> { + use rustix::fs::{Mode, fchmod}; + + let dst_fd = open_directory_for_metadata(directory)?; + let mut errors = Vec::new(); + if let Err(error) = preserve_metadata_owner(&snapshot.metadata, &dst_fd) { + errors.push(format!("owner/group: {error:#}")); + } + if let Err(error) = fchmod( + &dst_fd, + Mode::from_bits_truncate(snapshot.metadata.mode() & 0o7777), + ) { + errors.push(format!("mode: {error:#}")); + } + if let Err(error) = apply_xattrs_exact(&dst_fd, &snapshot.xattrs) { + errors.push(format!("xattrs: {error:#}")); + } + if let Err(error) = preserve_metadata_timestamps(&snapshot.metadata, &dst_fd) { + errors.push(format!("timestamps: {error:#}")); + } + if !errors.is_empty() { + anyhow::bail!("restoring directory metadata failed: {}", errors.join("; ")); + } + Ok(()) +} + +fn restore_directory_timestamps(directory: &CapStdDir, metadata: &Metadata) -> Result<()> { + let dst_fd = open_directory_for_metadata(directory)?; + preserve_metadata_timestamps(metadata, &dst_fd) +} + +/// Open a usable fd for fd-based metadata operations while remaining anchored +/// to the pinned directory capability. CapStdDir may hold an O_PATH fd, which +/// cannot be used with fchmod, xattr, or timestamp operations. +fn open_directory_for_metadata(directory: &CapStdDir) -> Result { + use rustix::fs::{Mode, OFlags, openat}; + + let directory_file = directory.as_filelike_view::(); + Ok(openat( + directory_file.as_fd(), + ".", + OFlags::CLOEXEC | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::RDONLY, + Mode::empty(), + )?) +} + +/// Preserve ownership, mode, timestamps, and available xattrs (including ACL +/// and SELinux labels) on a newly-created destination object. Callers only use +/// this for objects created by this migration, preserving the no-clobber rule. +/// For mountpoint file placeholders, source metadata and xattrs still use the +/// parent/name pair captured before the destination is opened; replacing that +/// source entry concurrently can therefore race those reads. Destination +/// updates remain anchored to the opened destination fd. Split directory +/// metadata and xattrs instead use the opened source directory handle. +fn preserve_metadata( + src_parent: &CapStdDir, + src_name: &OsStr, + metadata: &Metadata, + dst_fd: Fd, + target_policy_available: bool, +) -> Result<()> { + use cap_std_ext::dirext::CapStdExtDirExt; + + let mut source_xattrs = Vec::new(); + for attribute in src_parent.listxattrs(src_name)?.iter() { + let attribute_name = attribute.to_os_string(); + if !should_copy_placeholder_xattr(&attribute_name, target_policy_available) { + continue; + } + if let Some(value) = src_parent.getxattr(src_name, attribute)? { + source_xattrs.push((attribute.to_os_string(), value)); + } + } + preserve_metadata_owner_and_mode(metadata, &dst_fd)?; + apply_xattrs_exact(&dst_fd, &source_xattrs)?; + preserve_metadata_timestamps(metadata, &dst_fd) +} + +const SELINUX_XATTR: &str = "security.selinux"; + +fn should_copy_placeholder_xattr(name: &OsStr, target_policy_available: bool) -> bool { + !target_policy_available || name != OsStr::new(SELINUX_XATTR) +} + +/// Make the destination xattr set match the source snapshot, except that a +/// destination SELinux label is retained when the source has no label. +/// Removing extras first matters for inherited ACLs and labels as well as user +/// xattrs. +fn apply_xattrs_exact( + destination: Fd, + source_xattrs: &[(std::ffi::OsString, Vec)], +) -> Result<()> { + use rustix::fs::{fremovexattr, fsetxattr}; + use std::collections::BTreeSet; + + let destination_names = list_fd_xattrs(&destination)?; + let source_names = source_xattrs + .iter() + .map(|(name, _)| name.as_os_str()) + .collect::>(); + for name in destination_names { + if !source_names.contains(name.as_os_str()) && name.as_os_str() != OsStr::new(SELINUX_XATTR) + { + fremovexattr(&destination, &name).with_context(|| { + format!("Removing destination xattr {}", name.to_string_lossy()) + })?; + } + } + for (name, value) in source_xattrs { + fsetxattr(&destination, name, value, rustix::fs::XattrFlags::empty()) + .with_context(|| format!("Setting source xattr {}", name.to_string_lossy()))?; + } + Ok(()) +} + +fn list_fd_xattrs(fd: Fd) -> Result> { + use std::os::unix::ffi::OsStringExt; + + // Linux caps the complete xattr-name list at XATTR_LIST_MAX (64 KiB). + let mut names = vec![0; 64 * 1024]; + let length = rustix::fs::flistxattr(fd, &mut names[..])?; + names.truncate(length); + if names.is_empty() { + return Ok(Vec::new()); + } + anyhow::ensure!(names.last() == Some(&0), "Malformed xattr name list"); + names.pop(); + names + .split(|byte| *byte == 0) + .filter(|name| !name.is_empty()) + .map(|name| Ok(std::ffi::OsString::from_vec(name.to_vec()))) + .collect() +} + +fn preserve_metadata_owner_and_mode(metadata: &Metadata, dst_fd: Fd) -> Result<()> { + use rustix::fs::{Mode, fchmod}; + + preserve_metadata_owner(metadata, &dst_fd)?; + fchmod(&dst_fd, Mode::from_bits_truncate(metadata.mode() & 0o7777))?; + Ok(()) +} + +fn preserve_metadata_owner(metadata: &Metadata, dst_fd: Fd) -> Result<()> { + use rustix::fs::{Gid, Uid, fchown}; + + let destination_metadata = rustix::fs::fstat(&dst_fd)?; + if destination_metadata.st_uid != metadata.uid() + || destination_metadata.st_gid != metadata.gid() + { + fchown( + &dst_fd, + Some(Uid::from_raw(metadata.uid())), + Some(Gid::from_raw(metadata.gid())), + )?; + } + Ok(()) +} + +fn preserve_metadata_timestamps(metadata: &Metadata, dst_fd: Fd) -> Result<()> { + use rustix::fs::{Timestamps, futimens}; + use rustix::time::Timespec; + + futimens( + &dst_fd, + &Timestamps { + last_access: Timespec { + tv_sec: metadata.atime(), + tv_nsec: metadata.atime_nsec() as _, + }, + last_modification: Timespec { + tv_sec: metadata.mtime(), + tv_nsec: metadata.mtime_nsec() as _, + }, + }, + )?; + Ok(()) +} + +#[cfg(test)] +#[context("Copying /var into new deployment (Strategy D)")] +fn preserve_var_copy( + src_var: &CapStdDir, + new_var: &CapStdDir, + exclusions: &[String], +) -> Result<()> { + preserve_var_copy_mounts(src_var, new_var, exclusions, &BTreeSet::new()) +} + +/// Strategy D: plain recursive copy of `/var` for filesystems without reflink support. +/// +/// This performs `cp -a` for included state and is **unsafe for databases and +/// other applications with open write handles into `/var`** (for example, +/// PostgreSQL in `/var/lib/pgsql`, MySQL/MariaDB in `/var/lib/mysql`, or SQLite +/// databases under `/var/lib/*`). A live database copy may be corrupted. +/// Stop stateful services before copying, or use a consistent snapshot. +/// Reflinks (Strategy C) also clone files individually and are not an atomic +/// snapshot of `/var` or a live database. +/// +/// Additional exclusions can be configured or supplied with repeated +/// `--preserve-var-skip` options. Paths are relative to `/var`, with one +/// component or exactly one child component allowed. Excluded state is not +/// migrated and must be handled separately. Exclusions do not make a copied +/// live database consistent. +/// +/// See: +#[cfg(test)] +fn preserve_var_copy_mounts( + src_var: &CapStdDir, + new_var: &CapStdDir, + exclusions: &[String], + mountpoints: &BTreeSet, +) -> Result<()> { + preserve_var_copy_mounts_labeled(src_var, new_var, exclusions, mountpoints, None) +} + +fn preserve_var_copy_mounts_labeled( + src_var: &CapStdDir, + new_var: &CapStdDir, + exclusions: &[String], + mountpoints: &BTreeSet, + labeler: Option<&dyn StagedLabeler>, +) -> Result<()> { + copy_var_filtered( + src_var, + new_var, + Path::new(""), + exclusions, + mountpoints, + false, + false, + labeler, + labeler.is_some(), + OsStr::new(GNU_CP), + ) + .context("Copying /var into new deployment (Strategy D)")?; + println!(" /var copy complete."); + Ok(()) +} + +/// Open a single source child directory without following a symlink swapped +/// into place after the caller's metadata check. The entry name comes directly +/// from `read_dir`, so this is one path component beneath the capability root. +fn open_child_dir_nofollow(parent: &CapStdDir, name: &OsStr) -> std::io::Result { + use rustix::fs::{Mode, OFlags}; + + let fd = rustix::fs::openat( + parent.as_filelike_view::().as_fd(), + name, + OFlags::CLOEXEC | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::RDONLY, + Mode::empty(), + )?; + Ok(CapStdDir::from_std_file(std::fs::File::from(fd))) +} + +#[cfg(test)] +fn open_or_create_child_dir(parent: &CapStdDir, name: &OsStr) -> Result<(CapStdDir, bool)> { + let created = match parent.create_dir(name) { + Ok(()) => true, + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + anyhow::ensure!( + parent.symlink_metadata(name)?.is_dir(), + "Destination child {} exists and is not a directory", + name.to_string_lossy() + ); + false + } + Err(e) => return Err(e).context("Creating destination child directory"), + }; + let dir = + open_child_dir_nofollow(parent, name).context("Opening destination child directory")?; + Ok((dir, created)) +} + +/// Open an existing destination directory without following symlinks. The +/// opened handle pins the directory used for the recursive merge. +fn open_existing_child_dir(parent: &CapStdDir, name: &OsStr) -> Result> { + let existing: Result> = match open_child_dir_nofollow(parent, name) { + Ok(directory) => Ok(Some(directory)), + Err(error) + if matches!( + error.kind(), + std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory + ) || error.raw_os_error() == Some(libc::ELOOP) => + { + Ok(None) + } + Err(error) => Err(error.into()), + }; + existing.with_context(|| { + format!( + "Opening existing destination directory {}", + name.to_string_lossy() + ) + }) +} + +/// Validate exclusions before touching either tree. Paths are relative to `/var`. +pub(crate) fn validate_exclusions(exclusions: &[String]) -> Result> { + exclusions + .iter() + .map(|path| { + let path = path.trim_end_matches('/'); + anyhow::ensure!(!path.is_empty(), "empty /var exclusion"); + anyhow::ensure!( + Path::new(path) + .components() + .all(|component| matches!(component, Component::Normal(_))), + "invalid /var exclusion {path:?}; expected a relative path" + ); + anyhow::ensure!( + Path::new(path).components().count() <= 2, + "unsupported nested /var exclusion {path:?}; use at most one child path" + ); + Ok(path.to_string()) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + use cap_std_ext::dirext::CapStdExtDirExt; + use std::{ + fs, + os::unix::{ + ffi::{OsStrExt, OsStringExt}, + fs::{MetadataExt as StdMetadataExt, PermissionsExt, symlink}, + }, + sync::Mutex, + time::{Duration, SystemTime}, + }; + + #[derive(Default)] + struct FakeLabeler { + lookups: Mutex>, + applied: Mutex>, + no_match: BTreeSet, + fail_lookup: bool, + fail_apply: bool, + different_labels: bool, + } + + impl StagedLabeler for FakeLabeler { + fn lookup(&self, logical_path: &Path, _mode: u32) -> Result>> { + self.lookups.lock().unwrap().push(logical_path.to_owned()); + anyhow::ensure!( + logical_path.starts_with(VAR_LOGICAL_ROOT), + "policy lookup used non-logical path {logical_path:?}" + ); + anyhow::ensure!(!self.fail_lookup, "injected SELinux policy lookup failure"); + if self.no_match.contains(logical_path) { + return Ok(None); + } + let label = if self.different_labels && logical_path == Path::new("/var/alias-b") { + b"different-label".to_vec() + } else { + b"test-label".to_vec() + }; + Ok(Some(label)) + } + + fn apply( + &self, + logical_path: &Path, + _target: StagedLabelTarget<'_>, + _label: &[u8], + ) -> Result<()> { + self.applied.lock().unwrap().push(logical_path.to_owned()); + anyhow::ensure!( + !self.fail_apply, + "injected SELinux label application failure" + ); + Ok(()) + } + } + + fn cap_dir(path: &Path) -> CapStdDir { + CapStdDir::open_ambient_dir(path, ambient_authority()).unwrap() + } + + fn cap_dir_path(path: &Path) -> CapStdDir { + use rustix::fs::{Mode, OFlags, openat}; + + let directory = cap_dir(path); + let fd = openat( + directory.as_filelike_view::().as_fd(), + ".", + OFlags::CLOEXEC | OFlags::DIRECTORY | OFlags::PATH, + Mode::empty(), + ) + .unwrap(); + CapStdDir::from_std_file(std::fs::File::from(fd)) + } + + #[test] + fn relabels_only_staged_entries_with_logical_paths_for_both_copy_strategies() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + fs::create_dir_all(src_path.join("state"))?; + fs::write(src_path.join("state/existing"), b"source collision")?; + fs::write(src_path.join("state/new"), b"new source state")?; + let non_utf8_name = std::ffi::OsString::from_vec(vec![b'n', 0xff]); + fs::write(src_path.join(&non_utf8_name), b"non-UTF8 name")?; + symlink("state", src_path.join("link-to-state"))?; + let source = cap_dir(&src_path); + + for (strategy, reflink) in [("copy", false), ("reflink", true)] { + let dst_path = temp.path().join(format!("dst-{strategy}")); + fs::create_dir_all(dst_path.join("state"))?; + fs::write(dst_path.join("state/existing"), b"seeded winner")?; + let destination = cap_dir(&dst_path); + let labeler = FakeLabeler::default(); + if reflink { + preserve_var_reflink_mounts_labeled( + &source, + &destination, + &[], + &BTreeSet::new(), + Some(&labeler), + )?; + } else { + preserve_var_copy_mounts_labeled( + &source, + &destination, + &[], + &BTreeSet::new(), + Some(&labeler), + )?; + } + + assert_eq!(fs::read(dst_path.join("state/existing"))?, b"seeded winner"); + let lookups = labeler.lookups.lock().unwrap(); + assert!( + lookups + .iter() + .all(|path| path.starts_with(VAR_LOGICAL_ROOT)) + ); + assert!(lookups.iter().all(|path| { + !path + .as_os_str() + .as_bytes() + .windows(b".bootc-preserve-var.".len()) + .any(|window| window == b".bootc-preserve-var.") + })); + assert!(lookups.contains(&PathBuf::from("/var/state/new"))); + assert!(!lookups.contains(&PathBuf::from("/var/state/existing"))); + assert!(!lookups.contains(&PathBuf::from("/var/state"))); + assert!(lookups.contains(&PathBuf::from("/var/link-to-state"))); + assert!(!lookups.contains(&PathBuf::from("/var/link-to-state/new"))); + assert!( + lookups + .iter() + .any(|path| path.as_os_str().as_bytes().ends_with(&[b'n', 0xff])) + ); + } + Ok(()) + } + + #[test] + fn unmatched_mount_placeholders_are_allowed_without_walking_into_them() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir_all(src_path.join("lib/nfs/rpc_pipefs"))?; + fs::write( + src_path.join("lib/nfs/rpc_pipefs/mounted-only"), + b"not copied", + )?; + fs::create_dir_all(src_path.join("tree"))?; + fs::write(src_path.join("tree/file-bound"), b"")?; + fs::write(src_path.join("tree/sibling"), b"copied")?; + fs::create_dir(&dst_path)?; + let source = cap_dir(&src_path); + let destination = cap_dir(&dst_path); + let labeler = FakeLabeler { + no_match: BTreeSet::from([ + PathBuf::from("/var/lib/nfs/rpc_pipefs"), + PathBuf::from("/var/tree/file-bound"), + ]), + ..FakeLabeler::default() + }; + let mountpoints = BTreeSet::from([ + PathBuf::from("lib/nfs/rpc_pipefs"), + PathBuf::from("tree/file-bound"), + ]); + + preserve_var_copy_mounts_labeled(&source, &destination, &[], &mountpoints, Some(&labeler))?; + + assert_eq!( + fs::read_dir(dst_path.join("lib/nfs/rpc_pipefs"))?.count(), + 0 + ); + assert_eq!(fs::read(dst_path.join("tree/file-bound"))?, b""); + let lookups = labeler.lookups.lock().unwrap(); + assert!(lookups.contains(&PathBuf::from("/var/lib/nfs/rpc_pipefs"))); + assert!(lookups.contains(&PathBuf::from("/var/tree"))); + assert!(lookups.contains(&PathBuf::from("/var/tree/sibling"))); + assert!(lookups.contains(&PathBuf::from("/var/tree/file-bound"))); + assert!(!lookups.contains(&PathBuf::from("/var/lib/nfs/rpc_pipefs/mounted-only"))); + let applied = labeler.applied.lock().unwrap(); + assert!(!applied.contains(&PathBuf::from("/var/lib/nfs/rpc_pipefs"))); + assert!(!applied.contains(&PathBuf::from("/var/tree/file-bound"))); + + let matched_dst_path = temp.path().join("dst-matched"); + fs::create_dir(&matched_dst_path)?; + let matched_labeler = FakeLabeler::default(); + preserve_var_copy_mounts_labeled( + &source, + &cap_dir(&matched_dst_path), + &[], + &mountpoints, + Some(&matched_labeler), + )?; + let applied = matched_labeler.applied.lock().unwrap(); + assert!(applied.contains(&PathBuf::from("/var/lib/nfs/rpc_pipefs"))); + assert!(applied.contains(&PathBuf::from("/var/tree/file-bound"))); + assert!(!applied.contains(&PathBuf::from("/var/lib/nfs/rpc_pipefs/mounted-only"))); + Ok(()) + } + + #[test] + fn unmatched_nonempty_mountpoint_directory_fails_before_publish() -> Result<()> { + let temp = tempfile::tempdir()?; + let destination_path = temp.path().join("destination"); + fs::create_dir(&destination_path)?; + let destination = cap_dir(&destination_path); + let labeler = FakeLabeler { + no_match: BTreeSet::from([PathBuf::from("/var/lib/nfs/rpc_pipefs")]), + ..FakeLabeler::default() + }; + let mountpoints = BTreeSet::from([PathBuf::from("lib/nfs/rpc_pipefs")]); + + let error = with_staging_dir(&destination, |_, staging| { + staging.create_dir_all("lib/nfs/rpc_pipefs")?; + staging.write("lib/nfs/rpc_pipefs/unexpected-child", b"not empty")?; + + let names = [OsStr::new("lib")]; + relabel_staged_names(&labeler, staging, &names, Path::new(""), &mountpoints)?; + for name in names { + publish_staged_entry(staging, &destination, name, Path::new(name))?; + } + Ok(()) + }) + .unwrap_err(); + + assert!( + format!("{error:#}") + .contains("No SELinux policy label for \"/var/lib/nfs/rpc_pipefs\"") + ); + assert_eq!(fs::read_dir(&destination_path)?.count(), 0); + Ok(()) + } + + #[test] + fn unmatched_label_for_ordinary_files_fails_before_publish() -> Result<()> { + let temp = tempfile::tempdir()?; + for (name, contents) in [ + ("ordinary-empty", b"".as_slice()), + ("ordinary-data", b"data"), + ] { + let src_path = temp.path().join(format!("src-{name}")); + let dst_path = temp.path().join(format!("dst-{name}")); + fs::create_dir(&src_path)?; + fs::create_dir(&dst_path)?; + fs::write(src_path.join(name), contents)?; + let labeler = FakeLabeler { + no_match: BTreeSet::from([PathBuf::from(format!("/var/{name}"))]), + ..FakeLabeler::default() + }; + + let error = preserve_var_copy_mounts_labeled( + &cap_dir(&src_path), + &cap_dir(&dst_path), + &[], + &BTreeSet::new(), + Some(&labeler), + ) + .unwrap_err(); + + assert!( + format!("{error:#}") + .contains(&format!("No SELinux policy label for \"/var/{name}\"")) + ); + assert_eq!(fs::read_dir(dst_path)?.count(), 0); + } + Ok(()) + } + + #[test] + fn label_failure_cleans_stage_and_does_not_publish_for_both_strategies() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + fs::create_dir(&src_path)?; + fs::write(src_path.join("new-entry"), b"state")?; + let source = cap_dir(&src_path); + + for (failure, fail_lookup, fail_apply) in [("lookup", true, false), ("apply", false, true)] + { + for (strategy, reflink) in [("copy", false), ("reflink", true)] { + let dst_path = temp.path().join(format!("dst-{failure}-{strategy}")); + fs::create_dir(&dst_path)?; + let destination = cap_dir(&dst_path); + let labeler = FakeLabeler { + fail_lookup, + fail_apply, + ..FakeLabeler::default() + }; + let error = if reflink { + preserve_var_reflink_mounts_labeled( + &source, + &destination, + &[], + &BTreeSet::new(), + Some(&labeler), + ) + } else { + preserve_var_copy_mounts_labeled( + &source, + &destination, + &[], + &BTreeSet::new(), + Some(&labeler), + ) + } + .unwrap_err(); + let expected_error = if fail_lookup { + "SELinux policy lookup failure" + } else { + "SELinux label application failure" + }; + assert!(format!("{error:#}").contains(expected_error)); + assert_eq!(fs::read_dir(&dst_path)?.count(), 0); + } + } + Ok(()) + } + + #[test] + fn hardlinked_paths_require_matching_labels_before_publication() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + fs::create_dir(&src_path)?; + fs::write(src_path.join("alias-a"), b"shared state")?; + fs::hard_link(src_path.join("alias-a"), src_path.join("alias-b"))?; + let source = cap_dir(&src_path); + + let mismatch_path = temp.path().join("dst-mismatch"); + fs::create_dir(&mismatch_path)?; + let mismatch_destination = cap_dir(&mismatch_path); + let mismatch_labeler = FakeLabeler { + different_labels: true, + ..FakeLabeler::default() + }; + let error = preserve_var_copy_mounts_labeled( + &source, + &mismatch_destination, + &[], + &BTreeSet::new(), + Some(&mismatch_labeler), + ) + .unwrap_err(); + assert!(format!("{error:#}").contains("resolve to different SELinux labels")); + assert!(mismatch_labeler.applied.lock().unwrap().is_empty()); + assert_eq!(fs::read_dir(&mismatch_path)?.count(), 0); + + let missing_path = temp.path().join("dst-missing-label"); + fs::create_dir(&missing_path)?; + let missing_destination = cap_dir(&missing_path); + let missing_labeler = FakeLabeler { + no_match: BTreeSet::from([PathBuf::from("/var/alias-b")]), + ..FakeLabeler::default() + }; + let error = preserve_var_copy_mounts_labeled( + &source, + &missing_destination, + &[], + &BTreeSet::new(), + Some(&missing_labeler), + ) + .unwrap_err(); + assert!(format!("{error:#}").contains("No SELinux policy label for \"/var/alias-b\"")); + assert!(missing_labeler.applied.lock().unwrap().is_empty()); + assert_eq!(fs::read_dir(&missing_path)?.count(), 0); + + let matching_path = temp.path().join("dst-matching"); + fs::create_dir(&matching_path)?; + let matching_destination = cap_dir(&matching_path); + let matching_labeler = FakeLabeler::default(); + preserve_var_copy_mounts_labeled( + &source, + &matching_destination, + &[], + &BTreeSet::new(), + Some(&matching_labeler), + )?; + let alias_a = fs::metadata(matching_path.join("alias-a"))?; + let alias_b = fs::metadata(matching_path.join("alias-b"))?; + assert_eq!(alias_a.ino(), alias_b.ino()); + let applied = matching_labeler.applied.lock().unwrap(); + assert!(applied.contains(&PathBuf::from("/var/alias-a"))); + assert!(applied.contains(&PathBuf::from("/var/alias-b"))); + Ok(()) + } + + #[test] + fn exclusions_are_relative_and_normalized() { + assert_eq!( + validate_exclusions(&["lib/rpm/".into()]).unwrap(), + ["lib/rpm"] + ); + assert!(validate_exclusions(&["../etc".into()]).is_err()); + assert!(validate_exclusions(&["/etc".into()]).is_err()); + assert!(validate_exclusions(&["lib/containers/storage".into()]).is_err()); + } + + #[test] + fn migration_plan_preflights_source_and_combines_exclusions() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_var = temp.path().join("var"); + fs::create_dir(&src_var)?; + fs::write(src_var.join("sentinel"), b"source remains untouched")?; + + let plan = VarMigrationPlan::prepare(&src_var, &["lib/rpm".to_string()])?; + assert_eq!(plan.src_var, fs::canonicalize(&src_var)?); + assert_eq!( + plan.exclusions, + ["tmp", "cache", "log/journal", "lib/containers", "lib/rpm"] + ); + assert_eq!( + fs::read(src_var.join("sentinel"))?, + b"source remains untouched" + ); + + let error = VarMigrationPlan::prepare(&temp.path().join("missing"), &[]).unwrap_err(); + assert!( + format!("{error:#}").contains("Canonicalizing source /var"), + "{error:#}" + ); + Ok(()) + } + + #[test] + fn preflight_checks_only_reachable_mountpoint_types() -> Result<()> { + use std::os::unix::fs::symlink; + + let temp = tempfile::tempdir()?; + let src_var = temp.path().join("var"); + fs::create_dir_all(src_var.join("directory"))?; + fs::write(src_var.join("file"), b"")?; + symlink("file", src_var.join("symlink"))?; + let mounts = BTreeSet::from([ + PathBuf::from("directory"), + PathBuf::from("file"), + PathBuf::from("symlink"), + PathBuf::from("tmp/excluded"), + PathBuf::from("directory/nested"), + ]); + + let error = validate_mountpoint_types(&src_var, &["tmp".to_string()], &mounts).unwrap_err(); + assert!(format!("{error:#}").contains("/var/symlink")); + validate_mountpoint_types( + &src_var, + &["tmp".to_string(), "symlink".to_string()], + &mounts, + )?; + Ok(()) + } + + #[test] + fn deployment_var_is_stateroot_var() { + let deploy = Path::new("/sysroot/ostree/deploy/default/deploy/checksum.0"); + assert_eq!( + deployment_var_path(deploy).unwrap(), + Path::new("/sysroot/ostree/deploy/default/var") + ); + } + + #[test] + fn directory_metadata_restoration_accepts_path_capability() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir(&src_path)?; + fs::create_dir(&dst_path)?; + fs::set_permissions(&src_path, fs::Permissions::from_mode(0o751))?; + + let source = cap_dir(&src_path); + let has_user_xattr = source + .setxattr(".", "user.bootc_path_fd", b"metadata") + .is_ok(); + let destination = cap_dir_path(&dst_path); + let snapshot = snapshot_directory_metadata(&source)?; + restore_directory_metadata(&destination, &snapshot)?; + assert_eq!( + fs::metadata(&dst_path)?.permissions().mode() & 0o7777, + snapshot.metadata.mode() & 0o7777 + ); + if has_user_xattr { + assert_eq!( + destination.getxattr(".", "user.bootc_path_fd")?, + Some(b"metadata".to_vec()) + ); + } + + restore_directory_timestamps(&destination, &snapshot.metadata)?; + let restored = fs::metadata(&dst_path)?; + assert_eq!(restored.atime(), snapshot.metadata.atime()); + assert_eq!(restored.atime_nsec(), snapshot.metadata.atime_nsec()); + assert_eq!(restored.mtime(), snapshot.metadata.mtime()); + assert_eq!(restored.mtime_nsec(), snapshot.metadata.mtime_nsec()); + Ok(()) + } + + #[test] + fn both_copy_strategies_leave_nested_mountpoints_empty() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir_all(src_path.join("lib/bound"))?; + fs::create_dir_all(src_path.join("lib/sibling"))?; + fs::write(src_path.join("lib/file-bound"), b"do not migrate")?; + fs::write( + src_path.join("lib/bound/mounted-sentinel"), + b"do not migrate", + )?; + fs::write(src_path.join("lib/sibling/sentinel"), b"migrate")?; + fs::create_dir(&dst_path)?; + let mounts = BTreeSet::from([PathBuf::from("lib/bound"), PathBuf::from("lib/file-bound")]); + let src = cap_dir(&src_path); + let dst = cap_dir(&dst_path); + + preserve_var_copy_mounts(&src, &dst, &[], &mounts)?; + assert!(dst_path.join("lib/bound").is_dir()); + assert_eq!(fs::read_dir(dst_path.join("lib/bound"))?.count(), 0); + assert_eq!(fs::read(dst_path.join("lib/file-bound"))?, b""); + assert_eq!(fs::read(dst_path.join("lib/sibling/sentinel"))?, b"migrate"); + + let excluded_dst_path = temp.path().join("excluded-dst"); + fs::create_dir(&excluded_dst_path)?; + preserve_var_copy_mounts( + &src, + &cap_dir(&excluded_dst_path), + &["lib/bound".into()], + &mounts, + )?; + assert!(!excluded_dst_path.join("lib/bound").exists()); + + let reflink_dst = temp.path().join("reflink-dst"); + fs::create_dir(&reflink_dst)?; + let reflink_dst = cap_dir(&reflink_dst); + if supports_reflink(&src, &reflink_dst)? { + preserve_var_reflink_mounts(&src, &reflink_dst, &[], &mounts)?; + assert!(reflink_dst.symlink_metadata("lib/bound")?.is_dir()); + assert_eq!( + fs::read_dir(temp.path().join("reflink-dst/lib/bound"))?.count(), + 0 + ); + assert_eq!( + fs::read(temp.path().join("reflink-dst/lib/file-bound"))?, + b"" + ); + assert_eq!( + fs::read(temp.path().join("reflink-dst/lib/sibling/sentinel"))?, + b"migrate" + ); + } + Ok(()) + } + + #[test] + fn split_directories_and_mountpoint_placeholders_keep_metadata() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir_all(src_path.join("lib/bound"))?; + fs::create_dir(&dst_path)?; + fs::set_permissions(src_path.join("lib"), fs::Permissions::from_mode(0o751))?; + fs::set_permissions( + src_path.join("lib/bound"), + fs::Permissions::from_mode(0o711), + )?; + // Old atimes make traversal update them on relatime filesystems; the + // copy must use the metadata captured before reading child entries. + let expected_atime = SystemTime::UNIX_EPOCH + Duration::from_secs(1_600_000_001); + let expected_mtime = SystemTime::UNIX_EPOCH + Duration::from_secs(1_600_000_002); + for path in [src_path.join("lib"), src_path.join("lib/bound")] { + fs::File::open(path)?.set_times( + fs::FileTimes::new() + .set_accessed(expected_atime) + .set_modified(expected_mtime), + )?; + } + + let src = cap_dir(&src_path); + let dst = cap_dir(&dst_path); + let xattrs_supported = src + .setxattr("lib", "user.bootc_migrate_test", b"parent") + .is_ok() + && src + .setxattr("lib/bound", "user.bootc_migrate_test", b"placeholder") + .is_ok(); + preserve_var_copy_mounts( + &src, + &dst, + &[], + &BTreeSet::from([PathBuf::from("lib/bound")]), + )?; + + for (source, destination, expected_mode, expected_xattr) in [ + ("lib", "lib", 0o751, b"parent".as_slice()), + ("lib/bound", "lib/bound", 0o711, b"placeholder".as_slice()), + ] { + let source_metadata = fs::metadata(src_path.join(source))?; + let destination_metadata = fs::metadata(dst_path.join(destination))?; + assert_eq!(destination_metadata.mode() & 0o7777, expected_mode); + assert_eq!(destination_metadata.uid(), source_metadata.uid()); + assert_eq!(destination_metadata.gid(), source_metadata.gid()); + assert_eq!( + destination_metadata.atime(), + expected_atime + .duration_since(SystemTime::UNIX_EPOCH)? + .as_secs() as i64 + ); + assert_eq!( + destination_metadata.mtime(), + expected_mtime + .duration_since(SystemTime::UNIX_EPOCH)? + .as_secs() as i64 + ); + assert_eq!(destination_metadata.atime_nsec(), 0); + assert_eq!(destination_metadata.mtime_nsec(), 0); + if xattrs_supported { + assert_eq!( + dst.getxattr(destination, "user.bootc_migrate_test")?, + Some(expected_xattr.to_vec()) + ); + } + } + Ok(()) + } + + #[test] + fn split_directory_xattrs_match_source_exactly_when_supported() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir(&src_path)?; + fs::create_dir(&dst_path)?; + let source = cap_dir(&src_path); + let destination = cap_dir(&dst_path); + if source + .setxattr(".", "user.bootc_expected", b"source") + .is_err() + || destination + .setxattr(".", "user.bootc_inherited", b"extra") + .is_err() + { + return Ok(()); + } + + let snapshot = snapshot_directory_metadata(&source)?; + restore_directory_metadata(&destination, &snapshot)?; + + assert_eq!( + destination.getxattr(".", "user.bootc_expected")?, + Some(b"source".to_vec()) + ); + assert_eq!(destination.getxattr(".", "user.bootc_inherited")?, None); + Ok(()) + } + + #[test] + fn apply_xattrs_exact_preserves_destination_selinux_when_source_lacks_it() -> Result<()> { + let temp = tempfile::tempdir()?; + let path = temp.path().join("destination"); + fs::write(&path, b"")?; + let destination = cap_dir(temp.path()); + if destination + .setxattr("destination", SELINUX_XATTR, b"test-label") + .is_err() + { + // Some test filesystems or unprivileged environments cannot set + // security.selinux; exercise the behavior where supported. + return Ok(()); + } + + let file = fs::File::open(&path)?; + apply_xattrs_exact(&file, &[])?; + assert_eq!( + destination.getxattr("destination", SELINUX_XATTR)?, + Some(b"test-label".to_vec()) + ); + Ok(()) + } + + #[test] + fn target_policy_placeholder_xattrs_do_not_copy_source_selinux() -> Result<()> { + let source_xattrs: Vec<(std::ffi::OsString, Vec)> = vec![ + (SELINUX_XATTR.into(), b"source-label".to_vec()), + ("user.bootc_placeholder".into(), b"source".to_vec()), + ]; + let filtered = source_xattrs + .iter() + .filter(|(name, _)| should_copy_placeholder_xattr(name, true)) + .cloned() + .collect::>(); + assert_eq!(filtered.len(), 1); + assert_eq!( + filtered[0].0, + std::ffi::OsString::from("user.bootc_placeholder") + ); + assert_eq!( + source_xattrs + .iter() + .filter(|(name, _)| should_copy_placeholder_xattr(name, false)) + .count(), + source_xattrs.len() + ); + + let temp = tempfile::tempdir()?; + let path = temp.path().join("placeholder"); + fs::write(&path, b"")?; + let parent = cap_dir(temp.path()); + if parent + .setxattr("placeholder", SELINUX_XATTR, b"inherited-label") + .is_ok() + { + let file = fs::File::open(&path)?; + apply_xattrs_exact(&file, &filtered)?; + assert_eq!( + parent.getxattr("placeholder", SELINUX_XATTR)?, + Some(b"inherited-label".to_vec()) + ); + } + Ok(()) + } + + #[test] + fn filtered_copy_does_not_change_existing_destination_directory() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir_all(src_path.join("tree/excluded"))?; + fs::write(src_path.join("tree/sentinel"), b"source")?; + fs::write(src_path.join("tree/new-child"), b"new child")?; + fs::write(src_path.join("root-new-child"), b"new root child")?; + fs::create_dir_all(dst_path.join("tree"))?; + fs::write(dst_path.join("tree/sentinel"), b"destination")?; + fs::set_permissions(src_path.join("tree"), fs::Permissions::from_mode(0o751))?; + fs::set_permissions(&dst_path, fs::Permissions::from_mode(0o710))?; + fs::set_permissions(dst_path.join("tree"), fs::Permissions::from_mode(0o700))?; + let expected_atime = SystemTime::UNIX_EPOCH + Duration::from_secs(1_600_000_101); + let expected_mtime = SystemTime::UNIX_EPOCH + Duration::from_secs(1_600_000_102); + let expected_root_atime = SystemTime::UNIX_EPOCH + Duration::from_secs(1_600_000_104); + let expected_root_mtime = SystemTime::UNIX_EPOCH + Duration::from_secs(1_600_000_103); + fs::File::open(dst_path.join("tree"))?.set_times( + fs::FileTimes::new() + .set_accessed(expected_atime) + .set_modified(expected_mtime), + )?; + fs::File::open(&dst_path)?.set_times( + fs::FileTimes::new() + .set_accessed(expected_root_atime) + .set_modified(expected_root_mtime), + )?; + let destination = cap_dir(&dst_path); + let xattrs_supported = destination + .setxattr("tree", "user.bootc_migrate_test", b"existing") + .is_ok(); + let expected_owner = fs::metadata(dst_path.join("tree"))?; + + preserve_var_copy_mounts( + &cap_dir(&src_path), + &destination, + &["tree/excluded".into()], + &BTreeSet::new(), + )?; + + let destination_metadata = fs::metadata(dst_path.join("tree"))?; + assert_eq!(destination_metadata.mode() & 0o7777, 0o700); + assert_eq!(destination_metadata.uid(), expected_owner.uid()); + assert_eq!(destination_metadata.gid(), expected_owner.gid()); + assert_eq!( + destination_metadata.atime(), + expected_atime + .duration_since(SystemTime::UNIX_EPOCH)? + .as_secs() as i64 + ); + assert_eq!( + destination_metadata.mtime(), + expected_mtime + .duration_since(SystemTime::UNIX_EPOCH)? + .as_secs() as i64 + ); + assert_eq!(fs::read(dst_path.join("tree/sentinel"))?, b"destination"); + assert_eq!(fs::read(dst_path.join("tree/new-child"))?, b"new child"); + assert_eq!( + fs::read(dst_path.join("root-new-child"))?, + b"new root child" + ); + assert_eq!(fs::read_dir(dst_path.join("tree"))?.count(), 2); + assert_eq!(destination_metadata.modified()?, expected_mtime); + + let root_metadata = fs::metadata(&dst_path)?; + assert_eq!(root_metadata.mode() & 0o7777, 0o710); + assert_eq!( + root_metadata.atime(), + expected_root_atime + .duration_since(SystemTime::UNIX_EPOCH)? + .as_secs() as i64 + ); + assert_eq!(root_metadata.modified()?, expected_root_mtime); + if xattrs_supported { + assert_eq!( + destination.getxattr("tree", "user.bootc_migrate_test")?, + Some(b"existing".to_vec()) + ); + } + Ok(()) + } + + #[test] + fn staged_directory_collision_merges_missing_children_without_clobbering() -> Result<()> { + let temp = tempfile::tempdir()?; + let destination_path = temp.path().join("destination"); + fs::create_dir(&destination_path)?; + let destination = cap_dir(&destination_path); + with_staging_dir(&destination, |_, staging| { + staging.create_dir("tree")?; + staging.write("tree/existing", b"staged collision")?; + staging.write("tree/missing", b"new child")?; + + // Simulate a concurrent creator winning after staging completed. + fs::create_dir(destination_path.join("tree"))?; + fs::write(destination_path.join("tree/existing"), b"destination")?; + fs::set_permissions( + destination_path.join("tree"), + fs::Permissions::from_mode(0o700), + )?; + let destination_mtime = SystemTime::UNIX_EPOCH + Duration::from_secs(1_600_000_301); + fs::File::open(destination_path.join("tree"))? + .set_times(fs::FileTimes::new().set_modified(destination_mtime))?; + let expected_owner = fs::metadata(destination_path.join("tree"))?; + + assert!(!publish_staged_entry( + staging, + &destination, + OsStr::new("tree"), + Path::new("tree") + )?); + assert_eq!( + fs::read(destination_path.join("tree/existing"))?, + b"destination" + ); + assert_eq!( + fs::read(destination_path.join("tree/missing"))?, + b"new child" + ); + + let metadata = fs::metadata(destination_path.join("tree"))?; + assert_eq!(metadata.mode() & 0o7777, 0o700); + assert_eq!(metadata.uid(), expected_owner.uid()); + assert_eq!(metadata.gid(), expected_owner.gid()); + assert_eq!(metadata.modified()?, destination_mtime); + Ok(()) + })?; + Ok(()) + } + + #[test] + fn unfiltered_directory_collisions_preserve_destination_metadata() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + fs::create_dir_all(src_path.join("state"))?; + fs::write(src_path.join("state/existing"), b"source")?; + fs::write(src_path.join("state/new"), b"new")?; + fs::write(src_path.join("collision-file"), b"source collision")?; + fs::create_dir(src_path.join("directory-vs-file"))?; + fs::write(src_path.join("directory-vs-file/new"), b"must not replace")?; + fs::set_permissions(src_path.join("state"), fs::Permissions::from_mode(0o751))?; + let source_mtime = SystemTime::UNIX_EPOCH + Duration::from_secs(1_600_000_201); + fs::File::open(src_path.join("state"))? + .set_times(fs::FileTimes::new().set_modified(source_mtime))?; + + let src = cap_dir(&src_path); + for (strategy, reflink) in [("copy", false), ("reflink", true)] { + let dst_path = temp.path().join(format!("dst-{strategy}")); + fs::create_dir_all(dst_path.join("state"))?; + fs::write(dst_path.join("state/existing"), b"destination")?; + fs::write(dst_path.join("collision-file"), b"keep collision")?; + fs::write(dst_path.join("directory-vs-file"), b"keep file")?; + fs::set_permissions(dst_path.join("state"), fs::Permissions::from_mode(0o700))?; + let destination_mtime = SystemTime::UNIX_EPOCH + Duration::from_secs(1_600_000_202); + fs::File::open(dst_path.join("state"))? + .set_times(fs::FileTimes::new().set_modified(destination_mtime))?; + let dst = cap_dir(&dst_path); + if reflink && !supports_reflink(&src, &dst)? { + continue; + } + + if reflink { + preserve_var_reflink(&src, &dst, &[])?; + } else { + preserve_var_copy(&src, &dst, &[])?; + } + + let destination_metadata = fs::metadata(dst_path.join("state"))?; + assert_eq!(destination_metadata.mode() & 0o7777, 0o700); + assert_eq!(destination_metadata.modified()?, destination_mtime); + assert_eq!(fs::read(dst_path.join("state/existing"))?, b"destination"); + assert_eq!(fs::read(dst_path.join("state/new"))?, b"new"); + assert_eq!( + fs::read(dst_path.join("collision-file"))?, + b"keep collision" + ); + assert_eq!(fs::read(dst_path.join("directory-vs-file"))?, b"keep file"); + assert!(!dst_path.join("directory-vs-file/new").exists()); + } + Ok(()) + } + + #[test] + fn failed_copy_batch_is_cleaned_and_can_be_retried() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + fs::create_dir(&src_path)?; + fs::write(src_path.join("first"), b"linked data")?; + fs::hard_link(src_path.join("first"), src_path.join("second"))?; + let src = cap_dir(&src_path); + + let failing_cp = temp.path().join("cp-fails-after-copy"); + fs::write( + &failing_cp, + b"#!/bin/sh\nreflink=no\nwhile [ \"$1\" != -- ]; do\n [ \"$1\" = --reflink=auto ] && reflink=yes\n shift\ndone\nshift\nsource=$1\nshift\nwhile [ \"$#\" -gt 1 ]; do shift; done\ndestination=$1\nif [ \"$reflink\" = yes ]; then\n /bin/cp --reflink=auto -a -- \"$source\" \"$destination\"\nelse\n /bin/cp -a -- \"$source\" \"$destination\"\nfi\nexit 1\n", + )?; + fs::set_permissions(&failing_cp, fs::Permissions::from_mode(0o755))?; + + for (strategy, reflink) in [("copy", false), ("reflink", true)] { + let dst_path = temp.path().join(format!("dst-{strategy}")); + fs::create_dir(&dst_path)?; + let dst = cap_dir(&dst_path); + if reflink && !supports_reflink(&src, &dst)? { + continue; + } + + let error = copy_var_filtered( + &src, + &dst, + Path::new(""), + &[], + &BTreeSet::new(), + reflink, + false, + None, + false, + failing_cp.as_os_str(), + ); + assert!(error.is_err()); + assert_eq!(fs::read_dir(&dst_path)?.count(), 0); + + if reflink { + preserve_var_reflink(&src, &dst, &[])?; + } else { + preserve_var_copy(&src, &dst, &[])?; + } + let first = fs::metadata(dst_path.join("first"))?; + let second = fs::metadata(dst_path.join("second"))?; + assert_eq!(first.ino(), second.ino()); + assert_eq!(fs::read(dst_path.join("first"))?, b"linked data"); + } + Ok(()) + } + + #[test] + fn reflink_copy_uses_auto_fallback_for_individual_files() { + assert_eq!(CP_REFLINK_OPTIONS, ["--reflink=auto", "-a"]); + } + + #[test] + fn mountpoint_placeholders_reuse_seeded_destinations_and_check_type() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + fs::create_dir_all(src_path.join("directory-mount"))?; + fs::write(src_path.join("file-mount"), b"mounted data")?; + let src = cap_dir(&src_path); + + for (strategy, reflink) in [("copy", false), ("reflink", true)] { + let dst_path = temp.path().join(format!("dst-{strategy}")); + fs::create_dir(&dst_path)?; + let dst = cap_dir(&dst_path); + if reflink && !supports_reflink(&src, &dst)? { + continue; + } + + fs::create_dir(dst_path.join("directory-mount"))?; + fs::write(dst_path.join("file-mount"), b"")?; + let mounts = BTreeSet::from([ + PathBuf::from("directory-mount"), + PathBuf::from("file-mount"), + ]); + if reflink { + preserve_var_reflink_mounts(&src, &dst, &[], &mounts)?; + preserve_var_reflink_mounts(&src, &dst, &[], &mounts)?; + } else { + preserve_var_copy_mounts(&src, &dst, &[], &mounts)?; + preserve_var_copy_mounts(&src, &dst, &[], &mounts)?; + } + assert_eq!(fs::read_dir(dst_path.join("directory-mount"))?.count(), 0); + assert_eq!(fs::metadata(dst_path.join("file-mount"))?.len(), 0); + + // Image-seeded content at a destination that will be hidden by + // the mount is safe to retain when its type matches. + fs::write(dst_path.join("directory-mount/sentinel"), b"keep")?; + fs::write(dst_path.join("file-mount"), b"keep file")?; + if reflink { + preserve_var_reflink_mounts(&src, &dst, &[], &mounts)?; + } else { + preserve_var_copy_mounts(&src, &dst, &[], &mounts)?; + } + assert_eq!( + fs::read(dst_path.join("directory-mount/sentinel"))?, + b"keep" + ); + assert_eq!(fs::read(dst_path.join("file-mount"))?, b"keep file"); + + let wrong_type = temp.path().join(format!("wrong-type-{strategy}")); + fs::create_dir(&wrong_type)?; + fs::write(wrong_type.join("directory-mount"), b"keep wrong type")?; + fs::create_dir(wrong_type.join("file-mount"))?; + fs::write(wrong_type.join("file-mount/sentinel"), b"keep wrong type")?; + let wrong_dst = cap_dir(&wrong_type); + let error = if reflink { + preserve_var_reflink_mounts(&src, &wrong_dst, &[], &mounts) + } else { + preserve_var_copy_mounts(&src, &wrong_dst, &[], &mounts) + }; + assert!(error.is_err()); + assert_eq!( + fs::read(wrong_type.join("directory-mount"))?, + b"keep wrong type" + ); + assert_eq!( + fs::read(wrong_type.join("file-mount/sentinel"))?, + b"keep wrong type" + ); + } + Ok(()) + } + + #[test] + fn mountpoint_placeholders_fail_without_reusing_existing_destinations() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir_all(src_path.join("directory-mount"))?; + fs::write(src_path.join("file-mount"), b"source")?; + fs::create_dir_all(dst_path.join("directory-mount"))?; + fs::write(dst_path.join("directory-mount/sentinel"), b"keep")?; + fs::create_dir(dst_path.join("file-mount"))?; + fs::write(dst_path.join("file-mount/sentinel"), b"also keep")?; + + let src = cap_dir(&src_path); + let dst = cap_dir(&dst_path); + assert!( + create_mountpoint_placeholder(&src, &dst, OsStr::new("directory-mount"), false) + .is_err() + ); + assert!( + create_mountpoint_placeholder(&src, &dst, OsStr::new("file-mount"), false).is_err() + ); + assert_eq!( + fs::read(dst_path.join("directory-mount/sentinel"))?, + b"keep" + ); + assert_eq!( + fs::read(dst_path.join("file-mount/sentinel"))?, + b"also keep" + ); + Ok(()) + } + + #[test] + fn destination_directory_symlinks_are_rejected_without_following() -> Result<()> { + let temp = tempfile::tempdir()?; + let target = temp.path().join("target"); + fs::create_dir(&target)?; + fs::write(target.join("sentinel"), b"unchanged")?; + + let var_link = temp.path().join("var-link"); + symlink(&target, &var_link)?; + assert!(open_or_create_var_dir(&var_link).is_err()); + + let parent_path = temp.path().join("parent"); + fs::create_dir(&parent_path)?; + symlink(&target, parent_path.join("child-link"))?; + let parent = cap_dir(&parent_path); + assert!(open_or_create_child_dir(&parent, OsStr::new("child-link")).is_err()); + + let src_path = temp.path().join("src"); + fs::create_dir(&src_path)?; + fs::create_dir(src_path.join("mount"))?; + symlink(&target, parent_path.join("mount"))?; + assert!( + create_mountpoint_placeholder(&cap_dir(&src_path), &parent, OsStr::new("mount"), false) + .is_err() + ); + + assert_eq!(fs::read(target.join("sentinel"))?, b"unchanged"); + assert_eq!(fs::read_dir(target)?.count(), 1); + Ok(()) + } + + #[test] + fn plain_copy_skips_multiple_children_under_parent() -> Result<()> { + let exclusions = ["tmp".into(), "log/journal".into(), "log/private".into()]; + + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir_all(src_path.join("tmp"))?; + fs::create_dir_all(src_path.join("log/journal"))?; + fs::create_dir_all(src_path.join("log/private"))?; + fs::create_dir_all(src_path.join("log/keep"))?; + fs::write(src_path.join("tmp/ignored"), b"tmp")?; + fs::write(src_path.join("log/journal/ignored"), b"journal")?; + fs::write(src_path.join("log/private/ignored"), b"private")?; + fs::write(src_path.join("log/keep/preserved"), b"keep")?; + fs::hard_link( + src_path.join("log/keep/preserved"), + src_path.join("log/keep/preserved-hardlink"), + )?; + fs::create_dir(&dst_path)?; + + preserve_var_copy(&cap_dir(&src_path), &cap_dir(&dst_path), &exclusions)?; + + assert!(!dst_path.join("tmp").exists()); + assert!(!dst_path.join("log/journal").exists()); + assert!(!dst_path.join("log/private").exists()); + assert_eq!(fs::read(dst_path.join("log/keep/preserved"))?, b"keep"); + assert_eq!( + fs::metadata(dst_path.join("log/keep/preserved"))?.ino(), + fs::metadata(dst_path.join("log/keep/preserved-hardlink"))?.ino() + ); + Ok(()) + } + + #[test] + fn reflink_strategy_skips_multiple_children_under_parent() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir_all(src_path.join("log/journal"))?; + fs::create_dir_all(src_path.join("log/private"))?; + fs::create_dir_all(src_path.join("log/keep"))?; + fs::write(src_path.join("log/journal/ignored"), b"journal")?; + fs::write(src_path.join("log/private/ignored"), b"private")?; + fs::write(src_path.join("log/keep/preserved"), b"keep")?; + fs::create_dir(&dst_path)?; + let src = cap_dir(&src_path); + let dst = cap_dir(&dst_path); + let supports_reflink = supports_reflink(&src, &dst)?; + if !supports_reflink { + fs::remove_file(src_path.join("log/keep/preserved"))?; + fs::remove_dir(src_path.join("log/keep"))?; + } + + // Exercise Strategy C's exclusion routing on every filesystem and, if + // available, its reflink copy of a retained sibling. + preserve_var_reflink(&src, &dst, &["log/journal".into(), "log/private".into()])?; + + assert!(!dst_path.join("log/journal").exists()); + assert!(!dst_path.join("log/private").exists()); + assert_eq!( + dst_path.join("log/keep/preserved").exists(), + supports_reflink + ); + Ok(()) + } + + #[test] + fn copy_preserves_symlink_without_traversing_target() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + let outside = temp.path().join("outside"); + fs::create_dir(&src_path)?; + fs::create_dir(&dst_path)?; + fs::create_dir(&outside)?; + fs::write(outside.join("sentinel"), b"outside")?; + symlink(&outside, src_path.join("escape"))?; + + preserve_var_copy( + &cap_dir(&src_path), + &cap_dir(&dst_path), + &["escape/sentinel".into()], + )?; + + let copied = fs::symlink_metadata(dst_path.join("escape"))?; + assert!(copied.file_type().is_symlink()); + assert_eq!(fs::read_link(dst_path.join("escape"))?, outside); + // The target remains outside the destination tree; it was not copied. + assert!(!dst_path.join("sentinel").exists()); + Ok(()) + } + + #[test] + fn copy_preserves_archive_metadata_and_hardlinks() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + let src_state = src_path.join("state"); + fs::create_dir_all(&src_state)?; + fs::create_dir(&dst_path)?; + fs::write(src_state.join("one"), b"contents")?; + fs::hard_link(src_state.join("one"), src_state.join("two"))?; + fs::set_permissions(&src_state, fs::Permissions::from_mode(0o751))?; + fs::set_permissions(src_state.join("one"), fs::Permissions::from_mode(0o640))?; + let src_cap = cap_dir(&src_path); + let dst_cap = cap_dir(&dst_path); + let xattr_supported = src_cap + .setxattr("state/one", "user.bootc_migrate_test", b"xattr") + .is_ok(); + let expected_mtime = SystemTime::UNIX_EPOCH + Duration::from_secs(1_700_000_000); + fs::File::open(&src_state)?.set_times(fs::FileTimes::new().set_modified(expected_mtime))?; + + preserve_var_copy(&src_cap, &dst_cap, &[])?; + + let copied_dir = fs::metadata(dst_path.join("state"))?; + let copied_one = fs::metadata(dst_path.join("state/one"))?; + let copied_two = fs::metadata(dst_path.join("state/two"))?; + assert_eq!(copied_dir.permissions().mode() & 0o7777, 0o751); + assert_eq!(copied_one.permissions().mode() & 0o7777, 0o640); + assert_eq!(copied_dir.modified()?, expected_mtime); + assert_eq!(copied_one.ino(), copied_two.ino()); + assert_eq!(copied_one.nlink(), 2); + assert_eq!(copied_one.uid(), fs::metadata(src_state.join("one"))?.uid()); + assert_eq!(copied_one.gid(), fs::metadata(src_state.join("one"))?.gid()); + if xattr_supported { + assert_eq!( + dst_cap.getxattr("state/one", "user.bootc_migrate_test")?, + Some(b"xattr".to_vec()) + ); + } + Ok(()) + } + + #[test] + fn copy_keeps_existing_collisions_untouched() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir_all(src_path.join("state"))?; + fs::create_dir_all(dst_path.join("state"))?; + fs::write(src_path.join("state/existing"), b"source")?; + fs::write(src_path.join("state/new"), b"new")?; + fs::write(dst_path.join("state/existing"), b"destination")?; + + preserve_var_copy(&cap_dir(&src_path), &cap_dir(&dst_path), &[])?; + + assert_eq!(fs::read(dst_path.join("state/existing"))?, b"destination"); + assert_eq!(fs::read(dst_path.join("state/new"))?, b"new"); + Ok(()) + } +} diff --git a/crates/lib/src/install/migrate/mountinfo.rs b/crates/lib/src/install/migrate/mountinfo.rs new file mode 100644 index 0000000000..6b82bfd381 --- /dev/null +++ b/crates/lib/src/install/migrate/mountinfo.rs @@ -0,0 +1,264 @@ +//! Read `/proc/self/mountinfo` and select mounts nested beneath the source `/var`. +//! +//! The byte-oriented parser preserves non-UTF-8 Unix paths and decodes kernel +//! escapes. The `procfs` crate requires UTF-8 and leaves escaped mountpoint +//! paths undecoded; bootc-mount/findmnt exposes `String` targets and invokes +//! external `findmnt`, so neither preserves arbitrary path bytes. There is no +//! standard-library mountinfo API. + +use std::{ + collections::BTreeSet, + os::unix::ffi::OsStringExt, + path::{Component, Path, PathBuf}, +}; + +use anyhow::{Context, Result}; + +/// The mountpoint is the fifth space-separated mountinfo field. +const MOUNTINFO_MOUNTPOINT_FIELD: usize = 4; + +/// Read one snapshot and return the paths of mounts nested beneath `src_var`. +/// +/// `src_var` is the canonical path used to open the source directory; +/// mountinfo paths are already kernel-canonical. +pub(super) fn nested_mountpoints_for(src_var: &Path) -> Result> { + let snapshot = read_mountinfo_snapshot()?; + nested_mountpoints(src_var, parse_mountinfo(&snapshot)?) +} + +/// Read one complete installer mount namespace snapshot. +fn read_mountinfo_snapshot() -> Result> { + std::fs::read("/proc/self/mountinfo") + .context("Reading installer /proc/self/mountinfo for /var migration") +} + +fn nested_mountpoints( + src_var: &Path, + mountpoints: impl IntoIterator, +) -> Result> { + anyhow::ensure!(src_var.is_absolute(), "Source /var path must be absolute"); + let mut nested = BTreeSet::new(); + for mountpoint in mountpoints { + let Ok(relative) = mountpoint.strip_prefix(src_var) else { + continue; + }; + // The mount on /var itself is not a nested mount. Only child paths are + // filtered; Path::strip_prefix provides component-safe containment. + if relative.as_os_str().is_empty() { + continue; + } + anyhow::ensure!( + relative + .components() + .all(|component| matches!(component, Component::Normal(_))), + "Invalid nested mountpoint below {}: {}", + src_var.display(), + mountpoint.display() + ); + nested.insert(relative.to_path_buf()); + } + Ok(nested) +} + +/// Extract mountpoints from the byte-oriented Linux mountinfo format without +/// requiring UTF-8. Only field 5 is relevant; in particular, empty fields and +/// malformed trailing fields in records for unrelated mounts are ignored. +/// Literal spaces are the field delimiter, so CR, form-feed, and other path +/// bytes remain intact. Kernel pathname escapes are decoded before paths are +/// constructed. +fn parse_mountinfo(data: &[u8]) -> Result> { + let mountpoints = data + .split(|byte| *byte == b'\n') + .enumerate() + .filter(|(_, line)| !line.is_empty()) + .map(|(index, line)| { + let field = line + .split(|byte| *byte == b' ') + .nth(MOUNTINFO_MOUNTPOINT_FIELD) + .with_context(|| { + format!("Malformed mountinfo line {}: no mountpoint", index + 1) + })?; + Ok(PathBuf::from(std::ffi::OsString::from_vec( + decode_mountinfo_escapes(field), + ))) + }) + .collect::>>()?; + anyhow::ensure!(!mountpoints.is_empty(), "Mountinfo contained no records"); + Ok(mountpoints) +} + +/// Decode the kernel's `\ooo` escapes for space, tab, newline, and backslash. +/// Unknown or malformed escape sequences are retained as literal bytes. +fn decode_mountinfo_escapes(value: &[u8]) -> Vec { + let mut decoded = Vec::with_capacity(value.len()); + let mut rest = value; + while let Some((&byte, tail)) = rest.split_first() { + let escaped = (byte == b'\\') + .then(|| tail.get(..3)) + .flatten() + .filter(|digits| digits.iter().all(|digit| (b'0'..=b'7').contains(digit))) + .and_then(|digits| { + let value = digits + .iter() + .fold(0u16, |acc, digit| acc * 8 + u16::from(digit - b'0')); + u8::try_from(value).ok() + }) + .filter(|value| matches!(value, b'\t' | b'\n' | b' ' | b'\\')); + if let Some(escaped) = escaped { + decoded.push(escaped); + rest = &tail[3..]; + } else { + decoded.push(byte); + rest = tail; + } + } + decoded +} + +#[cfg(test)] +mod tests { + use super::*; + use std::{fs, os::unix::ffi::OsStrExt}; + + #[test] + fn mountinfo_decodes_escaped_and_non_utf8_paths() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_var = temp.path().join("var"); + fs::create_dir(&src_var)?; + let source = src_var.as_os_str().as_bytes(); + let mut mountinfo = format!( + "36 0 0:32 / {0} rw,relatime - tmpfs tmpfs rw\n37 25 0:33 / {0}/rpc\\040pipefs rw - nfs nfs rw\n", + src_var.display() + ) + .into_bytes(); + mountinfo.extend_from_slice(b"38 25 0:34 / "); + mountinfo.extend_from_slice(source); + mountinfo.extend_from_slice(b"/\xffstate rw - tmpfs tmpfs rw\n"); + let parsed = parse_mountinfo(&mountinfo)?; + let escaped_name = src_var.join("rpc pipefs"); + assert_eq!(parsed[1], escaped_name); + let mut non_utf8 = source.to_vec(); + non_utf8.extend_from_slice(b"/\xffstate"); + assert_eq!(parsed[2].as_os_str().as_bytes(), non_utf8); + + let nested = nested_mountpoints(&src_var, parsed)?; + assert_eq!( + nested, + BTreeSet::from([ + PathBuf::from("rpc pipefs"), + PathBuf::from(std::ffi::OsStr::from_bytes(b"\xffstate")) + ]) + ); + Ok(()) + } + + #[test] + fn mountinfo_preserves_cr_form_feed_and_decodes_kernel_escapes() -> Result<()> { + use std::os::unix::ffi::OsStrExt; + + let parsed = parse_mountinfo( + b"1 0 0:1 / /target/var rw - tmpfs tmpfs rw\n\ + 2 1 0:2 / /target/var/a\\040b\\011c\\012d\\134e\r\x0cf rw - x x rw\n", + )?; + let mountpoint = parsed[1].as_os_str().as_bytes(); + assert_eq!(mountpoint, b"/target/var/a b\tc\nd\\e\r\x0cf"); + Ok(()) + } + + #[test] + fn unrelated_mount_records_do_not_change_nested_mount_selection() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_var = temp.path().join("target/var"); + fs::create_dir_all(&src_var)?; + let base = format!( + "36 0 0:32 / {0} rw - tmpfs tmpfs rw\n37 25 0:33 / {0}/lib/data rw - nfs nfs rw\n", + src_var.display() + ); + let with_unrelated = format!( + "21 0 0:21 / / rw - overlay overlay rw\n{base}38 25 0:34 / /unrelated rw - tmpfs tmpfs rw\n" + ); + + let selected = |mountinfo: &str| -> Result> { + nested_mountpoints(&src_var, parse_mountinfo(mountinfo.as_bytes())?) + }; + assert_eq!( + selected(&base)?, + BTreeSet::from([PathBuf::from("lib/data")]) + ); + assert_eq!(selected(&with_unrelated)?, selected(&base)?); + Ok(()) + } + + #[test] + fn mountinfo_allows_relative_and_noncanonical_roots_with_valid_nested_mountpoints() -> Result<()> + { + let temp = tempfile::tempdir()?; + let src_var = temp.path().join("target/var"); + fs::create_dir_all(&src_var)?; + + for root in ["/some/../root", "relative/root"] { + let mountinfo = format!( + "36 0 0:32 / {0} rw - tmpfs tmpfs rw\n37 25 0:33 {1} {0}/lib/data rw - nfs nfs rw\n", + src_var.display(), + root + ); + + assert_eq!( + nested_mountpoints(&src_var, parse_mountinfo(mountinfo.as_bytes())?)?, + BTreeSet::from([PathBuf::from("lib/data")]), + "root field {root:?} should not affect mountpoint selection" + ); + } + Ok(()) + } + + #[test] + fn mountinfo_ignores_empty_source_and_unrelated_malformed_trailing_fields() -> Result<()> { + let parsed = parse_mountinfo( + b"36 25 0:32 / /target/var rw - tmpfs tmpfs rw\n\ + 37 36 0:33 / /target/var/lib/mounted rw,relatime shared:1 - tmpfs rw\n\ + 38 36 0:34 / /unrelated rw - missing-fields\n", + )?; + assert_eq!( + parsed, + [ + PathBuf::from("/target/var"), + PathBuf::from("/target/var/lib/mounted"), + PathBuf::from("/unrelated") + ] + ); + let nested = nested_mountpoints(Path::new("/target/var"), parsed)?; + assert_eq!(nested, BTreeSet::from([PathBuf::from("lib/mounted")])); + + for mountinfo in [b"".as_slice(), b"\n", b"36 25 0:32 /\n"] { + assert!( + parse_mountinfo(mountinfo).is_err(), + "accepted {mountinfo:?}" + ); + } + Ok(()) + } + + #[test] + fn nested_mountpoint_detection_is_component_safe() -> Result<()> { + use std::os::unix::fs::symlink; + + let temp = tempfile::tempdir()?; + let src_var = temp.path().join("target/var"); + fs::create_dir_all(&src_var)?; + let alias = temp.path().join("var-alias"); + symlink(&src_var, &alias)?; + let paths = [ + src_var.clone(), + src_var.join("lib/data"), + temp.path().join("target/var-old/other"), + PathBuf::from("/elsewhere/var/mount"), + ]; + let canonical_src_var = fs::canonicalize(&alias)?; + assert_eq!( + nested_mountpoints(&canonical_src_var, paths)?, + BTreeSet::from([PathBuf::from("lib/data")]) + ); + Ok(()) + } +} diff --git a/crates/lib/src/lsm.rs b/crates/lib/src/lsm.rs index 772358f02a..ac0a1a1297 100644 --- a/crates/lib/src/lsm.rs +++ b/crates/lib/src/lsm.rs @@ -3,6 +3,7 @@ use std::borrow::Cow; use std::ffi::CString; use std::io::Write; use std::os::fd::AsRawFd; +use std::os::unix::ffi::OsStrExt; use std::os::unix::process::CommandExt; use std::path::Path; use std::process::Command; @@ -280,6 +281,46 @@ pub(crate) fn optional_label( .map_err(Into::into) } +/// Look up a policy label for a Unix path without requiring the path to be +/// UTF-8. OSTree's safe wrapper accepts `&str`, while the underlying API takes +/// a NUL-terminated byte string. This narrowly-scoped FFI call owns both +/// returned GLib allocations before returning, including the label on the +/// unusual case where OSTree sets both outputs. +#[allow(unsafe_code)] +pub(crate) fn optional_label_os( + policy: &ostree::SePolicy, + path: &Path, + mode: u32, +) -> Result>> { + use ostree::glib::translate::{ToGlibPtr, from_glib_full}; + + let path = std::ffi::CString::new(path.as_os_str().as_bytes()) + .context("SELinux policy path contains an interior NUL")?; + let mut out_label = std::ptr::null_mut(); + let mut error = std::ptr::null_mut(); + // SAFETY: `path` is NUL-terminated and lives through the call; `policy` + // keeps the instance alive; all output pointers are initialized and their + // GLib-owned values are immediately converted with `from_glib_full` below. + let succeeded = unsafe { + ostree::ffi::ostree_sepolicy_get_label( + policy.to_glib_none().0, + path.as_ptr(), + mode, + &mut out_label, + std::ptr::null_mut(), + &mut error, + ) + }; + // Convert both outputs so even an unexpected label on error is freed. + let label: Option = unsafe { from_glib_full(out_label) }; + let error: Option = unsafe { from_glib_full(error) }; + debug_assert_eq!(succeeded == ostree::glib::ffi::GFALSE, error.is_some()); + if let Some(error) = error { + return Err(error.into()); + } + Ok(label.map(|label| label.as_bytes().to_vec())) +} + /// A thin wrapper for invoking fsetxattr(security.selinux) pub(crate) fn set_security_selinux(fd: std::os::fd::BorrowedFd, label: &[u8]) -> Result<()> { rustix::fs::fsetxattr( diff --git a/crates/lib/src/store/mod.rs b/crates/lib/src/store/mod.rs index 64470cecae..b0b9b8b9a8 100644 --- a/crates/lib/src/store/mod.rs +++ b/crates/lib/src/store/mod.rs @@ -131,6 +131,8 @@ use crate::podstorage::CStorage; use crate::spec::{BootloaderKind, ImageStatus}; use crate::utils::{EfiError, deployment_fd, open_dir_remount_rw, read_uefi_var}; +pub(crate) mod reflink; + /// See pub type ComposefsRepository = composefs::repository::Repository; diff --git a/crates/lib/src/store/reflink.rs b/crates/lib/src/store/reflink.rs new file mode 100644 index 0000000000..148a40153b --- /dev/null +++ b/crates/lib/src/store/reflink.rs @@ -0,0 +1,279 @@ +//! Pairwise reflink capability probing for storage directories. + +use std::io::Write as _; + +use anyhow::{Context, Result}; +use cap_std_ext::cap_std::fs::{Dir as CapStdDir, File, OpenOptions}; +use cap_std_ext::cap_std::io_lifetimes::AsFilelike; +use rustix::fd::AsFd as _; + +/// Returns whether a file in `src` can be reflinked into `dst`. +/// +/// The result is scoped to this source/destination pair; it is deliberately +/// not cached. Temporary files are created exclusively beneath the supplied +/// directory capabilities and removed before returning. +pub(crate) fn supports_reflink(src: &CapStdDir, dst: &CapStdDir) -> Result { + probe_with(src, dst, |destination, source| { + let source_view = source.as_filelike_view::(); + let destination_view = destination.as_filelike_view::(); + let source_fd = source_view.as_fd(); + let destination_fd = destination_view.as_fd(); + rustix::fs::ioctl_ficlone(destination_fd, source_fd) + }) +} + +fn probe_with( + src: &CapStdDir, + dst: &CapStdDir, + clone: impl FnOnce(&File, &File) -> rustix::io::Result<()>, +) -> Result { + let mut source = match create_probe_file(src) { + Ok(source) => source, + Err(error) => return source_probe_failure(error), + }; + let mut destination = match create_probe_file(dst) { + Ok(destination) => destination, + Err(error) => { + source + .cleanup() + .context("Removing reflink probe source after destination creation failed")?; + return Err(error); + } + }; + + let probe_result = match source + .file + .as_mut() + .expect("probe source file is open") + .write_all(b"bootc reflink probe") + { + Err(error) => { + source_probe_failure(anyhow::Error::from(error).context("Writing reflink probe source")) + } + Ok(()) => match clone( + destination + .file + .as_ref() + .expect("probe destination file is open"), + source.file.as_ref().expect("probe source file is open"), + ) { + Ok(()) => Ok(true), + Err(error) if reflink_unsupported(error) => Ok(false), + Err(error) => Err(anyhow::Error::from(error).context("Probing FICLONE support")), + }, + }; + + // Attempt both removals even if one fails. Drop provides a best-effort + // fallback if the operation above returned early (for example, a write + // error). + let source_cleanup = source.cleanup(); + let destination_cleanup = destination.cleanup(); + source_cleanup.context("Removing reflink probe source")?; + destination_cleanup.context("Removing reflink probe destination")?; + probe_result +} + +fn reflink_unsupported(error: rustix::io::Errno) -> bool { + // Both files are newly created regular files, so EINVAL here indicates + // that FICLONE is unsupported rather than an invalid source type. + matches!( + error, + rustix::io::Errno::OPNOTSUPP | rustix::io::Errno::XDEV | rustix::io::Errno::INVAL + ) +} + +fn source_probe_access_error(error: &anyhow::Error) -> bool { + error.chain().any(|cause| { + cause.downcast_ref::().is_some_and(|error| { + matches!( + error.kind(), + std::io::ErrorKind::PermissionDenied | std::io::ErrorKind::ReadOnlyFilesystem + ) + }) + }) +} + +fn source_probe_failure(error: anyhow::Error) -> Result { + if source_probe_access_error(&error) { + tracing::debug!("Cannot prepare reflink probe source, using full copy: {error:#}"); + Ok(false) + } else { + Err(error) + } +} + +struct ProbeFile<'a> { + dir: &'a CapStdDir, + name: String, + file: Option, + removed: bool, +} + +impl ProbeFile<'_> { + fn cleanup(&mut self) -> std::io::Result<()> { + self.cleanup_with(|dir, name| dir.remove_file(name)) + } + + fn cleanup_with( + &mut self, + remove_file: impl FnOnce(&CapStdDir, &str) -> std::io::Result<()>, + ) -> std::io::Result<()> { + drop(self.file.take()); + match remove_file(self.dir, &self.name) { + Ok(()) => { + self.removed = true; + Ok(()) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + self.removed = true; + Ok(()) + } + Err(error) => Err(error), + } + } +} + +impl Drop for ProbeFile<'_> { + fn drop(&mut self) { + if !self.removed { + let _ = self.cleanup(); + } + } +} + +fn create_probe_file(dir: &CapStdDir) -> Result> { + loop { + let name = format!(".bootc-reflink-probe-{}", uuid::Uuid::new_v4()); + match create_probe_file_with_name(dir, &name) { + Ok(file) => return Ok(file), + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(error) => return Err(error).context("Creating reflink probe file"), + } + } +} + +fn create_probe_file_with_name<'a>( + dir: &'a CapStdDir, + name: &str, +) -> std::io::Result> { + let file = dir.open_with( + name, + OpenOptions::new().read(true).write(true).create_new(true), + )?; + Ok(ProbeFile { + dir, + name: name.to_owned(), + file: Some(file), + removed: false, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use cap_std_ext::cap_std::ambient_authority; + use std::fs; + + fn cap_dir(path: &std::path::Path) -> CapStdDir { + CapStdDir::open_ambient_dir(path, ambient_authority()).unwrap() + } + + #[test] + fn probe_reports_success_and_cleans_up() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir(&src_path)?; + fs::create_dir(&dst_path)?; + + let supported = probe_with(&cap_dir(&src_path), &cap_dir(&dst_path), |_, _| Ok(()))?; + assert!(supported); + assert!(fs::read_dir(&src_path)?.next().is_none()); + assert!(fs::read_dir(&dst_path)?.next().is_none()); + Ok(()) + } + + #[test] + fn probe_classifies_unsupported_and_propagates_unexpected_errors() -> Result<()> { + let temp = tempfile::tempdir()?; + let src_path = temp.path().join("src"); + let dst_path = temp.path().join("dst"); + fs::create_dir(&src_path)?; + fs::create_dir(&dst_path)?; + let src = cap_dir(&src_path); + let dst = cap_dir(&dst_path); + + assert!(!probe_with(&src, &dst, |_, _| { + Err(rustix::io::Errno::OPNOTSUPP) + })?); + assert!(!probe_with(&src, &dst, |_, _| { + Err(rustix::io::Errno::INVAL) + })?); + assert!(fs::read_dir(&src_path)?.next().is_none()); + assert!(fs::read_dir(&dst_path)?.next().is_none()); + + assert!(probe_with(&src, &dst, |_, _| Err(rustix::io::Errno::IO)).is_err()); + assert!(fs::read_dir(&src_path)?.next().is_none()); + assert!(fs::read_dir(&dst_path)?.next().is_none()); + Ok(()) + } + + #[test] + fn classifies_only_known_unsupported_errors() { + assert!(reflink_unsupported(rustix::io::Errno::OPNOTSUPP)); + assert!(reflink_unsupported(rustix::io::Errno::XDEV)); + assert!(reflink_unsupported(rustix::io::Errno::INVAL)); + assert!(!reflink_unsupported(rustix::io::Errno::IO)); + } + + #[test] + fn source_permission_and_read_only_errors_select_full_copy() { + for error in [ + std::io::Error::from(std::io::ErrorKind::PermissionDenied), + std::io::Error::from(std::io::ErrorKind::ReadOnlyFilesystem), + ] { + let error = anyhow::Error::from(error).context("Creating reflink probe file"); + assert!(source_probe_access_error(&error)); + assert!(!source_probe_failure(error).unwrap()); + } + assert!(source_probe_failure(anyhow::anyhow!("unrelated error")).is_err()); + } + + #[test] + fn cleanup_retries_unlink_on_drop_after_failure() -> Result<()> { + let temp = tempfile::tempdir()?; + let path = temp.path().join("src"); + fs::create_dir(&path)?; + let dir = cap_dir(&path); + let name = ".bootc-reflink-probe-retry"; + let mut probe = create_probe_file_with_name(&dir, name)?; + + let error = probe + .cleanup_with(|_, _| Err(std::io::Error::from(std::io::ErrorKind::PermissionDenied))) + .unwrap_err(); + assert_eq!(error.kind(), std::io::ErrorKind::PermissionDenied); + assert!(path.join(name).exists()); + + drop(probe); + assert!(!path.join(name).exists()); + Ok(()) + } + + #[test] + fn probe_file_creation_never_overwrites_existing_files() -> Result<()> { + let temp = tempfile::tempdir()?; + let path = temp.path().join("src"); + fs::create_dir(&path)?; + let existing = path.join(".bootc-reflink-probe-collision"); + fs::write(&existing, b"leave this alone")?; + + let error = + match create_probe_file_with_name(&cap_dir(&path), ".bootc-reflink-probe-collision") { + Ok(_) => panic!("create_new unexpectedly replaced an existing probe file"), + Err(error) => error, + }; + assert_eq!(error.kind(), std::io::ErrorKind::AlreadyExists); + assert_eq!(fs::read(existing)?, b"leave this alone"); + Ok(()) + } +} diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 2c027e869e..028ced72e7 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -93,6 +93,7 @@ - [unified storage](bootc-experimental-unified-storage.7.md) - [fsck](bootc-experimental-fsck.7.md) - [install reset](bootc-experimental-install-reset.7.md) +- [`/var` preservation](bootc-experimental-preserve-var.7.md) - [--progress-fd](bootc-experimental-progress-fd.7.md) - [container export](bootc-experimental-container-export.7.md) - [`man bootc-uki-addon.8.md`](man/bootc-uki-addon.8.md) diff --git a/docs/src/bootc-experimental-preserve-var.7.md b/docs/src/bootc-experimental-preserve-var.7.md new file mode 100644 index 0000000000..d386b78c0f --- /dev/null +++ b/docs/src/bootc-experimental-preserve-var.7.md @@ -0,0 +1,86 @@ +# Preserving `/var` with `bootc install to-existing-root` + +This feature is experimental and requires the `--experimental` flag. Experimental +features are subject to change or removal. + +## Overview + +When installing to an existing root with the OSTree backend, `--preserve-var` +copies the old system's `/var` into the new deployment before rebooting. This +can retain machine-local state when converting an existing Linux system to +bootc. + +## Usage + +Enable preservation explicitly: + +```bash +bootc install to-existing-root --experimental --preserve-var +``` + +The option is only available with the OSTree backend. It does not change the +behavior of installs that do not pass `--preserve-var`. + +## Copy behavior and exclusions + +The migration uses reflinks when supported by the filesystem and otherwise +performs a full copy. It creates each new entry in a staging location and +renames it into place atomically for namespace visibility. + +Existing entries in the destination are not overwritten. When both source and +destination entries are directories, their contents are merged recursively and +children already present in the destination are retained. For other collisions, +the destination entry is kept, the source entry is skipped, and the collision +is reported in the install output. An incompatible destination type for a +nested mountpoint placeholder is an error. The preservation copy itself does +not modify the old system's source `/var`. If `--cleanup` is also specified, +cleanup of the previous install is scheduled for the next boot, and only after +the preservation copy succeeds. Before separately cleaning the old system, +verify that all important state has been copied or otherwise retained. + +The following paths below `/var` are always excluded: + +- `tmp` +- `cache` +- `log/journal` +- `lib/containers` + +To exclude additional paths, pass `--preserve-var-skip` one or more times. Paths +are relative to `/var`, and each may contain at most one slash: + +```bash +bootc install to-existing-root --experimental --preserve-var \ + --preserve-var-skip lib/rpm \ + --preserve-var-skip lib/my-application +``` + +The same exclusions can be configured with the `preserve-var-skip` install +configuration key; see +[`bootc-install-config(5)`](man/bootc-install-config.5.md). These configurable +exclusions apply only when `--preserve-var` is enabled. The always-excluded +paths cannot be re-included. + +## Limitations and cautions + +Do not combine `--preserve-var` with `--replace=wipe`: wiping the target would +erase the source `/var` before it can be copied. + +The copy proceeds over time; it is not a tree-wide point-in-time snapshot or a +global all-or-nothing transaction. A later failure can leave a partial +migration. Neither reflinks nor a full copy provide a consistent snapshot of +live data. Stop stateful services before copying their data, or use a +consistent snapshot. + +There is no crash-durability guarantee: copied content and renames are not +guaranteed to survive sudden power loss or a kernel failure. + +An interrupted migration may leave hidden `.bootc-preserve-var.*` staging +directories in the new `/var`; they are not automatically cleaned up. Verify +that no migration is still running and that an individual directory is an +abandoned migration artifact before removing it. + +## See also + +- [`bootc install to-existing-root`](bootc-installation.7.md#using-bootc-install-to-existing-root) +- [`bootc-install-to-existing-root(8)`](man/bootc-install-to-existing-root.8.md) +- [`bootc-install-config(5)`](man/bootc-install-config.5.md) diff --git a/docs/src/bootc-installation.7.md b/docs/src/bootc-installation.7.md index d2f845b5c9..8f97db1e28 100644 --- a/docs/src/bootc-installation.7.md +++ b/docs/src/bootc-installation.7.md @@ -295,6 +295,11 @@ and data from the old system (now accessible at `/sysroot`) to the new system. T useful for preserving network settings, user accounts, or application data from the previous installation. +With the OSTree backend, the experimental `--preserve-var` option copies the +old `/var` into the new deployment before rebooting. See the +[experimental `/var` preservation guide](bootc-experimental-preserve-var.7.md) +for its usage, exclusions, configuration, and limitations. + ##### Before reboot: Injecting new configuration After running `bootc install to-existing-root`, you may want to inject @@ -364,6 +369,14 @@ to automatically inherit root's SSH keys (which may have been injected from e.g. cloud instance userdata via a tool like `cloud-init`). To do this, add `--root-ssh-authorized-keys /target/root/.ssh/authorized_keys` to the install command. +This only preserves root's **authorized keys** for inbound access; SSH host keys +from the old `/etc/ssh` are not migrated. Provision host keys for the new system +before rebooting, or arrange for the new system to generate them on first boot. +If `/var/lib/cloud` is preserved, cloud-init may consider its initialization +already complete and skip first-boot key generation. An image that relies on +cloud-init for SSH host keys can therefore boot without usable SSH service +credentials unless they are provisioned separately. + See also the [bootc-install-to-existing-root(8)](man/bootc-install-to-existing-root.8.md) man page for more details. diff --git a/docs/src/man/bootc-install-config.5.md b/docs/src/man/bootc-install-config.5.md index 7211c4aab7..89ffb5bb7a 100644 --- a/docs/src/man/bootc-install-config.5.md +++ b/docs/src/man/bootc-install-config.5.md @@ -51,7 +51,13 @@ The `install` section supports these subfields: path) includes a default policy which requires signatures. When `true`, image pulls will be rejected if the policy file specifies `insecureAcceptAnything` as the default. Defaults to `false`. - This is equivalent to the `--enforce-container-sigpolicy` CLI flag. + This is equivalent to the `--enforce-container-sigpolicy` CLI flag. +- `preserve-var-skip`: An array of relative paths below `/var` to exclude when + the experimental `bootc install to-existing-root --experimental --preserve-var` + copies data. Each path may contain at most one slash. The always-excluded paths + cannot be re-included. See the + [experimental `/var` preservation guide](../bootc-experimental-preserve-var.7.md) + for details. # filesystem diff --git a/docs/src/man/bootc-install-to-existing-root.8.md b/docs/src/man/bootc-install-to-existing-root.8.md index ff70594221..04aab6119e 100644 --- a/docs/src/man/bootc-install-to-existing-root.8.md +++ b/docs/src/man/bootc-install-to-existing-root.8.md @@ -16,6 +16,38 @@ host root filesystem\'s `/boot` partition will be wiped, but the content of the existing root will otherwise be retained, and will need to be cleaned up if desired when rebooted into the new root. +The experimental `--preserve-var` option copies the running system's `/var` into +the new deployment before rebooting. It requires `--experimental` and the OSTree +backend, uses reflinks +when supported, and otherwise performs a full copy. Reflinks are created per +file. Each new entry is staged and renamed into place atomically for namespace +visibility, but the migration is not a tree-wide point-in-time snapshot or a +global all-or-nothing transaction: it proceeds over time, and a later failure +can leave a partial migration. The copy is not a consistent snapshot of live +data; stop stateful services or use a consistent snapshot before copying live +database data. There is no crash-durability guarantee: copied content and +renames are not guaranteed to survive sudden power loss or a kernel failure. +Do not combine `--preserve-var` with `--replace=wipe`, which would erase the +source `/var` before it can be copied. An interrupted migration may leave +hidden `.bootc-preserve-var.*` staging directories in the new `/var`; they are +not automatically cleaned up, so make sure no migration is still running and +verify an individual directory is abandoned before removing it. +The paths `tmp`, `cache`, `log/journal`, and `lib/containers` are always excluded. Repeat +`--preserve-var-skip` to exclude additional paths relative to `/var`; each path +may contain at most one slash, for example `lib/rpm`. + +Existing destination entries are never overwritten: matching directories merge +recursively, while other collisions keep the destination entry, skip the source +entry, and report the collision in the install output. An incompatible +destination type for a nested mountpoint placeholder fails preservation. The +preservation copy itself does not modify the old system's source `/var`. If +`--cleanup` is also specified, cleanup of the previous install is scheduled for +the next boot only after the preservation copy succeeds. Verify important state +before separately cleaning the old system. + +See the [experimental preserve-var guide](../bootc-experimental-preserve-var.7.md) +for usage details and limitations. + ## Managing configuration: before and after reboot When using `to-existing-root`, there are two distinct scenarios for managing @@ -236,6 +268,18 @@ of migrating the fstab entries. See the "Injecting kernel arguments" section abo Experimental. Name of the global UKI addons to install without the ".addon.efi" suffix. This option can be provided multiple times if multiple addons are to be installed (composefs backend only). On upgrade and switch, global addons already installed are carried forward; passing this flag adds further addons on top +**--experimental** + + Acknowledge that this feature is experimental + +**--preserve-var** + + Experimental. Requires `--experimental`; preserve the running system's `/var` data into the new bootc deployment + +**--preserve-var-skip**=*PATH* + + Relative paths below `/var` to leave out while preserving `/var`. Requires `--preserve-var` + # VERSION diff --git a/hack/provision-packit.sh b/hack/provision-packit.sh index a12a1a674e..905d51981c 100755 --- a/hack/provision-packit.sh +++ b/hack/provision-packit.sh @@ -15,6 +15,7 @@ trap 'rm -rf -- "$BOOTC_TEMPDIR"' EXIT cp -a . "$BOOTC_TEMPDIR" # Keep testing farm run folder +mkdir -p /var/ARTIFACTS cp -r /var/ARTIFACTS "$BOOTC_TEMPDIR" # Copy bootc repo @@ -95,6 +96,9 @@ fi # Keep these in sync with what's used in hack/lbi; ubi9/podman is pinned, see hack/lbi/podman.image podman pull -q --retry 5 --retry-delay 5s quay.io/curl/curl:latest quay.io/curl/curl-base:latest registry.access.redhat.com/ubi9/podman:9.8-1793032728 -# Run system-reinstall-bootc # TODO make it more scriptable instead of expect + send -./system-reinstall-bootc.exp +if [[ ${BOOTC_PROVISION_PACKIT_SKIP_REINSTALL:-0} == 1 ]]; then + echo "BOOTC_PROVISION_PACKIT_SKIP_REINSTALL=1: skipping system-reinstall-bootc conversion and reboot" +else + ./system-reinstall-bootc.exp +fi diff --git a/tmt/README.md b/tmt/README.md index 6744b17269..c5b0050fc2 100644 --- a/tmt/README.md +++ b/tmt/README.md @@ -4,3 +4,28 @@ In the bootc CI, integration tests are executed via Packit on the Testing Farm. See [CONTRIBUTING.md](../CONTRIBUTING.md#running-tmt-integration-tests) for instructions on running them locally. + +## Package-mode preserve-var test + +Run the package-mode migration test against either supported local VM image. +The command builds the configured `BOOTC_image` (default `localhost/bootc`), +transfers it as an OCI archive from a sanitized snapshot of the current working +tree, and runs the TMT plan without `sudo`. The Fedora test uses +`quay.io/fedora/fedora-bootc:44` and the CentOS test uses +`quay.io/centos-bootc/centos-bootc:stream10`; set `BOOTC_base` to override either +base image explicitly. + +```bash +just test-tmt-package-mode fedora-44 +just test-tmt-package-mode centos-stream-10 +``` + +The snapshot excludes `.git`, `/target`, `.worktrees`, and local package-mode +archives, while retaining uncommitted source changes. The controller workdir is +created under `$HOME/.cache/bootc-tmt-workdirs` without sudo or symlinks. On a +package-mode guest where `/home` is a real directory, the test stages the full +TMT run directory into the corresponding `/var/home/...` location before +migration; after boot, the normal `/home -> /var/home` view resolves the same +lexical path. Already-persistent `/var` workdirs are not duplicated. The test +checks the persistent path and rejects excluded or nested mounts. TMT run logs +and the testcloud image cache remain under that workdir root. diff --git a/tmt/plans/package-mode.fmf b/tmt/plans/package-mode.fmf new file mode 100644 index 0000000000..854081ef69 --- /dev/null +++ b/tmt/plans/package-mode.fmf @@ -0,0 +1,213 @@ +summary: Package-mode preserve-var integration test +environment: + PACKAGE_SOURCE: $@{package_source} + PACKAGE_MODE_IMAGE_NAME: localhost/bootc + # Keep these comma-separated refs aligned with Justfile's lbi_images. The + # podman LBI uses the signed version rather than the moving :latest tag. + PACKAGE_MODE_LBI_IMAGES: quay.io/curl/curl:latest,quay.io/curl/curl-base:latest,registry.access.redhat.com/ubi9/podman:9.8-1793032728 +provision: + how: virtual + image: $@{test_disk_image} + user: root +prepare: + # The VM cannot share controller Podman storage; TMT syncs the OCI archive + # through its workdir before prepare runs. + - how: install + order: 93 + package: + - podman + - jq + - e2fsprogs + - util-linux + - grubby + - attr + - bootc + - policycoreutils + when: package_source == local + # Packit build-only preparation needs the full integration toolchain. + - how: install + order: 93 + package: + - podman + - skopeo + - jq + - bootc + - system-reinstall-bootc + - zstd + - cpio + - rpm-build + - expect + - ansible-core + - parted + - lvm2 + - dosfstools + - e2fsprogs + - util-linux + - grubby + - attr + - policycoreutils + when: package_source == artifacts + # Reject missing/unknown sources before any mode-specific setup runs. + - how: shell + order: 95 + script: | + case "${PACKAGE_SOURCE:-}" in + artifacts|local) ;; + *) + printf 'package_source must be artifacts or local (got %q)\n' "${PACKAGE_SOURCE:-}" >&2 + exit 1 + ;; + esac + # Load the TMT-synced archive into rootful guest Podman, then clean its copy. + - how: shell + order: 96 + script: | + set -eu + archive_relative=${PACKAGE_MODE_IMAGE_ARCHIVE:-} + case "$archive_relative" in + tmt/plans/.package-mode-local.*/bootc.oci) ;; + *) + printf 'Unexpected PACKAGE_MODE_IMAGE_ARCHIVE path: %s\n' "$archive_relative" >&2 + exit 1 + ;; + esac + archive_directory_relative=${archive_relative%/*} + temporary_name=${archive_directory_relative##*/} + case "$temporary_name" in + .package-mode-local.*) ;; + *) + printf 'Unexpected package-mode archive directory: %s\n' "$archive_directory_relative" >&2 + exit 1 + ;; + esac + if [ "$archive_directory_relative" != "tmt/plans/$temporary_name" ]; then + printf 'Package-mode archive must be directly under tmt/plans: %s\n' "$archive_relative" >&2 + exit 1 + fi + + tree_root=$(realpath -e -- "${TMT_TREE:?TMT_TREE is required}") + if [ ! -d "$tree_root" ]; then + printf 'TMT_TREE is not a directory: %s\n' "$tree_root" >&2 + exit 1 + fi + plans_root=$(realpath -e -- "$tree_root/tmt/plans") + plans_root_prefix=${tree_root%/}/ + if [ "$plans_root" = "$tree_root" ]; then + printf 'TMT plans directory resolves to the worktree root: %s\n' "$plans_root" >&2 + exit 1 + fi + case "$plans_root" in + "$plans_root_prefix"*) ;; + *) + printf 'TMT plans directory resolves outside the worktree: %s\n' "$plans_root" >&2 + exit 1 + ;; + esac + archive_directory=$(realpath -m -- "$tree_root/$archive_directory_relative") + if [ "$archive_directory" != "$plans_root/$temporary_name" ]; then + printf 'Package-mode archive directory resolves outside tmt/plans: %s\n' "$archive_relative" >&2 + exit 1 + fi + cleanup() { + rm -rf -- "$archive_directory" + } + trap cleanup EXIT + trap 'exit 130' INT + trap 'exit 143' TERM + + archive_path=$(realpath -e -- "$archive_directory/bootc.oci") + if [ "$archive_path" != "$archive_directory/bootc.oci" ]; then + printf 'Package-mode archive must not resolve outside its temporary directory: %s\n' "$archive_relative" >&2 + exit 1 + fi + if [ ! -f "$archive_path" ]; then + printf 'Package-mode archive is not a regular file: %s\n' "$archive_relative" >&2 + exit 1 + fi + podman --remote=false load --input "$archive_path" + podman --remote=false image exists "${PACKAGE_MODE_IMAGE_NAME:-localhost/bootc}" + when: package_source == local + - how: shell + order: 98 + script: + - mkdir -p bootc && cp /var/share/test-artifacts/*.src.rpm bootc + # Packit artifacts are already staged in the guest before extraction. + - cd bootc && rpm2cpio *.src.rpm > pkg.cpio && cpio -idmv < pkg.cpio && rm pkg.cpio && rm -f *-vendor.tar.zstd && zstd -d *.tar.zstd && tar -xvf *.tar -C . --strip-components=1 && ls -al + - cd bootc/hack && BOOTC_PROVISION_PACKIT_SKIP_REINSTALL=1 ./provision-packit.sh + when: package_source == artifacts + # Both sources now provide the configured test image in guest rootful storage. + # Pull the LBI references needed to install the image. + - how: shell + order: 99 + script: | + set -eu + image_name=${PACKAGE_MODE_IMAGE_NAME:-localhost/bootc} + podman --remote=false image exists "$image_name" + IFS=, read -r -a lbi_images <<< "${PACKAGE_MODE_LBI_IMAGES:?PACKAGE_MODE_LBI_IMAGES is required}" + for image in "${lbi_images[@]}"; do + podman --remote=false pull "$image" + done + workdir=$(mktemp -d) + trap 'rm -rf -- "$workdir"' EXIT + cat > "$workdir/Containerfile" < "/etc/systemd/system/\$bind_unit" && \ + ln -s "../\$bind_unit" "/etc/systemd/system/local-fs.target.wants/\$bind_unit" && \ + printf "%s\n" \ + '[Unit]' \ + 'Description=bootc TMT preserve-var /var/log filesystem' \ + '' \ + '[Mount]' \ + 'What=/var/lib/bootc-tmt-preserve-var/logfs.img' \ + 'Where=/var/log' \ + 'Type=ext4' \ + 'Options=loop' \ + '' \ + '[Install]' \ + 'WantedBy=local-fs.target' \ + > /etc/systemd/system/var-log.mount && \ + ln -s ../var-log.mount /etc/systemd/system/local-fs.target.wants/var-log.mount && \ + printf "%s\n" \ + '[Unit]' \ + 'Description=bootc TMT preserve-var empty tmpfs' \ + '' \ + '[Mount]' \ + 'What=tmpfs' \ + 'Where=/var/lib/bootc-tmt-preserve-var/empty_tmpfs' \ + 'Type=tmpfs' \ + 'Options=mode=0755,size=16m' \ + '' \ + '[Install]' \ + 'WantedBy=local-fs.target' \ + > "/etc/systemd/system/\$tmpfs_unit" && \ + ln -s "../\$tmpfs_unit" "/etc/systemd/system/local-fs.target.wants/\$tmpfs_unit" + EOF + podman --remote=false build -t localhost/bootc-preserve-var-test "$workdir" + podman --remote=false image exists localhost/bootc-preserve-var-test + when: package_source == artifacts or package_source == local +execute: + how: tmt +discover: + how: fmf + # Keep the plan-local OCI archive in the tree synced to the guest. The + # package-mode runner uses a small sanitized snapshot, so pruning is not needed. + prune: false + test: + - /tmt/tests/test-50-to-existing-root-preserve-var diff --git a/tmt/tests/booted/package-mode/test-to-existing-root-preserve-var.sh b/tmt/tests/booted/package-mode/test-to-existing-root-preserve-var.sh new file mode 100755 index 0000000000..2516c3cd43 --- /dev/null +++ b/tmt/tests/booted/package-mode/test-to-existing-root-preserve-var.sh @@ -0,0 +1,798 @@ +#!/bin/bash +# Both local and Packit runs prepare one package-mode guest, then migrate and +# reboot that same guest. Local TMT workdirs are staged under preserved /var. +set -xeuo pipefail + +target_image=localhost/bootc-preserve-var-test +var_state=/var/lib/bootc-tmt-preserve-var +app_state=$var_state/app-state/state +log_fs_image=$var_state/logfs.img +empty_tmpfs=$var_state/empty_tmpfs +empty_tmpfs_marker=$empty_tmpfs/marker +bind_dir=/var/lib/bootc-tmt-preserve-var-bind +bind_source=$bind_dir/source +bind_target=$bind_dir/target +preflight_mountpoint=$var_state/preflight-special +tmt_plan_data_suffix=/tmt/plans/package-mode/data +tmt_plan_data_sentinel_name=plan-workdir-persistence-sentinel +tmt_artifacts_parent=/var/ARTIFACTS +tmt_test_data_sentinel_name=workdir-persistence-sentinel +tmt_test_data_post_reboot_marker_name=post-reboot-marker +postgres_version_file=/var/lib/pgsql/data/PG_VERSION +freeze_may_be_active=0 +persistent_tmt_root= +persistent_run_dir= + +boot_config_manifest() { + local relative_path + local absolute_path + local digest + local link_target + + while IFS= read -r -d '' relative_path; do + absolute_path=/boot/$relative_path + if [[ -L $absolute_path ]]; then + link_target=$(readlink -- "$absolute_path") + digest=- + if [[ -f $absolute_path ]]; then + digest=$(sha256sum -- "$absolute_path") + digest=${digest%% *} + fi + printf 'symlink %s %q -> %q\n' "$digest" "$relative_path" "$link_target" + else + digest=$(sha256sum -- "$absolute_path") + digest=${digest%% *} + printf 'file %s %q\n' "$digest" "$relative_path" + fi + done < <( + { + find /boot/loader/entries -maxdepth 1 \ + \( -type f -o -type l \) -name '*.conf' \ + -printf 'loader/entries/%f\0' + for relative_path in grub2/grub.cfg grub2/grubenv; do + if [[ -f /boot/$relative_path ]]; then + printf '%s\0' "$relative_path" + fi + done + if [[ -d /boot/efi/EFI/fedora ]]; then + find /boot/efi/EFI/fedora -maxdepth 1 \ + \( -type f -o -type l \) \ + \( -name grub.cfg -o -iname '*.efi' \) \ + -printf 'efi/EFI/fedora/%f\0' + fi + if [[ -d /boot/efi/EFI/BOOT ]]; then + find /boot/efi/EFI/BOOT -maxdepth 1 \ + \( -type f -o -type l \) \ + \( -name grub.cfg -o -iname '*.efi' \) \ + -printf 'efi/EFI/BOOT/%f\0' + fi + } | LC_ALL=C sort -z -u + ) +} + +is_always_excluded_var_path() { + case $1 in + /var/tmp|/var/tmp/*|/var/cache|/var/cache/*|/var/log/journal|/var/log/journal/*|/var/lib/containers|/var/lib/containers/*) + return 0 + ;; + esac + return 1 +} + +verify_tmt_workdir_directory() { + local run_dir=$1 + local canonical_run_dir=$2 + + if [[ ! -d $run_dir || -L $run_dir ]]; then + printf 'TMT workdir is missing or not a real directory: %s\n' "$run_dir" >&2 + return 1 + fi + if is_always_excluded_var_path "$canonical_run_dir"; then + printf 'TMT workdir is excluded from preserve-var: %s\n' \ + "$canonical_run_dir" >&2 + return 1 + fi +} + +cleanup_log_freeze() { + local status=$? + + # Prevent a second signal from interrupting or reentering the thaw attempt. + trap '' HUP INT TERM + trap - EXIT + set +e + if (( freeze_may_be_active )); then + if fsfreeze -u /var/log; then + freeze_may_be_active=0 + else + printf 'Failed to thaw /var/log during cleanup\n' >&2 + if (( status == 0 )); then + status=1 + fi + fi + fi + exit "$status" +} + +get_tmt_run_dir() { + local plan_data=${TMT_PLAN_DATA:-} + local run_dir + local resolved_plan_data + local resolved_run_dir + local workdir_root=${PACKAGE_MODE_TMT_WORKDIR_ROOT:-} + local resolved_workdir_root + local run_id + + if [[ $plan_data != *"$tmt_plan_data_suffix" ]]; then + printf 'TMT_PLAN_DATA must end in %s (got %q)\n' "$tmt_plan_data_suffix" "$plan_data" >&2 + return 1 + fi + run_dir=${plan_data%"$tmt_plan_data_suffix"} + if [[ -n $workdir_root ]]; then + if [[ $workdir_root != /* || $workdir_root == */ ]]; then + printf 'PACKAGE_MODE_TMT_WORKDIR_ROOT must be an absolute path without a trailing slash (got %q)\n' \ + "$workdir_root" >&2 + return 1 + fi + resolved_workdir_root=$(realpath -e -- "$workdir_root") || { + printf 'TMT workdir root does not exist: %s\n' "$workdir_root" >&2 + return 1 + } + if [[ $resolved_workdir_root == /var/* ]] && \ + is_always_excluded_var_path "$resolved_workdir_root"; then + printf 'TMT workdir root is excluded from preserve-var: %s\n' \ + "$resolved_workdir_root" >&2 + return 1 + fi + case $run_dir in + "$workdir_root"/run-*) + run_id=${run_dir#"$workdir_root"/} + ;; + "$resolved_workdir_root"/run-*) + run_id=${run_dir#"$resolved_workdir_root"/} + ;; + *) + printf 'Expected TMT run directory under configured root %q (got %q)\n' \ + "$workdir_root" "$run_dir" >&2 + return 1 + ;; + esac + if [[ ! $run_id =~ ^run-[[:alnum:]_.-]+$ ]]; then + printf 'Unexpected TMT run directory under configured root: %q\n' \ + "$run_dir" >&2 + return 1 + fi + resolved_run_dir=$(realpath -e -- "$run_dir") || { + printf 'TMT run directory does not exist: %s\n' "$run_dir" >&2 + return 1 + } + if [[ $resolved_run_dir != "$resolved_workdir_root/$run_id" ]]; then + printf 'TMT run directory must be a real directory below its configured root (got %q -> %q)\n' \ + "$run_dir" "$resolved_run_dir" >&2 + return 1 + fi + verify_tmt_workdir_directory "$run_dir" "$resolved_run_dir" || return 1 + case $resolved_run_dir in + /var/*) verify_tmt_workdir_in_preserved_var "$resolved_run_dir" || return 1 ;; + esac + resolved_plan_data=$(realpath -e -- "$plan_data") || { + printf 'TMT_PLAN_DATA does not exist: %s\n' "$plan_data" >&2 + return 1 + } + if [[ $resolved_plan_data != "$resolved_run_dir$tmt_plan_data_suffix" ]]; then + printf 'TMT_PLAN_DATA resolves outside its run directory (got %q -> %q)\n' \ + "$plan_data" "$resolved_plan_data" >&2 + return 1 + fi + elif [[ $run_dir =~ ^/var/ARTIFACTS/work-[[:alnum:]_.-]+$ ]]; then + resolved_run_dir=$(realpath -e -- "$run_dir") || { + printf 'TMT artifact workdir does not exist: %s\n' "$run_dir" >&2 + return 1 + } + verify_tmt_workdir_directory "$run_dir" "$resolved_run_dir" || return 1 + if [[ $resolved_run_dir != "$run_dir" ]]; then + printf 'TMT artifact workdir must be canonical (got %q -> %q)\n' \ + "$run_dir" "$resolved_run_dir" >&2 + return 1 + fi + case $resolved_run_dir in + /var/*) ;; + *) + printf 'TMT artifact workdir must resolve under /var (got %q)\n' \ + "$resolved_run_dir" >&2 + return 1 + ;; + esac + resolved_plan_data=$(realpath -e -- "$plan_data") || { + printf 'TMT_PLAN_DATA does not exist: %s\n' "$plan_data" >&2 + return 1 + } + if [[ $resolved_plan_data != "$resolved_run_dir$tmt_plan_data_suffix" ]]; then + printf 'TMT_PLAN_DATA resolves outside its run directory (got %q -> %q)\n' \ + "$plan_data" "$resolved_plan_data" >&2 + return 1 + fi + else + printf 'Expected TMT workdir under PACKAGE_MODE_TMT_WORKDIR_ROOT or %s (got %q)\n' \ + "$tmt_artifacts_parent" "$run_dir" >&2 + return 1 + fi + case ${TMT_REBOOT_COUNT:-0} in + 0|1|2) ;; + *) + printf 'Unexpected TMT_REBOOT_COUNT=%s\n' "${TMT_REBOOT_COUNT}" >&2 + return 1 + ;; + esac + printf '%s\n' "$run_dir" +} + +verify_tmt_workdir_in_preserved_var() { + local run_dir=$1 + local resolved_run_dir + local mount_target + + if ! resolved_run_dir=$(realpath -e -- "$run_dir"); then + printf 'TMT workdir path does not exist: %s\n' "$run_dir" >&2 + return 1 + fi + case $resolved_run_dir in + /var/*) ;; + *) + printf 'TMT workdir must resolve under preserved /var (got %q -> %q)\n' \ + "$run_dir" "$resolved_run_dir" >&2 + return 1 + ;; + esac + if is_always_excluded_var_path "$resolved_run_dir"; then + printf 'TMT workdir is excluded from preserve-var: %s\n' \ + "$resolved_run_dir" >&2 + return 1 + fi + if ! mount_target=$(findmnt --noheadings --output TARGET --target "$resolved_run_dir"); then + printf 'Could not determine the filesystem containing TMT workdir %s\n' \ + "$run_dir" >&2 + return 1 + fi + # A nested mount is excluded from preserve-var even if it is below /var. + if [[ $mount_target != / && $mount_target != /var ]]; then + printf 'TMT workdir %s is on nested mount %s, which --preserve-var does not copy\n' \ + "$run_dir" "$mount_target" >&2 + return 1 + fi +} + +prepare_tmt_workdir_persistence() { + local run_dir=$1 + local resolved_run_dir + local canonical_plan_data + local canonical_test_data + local canonical_metadata + local stage_parent + local stage_run_dir + local plan_sentinel + local test_sentinel + local data_path + + resolved_run_dir=$(realpath -e -- "$run_dir") || return 1 + set_persistent_tmt_paths "$run_dir" || return 1 + + canonical_plan_data=$(realpath -e -- "$TMT_PLAN_DATA") || return 1 + canonical_test_data=$(realpath -e -- "$TMT_TEST_DATA") || return 1 + canonical_metadata=$(realpath -e -- "$TMT_TEST_METADATA") || return 1 + for data_path in "$canonical_plan_data" "$canonical_test_data" "$canonical_metadata"; do + case $data_path in + "$resolved_run_dir"/*) ;; + *) + printf 'TMT data path is outside its run directory: %s\n' \ + "$data_path" >&2 + return 1 + ;; + esac + done + if [[ ! -d $canonical_plan_data || ! -d $canonical_test_data || ! -f $canonical_metadata ]]; then + printf 'TMT plan data, test data, or metadata is missing from %s\n' \ + "$resolved_run_dir" >&2 + return 1 + fi + plan_sentinel=$canonical_plan_data/$tmt_plan_data_sentinel_name + test_sentinel=$canonical_test_data/$tmt_test_data_sentinel_name + if [[ ! -f $plan_sentinel || ! -f $test_sentinel || \ + $(<"$plan_sentinel") != plan-workdir-persistence-sentinel || \ + $(<"$test_sentinel") != workdir-persistence-sentinel ]]; then + printf 'TMT source plan/test data sentinels do not match expected values\n' >&2 + return 1 + fi + + if is_always_excluded_var_path "$persistent_run_dir"; then + printf 'Persistent TMT workdir is excluded from preserve-var: %s\n' \ + "$persistent_run_dir" >&2 + return 1 + fi + if [[ $resolved_run_dir != "$persistent_run_dir" ]]; then + install -d -m 0755 -- "$persistent_tmt_root" + verify_tmt_workdir_in_preserved_var "$persistent_tmt_root" + if [[ -L $persistent_run_dir ]]; then + printf 'Refusing symlink at persistent TMT workdir destination: %s\n' \ + "$persistent_run_dir" >&2 + return 1 + elif [[ -e $persistent_run_dir ]]; then + printf 'Refusing to overwrite existing persistent TMT workdir: %s\n' \ + "$persistent_run_dir" >&2 + return 1 + fi + + stage_parent=$(mktemp -d -- "$persistent_tmt_root/.${persistent_run_dir##*/}.stage.XXXXXX") + stage_run_dir=$stage_parent/${persistent_run_dir##*/} + ( + set -euo pipefail + trap 'rm -rf -- "$stage_parent"' EXIT + cp --reflink=auto -a -- "$resolved_run_dir" "$stage_run_dir" + cmp -- "$canonical_metadata" "$stage_run_dir/${canonical_metadata#"$resolved_run_dir"/}" + cmp -- "$plan_sentinel" \ + "$stage_run_dir/${canonical_plan_data#"$resolved_run_dir"/}/$tmt_plan_data_sentinel_name" + cmp -- "$test_sentinel" \ + "$stage_run_dir/${canonical_test_data#"$resolved_run_dir"/}/$tmt_test_data_sentinel_name" + if [[ -L $persistent_run_dir ]]; then + printf 'Refusing symlink at persistent TMT workdir destination: %s\n' \ + "$persistent_run_dir" >&2 + exit 1 + elif [[ -e $persistent_run_dir ]]; then + printf 'Refusing to overwrite existing persistent TMT workdir: %s\n' \ + "$persistent_run_dir" >&2 + exit 1 + fi + mv -Tn -- "$stage_run_dir" "$persistent_run_dir" + if [[ -e $stage_run_dir || -L $persistent_run_dir || ! -d $persistent_run_dir ]]; then + printf 'Could not publish staged TMT workdir at %s\n' "$persistent_run_dir" >&2 + exit 1 + fi + ) + fi + if [[ -L $persistent_run_dir || ! -d $persistent_run_dir ]]; then + printf 'Persistent TMT workdir is missing or is a symlink: %s\n' \ + "$persistent_run_dir" >&2 + return 1 + fi + if [[ $(realpath -e -- "$persistent_run_dir") != "$persistent_run_dir" ]]; then + printf 'Persistent TMT workdir does not resolve to its canonical destination: %s\n' \ + "$persistent_run_dir" >&2 + return 1 + fi + cmp -- "$canonical_metadata" "$persistent_run_dir/${canonical_metadata#"$resolved_run_dir"/}" + cmp -- "$plan_sentinel" \ + "$persistent_run_dir/${canonical_plan_data#"$resolved_run_dir"/}/$tmt_plan_data_sentinel_name" + cmp -- "$test_sentinel" \ + "$persistent_run_dir/${canonical_test_data#"$resolved_run_dir"/}/$tmt_test_data_sentinel_name" + verify_tmt_workdir_in_preserved_var "$persistent_run_dir" +} + +set_persistent_tmt_paths() { + local run_dir=$1 + local workdir_root=${PACKAGE_MODE_TMT_WORKDIR_ROOT:-} + local canonical_root + local canonical_run_dir + local run_id + + if [[ -n $workdir_root ]]; then + canonical_root=$(realpath -e -- "$workdir_root") || { + printf 'TMT workdir root does not exist: %s\n' "$workdir_root" >&2 + return 1 + } + case $run_dir in + "$workdir_root"/run-*) run_id=${run_dir#"$workdir_root"/} ;; + "$canonical_root"/run-*) run_id=${run_dir#"$canonical_root"/} ;; + *) + printf 'TMT run directory is not a direct run-* child of its root: %s\n' \ + "$run_dir" >&2 + return 1 + ;; + esac + if [[ ! $run_id =~ ^run-[[:alnum:]_.-]+$ ]]; then + printf 'Unexpected TMT run ID: %q\n' "$run_id" >&2 + return 1 + fi + else + canonical_root=$(realpath -e -- "$tmt_artifacts_parent") || { + printf 'TMT artifact root does not exist: %s\n' "$tmt_artifacts_parent" >&2 + return 1 + } + if [[ $canonical_root != "$tmt_artifacts_parent" || + ! $run_dir =~ ^/var/ARTIFACTS/work-[[:alnum:]_.-]+$ ]]; then + printf 'Unexpected TMT artifact root or run directory: %q\n' "$run_dir" >&2 + return 1 + fi + run_id=${run_dir##*/} + fi + + canonical_run_dir=$(realpath -e -- "$run_dir") || { + printf 'TMT run directory does not exist: %s\n' "$run_dir" >&2 + return 1 + } + if [[ $canonical_run_dir != "$canonical_root/$run_id" ]]; then + printf 'TMT run directory is not the canonical direct child of its root (%q -> %q)\n' \ + "$run_dir" "$canonical_run_dir" >&2 + return 1 + fi + + case $canonical_root in + /home/*) persistent_tmt_root=/var/home/${canonical_root#/home/} ;; + /var/home/*|/var/ARTIFACTS|/var/ARTIFACTS/*) persistent_tmt_root=$canonical_root ;; + *) + printf 'Unsupported canonical TMT root; expected /home, /var/home, or /var/ARTIFACTS: %s\n' \ + "$canonical_root" >&2 + return 1 + ;; + esac + persistent_run_dir=$persistent_tmt_root/$run_id +} + +is_local_tmt_workdir() { + local workdir_root=${PACKAGE_MODE_TMT_WORKDIR_ROOT:-} + local resolved_workdir_root + + [[ -n $workdir_root ]] || return 1 + if [[ $1 == "$workdir_root"/run-* ]]; then + return 0 + fi + resolved_workdir_root=$(realpath -e -- "$workdir_root") || return 1 + [[ $1 == "$resolved_workdir_root"/run-* ]] +} + +case ${TMT_REBOOT_COUNT:-0} in + 0) + test ! -e /run/ostree-booted + # Always request the disabled source boot; this is harmless when the + # initial VM already has the argument and makes reboot accounting stable. + grubby --update-kernel=ALL --args=selinux=0 + tmt-reboot + ;; + 1) + podman --remote=false image exists "$target_image" + + # This must be a real HostDisabled boot, not setenforce or a container + # label override. /proc/1/root checks the host's SELinux filesystem. + test ! -e /run/ostree-booted + grep -Eq '(^|[[:space:]])selinux=0([[:space:]]|$)' /proc/cmdline + test "$(getenforce)" = Disabled + test ! -e /proc/1/root/sys/fs/selinux/enforce + + tmt_run_dir=$(get_tmt_run_dir) + if [[ ! -d $tmt_run_dir || -L $tmt_run_dir ]]; then + printf 'TMT workdir is missing or not a real directory: %s\n' "$tmt_run_dir" >&2 + exit 1 + fi + + test ! -e "$app_state" + test ! -e "$postgres_version_file" + install -d -m 0755 "$var_state" "${app_state%/*}" "$bind_source" "$bind_target" + printf '%s\n' preserved-application-state > "$app_state" + printf '%s\n' bind-source-data > "$bind_source/sentinel" + printf '%s\n' bind-mounted-data > "$bind_source/mounted-sentinel" + printf '%s\n' underlying-target-data > "$bind_target/underlying-sentinel" + mount --bind "$bind_source" "$bind_target" + test "$(<"$bind_target/mounted-sentinel")" = bind-mounted-data + test ! -e "$bind_target/underlying-sentinel" + + # Seed a database using the new deployment's own PostgreSQL version so + # the test also works when the guest and bootc base use different OSes. + podman --remote=false run --rm "$target_image" test -d /var/lib/pgsql + install -d -m 0755 /var/lib/pgsql + podman --remote=false run --rm \ + -v /var/lib/pgsql:/var/lib/pgsql "$target_image" \ + bash -euo pipefail -c ' + postgres_uid=$(id -u postgres) + postgres_gid=$(id -g postgres) + chown "$postgres_uid:$postgres_gid" /var/lib/pgsql + chmod 0700 /var/lib/pgsql + test "$(stat -c %u:%g /var/lib/pgsql)" = "$postgres_uid:$postgres_gid" + test "$(stat -c %a /var/lib/pgsql)" = 700 + runuser -u postgres -- initdb -D /var/lib/pgsql/data + if ! runuser -u postgres -- pg_ctl -D /var/lib/pgsql/data \ + -l /var/lib/pgsql/server.log \ + -o "-c listen_addresses=localhost -c unix_socket_directories=/tmp" -w start + then + cat /var/lib/pgsql/server.log >&2 + exit 1 + fi + runuser -u postgres -- psql -h 127.0.0.1 -v ON_ERROR_STOP=1 \ + -c "CREATE DATABASE bootc_preserve_var" + runuser -u postgres -- psql -h 127.0.0.1 -v ON_ERROR_STOP=1 -d bootc_preserve_var \ + -c "CREATE TABLE app_state (id integer PRIMARY KEY, value text NOT NULL); \ + INSERT INTO app_state VALUES (1, \$\$postgres-state-preserved\$\$)" + runuser -u postgres -- pg_ctl -D /var/lib/pgsql/data -m fast -w stop + ' + test -f "$postgres_version_file" + if getfattr --only-values -n security.selinux "$app_state" >/dev/null 2>&1; then + printf 'Application state unexpectedly has a SELinux label on the disabled source: %s\n' \ + "$app_state" >&2 + exit 1 + fi + if getfattr --only-values -n security.selinux "$postgres_version_file" >/dev/null 2>&1; then + printf 'PostgreSQL state unexpectedly has a SELinux label on the disabled source: %s\n' \ + "$postgres_version_file" >&2 + exit 1 + fi + + # Exercise source preflight with a nested mount whose source is a + # device node. No target boot configuration may be changed on failure. + truncate -s 128M "$log_fs_image" + mkfs.ext4 -F -m 0 "$log_fs_image" + if mountpoint -q /var/log; then + printf '/var/log is already a mountpoint before test setup\n' >&2 + exit 1 + fi + mount -o loop "$log_fs_image" /var/log + printf '%s\n' separate-var-log-filesystem > /var/log/bootc-tmt-sentinel + install -d -m 0755 "$empty_tmpfs" + mount -t tmpfs -o size=16m '' "$empty_tmpfs" + test -z "$(find "$empty_tmpfs" -mindepth 1 -print -quit)" + printf '%s\n' transient-tmpfs-data > "$empty_tmpfs_marker" + test "$(<"$empty_tmpfs_marker")" = transient-tmpfs-data + : > "$preflight_mountpoint" + mount --bind /dev/null "$preflight_mountpoint" + + # The package-mode baseline has a normal BLS entry; migration creates + # the OSTree-prefixed entries later. Snapshot all existing boot paths. + baseline_bls_entry=$(find /boot/loader/entries -maxdepth 1 -type f \ + -name '*.conf' ! -name 'ostree-*.conf' -print -quit) + if [[ -z $baseline_bls_entry ]]; then + printf 'No BLS entry found under /boot/loader/entries before migration\n' >&2 + exit 1 + fi + if [[ ! -f /boot/grub2/grub.cfg ]]; then + printf 'Expected GRUB configuration is missing: /boot/grub2/grub.cfg\n' >&2 + exit 1 + fi + + boot_snapshot=$(mktemp /run/boot-config-before.XXXXXX) + boot_snapshot_after=$(mktemp /run/boot-config-after.XXXXXX) + boot_config_manifest > "$boot_snapshot" + if podman --remote=false run --rm --privileged --pid=host --user=root:root \ + -v /dev:/dev \ + -v /:/target \ + -v /boot:/target/boot \ + -v /var/lib/containers:/var/lib/containers \ + --security-opt label=type:unconfined_t \ + "$target_image" \ + bootc install to-existing-root --experimental --preserve-var --acknowledge-destructive \ + > /run/preserve-var-preflight.log 2>&1; then + printf 'preserve-var unexpectedly accepted the special-file mountpoint\n' >&2 + exit 1 + fi + boot_config_manifest > "$boot_snapshot_after" + if ! diff -u --label 'boot manifest before preflight' \ + --label 'boot manifest after preflight' "$boot_snapshot" "$boot_snapshot_after"; then + printf 'Boot-critical file contents or paths changed during rejected preflight\n' >&2 + exit 1 + fi + grep -F "$preflight_mountpoint is a mountpoint that is neither a directory nor a file" \ + /run/preserve-var-preflight.log + umount "$preflight_mountpoint" + rm -f "$preflight_mountpoint" "$boot_snapshot" "$boot_snapshot_after" + + # Add a sentinel before copying so the full plan/test data and metadata + # are verified in the persistent copy, not just an empty run directory. + test -f "$TMT_TEST_METADATA" + tmt_plan_data_sentinel=$TMT_PLAN_DATA/$tmt_plan_data_sentinel_name + test ! -e "$tmt_plan_data_sentinel" + printf '%s\n' plan-workdir-persistence-sentinel > "$tmt_plan_data_sentinel" + tmt_test_data_sentinel=$TMT_TEST_DATA/$tmt_test_data_sentinel_name + test ! -e "$tmt_test_data_sentinel" + printf '%s\n' workdir-persistence-sentinel > "$tmt_test_data_sentinel" + prepare_tmt_workdir_persistence "$tmt_run_dir" + if [[ -n $persistent_run_dir ]]; then + verify_tmt_workdir_in_preserved_var "$persistent_run_dir" + fi + + # TMT connects as root. Preserve its authorized key in /var so it can + # reconnect after migration; the target policy relabeler labels it. + install -d -m 0700 /var/roothome/.ssh + install -m 0600 /root/.ssh/authorized_keys /var/roothome/.ssh/authorized_keys + + # Keep the backing image for the separate /var/log filesystem stable + # while preserve-var copies its image file from /var/lib. + trap cleanup_log_freeze EXIT + trap 'exit 129' HUP + trap 'exit 130' INT + trap 'exit 143' TERM + # Set before invoking fsfreeze: it may freeze successfully and still + # return failure or be interrupted before the shell observes success. + freeze_may_be_active=1 + fsfreeze -f /var/log + podman --remote=false run --rm --privileged --pid=host --user=root:root \ + -v /dev:/dev \ + -v /:/target \ + -v /boot:/target/boot \ + -v /var/lib/containers:/var/lib/containers \ + --security-opt label=type:unconfined_t \ + "$target_image" \ + bootc install to-existing-root --experimental --preserve-var --acknowledge-destructive + if fsfreeze -u /var/log; then + freeze_may_be_active=0 + else + status=$? + printf 'Failed to thaw /var/log after migration\n' >&2 + exit "$status" + fi + + # Find the generated stateroot by the copied bind-source sentinel. + new_var= + for candidate in /ostree/deploy/*/var; do + if [[ -f $candidate${bind_source#/var}/sentinel ]]; then + new_var=$candidate + break + fi + done + test -n "$new_var" + test "$(<"$new_var${bind_source#/var}/sentinel")" = bind-source-data + test "$(<"$new_var${app_state#/var}")" = preserved-application-state + test -f "$new_var${log_fs_image#/var}" + test "$(getfattr --only-values -n security.selinux "$new_var${app_state#/var}")" = \ + "$(podman --remote=false run --rm "$target_image" matchpathcon -n "$app_state")" + test "$(getfattr --only-values -n security.selinux "$new_var${postgres_version_file#/var}")" = \ + "$(podman --remote=false run --rm "$target_image" matchpathcon -n "$postgres_version_file")" + bind_target_in_new_var=$new_var${bind_target#/var} + test -d "$bind_target_in_new_var" + test -z "$(find "$bind_target_in_new_var" -mindepth 1 -print -quit)" + + # to-existing-root does not migrate the old host's /etc. Inject + # test-only SSH host keys and TMT tmpfiles config into the new /etc. + # The host-key generation is only for this VM test, not product behavior. + podman --remote=false run --rm --privileged --pid=host --user=root:root \ + -v /dev:/dev \ + -v /:/target \ + -v /boot:/target/boot \ + -v /var/lib/containers:/var/lib/containers \ + --security-opt label=type:unconfined_t \ + "$target_image" \ + bash -euo pipefail -c ' + deployment=/run/bootc-tmt-deployment + cleanup() { + local status=$? + trap - EXIT + if ! umount -R "$deployment"; then + printf "Failed to unmount deployment target %s\n" "$deployment" >&2 + status=1 + fi + exit "$status" + } + trap cleanup EXIT + + local_workdir_root=$1 + run_dir=$2 + workdir_root=$3 + mkdir -p "$deployment" + bootc install mount --sysroot /target --latest "$deployment" + + # New /etc must provide keys because old /etc/ssh is not migrated. + install -d -m 0755 "$deployment/etc/ssh" + ssh-keygen -q -t ed25519 -N "" \ + -f "$deployment/etc/ssh/ssh_host_ed25519_key" + # restorecon cannot write labels when the source kernel disables SELinux. + for ssh_path in \ + /etc/ssh/ssh_host_ed25519_key \ + /etc/ssh/ssh_host_ed25519_key.pub; do + expected_label=$(chroot "$deployment" matchpathcon -n "$ssh_path") + setfattr -n security.selinux -v "$expected_label" \ + "$deployment$ssh_path" + actual_label=$(getfattr --only-values -n security.selinux \ + "$deployment$ssh_path") + if [[ $actual_label != "$expected_label" ]]; then + printf "Unexpected SELinux label on target SSH key %s: %s (expected %s)\n" \ + "$ssh_path" "$actual_label" "$expected_label" >&2 + exit 1 + fi + done + + if [[ $local_workdir_root == 1 ]]; then + rule=$deployment/etc/tmpfiles.d/bootc-tmt-workdir.conf + install -d -m 0755 "${rule%/*}" + tmpfiles_rule=( + "d $workdir_root - - - - -" + "d $run_dir - - - - -" + ) + printf "%s\n" "${tmpfiles_rule[@]}" > "$rule" + printf "%s\n" "${tmpfiles_rule[@]}" | cmp - "$rule" + fi + ' install-test-configuration \ + "$(is_local_tmt_workdir "$tmt_run_dir" && printf 1 || printf 0)" \ + "$(if [[ -n $persistent_run_dir ]]; then printf '%s' "$persistent_run_dir"; else realpath -e -- "$tmt_run_dir"; fi)" \ + "$(if [[ -n $persistent_tmt_root ]]; then printf '%s' "$persistent_tmt_root"; elif [[ -n ${PACKAGE_MODE_TMT_WORKDIR_ROOT:-} ]]; then realpath -e -- "$PACKAGE_MODE_TMT_WORKDIR_ROOT"; fi)" + + # New target boot entries are generated by the migration; remove the + # source-only HostDisabled argument before rebooting into the target. + grubby --update-kernel=ALL --remove-args=selinux=0 + if grubby --info=ALL | grep -Eq '(^|[[:space:]"])selinux=0([[:space:]"]|$)'; then + printf 'selinux=0 remains in a target boot entry after migration\n' >&2 + exit 1 + fi + + bootc status + # Reboot the guest itself; this avoids testcloud's ACPI soft-poweroff. + tmt-reboot -c 'systemctl reboot' + ;; + 2) + test -e /run/ostree-booted + ! grep -Eq '(^|[[:space:]])selinux=0([[:space:]]|$)' /proc/cmdline + test "$(getenforce)" = Enforcing + test -e /proc/1/root/sys/fs/selinux/enforce + test "$(&2 + exit 1 + fi + if is_local_tmt_workdir "$tmt_run_dir"; then + test -d "$tmt_run_dir" + test ! -L "$tmt_run_dir" + verify_tmt_workdir_in_preserved_var "$tmt_run_dir" + expected_tmpfiles_rule=( + "d $persistent_tmt_root - - - - -" + "d $persistent_run_dir - - - - -" + ) + printf "%s\n" "${expected_tmpfiles_rule[@]}" | \ + cmp - /etc/tmpfiles.d/bootc-tmt-workdir.conf + else + test -d "$tmt_run_dir" + test ! -L "$tmt_run_dir" + verify_tmt_workdir_in_preserved_var "$tmt_run_dir" + fi + + test -d "$TMT_PLAN_DATA" + test -d "$TMT_TEST_DATA" + test -f "$TMT_TEST_METADATA" + tmt_plan_data_sentinel=$TMT_PLAN_DATA/$tmt_plan_data_sentinel_name + tmt_test_data_sentinel=$TMT_TEST_DATA/$tmt_test_data_sentinel_name + tmt_test_data_post_reboot_marker=$TMT_TEST_DATA/$tmt_test_data_post_reboot_marker_name + test "$(<"$tmt_plan_data_sentinel")" = plan-workdir-persistence-sentinel + test "$(<"$tmt_test_data_sentinel")" = workdir-persistence-sentinel + printf '%s\n' post-reboot-workdir-writable > "$tmt_test_data_post_reboot_marker" + test "$(<"$tmt_test_data_post_reboot_marker")" = post-reboot-workdir-writable + + mountpoint -q "$bind_target" + test "$(<"$bind_target/mounted-sentinel")" = bind-mounted-data + test ! -e "$bind_target/underlying-sentinel" + mountpoint -q /var/log + test "$(findmnt --noheadings --output FSTYPE --target /var/log)" = ext4 + test "$(&2 + printf 'Full bootc status:\n%s\n' "$bootc_status" >&2 + exit 1 + fi + ;; + *) + printf 'Unexpected TMT_REBOOT_COUNT=%s\n' "$TMT_REBOOT_COUNT" >&2 + exit 1 + ;; +esac diff --git a/tmt/tests/test-50-to-existing-root-preserve-var.fmf b/tmt/tests/test-50-to-existing-root-preserve-var.fmf new file mode 100644 index 0000000000..83c8b5aafc --- /dev/null +++ b/tmt/tests/test-50-to-existing-root-preserve-var.fmf @@ -0,0 +1,10 @@ +# The VM cannot share controller Podman storage, so TMT syncs the local OCI +# archive to one guest before migration. The test checks preflight failure, +# separate and temporary mounts, stateful data, then reboots and verifies them. +summary: Package-mode preserve-var state and mounts +duration: 45m +adjust: + - when: running_env == image_mode + enabled: false + because: this migration test requires a package-mode target +test: bash booted/package-mode/test-to-existing-root-preserve-var.sh