From f73bc7c587f510f109438ce121e6d087244f1a33 Mon Sep 17 00:00:00 2001 From: Pragyan Poudyal Date: Thu, 1 Oct 2026 14:21:08 +0530 Subject: [PATCH 1/8] switch: Add a target reference for future updates Allow switch to accept an update origin separate from the image fetched now. Pass the target through OSTree staging and composefs switch so later updates can follow the recorded reference. Signed-off-by: Pragyan Poudyal Co-authored-by: HarshwardhanPatil07 --- crates/lib/src/bootc_composefs/switch.rs | 6 +++++- crates/lib/src/bootc_composefs/update.rs | 6 +++++- crates/lib/src/cli.rs | 23 ++++++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/crates/lib/src/bootc_composefs/switch.rs b/crates/lib/src/bootc_composefs/switch.rs index 336a88594f..73ce261344 100644 --- a/crates/lib/src/bootc_composefs/switch.rs +++ b/crates/lib/src/bootc_composefs/switch.rs @@ -10,7 +10,7 @@ use crate::{ lookup_config_splitstream, validate_update, }, }, - cli::{SwitchOpts, imgref_for_switch}, + cli::{SwitchOpts, imgref_for_switch, target_imgref_for_switch}, progress_jsonl::ProgressWriter, store::{BootedComposefs, Storage}, }; @@ -133,6 +133,8 @@ pub(crate) async fn switch_composefs( } }; + let final_target_imgref = target_imgref_for_switch(&opts)?; + if let Some(cfg_verity) = image { let action = validate_update( storage, @@ -155,6 +157,7 @@ pub(crate) async fn switch_composefs( booted_cfs, &host, &target_imgref, + final_target_imgref.as_ref(), &do_upgrade_opts, &manifest, ) @@ -168,6 +171,7 @@ pub(crate) async fn switch_composefs( booted_cfs, &host, &target_imgref, + final_target_imgref.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..3ad4f741d6 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<()> { @@ -481,7 +482,7 @@ pub(crate) async fn do_upgrade( write_composefs_state( &Utf8PathBuf::from("/sysroot"), &deploy_id, - imgref, + target_imgref.unwrap_or(imgref), false, boot_type, boot_digest, @@ -748,6 +749,7 @@ pub(crate) async fn upgrade_composefs( composefs, &host, booted_imgref, + None, &do_upgrade_opts, &manifest, ) @@ -780,6 +782,7 @@ pub(crate) async fn upgrade_composefs( composefs, &host, booted_imgref, + None, &do_upgrade_opts, &manifest, ) @@ -800,6 +803,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..21a861ed9f 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,11 @@ 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, + + /// The image to use for subsequent upgrades + /// Useful when switching to an, say containers-storage, image + /// but subsequent updates are to be pulled from registry + pub(crate) target_imgref: Option, } /// Finalize a staged composefs deployment. @@ -1693,6 +1698,21 @@ 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 imgref = containers_image_proxy::ImageReference::try_from(target_imgref.as_str()) + .with_context(|| format!("Parsing imgref {target_imgref}"))?; + + let sigverify = sigpolicy_from_opt(opts.enforce_container_sigpolicy); + let target = ostree_container::OstreeImageReference { sigverify, imgref }; + + Ok(Some(ImageReference::from(target))) +} + #[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())?; @@ -1888,6 +1908,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) => { From 457dc3e9f96e4e38b77acd659ce168c889cc80e7 Mon Sep 17 00:00:00 2001 From: Pragyan Poudyal Date: Tue, 13 Oct 2026 09:47:15 +0530 Subject: [PATCH 2/8] composefs: Record the delivery source for redirected staging Keep the pulled image name alongside a staged deployment when switch chooses a different update origin. This prepares the staged state to distinguish the delivery source from the recorded origin. Signed-off-by: Pragyan Poudyal Co-authored-by: HarshwardhanPatil07 --- crates/lib/src/bootc_composefs/aboot.rs | 1 + crates/lib/src/bootc_composefs/status.rs | 8 ++++++++ crates/lib/src/bootc_composefs/update.rs | 1 + crates/lib/src/cli.rs | 24 ++++++++++++++++++++++-- crates/lib/src/deploy.rs | 8 +++++++- crates/lib/src/install.rs | 2 +- 6 files changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/lib/src/bootc_composefs/aboot.rs b/crates/lib/src/bootc_composefs/aboot.rs index f4b5fdb5bf..f2b8036de8 100644 --- a/crates/lib/src/bootc_composefs/aboot.rs +++ b/crates/lib/src/bootc_composefs/aboot.rs @@ -135,6 +135,7 @@ impl PendingDeployment { StagedDeployment { depl_id: self.depl_id.clone(), finalization_locked: self.finalization_locked, + original_imgref: None, } } } diff --git a/crates/lib/src/bootc_composefs/status.rs b/crates/lib/src/bootc_composefs/status.rs index b0efe2b1a9..b470e5bc60 100644 --- a/crates/lib/src/bootc_composefs/status.rs +++ b/crates/lib/src/bootc_composefs/status.rs @@ -159,6 +159,12 @@ pub(crate) struct StagedDeployment { /// Whether to finalize this staged deployment on reboot or not /// This also maps to `download_only` field in `BootEntry` pub(crate) finalization_locked: bool, + /// This is the image that's pulled into the composefs repository + /// Might or might not be the same as the imgref in the ".origin" file + /// because of the `--target-imgref` flag in `bootc switch` + /// + /// Is only `Some` if target-imgref is different than the image pulled + pub(crate) original_imgref: Option, } #[derive(Debug, PartialEq)] @@ -1386,6 +1392,7 @@ mod tests { let staged = StagedDeployment { depl_id: staged_id.clone(), finalization_locked: true, + original_imgref: None, }; let host = composefs_aboot_status_from( &storage, @@ -1410,6 +1417,7 @@ mod tests { let staged = StagedDeployment { depl_id: b.clone(), finalization_locked: false, + original_imgref: None, }; let host = composefs_aboot_status_from( &storage, diff --git a/crates/lib/src/bootc_composefs/update.rs b/crates/lib/src/bootc_composefs/update.rs index 3ad4f741d6..9553bc881b 100644 --- a/crates/lib/src/bootc_composefs/update.rs +++ b/crates/lib/src/bootc_composefs/update.rs @@ -477,6 +477,7 @@ pub(crate) async fn do_upgrade( let staged_state = StagedDeployment { depl_id: deploy_id.to_hex(), finalization_locked: opts.download_only, + original_imgref: target_imgref.map(|_| imgref.to_string()), }; write_composefs_state( diff --git a/crates/lib/src/cli.rs b/crates/lib/src/cli.rs index 21a861ed9f..2a1146d321 100644 --- a/crates/lib/src/cli.rs +++ b/crates/lib/src/cli.rs @@ -251,6 +251,7 @@ pub(crate) struct SwitchOpts { /// The image to use for subsequent upgrades /// Useful when switching to an, say containers-storage, image /// but subsequent updates are to be pulled from registry + #[clap(long)] pub(crate) target_imgref: Option, } @@ -1666,6 +1667,7 @@ async fn upgrade( &spec, prog.clone(), opts.download_opts.download_only, + None, ) .await?; changed = true; @@ -1758,7 +1760,7 @@ async fn switch_ostree( } let target = imgref_for_switch(&opts)?; - let prog: ProgressWriter = opts.progress.try_into()?; + let prog: ProgressWriter = opts.progress.clone().try_into()?; let cancellable = gio::Cancellable::NONE; let repo = &booted_ostree.repo(); @@ -1769,6 +1771,13 @@ async fn switch_ostree( new_spec }; + // 1. Check if there's a staged one and compare the image digest + // 2. Also need to make sure that `--target-imgeref hi ` and `--target-imgeref hi ` + // actually ends up switching + + println!("new_spec: {new_spec:#?}"); + println!("host.spec: {:#?}", host.spec); + if new_spec == host.spec { println!("Image specification is unchanged."); if opts.apply && host.status.staged.is_some() { @@ -1856,6 +1865,7 @@ async fn switch_ostree( let stateroot = booted_ostree.stateroot(); let from = MergeState::from_stateroot(storage, &stateroot)?; + crate::deploy::stage( storage, from, @@ -1863,6 +1873,7 @@ async fn switch_ostree( &new_spec, prog.clone(), opts.download_opts.download_only, + target_imgref_for_switch(&opts)?.as_ref(), ) .await?; @@ -2010,7 +2021,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()?; diff --git a/crates/lib/src/deploy.rs b/crates/lib/src/deploy.rs index 1f19afc622..8111f731f5 100644 --- a/crates/lib/src/deploy.rs +++ b/crates/lib/src/deploy.rs @@ -1255,6 +1255,7 @@ pub(crate) async fn stage( spec: &RequiredHostSpec<'_>, prog: ProgressWriter, lock_finalization: bool, + target_imgref: Option<&ImageReference>, ) -> Result<()> { // Log the staging operation to systemd journal with comprehensive upgrade information const STAGE_JOURNAL_ID: &str = "8f7a2b1c3d4e5f6a7b8c9d0e1f2a3b4c"; @@ -1333,7 +1334,12 @@ pub(crate) async fn stage( .collect(), }) .await; - let origin = origin_from_imageref(spec.image)?; + + let origin = match target_imgref { + Some(i) => origin_from_imageref(i)?, + None => origin_from_imageref(spec.image)?, + }; + crate::deploy::deploy(sysroot, from, image, &origin, lock_finalization).await?; subtask.completed = true; 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)? { From c472be3316e0d56f4b5bd2e2e980a610f3c912d1 Mon Sep 17 00:00:00 2001 From: HarshwardhanPatil07 Date: Fri, 30 Oct 2026 14:58:39 +0530 Subject: [PATCH 3/8] switch: Validate and normalize target image references Require an explicit transport for the target and parse it using the existing image reference type. Normalize references before fetching, reject incompatible options, and avoid advertising enforced verification on an unsupported composefs path. Assisted-by: AI Signed-off-by: HarshwardhanPatil07 --- crates/lib/src/bootc_composefs/switch.rs | 7 ++ crates/lib/src/cli.rs | 133 +++++++++++++++++++++-- docs/src/man/bootc-switch.8.md | 4 + 3 files changed, 134 insertions(+), 10 deletions(-) diff --git a/crates/lib/src/bootc_composefs/switch.rs b/crates/lib/src/bootc_composefs/switch.rs index 73ce261344..db4890aa49 100644 --- a/crates/lib/src/bootc_composefs/switch.rs +++ b/crates/lib/src/bootc_composefs/switch.rs @@ -45,6 +45,13 @@ pub(crate) async fn switch_composefs( return apply_upgrade_from_downloaded(storage, booted_cfs, &host, &do_upgrade_opts).await; } + 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 target = imgref_for_switch(&opts)?; if let Some(delta) = delta.as_ref() { delta.validate_image_reference(&target)?; diff --git a/crates/lib/src/cli.rs b/crates/lib/src/cli.rs index 2a1146d321..1f672f8cae 100644 --- a/crates/lib/src/cli.rs +++ b/crates/lib/src/cli.rs @@ -248,11 +248,16 @@ pub(crate) struct SwitchOpts { #[clap(flatten)] pub(crate) uki_addon_opts: UkiAddonOpts, - /// The image to use for subsequent upgrades - /// Useful when switching to an, say containers-storage, image - /// but subsequent updates are to be pulled from registry - #[clap(long)] - pub(crate) target_imgref: Option, + /// 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. @@ -1706,13 +1711,13 @@ pub(crate) fn target_imgref_for_switch(opts: &SwitchOpts) -> Result 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_callname() { use std::os::unix::ffi::OsStrExt; diff --git a/docs/src/man/bootc-switch.8.md b/docs/src/man/bootc-switch.8.md index 5e6b3d3b3b..c7f2b8ac88 100644 --- a/docs/src/man/bootc-switch.8.md +++ b/docs/src/man/bootc-switch.8.md @@ -88,6 +88,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 From 33ef3828ccc0ec5cb05824c941054f130e7611f0 Mon Sep 17 00:00:00 2001 From: HarshwardhanPatil07 Date: Fri, 30 Oct 2026 14:59:19 +0530 Subject: [PATCH 4/8] ostree: Retain target references for cached image imports A cached import must retain the reference used by the deployment origin so cleanup does not discard its image metadata. Handle this in the importer, respecting set_no_imgref, so direct and unified pulls share the same behavior. Reuse the existing pull preparation helper. Assisted-by: AI Signed-off-by: HarshwardhanPatil07 --- crates/lib/src/deploy.rs | 17 +----- crates/ostree-ext/src/container/store.rs | 40 +++++++++----- crates/ostree-ext/tests/it/main.rs | 67 ++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 28 deletions(-) diff --git a/crates/lib/src/deploy.rs b/crates/lib/src/deploy.rs index 8111f731f5..0df5031376 100644 --- a/crates/lib/src/deploy.rs +++ b/crates/lib/src/deploy.rs @@ -746,12 +746,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 +760,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))) } 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(()) } From a9f10e4dda4bc0791ef9a4a26bb0379b9dfedde8 Mon Sep 17 00:00:00 2001 From: HarshwardhanPatil07 Date: Fri, 30 Oct 2026 14:59:56 +0530 Subject: [PATCH 5/8] switch: Persist source provenance for repeated requests The update origin cannot identify which offline delivery was staged. Record the pull source in each deployment origin and compare both references before fetching, preserving download-only locks on repeats. Route OSTree imports through the source while retaining the target. Cover offline repeats, source replacement, activation, and provenance after reboot on both backends. Assisted-by: AI Signed-off-by: HarshwardhanPatil07 --- crates/lib/src/bootc_composefs/aboot.rs | 1 - crates/lib/src/bootc_composefs/boot.rs | 1 + crates/lib/src/bootc_composefs/state.rs | 25 +++- crates/lib/src/bootc_composefs/status.rs | 8 -- crates/lib/src/bootc_composefs/switch.rs | 89 ++++++++---- crates/lib/src/bootc_composefs/update.rs | 4 +- crates/lib/src/cli.rs | 134 +++++++++++++++--- crates/lib/src/deploy.rs | 58 +++++++- tmt/plans/integration.fmf | 7 + tmt/tests/booted/test-switch-target-imgref.nu | 96 +++++++++++++ tmt/tests/tests.fmf | 5 + 11 files changed, 354 insertions(+), 74 deletions(-) create mode 100644 tmt/tests/booted/test-switch-target-imgref.nu diff --git a/crates/lib/src/bootc_composefs/aboot.rs b/crates/lib/src/bootc_composefs/aboot.rs index f2b8036de8..f4b5fdb5bf 100644 --- a/crates/lib/src/bootc_composefs/aboot.rs +++ b/crates/lib/src/bootc_composefs/aboot.rs @@ -135,7 +135,6 @@ impl PendingDeployment { StagedDeployment { depl_id: self.depl_id.clone(), finalization_locked: self.finalization_locked, - original_imgref: None, } } } 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/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/status.rs b/crates/lib/src/bootc_composefs/status.rs index b470e5bc60..b0efe2b1a9 100644 --- a/crates/lib/src/bootc_composefs/status.rs +++ b/crates/lib/src/bootc_composefs/status.rs @@ -159,12 +159,6 @@ pub(crate) struct StagedDeployment { /// Whether to finalize this staged deployment on reboot or not /// This also maps to `download_only` field in `BootEntry` pub(crate) finalization_locked: bool, - /// This is the image that's pulled into the composefs repository - /// Might or might not be the same as the imgref in the ".origin" file - /// because of the `--target-imgref` flag in `bootc switch` - /// - /// Is only `Some` if target-imgref is different than the image pulled - pub(crate) original_imgref: Option, } #[derive(Debug, PartialEq)] @@ -1392,7 +1386,6 @@ mod tests { let staged = StagedDeployment { depl_id: staged_id.clone(), finalization_locked: true, - original_imgref: None, }; let host = composefs_aboot_status_from( &storage, @@ -1417,7 +1410,6 @@ mod tests { let staged = StagedDeployment { depl_id: b.clone(), finalization_locked: false, - original_imgref: None, }; let host = composefs_aboot_status_from( &storage, diff --git a/crates/lib/src/bootc_composefs/switch.rs b/crates/lib/src/bootc_composefs/switch.rs index db4890aa49..25fb7fb41e 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, target_imgref_for_switch}, + cli::{ + SwitchOpts, handle_unchanged_switch, imgref_for_switch, switch_images_unchanged, + target_imgref_for_switch, + }, progress_jsonl::ProgressWriter, store::{BootedComposefs, Storage}, }; @@ -52,12 +55,14 @@ pub(crate) async fn switch_composefs( ); } - let target = imgref_for_switch(&opts)?; + 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 @@ -65,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 @@ -73,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, @@ -119,7 +150,7 @@ pub(crate) async fn switch_composefs( false }; let target_unified = - crate::deploy::image_exists_in_unified_storage(storage, &target_imgref).await?; + crate::deploy::image_exists_in_unified_storage(storage, &source).await?; booted_unified || target_unified }; @@ -135,13 +166,11 @@ 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) } }; - let final_target_imgref = target_imgref_for_switch(&opts)?; - if let Some(cfg_verity) = image { let action = validate_update( storage, @@ -154,7 +183,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(()); } @@ -163,8 +192,8 @@ pub(crate) async fn switch_composefs( storage, booted_cfs, &host, - &target_imgref, - final_target_imgref.as_ref(), + &source, + target.as_ref(), &do_upgrade_opts, &manifest, ) @@ -177,8 +206,8 @@ pub(crate) async fn switch_composefs( storage, booted_cfs, &host, - &target_imgref, - final_target_imgref.as_ref(), + &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 9553bc881b..81e125a717 100644 --- a/crates/lib/src/bootc_composefs/update.rs +++ b/crates/lib/src/bootc_composefs/update.rs @@ -477,13 +477,13 @@ pub(crate) async fn do_upgrade( let staged_state = StagedDeployment { depl_id: deploy_id.to_hex(), finalization_locked: opts.download_only, - original_imgref: target_imgref.map(|_| imgref.to_string()), }; write_composefs_state( &Utf8PathBuf::from("/sysroot"), &deploy_id, - target_imgref.unwrap_or(imgref), + imgref, + target_imgref, false, boot_type, boot_digest, diff --git a/crates/lib/src/cli.rs b/crates/lib/src/cli.rs index 1f672f8cae..69aab1366a 100644 --- a/crates/lib/src/cli.rs +++ b/crates/lib/src/cli.rs @@ -1720,6 +1720,41 @@ pub(crate) fn target_imgref_for_switch(opts: &SwitchOpts) -> Result, + 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())?; @@ -1749,7 +1784,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( @@ -1764,7 +1799,12 @@ async fn switch_ostree( .await; } - let target = imgref_for_switch(&opts)?; + 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; @@ -1776,19 +1816,41 @@ async fn switch_ostree( new_spec }; - // 1. Check if there's a staged one and compare the image digest - // 2. Also need to make sure that `--target-imgeref hi ` and `--target-imgeref hi ` - // actually ends up switching - - println!("new_spec: {new_spec:#?}"); - println!("host.spec: {:#?}", host.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 @@ -1805,6 +1867,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 @@ -1818,7 +1882,7 @@ 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? }; if let Some(delta) = delta.as_deref() { crate::delta::reject_unified_storage(delta, use_unified)?; @@ -1827,7 +1891,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(), @@ -1837,8 +1901,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, @@ -1848,8 +1912,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), @@ -1878,7 +1942,7 @@ async fn switch_ostree( &new_spec, prog.clone(), opts.download_opts.download_only, - target_imgref_for_switch(&opts)?.as_ref(), + Some(&source), ) .await?; @@ -3133,6 +3197,34 @@ mod tests { 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 0df5031376..f69e75ab06 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. @@ -1168,6 +1172,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 { @@ -1240,7 +1255,7 @@ pub(crate) async fn stage( spec: &RequiredHostSpec<'_>, prog: ProgressWriter, lock_finalization: bool, - target_imgref: Option<&ImageReference>, + source_imgref: Option<&ImageReference>, ) -> Result<()> { // Log the staging operation to systemd journal with comprehensive upgrade information const STAGE_JOURNAL_ID: &str = "8f7a2b1c3d4e5f6a7b8c9d0e1f2a3b4c"; @@ -1320,10 +1335,7 @@ pub(crate) async fn stage( }) .await; - let origin = match target_imgref { - Some(i) => origin_from_imageref(i)?, - None => origin_from_imageref(spec.image)?, - }; + let origin = origin_for_switch(spec.image, source_imgref)?; crate::deploy::deploy(sysroot, from, image, &origin, lock_finalization).await?; @@ -1631,8 +1643,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/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..29367d09f7 --- /dev/null +++ b/tmt/tests/booted/test-switch-target-imgref.nu @@ -0,0 +1,96 @@ +# 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:5555/target-imgref:latest" +const next_origin = "localhost:5555/target-imgref:redirected" +const archive = "/var/tmp/target-imgref-source.tar" +const registry = "bootc-target-imgref-registry" +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 $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 + + mkdir /etc/containers/registries.conf.d + '[[registry]] +location = "localhost:5555" +insecure = true +' | save /etc/containers/registries.conf.d/target-imgref-test.conf + podman run -d --name $registry -p 127.0.0.1:5555:5000 docker.io/library/registry:2 + # Retry startup readiness through podman's push retry handling. + podman push --tls-verify=false $source $origin + # Deliver the exact registry manifest through local storage, as with an + # offline archive. A push can change layer compression and manifest digests. + podman pull --tls-verify=false $origin + podman tag $origin $source + podman rmi $origin + podman save --format oci-archive -o $archive $source + podman stop $registry + + # A new source must still be processed when the update origin is unchanged. + bootc switch --download-only --transport containers-storage --target-imgref $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 "registry" + assert $staged.downloadOnly + + # Remove the source while the target registry is also unavailable. A repeat + # must use deployment metadata without contacting either image location. + podman rmi $source + stage + assert equal (status).status.staged $staged + let applied = do { bootc switch --apply --transport containers-storage --target-imgref $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 + assert equal $booted.image.image.image $origin + assert equal (open $marker | str trim) "v2" + + # Provenance survives reboot. The source remains absent and registry stopped. + bootc switch --transport containers-storage --target-imgref $origin $source + assert equal (status).status.booted $booted + assert equal (status).status.staged null + + podman rm $registry + tap ok +} + +def main [] { + match $env.TMT_REBOOT_COUNT? { + null | "0" => first_boot, + "1" => second_boot, + $n => { error make { msg: $"Invalid reboot count ($n)" } }, + } +} 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 From b8e7f67c32eb9e23729943aa8b44ee4a9ee82354 Mon Sep 17 00:00:00 2001 From: HarshwardhanPatil07 Date: Fri, 30 Oct 2026 15:01:15 +0530 Subject: [PATCH 6/8] tests: Verify updates after redirected switches Recording an origin is only useful if later updates actually use it. Extend the reboot test to check unchanged content and a newer image at the recorded update origin after completing an offline delivery. Assisted-by: AI Signed-off-by: HarshwardhanPatil07 --- tmt/tests/booted/test-switch-target-imgref.nu | 135 ++++++++++++++---- 1 file changed, 108 insertions(+), 27 deletions(-) diff --git a/tmt/tests/booted/test-switch-target-imgref.nu b/tmt/tests/booted/test-switch-target-imgref.nu index 29367d09f7..e4420a7b5c 100644 --- a/tmt/tests/booted/test-switch-target-imgref.nu +++ b/tmt/tests/booted/test-switch-target-imgref.nu @@ -7,10 +7,9 @@ use std assert use tap.nu const source = "localhost/target-imgref-source:v2" -const origin = "localhost:5555/target-imgref:latest" -const next_origin = "localhost:5555/target-imgref:redirected" +const origin = "localhost/target-imgref:latest" +const next_origin = "localhost/target-imgref:redirected" const archive = "/var/tmp/target-imgref-source.tar" -const registry = "bootc-target-imgref-registry" const marker = "/usr/share/testing-target-imgref" def status [] { bootc status --json | from json } @@ -22,7 +21,7 @@ def build_image [version: string, image: string] { } def stage [] { - bootc switch --download-only --transport containers-storage --target-imgref $origin $source + bootc switch --download-only --transport containers-storage --target-imgref $"containers-storage:($origin)" $source } def first_boot [] { @@ -32,39 +31,41 @@ def first_boot [] { build_image "v1" "localhost/target-imgref-source:v1" build_image "v2" $source - mkdir /etc/containers/registries.conf.d - '[[registry]] -location = "localhost:5555" -insecure = true -' | save /etc/containers/registries.conf.d/target-imgref-test.conf - podman run -d --name $registry -p 127.0.0.1:5555:5000 docker.io/library/registry:2 - # Retry startup readiness through podman's push retry handling. - podman push --tls-verify=false $source $origin - # Deliver the exact registry manifest through local storage, as with an - # offline archive. A push can change layer compression and manifest digests. - podman pull --tls-verify=false $origin - podman tag $origin $source - podman rmi $origin podman save --format oci-archive -o $archive $source - podman stop $registry + # 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 - # A new source must still be processed when the update origin is unchanged. - bootc switch --download-only --transport containers-storage --target-imgref $origin localhost/target-imgref-source:v1 + 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 "registry" + assert equal $staged.image.image.transport "containers-storage" assert $staged.downloadOnly - # Remove the source while the target registry is also unavailable. A repeat - # must use deployment metadata without contacting either image location. + # 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 $origin $source } | complete + 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 @@ -75,15 +76,86 @@ insecure = true 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. The source remains absent and registry stopped. - bootc switch --transport containers-storage --target-imgref $origin $source + # 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) +} - podman rm $registry +def finish [update_origin: string] { + assert equal (open $marker | str trim) "v3" + assert equal (status).status.booted.image.image.image $update_origin tap ok } @@ -91,6 +163,15 @@ 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)" } }, } } From 75ce847b70638e0c11f4ed1257723348517fe15b Mon Sep 17 00:00:00 2001 From: HarshwardhanPatil07 Date: Fri, 30 Oct 2026 15:01:15 +0530 Subject: [PATCH 7/8] docs: Explain redirected switch and activation behavior Explain how to obtain an image from a temporary source while retaining the normal update origin. Document repeat behavior, activation, and user-visible restrictions with local-storage and registry examples. Assisted-by: AI Signed-off-by: HarshwardhanPatil07 --- docs/src/man/bootc-switch.8.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/src/man/bootc-switch.8.md b/docs/src/man/bootc-switch.8.md index c7f2b8ac88..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 @@ -108,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) From 686c19eeff2278e64eb5cf2861b4246ce3fb0529 Mon Sep 17 00:00:00 2001 From: HarshwardhanPatil07 Date: Sat, 31 Oct 2026 19:09:53 +0530 Subject: [PATCH 8/8] storage: Preserve unified storage across redirected switches A redirected deployment records its update origin while containers-storage keeps the delivery name. Detect the stored host image by its OCI config ID and retain that ID during cleanup, so later updates keep using unified storage without requiring another explicit opt-in. Use the existing deployment metadata for both backends without adding persistent state or changing image aliases. Copy local deliveries from host storage before importing them into the unified store. Assisted-by: AI Signed-off-by: HarshwardhanPatil07 --- crates/lib/src/bootc_composefs/gc.rs | 16 +++++- crates/lib/src/bootc_composefs/switch.rs | 18 +++---- crates/lib/src/bootc_composefs/update.rs | 14 ++++-- crates/lib/src/cli.rs | 20 +++++++- crates/lib/src/deploy.rs | 57 +++++++++++++++++----- crates/lib/src/image.rs | 2 +- crates/lib/src/podstorage.rs | 12 +++-- crates/lib/src/status.rs | 19 ++++++++ tmt/tests/booted/test-switch-to-unified.nu | 23 ++++++--- 9 files changed, 135 insertions(+), 46 deletions(-) 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/switch.rs b/crates/lib/src/bootc_composefs/switch.rs index 25fb7fb41e..90b430859c 100644 --- a/crates/lib/src/bootc_composefs/switch.rs +++ b/crates/lib/src/bootc_composefs/switch.rs @@ -136,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 = + 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?; - booted_unified || target_unified + current_unified || source_unified }; // With a delta the target is whatever the delta says it is, and we can look diff --git a/crates/lib/src/bootc_composefs/update.rs b/crates/lib/src/bootc_composefs/update.rs index 81e125a717..88a5f953a8 100644 --- a/crates/lib/src/bootc_composefs/update.rs +++ b/crates/lib/src/bootc_composefs/update.rs @@ -670,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?; diff --git a/crates/lib/src/cli.rs b/crates/lib/src/cli.rs index 69aab1366a..a1a11c41e0 100644 --- a/crates/lib/src/cli.rs +++ b/crates/lib/src/cli.rs @@ -1552,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)?; } @@ -1883,6 +1892,15 @@ async fn switch_ostree( true } else { 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)?; diff --git a/crates/lib/src/deploy.rs b/crates/lib/src/deploy.rs index f69e75ab06..94a01168f7 100644 --- a/crates/lib/src/deploy.rs +++ b/crates/lib/src/deploy.rs @@ -445,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(()) @@ -689,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( @@ -710,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"); 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/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/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 }