diff --git a/crates/lib/src/bootc_composefs/boot.rs b/crates/lib/src/bootc_composefs/boot.rs index e755ae797a..8ca9eb6d5b 100644 --- a/crates/lib/src/bootc_composefs/boot.rs +++ b/crates/lib/src/bootc_composefs/boot.rs @@ -2976,6 +2976,7 @@ pub(crate) async fn setup_composefs_boot( &root_setup.physical_root_path, &deploy_id, &crate::spec::ImageReference::from(state.target_imgref.clone()), + None, true, boot_type, boot_digest, diff --git a/crates/lib/src/bootc_composefs/gc.rs b/crates/lib/src/bootc_composefs/gc.rs index 7689839999..30b1217f22 100644 --- a/crates/lib/src/bootc_composefs/gc.rs +++ b/crates/lib/src/bootc_composefs/gc.rs @@ -682,8 +682,14 @@ pub(crate) async fn composefs_gc( if let Some(ini) = read_origin(sysroot, verity)? { // Collect the container image name for containers-storage GC. - if let Some(container_ref) = - ini.get::("origin", ostree_ext::container::deploy::ORIGIN_CONTAINER) + if let Some(container_ref) = ini + .get::( + crate::deploy::ORIGIN_BOOTC, + crate::deploy::ORIGIN_SOURCE_IMAGE, + ) + .or_else(|| { + ini.get::("origin", ostree_ext::container::deploy::ORIGIN_CONTAINER) + }) { // Parse the ostree image reference to extract the bare image name // (e.g. "quay.io/foo:tag" from "ostree-unverified-image:docker://quay.io/foo:tag") @@ -694,6 +700,12 @@ pub(crate) async fn composefs_gc( live_container_images.insert(image_name); } + // A delivery tag may have moved since a rollback deployment was pulled. + // Retain its immutable image ID even if that image no longer has a name. + if let Some(id) = deployment.container_image_id(storage)? { + live_container_images.insert(id); + } + if let Some(manifest_digest_str) = ini.get::(ORIGIN_KEY_IMAGE, ORIGIN_KEY_MANIFEST_DIGEST) { diff --git a/crates/lib/src/bootc_composefs/state.rs b/crates/lib/src/bootc_composefs/state.rs index 57055a3de6..26843542fb 100644 --- a/crates/lib/src/bootc_composefs/state.rs +++ b/crates/lib/src/bootc_composefs/state.rs @@ -236,7 +236,8 @@ pub(crate) fn update_boot_digest_in_origin( /// /// * `root_path` - The root filesystem path (typically `/sysroot`) /// * `deployment_id` - Unique SHA512 hash identifier for this deployment -/// * `imgref` - Container image reference for the deployment +/// * `source_imgref` - Container image reference used to obtain this image +/// * `target_imgref` - Optional reference for subsequent updates /// * `initialize_var` - Whether to populate the shared `/var` from the image /// * `boot_type` - Boot artifact type (`Bls`, `Uki`, or `Aboot`) /// * `boot_digest` - Optional boot digest for verification @@ -254,7 +255,8 @@ pub(crate) fn update_boot_digest_in_origin( pub(crate) async fn write_composefs_state( root_path: &Utf8PathBuf, deployment_id: &Sha512HashValue, - target_imgref: &ImageReference, + source_imgref: &ImageReference, + target_imgref: Option<&ImageReference>, initialize_var: bool, boot_type: BootType, boot_digest: String, @@ -285,13 +287,22 @@ pub(crate) async fn write_composefs_state( allow_missing_fsverity, )?; + let target_imgref = target_imgref.unwrap_or(source_imgref); let imgref = target_imgref.to_image_proxy_ref()?; - - let mut config = tini::Ini::new().section("origin").item( - ORIGIN_CONTAINER, + let mut config = tini::Ini::new() + .section("origin") // TODO (Johan-Liebert1): The image won't always be unverified - format!("ostree-unverified-image:{imgref}"), - ); + .item( + ORIGIN_CONTAINER, + format!("ostree-unverified-image:{imgref}"), + ); + + if source_imgref != target_imgref { + config = config.section(crate::deploy::ORIGIN_BOOTC).item( + crate::deploy::ORIGIN_SOURCE_IMAGE, + source_imgref.to_string(), + ); + } config = config .section(ORIGIN_KEY_BOOT) diff --git a/crates/lib/src/bootc_composefs/switch.rs b/crates/lib/src/bootc_composefs/switch.rs index 336a88594f..90b430859c 100644 --- a/crates/lib/src/bootc_composefs/switch.rs +++ b/crates/lib/src/bootc_composefs/switch.rs @@ -10,7 +10,10 @@ use crate::{ lookup_config_splitstream, validate_update, }, }, - cli::{SwitchOpts, imgref_for_switch}, + cli::{ + SwitchOpts, handle_unchanged_switch, imgref_for_switch, switch_images_unchanged, + target_imgref_for_switch, + }, progress_jsonl::ProgressWriter, store::{BootedComposefs, Storage}, }; @@ -45,12 +48,21 @@ pub(crate) async fn switch_composefs( return apply_upgrade_from_downloaded(storage, booted_cfs, &host, &do_upgrade_opts).await; } - let target = imgref_for_switch(&opts)?; + if opts.target_imgref.is_some() && opts.enforce_container_sigpolicy { + // Parsing a signed reference does not enforce its policy in the composefs pull path. + anyhow::bail!( + "--target-imgref with --enforce-container-sigpolicy is not supported by the composefs backend" + ); + } + + let source = imgref_for_switch(&opts)?; + let target = target_imgref_for_switch(&opts)?; + let origin = target.as_ref().unwrap_or(&source); if let Some(delta) = delta.as_ref() { - delta.validate_image_reference(&target)?; + delta.validate_image_reference(&source)?; } - // There may be an existing failed update with the same name as "target", and + // There may be an existing failed update with the same update origin, and // in that case we don't want the "nothing changed" check below to trigger. // Instead we want to discard the failed attempt and redo the update. let failed_target_matches = host @@ -58,7 +70,7 @@ pub(crate) async fn switch_composefs( .staged .as_ref() .and_then(|entry| entry.image.as_ref()) - .is_some_and(|image| image.image == target); + .is_some_and(|image| &image.image == origin); let discarded_failed_update = discard_failed_aboot_update(storage, &host)?; if discarded_failed_update { // Refresh host to skip discarded update @@ -66,30 +78,56 @@ pub(crate) async fn switch_composefs( } let retry_failed_target = discarded_failed_update && failed_target_matches; - let new_spec = { - let mut new_spec = host.spec.clone(); - new_spec.image = Some(target.clone()); - new_spec + let current = host + .status + .staged + .as_ref() + .or(host.status.booted.as_ref()) + .context("No current deployment")?; + let current_origin = host + .spec + .image + .as_ref() + .context("No image for current deployment")?; + let current_delivery_source = if target.is_some() && origin == current_origin { + let current_origin_file = super::state::read_origin( + &storage.physical_root, + ¤t.require_composefs()?.verity, + )? + .context("No origin for current deployment")?; + current_origin_file + .get::( + crate::deploy::ORIGIN_BOOTC, + crate::deploy::ORIGIN_SOURCE_IMAGE, + ) + .map(|source| { + source + .parse::() + .map(crate::spec::ImageReference::from) + }) + .transpose() + .context("Parsing current deployment's delivery source")? + } else { + None }; - - if new_spec == host.spec && !retry_failed_target { - println!("Image specification is unchanged."); - if opts.apply && host.status.staged.is_some() { - crate::reboot::reboot()?; - } - return Ok(()); + if !retry_failed_target + && switch_images_unchanged( + &source, + target.as_ref(), + current_origin, + current_delivery_source.as_ref(), + )? + { + return handle_unchanged_switch(&opts, &host); } - let Some(target_imgref) = new_spec.image else { - anyhow::bail!("Target image is undefined") - }; - const COMPOSEFS_SWITCH_JOURNAL_ID: &str = "7a6b5c4d3e2f1a0b9c8d7e6f5a4b3c2d1"; tracing::info!( message_id = COMPOSEFS_SWITCH_JOURNAL_ID, bootc.operation = "switch", - bootc.target_image = target_imgref.to_string(), + bootc.target_image = origin.to_string(), + bootc.source_image = source.to_string(), bootc.apply_mode = opts.apply, bootc.download_only = opts.download_opts.download_only, bootc.from_downloaded = opts.download_opts.from_downloaded, @@ -98,22 +136,16 @@ pub(crate) async fn switch_composefs( let repo = &*booted_cfs.repo; - // Use unified storage if explicitly requested, or auto-detect: either the - // target image is already in bootc-owned containers-storage, OR the booted - // image is — which means the user has opted into unified storage and all - // subsequent operations (including switch to a new image) should use it. + // Preserve unified storage across switches, including when the current + // deployment's origin differs from its name in containers-storage. do_upgrade_opts.use_unified = if opts.unified_storage_exp { true } else { - let booted_imgref = host.spec.image.as_ref(); - let booted_unified = if let Some(booted) = booted_imgref { - crate::deploy::image_exists_in_unified_storage(storage, booted).await? - } else { - false - }; - let target_unified = - crate::deploy::image_exists_in_unified_storage(storage, &target_imgref).await?; - booted_unified || target_unified + let current_unified = + crate::deploy::deployment_uses_unified_storage(storage, current).await?; + let source_unified = + crate::deploy::image_exists_in_unified_storage(storage, &source).await?; + current_unified || source_unified }; // With a delta the target is whatever the delta says it is, and we can look @@ -128,7 +160,7 @@ pub(crate) async fn switch_composefs( ) } None => { - let (image, img_config, _) = is_image_pulled(repo, &target_imgref).await?; + let (image, img_config, _) = is_image_pulled(repo, &source).await?; (image, img_config.manifest) } }; @@ -145,7 +177,7 @@ pub(crate) async fn switch_composefs( match action { UpdateAction::Skip => { - println!("No changes in image: {target_imgref:#}"); + println!("No changes in image: {source:#}"); return Ok(()); } @@ -154,7 +186,8 @@ pub(crate) async fn switch_composefs( storage, booted_cfs, &host, - &target_imgref, + &source, + target.as_ref(), &do_upgrade_opts, &manifest, ) @@ -167,7 +200,8 @@ pub(crate) async fn switch_composefs( storage, booted_cfs, &host, - &target_imgref, + &source, + target.as_ref(), &do_upgrade_opts, &manifest, ) diff --git a/crates/lib/src/bootc_composefs/update.rs b/crates/lib/src/bootc_composefs/update.rs index ef2530d798..88a5f953a8 100644 --- a/crates/lib/src/bootc_composefs/update.rs +++ b/crates/lib/src/bootc_composefs/update.rs @@ -327,6 +327,7 @@ pub(crate) async fn do_upgrade( booted_cfs: &BootedComposefs, host: &Host, imgref: &ImageReference, + target_imgref: Option<&ImageReference>, opts: &DoUpgradeOpts<'_>, manifest: &ostree_ext::oci_spec::image::ImageManifest, ) -> Result<()> { @@ -482,6 +483,7 @@ pub(crate) async fn do_upgrade( &Utf8PathBuf::from("/sysroot"), &deploy_id, imgref, + target_imgref, false, boot_type, boot_digest, @@ -668,11 +670,15 @@ pub(crate) async fn upgrade_composefs( // already in bootc-owned containers-storage, OR if the booted image is — // the latter means the user has opted into unified storage and all // subsequent operations should use it. - let current_unified = if let Some(current) = current_image { - crate::deploy::image_exists_in_unified_storage(storage, current).await? - } else { - false - }; + let current_unified = crate::deploy::deployment_uses_unified_storage( + storage, + host.status + .staged + .as_ref() + .or(host.status.booted.as_ref()) + .context("No current deployment")?, + ) + .await?; do_upgrade_opts.use_unified = current_unified || crate::deploy::image_exists_in_unified_storage(storage, booted_imgref).await?; @@ -748,6 +754,7 @@ pub(crate) async fn upgrade_composefs( composefs, &host, booted_imgref, + None, &do_upgrade_opts, &manifest, ) @@ -780,6 +787,7 @@ pub(crate) async fn upgrade_composefs( composefs, &host, booted_imgref, + None, &do_upgrade_opts, &manifest, ) @@ -800,6 +808,7 @@ pub(crate) async fn upgrade_composefs( composefs, &host, booted_imgref, + None, &do_upgrade_opts, &manifest, ) diff --git a/crates/lib/src/cli.rs b/crates/lib/src/cli.rs index 0059f45f82..a1a11c41e0 100644 --- a/crates/lib/src/cli.rs +++ b/crates/lib/src/cli.rs @@ -31,7 +31,7 @@ use indoc::indoc; use ocidir::cap_std::ambient_authority; use ostree::gio; use ostree_container::store::PrepareResult; -use ostree_ext::container as ostree_container; +use ostree_ext::{container as ostree_container, containers_image_proxy}; use ostree_ext::keyfileext::KeyFileExt; use ostree_ext::ostree; @@ -247,6 +247,17 @@ pub(crate) struct SwitchOpts { // This is kinda unfortunate that we can't gate this only for composefs systems #[clap(flatten)] pub(crate) uki_addon_opts: UkiAddonOpts, + + /// Record a different image reference for subsequent updates. + /// + /// The positional image and --transport select where to fetch now. This + /// reference is recorded without fetching it. Requires a transport prefix, + /// such as docker://quay.io/example/os:latest. + /// Repeating the same source and update origin leaves the deployment unchanged. + /// Use --from-downloaded to unlock a deployment staged with --download-only. + #[clap(long, + conflicts_with_all = ["from_downloaded", "mutate_in_place", "from_delta"])] + pub(crate) target_imgref: Option, } /// Finalize a staged composefs deployment. @@ -1541,7 +1552,16 @@ async fn upgrade( // Ensure the bootc storage directory is initialized; the --check path // needs this for update_mtime() and the non-check path needs it for // unified pull detection. - let use_unified = crate::deploy::image_exists_in_unified_storage(storage, imgref).await?; + let use_unified = crate::deploy::image_exists_in_unified_storage(storage, imgref).await? + || crate::deploy::deployment_uses_unified_storage( + storage, + host.status + .staged + .as_ref() + .or(host.status.booted.as_ref()) + .context("No current deployment")?, + ) + .await?; if let Some(delta) = delta.as_deref() { crate::delta::reject_unified_storage(delta, use_unified)?; } @@ -1661,6 +1681,7 @@ async fn upgrade( &spec, prog.clone(), opts.download_opts.download_only, + None, ) .await?; changed = true; @@ -1693,6 +1714,56 @@ async fn upgrade( Ok(()) } +#[context("Getting target imgref for switch")] +pub(crate) fn target_imgref_for_switch(opts: &SwitchOpts) -> Result> { + let Some(target_imgref) = &opts.target_imgref else { + return Ok(None); + }; + + let sigverify = sigpolicy_from_opt(opts.enforce_container_sigpolicy); + let target = ostree_container::OstreeImageReference { + sigverify, + imgref: target_imgref.clone(), + }; + + Ok(Some(ImageReference::from(target).canonicalize()?)) +} + +/// Compare the requested update origin and, for redirected switches, the delivery +/// source recorded by the current deployment. Older deployments without a saved +/// delivery source used their update origin as the source. +pub(crate) fn switch_images_unchanged( + source: &ImageReference, + target: Option<&ImageReference>, + current_origin: &ImageReference, + current_delivery_source: Option<&ImageReference>, +) -> Result { + if target.unwrap_or(source) != current_origin { + return Ok(false); + } + // Ordinary switches compare only the update origin. Provenance matters + // when a target override distinguishes it from a delivery source. + if target.is_none() { + return Ok(true); + } + let current_delivery_source = current_delivery_source.unwrap_or(current_origin); + Ok(source.clone().canonicalize()? == current_delivery_source.clone().canonicalize()?) +} + +pub(crate) fn handle_unchanged_switch(opts: &SwitchOpts, host: &Host) -> Result<()> { + if opts.apply + && let Some(staged) = &host.status.staged + { + anyhow::ensure!( + !staged.download_only, + "Staged deployment is download-only; use bootc switch --from-downloaded --apply" + ); + crate::reboot::reboot()?; + } + println!("Image specification is unchanged."); + Ok(()) +} + #[context("Getting imgref for switch")] pub(crate) fn imgref_for_switch(opts: &SwitchOpts) -> Result { let transport = ostree_container::Transport::try_from(opts.transport.as_str())?; @@ -1722,7 +1793,7 @@ async fn switch_ostree( .await? .map(std::sync::Arc::new); - let (_, host) = crate::status::get_status(booted_ostree)?; + let (deployments, host) = crate::status::get_status(booted_ostree)?; if opts.download_opts.from_downloaded { return apply_from_downloaded_ostree( @@ -1737,8 +1808,13 @@ async fn switch_ostree( .await; } - let target = imgref_for_switch(&opts)?; - let prog: ProgressWriter = opts.progress.try_into()?; + let source = imgref_for_switch(&opts)?; + let target_imgref = target_imgref_for_switch(&opts)?; + let target = target_imgref.as_ref().unwrap_or(&source); + let import_target = target_imgref + .clone() + .map(ostree_container::OstreeImageReference::from); + let prog: ProgressWriter = opts.progress.clone().try_into()?; let cancellable = gio::Cancellable::NONE; let repo = &booted_ostree.repo(); @@ -1749,12 +1825,41 @@ async fn switch_ostree( new_spec }; - if new_spec == host.spec { - println!("Image specification is unchanged."); - if opts.apply && host.status.staged.is_some() { - crate::reboot::reboot()?; + let current = deployments + .staged + .as_ref() + .unwrap_or(&booted_ostree.deployment); + // A non-container OSTree deployment may not have an image origin. + if let Some(current_origin) = host.spec.image.as_ref() { + let current_delivery_source = if target_imgref.is_some() && target == current_origin { + current + .origin() + .map(|origin| { + origin.optional_string( + crate::deploy::ORIGIN_BOOTC, + crate::deploy::ORIGIN_SOURCE_IMAGE, + ) + }) + .transpose()? + .flatten() + .map(|source| { + source + .parse::() + .map(ImageReference::from) + }) + .transpose() + .context("Parsing current deployment's delivery source")? + } else { + None + }; + if switch_images_unchanged( + &source, + target_imgref.as_ref(), + current_origin, + current_delivery_source.as_ref(), + )? { + return handle_unchanged_switch(&opts, &host); } - return Ok(()); } // Log the switch operation to systemd journal @@ -1771,6 +1876,8 @@ async fn switch_ostree( bootc.old_image_reference = old_image, bootc.new_image_reference = &target.image, bootc.new_image_transport = &target.transport, + bootc.source_image_reference = &source.image, + bootc.source_image_transport = &source.transport, "Switching from image {} to {}", old_image, target.image @@ -1784,7 +1891,16 @@ async fn switch_ostree( let use_unified = if opts.unified_storage_exp { true } else { - crate::deploy::image_exists_in_unified_storage(storage, &target).await? + crate::deploy::image_exists_in_unified_storage(storage, &source).await? + || crate::deploy::deployment_uses_unified_storage( + storage, + host.status + .staged + .as_ref() + .or(host.status.booted.as_ref()) + .context("No current deployment")?, + ) + .await? }; if let Some(delta) = delta.as_deref() { crate::delta::reject_unified_storage(delta, use_unified)?; @@ -1793,7 +1909,7 @@ async fn switch_ostree( let fetched = if let Some(delta) = delta.clone() { crate::deploy::pull_delta( repo, - &target, + &source, delta, opts.quiet, prog.clone(), @@ -1803,8 +1919,8 @@ async fn switch_ostree( } else if use_unified { crate::deploy::pull_unified( repo, - &target, - None, + &source, + import_target.as_ref(), opts.quiet, prog.clone(), storage, @@ -1814,8 +1930,8 @@ async fn switch_ostree( } else { crate::deploy::pull( repo, - &target, - None, + &source, + import_target.as_ref(), opts.quiet, prog.clone(), Some(&booted_ostree.deployment), @@ -1836,6 +1952,7 @@ async fn switch_ostree( let stateroot = booted_ostree.stateroot(); let from = MergeState::from_stateroot(storage, &stateroot)?; + crate::deploy::stage( storage, from, @@ -1843,6 +1960,7 @@ async fn switch_ostree( &new_spec, prog.clone(), opts.download_opts.download_only, + Some(&source), ) .await?; @@ -1888,6 +2006,7 @@ async fn switch(opts: SwitchOpts) -> Result<()> { println!("Updated {deployid} to pull from {target}"); return Ok(()); } + let storage = &get_storage().await?; match storage.kind()? { BootedStorageKind::Ostree(booted_ostree) => { @@ -1989,7 +2108,16 @@ async fn edit_ostree( let stateroot = booted_ostree.stateroot(); let from = MergeState::from_stateroot(storage, &stateroot)?; - crate::deploy::stage(storage, from, &fetched, &new_spec, prog.clone(), false).await?; + crate::deploy::stage( + storage, + from, + &fetched, + &new_spec, + prog.clone(), + false, + None, + ) + .await?; storage.update_mtime()?; @@ -2979,6 +3107,142 @@ async fn run_from_opt(opt: Opt) -> Result { mod tests { use super::*; + #[test] + fn test_switch_target_imgref() -> Result<()> { + for (value, transport, image) in [ + ( + "docker://quay.io/example/os:v2", + "registry", + "quay.io/example/os:v2", + ), + ( + "docker://localhost:5000/os:v2", + "registry", + "localhost:5000/os:v2", + ), + ( + "registry:localhost:5000/os:v2", + "registry", + "localhost:5000/os:v2", + ), + ( + "containers-storage:localhost:5000/os:v2", + "containers-storage", + "localhost:5000/os:v2", + ), + ( + "registry:quay.io/example/os:v2", + "registry", + "quay.io/example/os:v2", + ), + ( + "containers-storage:localhost/os:v2", + "containers-storage", + "localhost/os:v2", + ), + ("oci:/var/tmp/os", "oci", "/var/tmp/os"), + ("oci:./5000/os", "oci", "./5000/os"), + ("oci:relative/os", "oci", "relative/os"), + ] { + let Opt::Switch(opts) = Opt::try_parse_from([ + "bootc", + "switch", + "--transport", + "containers-storage", + "--target-imgref", + value, + "localhost/offline:v2", + ])? + else { + unreachable!() + }; + let source = imgref_for_switch(&opts)?; + assert_eq!(source.transport, "containers-storage"); + assert_eq!(source.image, "localhost/offline:v2"); + let parsed = opts.target_imgref.as_ref().unwrap(); + assert_eq!( + parsed.transport, + ostree_container::Transport::try_from(transport)? + ); + assert_eq!(parsed.name, image); + let target = target_imgref_for_switch(&opts)?.unwrap(); + assert_eq!(target.transport, transport); + assert_eq!(target.image, image); + } + for value in [ + "", + "quay.io/example/os", + "quay.io/example/os:v2", + "localhost:5000/os:v2", + "docker:5000/os:v2", + "docker:quay.io/example/os", + "docker://", + "oci:", + "https://quay.io/example/os", + "bad image", + ] { + let error = Opt::try_parse_from([ + "bootc", + "switch", + "--target-imgref", + value, + "localhost/offline:v2", + ]) + .unwrap_err(); + assert_eq!( + error.kind(), + clap::error::ErrorKind::ValueValidation, + "{value}" + ); + } + for args in [ + vec!["--from-downloaded"], + vec!["--mutate-in-place", "localhost/offline:v2"], + vec!["--from-delta", "/var/tmp/delta", "localhost/offline:v2"], + ] { + let mut argv = vec![ + "bootc", + "switch", + "--target-imgref", + "docker://quay.io/example/os", + ]; + argv.extend(args); + assert_eq!( + Opt::try_parse_from(argv).unwrap_err().kind(), + clap::error::ErrorKind::ArgumentConflict, + ); + } + Ok(()) + } + + #[test] + fn test_switch_images_unchanged() -> Result<()> { + let origin = ImageReference::from(ostree_container::OstreeImageReference::try_from( + "ostree-unverified-registry:quay.io/example/os:v2", + )?); + let source = ImageReference::from(ostree_container::OstreeImageReference::try_from( + "ostree-unverified-image:containers-storage:localhost/offline:v2", + )?); + let signed_source = ImageReference { + signature: Some(crate::spec::ImageSignature::ContainerPolicy), + ..source.clone() + }; + // Older deployments have no saved delivery source. Signature policy is + // also part of the reference, even when the image name is unchanged. + for (requested_source, saved_source, unchanged) in [ + (&origin, None, true), + (&source, None, false), + (&source, Some(&source), true), + (&signed_source, Some(&source), false), + ] { + assert_eq!( + switch_images_unchanged(requested_source, Some(&origin), &origin, saved_source)?, + unchanged, + ); + } + Ok(()) + } + #[test] fn test_callname() { use std::os::unix::ffi::OsStrExt; diff --git a/crates/lib/src/deploy.rs b/crates/lib/src/deploy.rs index 1f19afc622..94a01168f7 100644 --- a/crates/lib/src/deploy.rs +++ b/crates/lib/src/deploy.rs @@ -78,6 +78,10 @@ use crate::utils::async_task_with_spinner; // TODO use https://github.com/ostreedev/ostree-rs-ext/pull/493/commits/afc1837ff383681b947de30c0cefc70080a4f87a const BASE_IMAGE_PREFIX: &str = "ostree/container/baseimage/bootc"; +/// Deployment-local provenance; only the normal origin controls future updates. +pub(crate) const ORIGIN_BOOTC: &str = "bootc"; +pub(crate) const ORIGIN_SOURCE_IMAGE: &str = "source-image-reference"; + // Match podman's default registry retry policy. A failed attempt has to rebuild // the importer, so retries are made at the whole-pull boundary instead of for // individual layers. @@ -441,27 +445,34 @@ pub(crate) async fn prune_container_store(sysroot: &Storage) -> Result<()> { let ostree = sysroot.get_ostree()?; let deployments = ostree.deployments(); let mut all_bound_images = Vec::new(); + let mut image_ids = Vec::new(); for deployment in deployments { let bound = crate::boundimage::query_bound_images_for_deployment(ostree, &deployment)?; all_bound_images.extend(bound.into_iter()); // Also include the host image itself // Note: Use just the image name (not the full transport:image format) because // podman's image names don't include the transport prefix. - if let Some(host_image) = crate::status::boot_entry_from_deployment(ostree, &deployment)? - .image - .map(|i| i.image) - { + let entry = crate::status::boot_entry_from_deployment(ostree, &deployment)?; + if let Some(id) = entry.container_image_id(sysroot)? { + image_ids.push(id); + } + if let Some(host_image) = entry.image.map(|i| i.image) { all_bound_images.push(crate::boundimage::BoundImage { image: host_image.image.clone(), auth_file: None, }); } } - // Convert to a hashset of just the image names - let image_names = HashSet::from_iter(all_bound_images.iter().map(|img| img.image.as_str())); + // Names retain bound images; IDs also retain host images with a redirected origin. + let roots = HashSet::from_iter( + all_bound_images + .iter() + .map(|img| img.image.as_str()) + .chain(image_ids.iter().map(String::as_str)), + ); let pruned = sysroot .get_ensure_imgstore()? - .prune_except_roots(&image_names) + .prune_except_roots(&roots) .await?; tracing::debug!("Pruned images: {}", pruned.len()); Ok(()) @@ -685,6 +696,21 @@ pub(crate) async fn image_exists_in_unified_storage( imgstore.exists(&image_ref_str).await } +/// Detect a deployment's stored image even when its pull source and origin differ. +pub(crate) async fn deployment_uses_unified_storage( + store: &Storage, + deployment: &crate::spec::BootEntry, +) -> Result { + // Non-container OSTree deployments have no image to keep in unified storage. + if deployment.image.is_none() { + return Ok(false); + } + let id = deployment + .container_image_id(store)? + .context("No container image associated with deployment")?; + store.get_ensure_imgstore()?.exists(&id).await +} + /// Unified approach: Use bootc's CStorage to pull the image, then prepare from containers-storage. /// This reuses the same infrastructure as LBIs. pub(crate) async fn prepare_for_pull_unified( @@ -706,12 +732,21 @@ pub(crate) async fn prepare_for_pull_unified( &imgref.transport ); - // Pull the image into bootc containers-storage with per-layer - // download progress via the podman native API. - imgstore - .pull_with_progress(&image_ref_str) - .await - .context("Pulling image into bootc containers-storage")?; + // Refresh host-storage deliveries, but preserve the existing pull path for + // images built directly in bootc storage. + if imgref.to_image_proxy_ref()?.transport == ostree_container::Transport::ContainerStorage + && crate::image::image_exists_in_host_storage(&imgref.image).await? + { + imgstore + .pull_from_host_storage(&imgref.image) + .await + .context("Copying image from host containers-storage into bootc storage")?; + } else { + imgstore + .pull_with_progress(&image_ref_str) + .await + .context("Pulling image into bootc containers-storage")?; + } // Now create a containers-storage reference to read from bootc storage tracing::info!("Unified pull: now importing from containers-storage transport"); @@ -746,12 +781,7 @@ pub(crate) async fn prepare_for_pull_unified( } PrepareResult::Ready(p) => p, }; - check_bootc_label(&prep.config); - if let Some(warning) = prep.deprecated_warning() { - ostree_ext::cli::print_deprecated_warning(warning).await; - } - ostree_ext::cli::print_layer_status(&prep); - let layers_to_fetch = prep.layers_to_fetch().collect::>>()?; + let prepared_image = prepared_import_meta(imp, prep).await?; // Log that we're importing a new image from containers-storage const PULLING_NEW_IMAGE_ID: &str = "6d5e4f3a2b1c0d9e8f7a6b5c4d3e2f1a0"; @@ -765,16 +795,6 @@ pub(crate) async fn prepare_for_pull_unified( ostree_imgref ); - let prepared_image = PreparedImportMeta { - imp, - n_layers_to_fetch: layers_to_fetch.len(), - layers_total: prep.all_layers().count(), - bytes_to_fetch: layers_to_fetch.iter().map(|(l, _)| l.layer.size()).sum(), - bytes_total: prep.all_layers().map(|l| l.layer.size()).sum(), - digest: prep.manifest_digest.clone(), - prep, - }; - Ok(PreparedPullResult::Ready(Box::new(prepared_image))) } @@ -1183,6 +1203,17 @@ fn origin_from_imageref(imgref: &ImageReference) -> Result { Ok(origin) } +fn origin_for_switch( + image: &ImageReference, + source: Option<&ImageReference>, +) -> Result { + let origin = origin_from_imageref(image)?; + if let Some(source) = source.filter(|source| *source != image) { + origin.set_string(ORIGIN_BOOTC, ORIGIN_SOURCE_IMAGE, &source.to_string()); + } + Ok(origin) +} + /// The source of data for staging a new deployment #[derive(Debug)] pub(crate) enum MergeState { @@ -1255,6 +1286,7 @@ pub(crate) async fn stage( spec: &RequiredHostSpec<'_>, prog: ProgressWriter, lock_finalization: bool, + source_imgref: Option<&ImageReference>, ) -> Result<()> { // Log the staging operation to systemd journal with comprehensive upgrade information const STAGE_JOURNAL_ID: &str = "8f7a2b1c3d4e5f6a7b8c9d0e1f2a3b4c"; @@ -1333,7 +1365,9 @@ pub(crate) async fn stage( .collect(), }) .await; - let origin = origin_from_imageref(spec.image)?; + + let origin = origin_for_switch(spec.image, source_imgref)?; + crate::deploy::deploy(sysroot, from, image, &origin, lock_finalization).await?; subtask.completed = true; @@ -1640,8 +1674,44 @@ pub(crate) fn fixup_etc_fstab(root: &Dir) -> Result<()> { #[cfg(test)] mod tests { use super::*; + use ostree_ext::keyfileext::KeyFileExt; use tokio::io::{AsyncBufReadExt, BufReader}; + #[test] + fn test_switch_origin_provenance() -> Result<()> { + let target = ImageReference { + image: "quay.io/example/os:v2".into(), + transport: "registry".into(), + signature: None, + }; + let source = ImageReference { + image: "localhost/offline:v2".into(), + transport: "containers-storage".into(), + signature: None, + }; + for (pull_source, expected) in [ + (None, None), + (Some(&target), None), + (Some(&source), Some(source.to_string())), + ] { + let origin = origin_for_switch(&target, pull_source)?; + let reloaded = glib::KeyFile::new(); + reloaded.load_from_data(&origin.to_data(), glib::KeyFileFlags::NONE)?; + assert_eq!( + reloaded.string("origin", ostree_container::deploy::ORIGIN_CONTAINER)?, + target.to_string() + ); + assert_eq!( + reloaded + .optional_string(ORIGIN_BOOTC, ORIGIN_SOURCE_IMAGE)? + .as_deref(), + expected.as_deref() + ); + assert!(!crate::utils::origin_has_rpmostree_stuff(&reloaded)); + } + Ok(()) + } + fn get_blob_failure(error: ostree_ext::containers_image_proxy::GetBlobError) -> anyhow::Error { let error = ostree_ext::containers_image_proxy::Error::BlobError(error); anyhow::Error::from(error).context("Unencapsulating base") diff --git a/crates/lib/src/image.rs b/crates/lib/src/image.rs index 2221db4300..659c8c1464 100644 --- a/crates/lib/src/image.rs +++ b/crates/lib/src/image.rs @@ -39,7 +39,7 @@ pub(crate) const IMAGE_DEFAULT: &str = "localhost/bootc"; /// TODO: Using exit codes to check image existence is not ideal. We should use /// the podman native libpod HTTP API to properly communicate with podman and /// get structured responses. -async fn image_exists_in_host_storage(image: &str) -> Result { +pub(crate) async fn image_exists_in_host_storage(image: &str) -> Result { use tokio::process::Command as AsyncCommand; let mut cmd = AsyncCommand::new(bootc_utils::podman_bin()); cmd.args(["image", "exists", image]); diff --git a/crates/lib/src/install.rs b/crates/lib/src/install.rs index c972c0f226..6417dcf99d 100644 --- a/crates/lib/src/install.rs +++ b/crates/lib/src/install.rs @@ -3202,7 +3202,7 @@ pub(crate) async fn install_reset(opts: InstallResetOpts) -> Result<()> { stateroot: target_stateroot.clone(), kargs, }; - crate::deploy::stage(sysroot, from, &fetched, &spec, prog.clone(), false).await?; + crate::deploy::stage(sysroot, from, &fetched, &spec, prog.clone(), false, None).await?; // Copy /boot entry from /etc/fstab to the new stateroot if it exists if let Some(boot_spec) = read_boot_fstab_entry(rootfs)? { diff --git a/crates/lib/src/podstorage.rs b/crates/lib/src/podstorage.rs index d55aa2d9b2..cd199aadee 100644 --- a/crates/lib/src/podstorage.rs +++ b/crates/lib/src/podstorage.rs @@ -415,16 +415,18 @@ impl CStorage { } #[context("Pruning")] + /// Retain images referenced by a name or a full containers-storage image ID. pub(crate) async fn prune_except_roots(&self, roots: &HashSet<&str>) -> Result> { let all_images = self.list_images().await?; tracing::debug!("Images total: {}", all_images.len(),); let mut garbage = Vec::new(); for image in all_images { - if image - .names - .iter() - .flatten() - .all(|name| !roots.contains(name.as_str())) + if !roots.contains(image.id.as_str()) + && image + .names + .iter() + .flatten() + .all(|name| !roots.contains(name.as_str())) { garbage.push(image.id); } diff --git a/crates/lib/src/status.rs b/crates/lib/src/status.rs index 6399395b6a..6dba5f5089 100644 --- a/crates/lib/src/status.rs +++ b/crates/lib/src/status.rs @@ -290,6 +290,25 @@ pub(crate) fn boot_entry_from_deployment( } impl BootEntry { + /// The containers-storage image ID is the OCI config digest, independent of + /// the name used to pull the image or the deployment's update origin. + pub(crate) fn container_image_id( + &self, + storage: &crate::store::Storage, + ) -> Result> { + if self.image.is_none() { + return Ok(None); + } + if let Some(composefs) = &self.composefs { + let (image, _) = + crate::bootc_composefs::status::get_imginfo(storage, &composefs.verity)?; + return Ok(Some(image.manifest.config().digest().digest().to_owned())); + } + Ok(self + .query_image(&storage.get_ostree()?.repo())? + .map(|image| image.manifest.config().digest().digest().to_owned())) + } + /// Given a boot entry, find its underlying ostree container image pub(crate) fn query_image( &self, diff --git a/crates/ostree-ext/src/container/store.rs b/crates/ostree-ext/src/container/store.rs index 9316d85470..1ee6649c22 100644 --- a/crates/ostree-ext/src/container/store.rs +++ b/crates/ostree-ext/src/container/store.rs @@ -709,6 +709,17 @@ impl ImageImporter { self.target_imgref = Some(target.clone()) } + fn already_present(&self, state: Box) -> Result { + // A cache hit skips import(), but must retain the requested target just + // as a fresh import would. Leave existing source refs to normal cleanup. + if !self.no_imgref + && let Some(target) = &self.target_imgref + { + set_image_reference(&self.repo, &target.imgref, &state.merge_commit)?; + } + Ok(PrepareResult::AlreadyPresent(state)) + } + /// Do not write the final image ref, but do write refs for shared layers. /// This is useful in scenarios where you want to "pre-pull" an image, /// but in such a way that it does not need to be manually removed later. @@ -1087,7 +1098,7 @@ impl ImageImporter { &manifest_digest, manifest.config().digest(), ) { - PreviousState::UpToDate(state) => return Ok(PrepareResult::AlreadyPresent(state)), + PreviousState::UpToDate(state) => return self.already_present(state), PreviousState::Outdated(previous) => previous.unzip(), }; @@ -1124,7 +1135,7 @@ impl ImageImporter { // A digested pull spec, and our existing state matches. if previous_state.manifest_digest == target_digest { tracing::debug!("Digest-based pullspec {:?} already present", self.imgref); - return Ok(PrepareResult::AlreadyPresent(previous_state)); + return self.already_present(previous_state); } Some(previous_state) } else { @@ -1155,7 +1166,7 @@ impl ImageImporter { &manifest_digest, manifest.config().digest(), ) { - PreviousState::UpToDate(state) => return Ok(PrepareResult::AlreadyPresent(state)), + PreviousState::UpToDate(state) => return self.already_present(state), PreviousState::Outdated(previous) => previous.unzip(), }; @@ -1902,6 +1913,19 @@ pub fn query_image_commit(repo: &ostree::Repo, commit: &str) -> Result Result<()> { + repo.set_ref_immediate( + None, + &ref_for_image(imgref)?, + Some(commit), + gio::Cancellable::NONE, + )?; + Ok(()) +} + fn manifest_for_image(repo: &ostree::Repo, imgref: &ImageReference) -> Result { let ostree_ref = ref_for_image(imgref)?; let rev = repo.require_rev(&ostree_ref)?; @@ -1949,15 +1973,7 @@ pub async fn copy( .await?; } - let dest_ostree_ref = ref_for_image(dest_imgref)?; - dest_repo.set_ref_immediate( - None, - &dest_ostree_ref, - Some(&src_commit), - gio::Cancellable::NONE, - )?; - - Ok(()) + set_image_reference(dest_repo, dest_imgref, &src_commit) } /// Options controlling commit export into OCI diff --git a/crates/ostree-ext/tests/it/main.rs b/crates/ostree-ext/tests/it/main.rs index 8f973d3c44..bd14f13049 100644 --- a/crates/ostree-ext/tests/it/main.rs +++ b/crates/ostree-ext/tests/it/main.rs @@ -757,14 +757,70 @@ async fn test_no_fetch_digested() -> Result<()> { .unwrap(); // And the key test, we shouldn't reach out to the registry here imp.set_offline(); + let redirected = OstreeImageReference { + imgref: ImageReference::try_from("docker://quay.io/exampleos/redirected:latest")?, + ..target_imgref.clone() + }; + imp.set_target(&redirected); match imp.prepare().await.context("Init prep derived").unwrap() { store::PrepareResult::AlreadyPresent(_) => {} store::PrepareResult::Ready(_) => panic!("Should have image already"), }; + assert_eq!( + store::query_image(fixture.destrepo(), &redirected.imgref)? + .unwrap() + .merge_commit, + r.merge_commit + ); Ok(()) } +#[tokio::test] +async fn test_cached_import_target() -> Result<()> { + if !check_skopeo() { + return Ok(()); + } + let fixture = Fixture::new_v1()?; + let (source, _) = fixture.export_container().await?; + let imported = fixture.must_import(&source).await?; + let source = OstreeImageReference { + sigverify: SignatureSource::ContainerPolicyAllowInsecure, + imgref: source, + }; + let target = OstreeImageReference { + imgref: ImageReference::try_from("docker://quay.io/exampleos/cached:latest")?, + ..source.clone() + }; + for no_imgref in [true, false] { + let mut imp = + store::ImageImporter::new(fixture.destrepo(), &source, Default::default()).await?; + imp.set_target(&target); + if no_imgref { + imp.set_no_imgref(); + } + let store::PrepareResult::AlreadyPresent(state) = imp.prepare().await? else { + panic!("Cached image must not require another import"); + }; + assert_eq!(state.merge_commit, imported.merge_commit); + let retained = store::query_image(fixture.destrepo(), &target.imgref)?; + if no_imgref { + assert!(retained.is_none()); + } else { + assert_eq!(retained.unwrap().merge_commit, imported.merge_commit); + } + } + // Source aliases remain independently removable; the target retains the layers. + assert!(store::remove_image(fixture.destrepo(), &source.imgref)?); + assert_eq!(store::gc_image_layers(fixture.destrepo())?, 0); + assert!(store::query_image(fixture.destrepo(), &source.imgref)?.is_none()); + assert_eq!(store::list_images(fixture.destrepo())?.len(), 1); + assert!(store::remove_image(fixture.destrepo(), &target.imgref)?); + assert!(store::gc_image_layers(fixture.destrepo())? > 0); + assert!(store::list_images(fixture.destrepo())?.is_empty()); + Ok(()) +} + /// A [`LayerSource`] which reads blobs straight out of an OCI directory, /// standing in for anything that produces layer content without a registry. #[derive(Debug)] @@ -850,10 +906,21 @@ async fn test_prepare_from_manifest() -> Result<()> { // And now it is present, established without ever contacting a registry. let mut imp = store::ImageImporter::new_without_proxy(fixture.destrepo(), &imgref)?; + let target = OstreeImageReference { + imgref: ImageReference::try_from("docker://quay.io/exampleos/from-manifest:latest")?, + ..imgref.clone() + }; + imp.set_target(&target); let prep = imp .prepare_from_manifest(expected_digest, manifest, config, open_source()?) .await?; assert!(matches!(prep, store::PrepareResult::AlreadyPresent(_))); + assert_eq!( + store::query_image(fixture.destrepo(), &target.imgref)? + .unwrap() + .merge_commit, + state.merge_commit + ); Ok(()) } diff --git a/docs/src/man/bootc-switch.8.md b/docs/src/man/bootc-switch.8.md index 5e6b3d3b3b..7dd7d4e766 100644 --- a/docs/src/man/bootc-switch.8.md +++ b/docs/src/man/bootc-switch.8.md @@ -21,6 +21,26 @@ are tracking `:v1.0`, and as a rollout progresses, machines can be switched to ` It is also supported to provide explicit digests, via e.g. `bootc switch quay.io/exampleos/someuser@sha256:9cca0703342e24806a9f64e08c053dca7f2cd90f10529af8ea872afb0a0c77d4`. When you do this, `bootc upgrade` will always be a no-op. In this model, upgrades are then always triggered by further `switch` operations. +Use `--target-imgref` to fetch an image from local container storage or a temporary +registry while recording a different reference for subsequent updates. The +positional image and `--transport` select the source to fetch now; the target +reference is recorded without contacting it and requires a transport prefix, +such as `docker://quay.io/exampleos/myapp:v1.1` or +`containers-storage:localhost/myapp:v1.1`. + +Repeating the same source and target leaves the current deployment unchanged, +even if the temporary source is no longer available. Use `--from-downloaded` to +make a deployment staged with `--download-only` ready for the next boot. Use +`bootc upgrade` to check the recorded update source for newer content. + +On composefs, the existing deployment collision checks still apply: changing the +target reference does not allow switching to content with the same fs-verity +digest as an existing deployment. + +`--target-imgref` cannot be combined with `--from-downloaded`, `--mutate-in-place`, +or `--from-delta`. On composefs, it also cannot be combined with +`--enforce-container-sigpolicy`. + ## Applying Changes The `--apply` option will automatically restart the system if it has @@ -88,6 +108,10 @@ For shared `--apply` and `--soft-reboot` behavior, see 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 +**--target-imgref**=*TARGET_IMGREF* + + Record a different image reference for subsequent updates + # EXAMPLES @@ -104,6 +128,18 @@ Switch with soft reboot if possible: bootc switch --apply --soft-reboot=auto quay.io/exampleos/myapp:v1.1 +Stage a locally loaded image while retaining a registry as the update source: + + bootc switch --download-only --transport containers-storage \ + --target-imgref docker://quay.io/exampleos/myapp:v1.1 localhost/offline:v1.1 + bootc switch --from-downloaded + systemctl reboot + +Fetch from a technician's temporary registry and track the normal registry: + + bootc switch --target-imgref docker://quay.io/exampleos/myapp:v1.1 \ + technician-laptop:5000/myapp:v1.1 + # SEE ALSO **bootc**(8), **bootc-upgrade**(8), **bootc-status**(8), **bootc-rollback**(8) diff --git a/tmt/plans/integration.fmf b/tmt/plans/integration.fmf index b7d1e932f4..014db580a1 100644 --- a/tmt/plans/integration.fmf +++ b/tmt/plans/integration.fmf @@ -441,6 +441,13 @@ execute: - /tmt/tests/tests/test-63-install-shared-propagation extra-fixme_skip_if_composefs: true +/plan-64-switch-target-imgref: + summary: Offline switch preserves the update origin and survives repeated requests + discover: + how: fmf + test: + - /tmt/tests/tests/test-64-switch-target-imgref + /plan-66-encrypted-swap-ordering: summary: Verify a random-key encrypted swap boots without an ordering cycle discover: diff --git a/tmt/tests/booted/test-switch-target-imgref.nu b/tmt/tests/booted/test-switch-target-imgref.nu new file mode 100644 index 0000000000..e4420a7b5c --- /dev/null +++ b/tmt/tests/booted/test-switch-target-imgref.nu @@ -0,0 +1,177 @@ +# number: 64 +# tmt: +# summary: Offline switch preserves the update origin and survives repeated requests +# duration: 40m +# +use std assert +use tap.nu + +const source = "localhost/target-imgref-source:v2" +const origin = "localhost/target-imgref:latest" +const next_origin = "localhost/target-imgref:redirected" +const archive = "/var/tmp/target-imgref-source.tar" +const marker = "/usr/share/testing-target-imgref" + +def status [] { bootc status --json | from json } + +def build_image [version: string, image: string] { + let containerfile = $"FROM localhost/bootc as base\nRUN echo ($version) > ($marker)\n" + (tap make_uki_containerfile $containerfile) | save --force Containerfile + podman build -t $image -f Containerfile . +} + +def stage [] { + bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($origin)" $source +} + +def first_boot [] { + tap begin "offline source and persistent update origin" + bootc image copy-to-storage + cd (mktemp -d) + build_image "v1" "localhost/target-imgref-source:v1" + build_image "v2" $source + + podman save --format oci-archive -o $archive $source + # Normalize the source to the archive's manifest before comparing digests + # across subsequent loads; saving can convert a Docker manifest to OCI. + podman rmi $source + podman load -i $archive + + bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($origin)" localhost/target-imgref-source:v1 + let original_stage = (status).status.staged + if (tap is_composefs) { + let collision = do { bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($next_origin)" localhost/target-imgref-source:v1 } | complete + assert ($collision.exit_code != 0) + assert ($collision.stderr | str contains "same fs-verity digest") + assert equal (status).status.staged $original_stage + } else { + # OSTree can stage cached content with a different update origin. + bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($next_origin)" localhost/target-imgref-source:v1 + assert equal (status).status.staged.image.imageDigest $original_stage.image.imageDigest + assert equal (status).status.staged.image.image.image $next_origin + bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($origin)" localhost/target-imgref-source:v1 + } + let previous = (status).status.staged.image.imageDigest + # A new source must still be processed when the update origin is unchanged. + stage + let staged = (status).status.staged + assert ($staged.image.imageDigest != $previous) + assert equal $staged.image.image.image $origin + assert equal $staged.image.image.transport "containers-storage" + assert $staged.downloadOnly + + # The target is absent. Remove the source too: repeats must use deployment metadata. + podman rmi $source + stage + bootc switch --transport containers-storage $origin + assert equal (status).status.staged $staged + let applied = do { bootc switch --apply --transport containers-storage --target-imgref $"containers-storage:($origin)" $source } | complete + assert ($applied.exit_code != 0) "--apply must not bypass a download-only lock" + assert equal (status).status.staged $staged + + bootc switch --from-downloaded + assert (not (status).status.staged.downloadOnly) + tmt-reboot +} + +def second_boot [] { + let booted = (status).status.booted + let rollback = (status).status.rollback + assert equal $booted.image.image.image $origin + assert equal (open $marker | str trim) "v2" + + # Provenance survives reboot, with both source and target absent. + bootc switch --transport containers-storage --target-imgref $"containers-storage:($origin)" $source + bootc switch --transport containers-storage $origin + assert equal (status).status.booted $booted + assert equal (status).status.staged null + + podman load -i $archive + if (tap is_composefs) { + let collision = do { bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($next_origin)" $source } | complete + assert ($collision.exit_code != 0) + assert ($collision.stderr | str contains "same fs-verity digest") + assert equal (status).status.booted $booted + assert equal (status).status.rollback $rollback + assert equal (status).status.staged null + podman rmi $source + verify_updates $origin + tmt-reboot + return + } + + # OSTree supports a separate deployment of the identical booted image. + bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($next_origin)" $source + assert equal (status).status.booted $booted + assert equal (status).status.staged.image.imageDigest $booted.image.imageDigest + assert equal (status).status.staged.image.image.image $next_origin + assert (status).status.staged.downloadOnly + podman rmi $source + bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($next_origin)" $source + tmt-reboot +} + +def third_boot [] { + assert equal (status).status.booted.image.image.image $origin + # libostree discards an unfinalized staged deployment across reboot. + # Restore it from the offline delivery before explicitly unlocking it. + podman load -i $archive + bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($next_origin)" $source + podman rmi $source + assert equal (status).status.staged.image.image.image $next_origin + assert (status).status.staged.downloadOnly + bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($next_origin)" $source + bootc switch --from-downloaded + assert (not (status).status.staged.downloadOnly) + tmt-reboot +} + +def fourth_boot [] { + let booted = (status).status.booted + assert equal $booted.image.image.image $next_origin + assert equal (status).status.staged null + assert equal (open $marker | str trim) "v2" + bootc switch --transport containers-storage --target-imgref $"containers-storage:($next_origin)" $source + verify_updates $next_origin + tmt-reboot +} + +# Ordinary updates must use the recorded origin after the delivery source is gone. +def verify_updates [update_origin: string] { + let booted = (status).status.booted + podman load -i $archive + podman tag $source $update_origin + podman rmi $source + bootc update + assert equal (status).status.staged null + + cd (mktemp -d) + build_image "v3" $update_origin + bootc update + let staged = (status).status.staged + assert equal $staged.image.image.image $update_origin + assert ($staged.image.imageDigest != $booted.image.imageDigest) +} + +def finish [update_origin: string] { + assert equal (open $marker | str trim) "v3" + assert equal (status).status.booted.image.image.image $update_origin + tap ok +} + +def main [] { + match $env.TMT_REBOOT_COUNT? { + null | "0" => first_boot, + "1" => second_boot, + "2" => { + if (tap is_composefs) { + finish $origin + } else { + third_boot + } + }, + "3" => fourth_boot, + "4" => { finish $next_origin }, + $n => { error make { msg: $"Invalid reboot count ($n)" } }, + } +} diff --git a/tmt/tests/booted/test-switch-to-unified.nu b/tmt/tests/booted/test-switch-to-unified.nu index 4e8b61442e..f924198da2 100644 --- a/tmt/tests/booted/test-switch-to-unified.nu +++ b/tmt/tests/booted/test-switch-to-unified.nu @@ -70,6 +70,18 @@ RUN echo 'unified-storage-test-marker' > /usr/share/unified-storage-test.txt print "Switching to localhost/bootc-unified-derived" bootc switch --transport containers-storage localhost/bootc-unified-derived + # A second delivery exists only in host storage. Switching must copy it into + # bootc storage while retaining a separate update origin. + $"FROM localhost/bootc +RUN echo 'host-storage-test-marker' > /usr/share/unified-storage-test.txt +" | save --force Dockerfile + podman build -t localhost/bootc-host-derived . + let stored = bootc image cmd list --format json --filter reference=localhost/bootc-host-derived | from json + assert equal ($stored | length) 0 + bootc switch --transport containers-storage --target-imgref containers-storage:localhost/bootc-updates localhost/bootc-host-derived + let stored = bootc image cmd list --format json --filter reference=localhost/bootc-host-derived | from json + assert equal ($stored | length) 1 + tmt-reboot } @@ -78,12 +90,14 @@ def third_boot [] { # Verify we're booted from containers-storage transport assert equal $booted.image.transport containers-storage - assert equal $booted.image.image localhost/bootc-unified-derived + assert equal $booted.image.image localhost/bootc-updates # Verify the marker file from our derived image exists assert ("/usr/share/unified-storage-test.txt" | path exists) let marker = open /usr/share/unified-storage-test.txt | str trim - assert equal $marker "unified-storage-test-marker" + assert equal $marker "host-storage-test-marker" + let stored = bootc image cmd list --format json --filter reference=localhost/bootc-host-derived | from json + assert equal ($stored | length) 1 # Verify that bootc storage is accessible print "Listing images in bootc storage:" @@ -94,11 +108,6 @@ def third_boot [] { let images = podman --storage-opt=additionalimagestore=/usr/lib/bootc/storage images --format "{{.Repository}}" print $"Images visible via podman: ($images)" - # The derived image (localhost/bootc-unified-derived) should persist in bootc storage - # since /usr/lib/bootc/storage is a symlink to persistent storage under /sysroot. - # The key verification is that we successfully booted into the derived image, - # which we already confirmed above via transport and image name checks. - tap ok } diff --git a/tmt/tests/tests.fmf b/tmt/tests/tests.fmf index 21aab2ea64..4108da2b29 100644 --- a/tmt/tests/tests.fmf +++ b/tmt/tests/tests.fmf @@ -286,6 +286,11 @@ check: duration: 30m test: nu booted/test-install-shared-propagation.nu +/test-64-switch-target-imgref: + summary: Offline switch preserves the update origin and survives repeated requests + duration: 40m + test: nu booted/test-switch-target-imgref.nu + /test-66-encrypted-swap-ordering: summary: Verify a random-key encrypted swap boots without an ordering cycle duration: 30m