Skip to main content

bootc_lib/bootc_composefs/
update.rs

1use anyhow::{Context, Result};
2use camino::Utf8PathBuf;
3use cap_std_ext::{cap_std::fs::Dir, dirext::CapStdExtDirExt};
4use composefs::fsverity::{FsVerityHashValue, Sha512HashValue};
5use composefs_boot::BootOps;
6use composefs_ctl::composefs;
7use composefs_ctl::composefs_boot;
8use composefs_ctl::composefs_oci;
9use composefs_oci::image::create_filesystem;
10use fn_error_context::context;
11use ocidir::cap_std::ambient_authority;
12use ostree_ext::container::ManifestDiff;
13
14use crate::{
15    bootc_composefs::{
16        boot::{BootSetupType, BootType, setup_composefs_bls_boot, setup_composefs_uki_boot},
17        gc::composefs_gc,
18        repo::pull_composefs_repo,
19        service::start_finalize_stated_svc,
20        soft_reboot::prepare_soft_reboot_composefs,
21        state::write_composefs_state,
22        status::{
23            ImgConfigManifest, StagedDeployment, get_bootloader, get_composefs_status,
24            get_container_manifest_and_config, get_imginfo,
25        },
26    },
27    cli::{SoftRebootMode, UpgradeOpts},
28    composefs_consts::{
29        COMPOSEFS_STAGED_DEPLOYMENT_FNAME, COMPOSEFS_TRANSIENT_STATE_DIR, STATE_DIR_RELATIVE,
30        TYPE1_ENT_PATH_STAGED, USER_CFG_STAGED,
31    },
32    spec::{Bootloader, Host, ImageReference},
33    store::{BootedComposefs, ComposefsRepository, Storage},
34};
35
36/// Checks if a container image has been pulled to the local composefs repository.
37///
38/// This function verifies whether the specified container image exists in the local
39/// composefs repository by checking if the image's configuration digest stream is
40/// available. It retrieves the image manifest and configuration from the container
41/// registry and uses the configuration digest to perform the local availability check.
42///
43/// # Arguments
44///
45/// * `repo` - The composefs repository
46/// * `imgref` - Reference to the container image to check
47///
48/// # Returns
49///
50/// Returns a tuple containing:
51/// * `Some<Sha512HashValue>` if the image is pulled/available locally, `None` otherwise
52/// * The container image manifest
53/// * The container image configuration
54#[context("Checking if image {} is pulled", imgref.image)]
55pub(crate) async fn is_image_pulled(
56    repo: &ComposefsRepository,
57    imgref: &ImageReference,
58) -> Result<(Option<Sha512HashValue>, ImgConfigManifest)> {
59    let imgref_repr = imgref.to_image_proxy_ref()?;
60    let img_config_manifest = get_container_manifest_and_config(&imgref_repr.to_string()).await?;
61
62    let img_digest = img_config_manifest.manifest.config().digest().digest();
63
64    // TODO: export config_identifier function from composefs-oci/src/lib.rs and use it here
65    let img_id = format!("oci-config-sha256:{img_digest}");
66
67    // NB: add deep checking?
68    let container_pulled = repo.has_stream(&img_id).context("Checking stream")?;
69
70    Ok((container_pulled, img_config_manifest))
71}
72
73fn rm_staged_type1_ent(boot_dir: &Dir) -> Result<()> {
74    if boot_dir.exists(TYPE1_ENT_PATH_STAGED) {
75        boot_dir
76            .remove_dir_all(TYPE1_ENT_PATH_STAGED)
77            .context("Removing staged bootloader entry")?;
78    }
79
80    Ok(())
81}
82
83#[derive(Debug)]
84pub(crate) enum UpdateAction {
85    /// Skip the update. We probably have the update in our deployments
86    Skip,
87    /// Proceed with the update
88    Proceed,
89}
90
91/// Determines what action should be taken for the update
92///
93/// Cases:
94///
95/// - The verity is the same as that of the currently booted deployment
96///
97///    Nothing to do here as we're currently booted
98///
99/// - The verity is the same as that of the staged deployment
100///
101///    Nothing to do, as we only get a "staged" deployment if we have
102///    /run/composefs/staged-deployment which is the last thing we create while upgrading
103///
104/// - The verity is the same as that of the rollback deployment
105///
106///    Nothing to do since this is a rollback deployment which means this was unstaged at some
107///    point
108///
109/// - The verity is not found
110///
111///    The update/switch might've been canceled before /run/composefs/staged-deployment
112///    was created, or at any other point in time, or it's a new one.
113///    Any which way, we can overwrite everything
114///
115/// # Arguments
116///
117/// * `storage`       - The global storage object
118/// * `booted_cfs`    - Reference to the booted composefs deployment
119/// * `host`          - Object returned by `get_composefs_status`
120/// * `img_digest`    - The SHA256 sum of the target image
121/// * `config_verity` - The verity of the Image config splitstream
122/// * `is_switch`     - Whether this is an update operation or a switch operation
123///
124/// # Returns
125/// * UpdateAction::Skip    - Skip the update/switch as we have it as a deployment
126/// * UpdateAction::Proceed - Proceed with the update
127pub(crate) fn validate_update(
128    storage: &Storage,
129    booted_cfs: &BootedComposefs,
130    host: &Host,
131    img_digest: &str,
132    config_verity: &Sha512HashValue,
133    is_switch: bool,
134) -> Result<UpdateAction> {
135    let repo = &*booted_cfs.repo;
136
137    let oci_digest: composefs_oci::OciDigest = img_digest
138        .parse()
139        .with_context(|| format!("Parsing config digest {img_digest}"))?;
140    let mut fs = create_filesystem(repo, &oci_digest, Some(config_verity))?;
141    fs.transform_for_boot(&repo)?;
142
143    let image_id = fs.compute_image_id();
144
145    let all_deployments = host.all_composefs_deployments()?;
146
147    let found_depl = all_deployments
148        .iter()
149        .find(|d| d.deployment.verity == image_id.to_hex());
150
151    if let Some(collision) = found_depl {
152        if is_switch {
153            // For `bootc switch`, any digest collision is an error: two images
154            // from different sources can produce identical composefs roots and we
155            // cannot safely reuse an existing state directory seeded from a
156            // different image.
157            anyhow::bail!(
158                "Target image has the same fs-verity digest as the existing {:?} deployment.",
159                collision.ty,
160            );
161        }
162        // For `bootc upgrade`, matching the booted deployment means nothing to
163        // do; matching a non-booted deployment (staged/rollback) means skip.
164        return Ok(UpdateAction::Skip);
165    }
166
167    let booted = host.require_composefs_booted()?;
168    let boot_dir = storage.require_boot_dir()?;
169
170    // Remove staged bootloader entries, if any
171    // GC should take care of the UKI PEs and other binaries
172    match get_bootloader()? {
173        Bootloader::Grub => match booted.boot_type {
174            BootType::Bls => rm_staged_type1_ent(boot_dir)?,
175
176            BootType::Uki => {
177                let grub = boot_dir.open_dir("grub2").context("Opening grub dir")?;
178
179                if grub.exists(USER_CFG_STAGED) {
180                    grub.remove_file(USER_CFG_STAGED)
181                        .context("Removing staged grub user config")?;
182                }
183            }
184        },
185
186        Bootloader::Systemd => rm_staged_type1_ent(boot_dir)?,
187
188        Bootloader::None => unreachable!("Checked at install time"),
189    }
190
191    // Remove state directory
192    let state_dir = storage
193        .physical_root
194        .open_dir(STATE_DIR_RELATIVE)
195        .context("Opening state dir")?;
196
197    if state_dir.exists(image_id.to_hex()) {
198        state_dir
199            .remove_dir_all(image_id.to_hex())
200            .context("Removing state")?;
201    }
202
203    Ok(UpdateAction::Proceed)
204}
205
206/// This is just an intersection of SwitchOpts and UpgradeOpts
207pub(crate) struct DoUpgradeOpts {
208    pub(crate) apply: bool,
209    pub(crate) soft_reboot: Option<SoftRebootMode>,
210    pub(crate) download_only: bool,
211    /// Whether to use unified storage (containers-storage + composefs).
212    pub(crate) use_unified: bool,
213}
214
215async fn apply_upgrade(
216    storage: &Storage,
217    booted_cfs: &BootedComposefs,
218    depl_id: &String,
219    opts: &DoUpgradeOpts,
220) -> Result<()> {
221    if let Some(soft_reboot_mode) = opts.soft_reboot {
222        return prepare_soft_reboot_composefs(
223            storage,
224            booted_cfs,
225            Some(depl_id),
226            soft_reboot_mode,
227            opts.apply,
228        )
229        .await;
230    };
231
232    if opts.apply {
233        return crate::reboot::reboot();
234    }
235
236    Ok(())
237}
238
239/// Performs the Update or Switch operation
240#[context("Performing Upgrade Operation")]
241pub(crate) async fn do_upgrade(
242    storage: &Storage,
243    booted_cfs: &BootedComposefs,
244    host: &Host,
245    imgref: &ImageReference,
246    opts: &DoUpgradeOpts,
247    manifest: &ostree_ext::oci_spec::image::ImageManifest,
248) -> Result<()> {
249    // Pre-flight disk space check before pulling.
250    crate::deploy::check_disk_space_composefs(&*booted_cfs.repo, manifest, imgref)?;
251
252    start_finalize_stated_svc()?;
253
254    let crate::bootc_composefs::repo::PullRepoResult {
255        repo,
256        entries,
257        id,
258        manifest_digest,
259    } = pull_composefs_repo(
260        imgref,
261        booted_cfs.cmdline.allow_missing_fsverity,
262        opts.use_unified,
263    )
264    .await?;
265
266    // If the target image produces the same fs-verity digest as any existing
267    // deployment (booted, staged, rollback, or pinned), error out.  Two images
268    // from different sources can have identical content; we cannot silently reuse
269    // an existing state directory whose /etc was seeded from a different image.
270    let all_deployments = host.all_composefs_deployments()?;
271    if let Some(collision) = all_deployments
272        .iter()
273        .find(|d| d.deployment.verity == id.to_hex())
274    {
275        anyhow::bail!(
276            "Target image has the same fs-verity digest as the existing {:?} deployment.",
277            collision.ty,
278        );
279    }
280
281    let Some(entry) = entries.iter().next() else {
282        anyhow::bail!("No boot entries!");
283    };
284
285    let mounted_fs = Dir::reopen_dir(
286        &repo
287            .mount(&id.to_hex())
288            .context("Failed to mount composefs image")?,
289    )?;
290
291    let boot_type = BootType::from(entry);
292
293    let boot_digest = match boot_type {
294        BootType::Bls => setup_composefs_bls_boot(
295            BootSetupType::Upgrade((storage, booted_cfs, &host)),
296            repo,
297            &id,
298            entry,
299            &mounted_fs,
300        )?,
301
302        BootType::Uki => setup_composefs_uki_boot(
303            BootSetupType::Upgrade((storage, booted_cfs, &host)),
304            repo,
305            &id,
306            entries,
307        )?,
308    };
309
310    write_composefs_state(
311        &Utf8PathBuf::from("/sysroot"),
312        &id,
313        imgref,
314        Some(StagedDeployment {
315            depl_id: id.to_hex(),
316            finalization_locked: opts.download_only,
317        }),
318        boot_type,
319        boot_digest,
320        &manifest_digest,
321        booted_cfs.cmdline.allow_missing_fsverity,
322    )
323    .await?;
324
325    // We take into account the staged bootloader entries so this won't remove
326    // the currently staged entry
327    composefs_gc(storage, booted_cfs, false, true).await?;
328
329    apply_upgrade(storage, booted_cfs, &id.to_hex(), opts).await
330}
331
332#[context("Upgrading composefs")]
333pub(crate) async fn upgrade_composefs(
334    opts: UpgradeOpts,
335    storage: &Storage,
336    composefs: &BootedComposefs,
337) -> Result<()> {
338    const COMPOSEFS_UPGRADE_JOURNAL_ID: &str = "9c8d7f6e5a4b3c2d1e0f9a8b7c6d5e4f3";
339
340    tracing::info!(
341        message_id = COMPOSEFS_UPGRADE_JOURNAL_ID,
342        bootc.operation = "upgrade",
343        bootc.apply_mode = opts.apply,
344        bootc.download_only = opts.download_only,
345        bootc.from_downloaded = opts.from_downloaded,
346        "Starting composefs upgrade operation"
347    );
348
349    let host = get_composefs_status(storage, composefs)
350        .await
351        .context("Getting composefs deployment status")?;
352
353    let current_image = host.spec.image.as_ref();
354
355    // Handle --tag: derive target from current image + new tag
356    let derived_image = if let Some(ref tag) = opts.tag {
357        let image = current_image.ok_or_else(|| {
358            anyhow::anyhow!("--tag requires a booted image with a specified source")
359        })?;
360        Some(image.with_tag(tag)?)
361    } else {
362        None
363    };
364
365    let mut do_upgrade_opts = DoUpgradeOpts {
366        soft_reboot: opts.soft_reboot,
367        apply: opts.apply,
368        download_only: opts.download_only,
369        use_unified: false,
370    };
371
372    if opts.from_downloaded {
373        let staged = host
374            .status
375            .staged
376            .as_ref()
377            .ok_or_else(|| anyhow::anyhow!("No staged deployment found"))?;
378
379        // Staged deployment exists, but it will be finalized
380        if !staged.download_only {
381            println!("Staged deployment is present and not in download only mode.");
382            println!("Use `bootc update --apply` to apply the update.");
383            return Ok(());
384        }
385
386        start_finalize_stated_svc()?;
387
388        // Make the staged deployment not download_only
389        let new_staged = StagedDeployment {
390            depl_id: staged.require_composefs()?.verity.clone(),
391            finalization_locked: false,
392        };
393
394        let staged_depl_dir =
395            Dir::open_ambient_dir(COMPOSEFS_TRANSIENT_STATE_DIR, ambient_authority())
396                .context("Opening transient state directory")?;
397
398        staged_depl_dir
399            .atomic_replace_with(
400                COMPOSEFS_STAGED_DEPLOYMENT_FNAME,
401                |f| -> std::io::Result<()> {
402                    serde_json::to_writer(f, &new_staged).map_err(std::io::Error::from)
403                },
404            )
405            .context("Writing staged file")?;
406
407        return apply_upgrade(
408            storage,
409            composefs,
410            &staged.require_composefs()?.verity,
411            &do_upgrade_opts,
412        )
413        .await;
414    }
415
416    let imgref = derived_image.as_ref().or(current_image);
417    let mut booted_imgref = imgref.ok_or_else(|| anyhow::anyhow!("No image source specified"))?;
418
419    // Auto-detect unified storage: use the unified path if the target image is
420    // already in bootc-owned containers-storage, OR if the booted image is —
421    // the latter means the user has opted into unified storage and all
422    // subsequent operations should use it.
423    let current_unified = if let Some(current) = current_image {
424        crate::deploy::image_exists_in_unified_storage(storage, current).await?
425    } else {
426        false
427    };
428    do_upgrade_opts.use_unified = current_unified
429        || crate::deploy::image_exists_in_unified_storage(storage, booted_imgref).await?;
430
431    let repo = &*composefs.repo;
432
433    let (img_pulled, mut img_config) = is_image_pulled(&repo, booted_imgref).await?;
434    let booted_img_digest = img_config.manifest.config().digest().to_string();
435
436    // Check if we already have this update staged
437    // Or if we have another staged deployment with a different image
438    let staged_image = host.status.staged.as_ref().and_then(|i| i.image.as_ref());
439
440    if let Some(staged_image) = staged_image {
441        // We have a staged image and it has the same digest as the currently booted image's latest
442        // digest
443        if staged_image.image_digest == booted_img_digest {
444            if opts.apply {
445                return crate::reboot::reboot();
446            }
447
448            println!("Update already staged. To apply update run `bootc update --apply`");
449
450            return Ok(());
451        }
452
453        // We have a staged image but it's not the update image.
454        // Maybe it's something we got by `bootc switch`
455        // Switch takes precedence over update, so we change the imgref
456        booted_imgref = &staged_image.image;
457
458        let (img_pulled, staged_img_config) = is_image_pulled(&repo, booted_imgref).await?;
459        img_config = staged_img_config;
460
461        if let Some(cfg_verity) = img_pulled {
462            let action = validate_update(
463                storage,
464                composefs,
465                &host,
466                img_config.manifest.config().digest().as_ref(),
467                &cfg_verity,
468                false,
469            )?;
470
471            match action {
472                UpdateAction::Skip => {
473                    println!("No changes in staged image: {booted_imgref:#}");
474                    return Ok(());
475                }
476
477                UpdateAction::Proceed => {
478                    return do_upgrade(
479                        storage,
480                        composefs,
481                        &host,
482                        booted_imgref,
483                        &do_upgrade_opts,
484                        &img_config.manifest,
485                    )
486                    .await;
487                }
488            }
489        }
490    }
491
492    // We already have this container config
493    if let Some(cfg_verity) = img_pulled {
494        let action = validate_update(
495            storage,
496            composefs,
497            &host,
498            &booted_img_digest,
499            &cfg_verity,
500            false,
501        )?;
502
503        match action {
504            UpdateAction::Skip => {
505                println!("No changes in: {booted_imgref:#}");
506                return Ok(());
507            }
508
509            UpdateAction::Proceed => {
510                return do_upgrade(
511                    storage,
512                    composefs,
513                    &host,
514                    booted_imgref,
515                    &do_upgrade_opts,
516                    &img_config.manifest,
517                )
518                .await;
519            }
520        }
521    }
522
523    if opts.check {
524        let current_manifest = get_imginfo(storage, &*composefs.cmdline.digest)?;
525        let diff = ManifestDiff::new(&current_manifest.manifest, &img_config.manifest);
526        diff.print();
527        return Ok(());
528    }
529
530    do_upgrade(
531        storage,
532        composefs,
533        &host,
534        booted_imgref,
535        &do_upgrade_opts,
536        &img_config.manifest,
537    )
538    .await?;
539
540    Ok(())
541}