Skip to content
Open
1 change: 1 addition & 0 deletions crates/lib/src/bootc_composefs/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 14 additions & 2 deletions crates/lib/src/bootc_composefs/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>("origin", ostree_ext::container::deploy::ORIGIN_CONTAINER)
if let Some(container_ref) = ini
.get::<String>(
crate::deploy::ORIGIN_BOOTC,
crate::deploy::ORIGIN_SOURCE_IMAGE,
)
.or_else(|| {
ini.get::<String>("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")
Expand All @@ -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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this looks like something that should have been split out as a separate bugfix

// 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::<String>(ORIGIN_KEY_IMAGE, ORIGIN_KEY_MANIFEST_DIGEST)
{
Expand Down
25 changes: 18 additions & 7 deletions crates/lib/src/bootc_composefs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to better understand why we need to track the source image. This is the most complex part of this PR.

What would break exactly if we didn't?

source_imgref.to_string(),
);
}

config = config
.section(ORIGIN_KEY_BOOT)
Expand Down
110 changes: 72 additions & 38 deletions crates/lib/src/bootc_composefs/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -45,51 +48,86 @@ 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
.status
.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
host = get_composefs_status(storage, booted_cfs).await?;
}
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,
&current.require_composefs()?.verity,
)?
.context("No origin for current deployment")?;
current_origin_file
.get::<String>(
crate::deploy::ORIGIN_BOOTC,
crate::deploy::ORIGIN_SOURCE_IMAGE,
)
.map(|source| {
source
.parse::<ostree_ext::container::OstreeImageReference>()
.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,
Expand All @@ -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
Expand All @@ -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)
}
};
Expand All @@ -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(());
}

Expand All @@ -154,7 +186,8 @@ pub(crate) async fn switch_composefs(
storage,
booted_cfs,
&host,
&target_imgref,
&source,
target.as_ref(),
&do_upgrade_opts,
&manifest,
)
Expand All @@ -167,7 +200,8 @@ pub(crate) async fn switch_composefs(
storage,
booted_cfs,
&host,
&target_imgref,
&source,
target.as_ref(),
&do_upgrade_opts,
&manifest,
)
Expand Down
19 changes: 14 additions & 5 deletions crates/lib/src/bootc_composefs/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -482,6 +483,7 @@ pub(crate) async fn do_upgrade(
&Utf8PathBuf::from("/sysroot"),
&deploy_id,
imgref,
target_imgref,
false,
boot_type,
boot_digest,
Expand Down Expand Up @@ -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?;

Expand Down Expand Up @@ -748,6 +754,7 @@ pub(crate) async fn upgrade_composefs(
composefs,
&host,
booted_imgref,
None,
&do_upgrade_opts,
&manifest,
)
Expand Down Expand Up @@ -780,6 +787,7 @@ pub(crate) async fn upgrade_composefs(
composefs,
&host,
booted_imgref,
None,
&do_upgrade_opts,
&manifest,
)
Expand All @@ -800,6 +808,7 @@ pub(crate) async fn upgrade_composefs(
composefs,
&host,
booted_imgref,
None,
&do_upgrade_opts,
&manifest,
)
Expand Down
Loading
Loading