-
Notifications
You must be signed in to change notification settings - Fork 156
Composefs finalize #1604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cgwalters
merged 4 commits into
bootc-dev:composefs-backend
from
Johan-Liebert1:composefs-finalize
Sep 11, 2025
Merged
Composefs finalize #1604
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| use std::path::Path; | ||
|
|
||
| use crate::bootc_composefs::boot::{get_esp_partition, get_sysroot_parent_dev, BootType}; | ||
| use crate::bootc_composefs::rollback::{rename_exchange_bls_entries, rename_exchange_user_cfg}; | ||
| use crate::spec::Bootloader; | ||
| use crate::{ | ||
| bootc_composefs::status::composefs_deployment_status, composefs_consts::STATE_DIR_ABS, | ||
| }; | ||
| use anyhow::{Context, Result}; | ||
| use bootc_initramfs_setup::{mount_composefs_image, open_dir}; | ||
| use bootc_mount::tempmount::TempMount; | ||
| use cap_std_ext::cap_std::{ambient_authority, fs::Dir}; | ||
| use cap_std_ext::dirext::CapStdExtDirExt; | ||
| use etc_merge::{compute_diff, merge, traverse_etc}; | ||
| use rustix::fs::{fsync, renameat, CWD}; | ||
| use rustix::path::Arg; | ||
|
|
||
| use fn_error_context::context; | ||
|
|
||
| pub(crate) async fn composefs_native_finalize() -> Result<()> { | ||
| let host = composefs_deployment_status().await?; | ||
|
|
||
| let booted_composefs = host.require_composefs_booted()?; | ||
|
|
||
| let Some(staged_depl) = host.status.staged.as_ref() else { | ||
| tracing::debug!("No staged deployment found"); | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| let staged_composefs = staged_depl.composefs.as_ref().ok_or(anyhow::anyhow!( | ||
| "Staged deployment is not a composefs deployment" | ||
| ))?; | ||
|
|
||
| // Mount the booted EROFS image to get pristine etc | ||
| let sysroot = open_dir(CWD, "/sysroot")?; | ||
| let composefs_fd = mount_composefs_image(&sysroot, &booted_composefs.verity, false)?; | ||
|
|
||
| let erofs_tmp_mnt = TempMount::mount_fd(&composefs_fd)?; | ||
|
|
||
| // Perform the /etc merge | ||
| let pristine_etc = | ||
| Dir::open_ambient_dir(erofs_tmp_mnt.dir.path().join("etc"), ambient_authority())?; | ||
| let current_etc = Dir::open_ambient_dir("/etc", ambient_authority())?; | ||
|
|
||
| let new_etc_path = Path::new(STATE_DIR_ABS) | ||
| .join(&staged_composefs.verity) | ||
| .join("etc"); | ||
|
|
||
| let new_etc = Dir::open_ambient_dir(new_etc_path, ambient_authority())?; | ||
|
|
||
| let (pristine_files, current_files, new_files) = | ||
| traverse_etc(&pristine_etc, ¤t_etc, &new_etc)?; | ||
|
|
||
| let diff = compute_diff(&pristine_files, ¤t_files)?; | ||
| merge(¤t_etc, ¤t_files, &new_etc, &new_files, diff)?; | ||
|
|
||
| // Unmount EROFS | ||
| drop(erofs_tmp_mnt); | ||
|
|
||
| let sysroot_parent = get_sysroot_parent_dev()?; | ||
| // NOTE: Assumption here that ESP will always be present | ||
| let (esp_part, ..) = get_esp_partition(&sysroot_parent)?; | ||
|
|
||
| let esp_mount = TempMount::mount_dev(&esp_part)?; | ||
| let boot_dir = Dir::open_ambient_dir("/sysroot/boot", ambient_authority()) | ||
| .context("Opening sysroot/boot")?; | ||
|
|
||
| // NOTE: Assuming here we won't have two bootloaders at the same time | ||
| match booted_composefs.bootloader { | ||
| Bootloader::Grub => match staged_composefs.boot_type { | ||
| BootType::Bls => { | ||
| let entries_dir = boot_dir.open_dir("loader")?; | ||
| rename_exchange_bls_entries(&entries_dir)?; | ||
| } | ||
| BootType::Uki => finalize_staged_grub_uki(&esp_mount.fd, &boot_dir)?, | ||
| }, | ||
|
|
||
| Bootloader::Systemd => match staged_composefs.boot_type { | ||
| BootType::Bls => { | ||
| let entries_dir = esp_mount.fd.open_dir("loader")?; | ||
| rename_exchange_bls_entries(&entries_dir)?; | ||
| } | ||
| BootType::Uki => rename_staged_uki_entries(&esp_mount.fd)?, | ||
| }, | ||
| }; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[context("Grub: Finalizing staged UKI")] | ||
| fn finalize_staged_grub_uki(esp_mount: &Dir, boot_fd: &Dir) -> Result<()> { | ||
| rename_staged_uki_entries(esp_mount)?; | ||
|
|
||
| let entries_dir = boot_fd.open_dir("grub2")?; | ||
| rename_exchange_user_cfg(&entries_dir)?; | ||
|
|
||
| let entries_dir = entries_dir.reopen_as_ownedfd()?; | ||
| fsync(entries_dir).context("fsync")?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[context("Renaming staged UKI entries")] | ||
| fn rename_staged_uki_entries(esp_mount: &Dir) -> Result<()> { | ||
| for entry in esp_mount.entries()? { | ||
| let entry = entry?; | ||
|
|
||
| let filename = entry.file_name(); | ||
| let filename = filename.as_str()?; | ||
|
|
||
| if !filename.ends_with(".staged") { | ||
| continue; | ||
| } | ||
|
|
||
| renameat( | ||
| &esp_mount, | ||
| filename, | ||
| &esp_mount, | ||
| // SAFETY: We won't reach here if not for the above condition | ||
| filename.strip_suffix(".staged").unwrap(), | ||
| ) | ||
| .context("Renaming {filename}")?; | ||
| } | ||
|
|
||
| let esp_mount = esp_mount.reopen_as_ownedfd()?; | ||
| fsync(esp_mount).context("fsync")?; | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,11 +12,60 @@ use crate::bootc_composefs::status::{composefs_deployment_status, get_sorted_bls | |
| use crate::{ | ||
| bootc_composefs::{boot::get_efi_uuid_source, status::get_sorted_uki_boot_entries}, | ||
| composefs_consts::{ | ||
| BOOT_LOADER_ENTRIES, ROLLBACK_BOOT_LOADER_ENTRIES, USER_CFG, USER_CFG_ROLLBACK, | ||
| BOOT_LOADER_ENTRIES, STAGED_BOOT_LOADER_ENTRIES, USER_CFG, USER_CFG_STAGED, | ||
| }, | ||
| spec::BootOrder, | ||
| }; | ||
|
|
||
| pub(crate) fn rename_exchange_user_cfg(entries_dir: &Dir) -> Result<()> { | ||
| tracing::debug!("Atomically exchanging {USER_CFG_STAGED} and {USER_CFG}"); | ||
| renameat_with( | ||
| &entries_dir, | ||
| USER_CFG_STAGED, | ||
| &entries_dir, | ||
| USER_CFG, | ||
| RenameFlags::EXCHANGE, | ||
| ) | ||
| .context("renameat")?; | ||
|
|
||
| tracing::debug!("Removing {USER_CFG_STAGED}"); | ||
| rustix::fs::unlinkat(&entries_dir, USER_CFG_STAGED, AtFlags::empty()).context("unlinkat")?; | ||
|
|
||
| tracing::debug!("Syncing to disk"); | ||
| let entries_dir = entries_dir | ||
| .reopen_as_ownedfd() | ||
| .context(format!("Reopening entries dir as owned fd"))?; | ||
|
|
||
| fsync(entries_dir).context(format!("fsync entries dir"))?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub(crate) fn rename_exchange_bls_entries(entries_dir: &Dir) -> Result<()> { | ||
| tracing::debug!("Atomically exchanging {STAGED_BOOT_LOADER_ENTRIES} and {BOOT_LOADER_ENTRIES}"); | ||
| renameat_with( | ||
| &entries_dir, | ||
| STAGED_BOOT_LOADER_ENTRIES, | ||
| &entries_dir, | ||
| BOOT_LOADER_ENTRIES, | ||
| RenameFlags::EXCHANGE, | ||
| ) | ||
| .context("renameat")?; | ||
|
|
||
| tracing::debug!("Removing {STAGED_BOOT_LOADER_ENTRIES}"); | ||
| rustix::fs::unlinkat(&entries_dir, STAGED_BOOT_LOADER_ENTRIES, AtFlags::REMOVEDIR) | ||
| .context("unlinkat")?; | ||
|
|
||
| tracing::debug!("Syncing to disk"); | ||
| let entries_dir = entries_dir | ||
| .reopen_as_ownedfd() | ||
| .with_context(|| format!("Reopening /sysroot/boot/loader as owned fd"))?; | ||
|
|
||
| fsync(entries_dir).context("fsync")?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[context("Rolling back UKI")] | ||
| pub(crate) fn rollback_composefs_uki() -> Result<()> { | ||
| let user_cfg_path = PathBuf::from("/sysroot/boot/grub2"); | ||
|
|
@@ -45,31 +94,10 @@ pub(crate) fn rollback_composefs_uki() -> Result<()> { | |
| .with_context(|| format!("Opening {user_cfg_path:?}"))?; | ||
|
|
||
| entries_dir | ||
| .atomic_write(USER_CFG_ROLLBACK, buffer) | ||
| .with_context(|| format!("Writing to {USER_CFG_ROLLBACK}"))?; | ||
|
|
||
| tracing::debug!("Atomically exchanging for {USER_CFG_ROLLBACK} and {USER_CFG}"); | ||
| renameat_with( | ||
| &entries_dir, | ||
| USER_CFG_ROLLBACK, | ||
| &entries_dir, | ||
| USER_CFG, | ||
| RenameFlags::EXCHANGE, | ||
| ) | ||
| .context("renameat")?; | ||
|
|
||
| tracing::debug!("Removing {USER_CFG_ROLLBACK}"); | ||
| rustix::fs::unlinkat(&entries_dir, USER_CFG_ROLLBACK, AtFlags::empty()).context("unlinkat")?; | ||
|
|
||
| tracing::debug!("Syncing to disk"); | ||
| fsync( | ||
| entries_dir | ||
| .reopen_as_ownedfd() | ||
| .with_context(|| format!("Reopening {user_cfg_path:?} as owned fd"))?, | ||
| ) | ||
| .with_context(|| format!("fsync {user_cfg_path:?}"))?; | ||
| .atomic_write(USER_CFG_STAGED, buffer) | ||
| .with_context(|| format!("Writing to {USER_CFG_STAGED}"))?; | ||
|
|
||
| Ok(()) | ||
| rename_exchange_user_cfg(&entries_dir) | ||
| } | ||
|
|
||
| #[context("Rolling back BLS")] | ||
|
|
@@ -93,9 +121,7 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> { | |
| assert!(all_configs.len() == 2); | ||
|
|
||
| // Write these | ||
| let dir_path = PathBuf::from(format!( | ||
| "/sysroot/boot/loader/{ROLLBACK_BOOT_LOADER_ENTRIES}", | ||
| )); | ||
| let dir_path = PathBuf::from(format!("/sysroot/boot/loader/{STAGED_BOOT_LOADER_ENTRIES}",)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not new but we should centralize |
||
| create_dir_all(&dir_path).with_context(|| format!("Failed to create dir: {dir_path:?}"))?; | ||
|
|
||
| let rollback_entries_dir = | ||
|
|
@@ -124,30 +150,7 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> { | |
| let dir = Dir::open_ambient_dir("/sysroot/boot/loader", cap_std::ambient_authority()) | ||
| .context("Opening loader dir")?; | ||
|
|
||
| tracing::debug!( | ||
| "Atomically exchanging for {ROLLBACK_BOOT_LOADER_ENTRIES} and {BOOT_LOADER_ENTRIES}" | ||
| ); | ||
| renameat_with( | ||
| &dir, | ||
| ROLLBACK_BOOT_LOADER_ENTRIES, | ||
| &dir, | ||
| BOOT_LOADER_ENTRIES, | ||
| RenameFlags::EXCHANGE, | ||
| ) | ||
| .context("renameat")?; | ||
|
|
||
| tracing::debug!("Removing {ROLLBACK_BOOT_LOADER_ENTRIES}"); | ||
| rustix::fs::unlinkat(&dir, ROLLBACK_BOOT_LOADER_ENTRIES, AtFlags::empty()) | ||
| .context("unlinkat")?; | ||
|
|
||
| tracing::debug!("Syncing to disk"); | ||
| fsync( | ||
| dir.reopen_as_ownedfd() | ||
| .with_context(|| format!("Reopening /sysroot/boot/loader as owned fd"))?, | ||
| ) | ||
| .context("fsync")?; | ||
|
|
||
| Ok(()) | ||
| rename_exchange_bls_entries(&dir) | ||
| } | ||
|
|
||
| #[context("Rolling back composefs")] | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This relates to #1190 (comment) in that what I think would help us overall is to pass around a
Storageinstance that has a canonical opened fd for the sysroot instead of having a lot of places that load it ambiently.The biggest reason to do this is that it will work the same way at install time, where we're actually operating on a different mount point and not
/sysroot.