|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use crate::bootc_composefs::boot::{get_esp_partition, get_sysroot_parent_dev, BootType}; |
| 4 | +use crate::bootc_composefs::rollback::{rename_exchange_bls_entries, rename_exchange_user_cfg}; |
| 5 | +use crate::spec::Bootloader; |
| 6 | +use crate::{ |
| 7 | + bootc_composefs::status::composefs_deployment_status, composefs_consts::STATE_DIR_ABS, |
| 8 | +}; |
| 9 | +use anyhow::{Context, Result}; |
| 10 | +use bootc_initramfs_setup::{mount_composefs_image, open_dir}; |
| 11 | +use bootc_mount::tempmount::TempMount; |
| 12 | +use cap_std_ext::cap_std::{ambient_authority, fs::Dir}; |
| 13 | +use cap_std_ext::dirext::CapStdExtDirExt; |
| 14 | +use etc_merge::{compute_diff, merge, traverse_etc}; |
| 15 | +use rustix::fs::{fsync, renameat, CWD}; |
| 16 | +use rustix::path::Arg; |
| 17 | + |
| 18 | +use fn_error_context::context; |
| 19 | + |
| 20 | +pub(crate) async fn composefs_native_finalize() -> Result<()> { |
| 21 | + let host = composefs_deployment_status().await?; |
| 22 | + |
| 23 | + let booted_composefs = host.require_composefs_booted()?; |
| 24 | + |
| 25 | + let Some(staged_depl) = host.status.staged.as_ref() else { |
| 26 | + tracing::debug!("No staged deployment found"); |
| 27 | + return Ok(()); |
| 28 | + }; |
| 29 | + |
| 30 | + let staged_composefs = staged_depl.composefs.as_ref().ok_or(anyhow::anyhow!( |
| 31 | + "Staged deployment is not a composefs deployment" |
| 32 | + ))?; |
| 33 | + |
| 34 | + // Mount the booted EROFS image to get pristine etc |
| 35 | + let sysroot = open_dir(CWD, "/sysroot")?; |
| 36 | + let composefs_fd = mount_composefs_image(&sysroot, &booted_composefs.verity, false)?; |
| 37 | + |
| 38 | + let erofs_tmp_mnt = TempMount::mount_fd(&composefs_fd)?; |
| 39 | + |
| 40 | + // Perform the /etc merge |
| 41 | + let pristine_etc = |
| 42 | + Dir::open_ambient_dir(erofs_tmp_mnt.dir.path().join("etc"), ambient_authority())?; |
| 43 | + let current_etc = Dir::open_ambient_dir("/etc", ambient_authority())?; |
| 44 | + |
| 45 | + let new_etc_path = Path::new(STATE_DIR_ABS) |
| 46 | + .join(&staged_composefs.verity) |
| 47 | + .join("etc"); |
| 48 | + |
| 49 | + let new_etc = Dir::open_ambient_dir(new_etc_path, ambient_authority())?; |
| 50 | + |
| 51 | + let (pristine_files, current_files, new_files) = |
| 52 | + traverse_etc(&pristine_etc, ¤t_etc, &new_etc)?; |
| 53 | + |
| 54 | + let diff = compute_diff(&pristine_files, ¤t_files)?; |
| 55 | + merge(¤t_etc, ¤t_files, &new_etc, &new_files, diff)?; |
| 56 | + |
| 57 | + // Unmount EROFS |
| 58 | + drop(erofs_tmp_mnt); |
| 59 | + |
| 60 | + let sysroot_parent = get_sysroot_parent_dev()?; |
| 61 | + // NOTE: Assumption here that ESP will always be present |
| 62 | + let (esp_part, ..) = get_esp_partition(&sysroot_parent)?; |
| 63 | + |
| 64 | + let esp_mount = TempMount::mount_dev(&esp_part)?; |
| 65 | + let boot_dir = Dir::open_ambient_dir("/sysroot/boot", ambient_authority()) |
| 66 | + .context("Opening sysroot/boot")?; |
| 67 | + |
| 68 | + // NOTE: Assuming here we won't have two bootloaders at the same time |
| 69 | + match booted_composefs.bootloader { |
| 70 | + Bootloader::Grub => match staged_composefs.boot_type { |
| 71 | + BootType::Bls => { |
| 72 | + let entries_dir = boot_dir.open_dir("loader")?; |
| 73 | + rename_exchange_bls_entries(&entries_dir)?; |
| 74 | + } |
| 75 | + BootType::Uki => finalize_staged_grub_uki(&esp_mount.fd, &boot_dir)?, |
| 76 | + }, |
| 77 | + |
| 78 | + Bootloader::Systemd => match staged_composefs.boot_type { |
| 79 | + BootType::Bls => { |
| 80 | + let entries_dir = esp_mount.fd.open_dir("loader")?; |
| 81 | + rename_exchange_bls_entries(&entries_dir)?; |
| 82 | + } |
| 83 | + BootType::Uki => rename_staged_uki_entries(&esp_mount.fd)?, |
| 84 | + }, |
| 85 | + }; |
| 86 | + |
| 87 | + Ok(()) |
| 88 | +} |
| 89 | + |
| 90 | +#[context("Grub: Finalizing staged UKI")] |
| 91 | +fn finalize_staged_grub_uki(esp_mount: &Dir, boot_fd: &Dir) -> Result<()> { |
| 92 | + rename_staged_uki_entries(esp_mount)?; |
| 93 | + |
| 94 | + let entries_dir = boot_fd.open_dir("grub2")?; |
| 95 | + rename_exchange_user_cfg(&entries_dir)?; |
| 96 | + |
| 97 | + let entries_dir = entries_dir.reopen_as_ownedfd()?; |
| 98 | + fsync(entries_dir).context("fsync")?; |
| 99 | + |
| 100 | + Ok(()) |
| 101 | +} |
| 102 | + |
| 103 | +#[context("Renaming staged UKI entries")] |
| 104 | +fn rename_staged_uki_entries(esp_mount: &Dir) -> Result<()> { |
| 105 | + for entry in esp_mount.entries()? { |
| 106 | + let entry = entry?; |
| 107 | + |
| 108 | + let filename = entry.file_name(); |
| 109 | + let filename = filename.as_str()?; |
| 110 | + |
| 111 | + if !filename.ends_with(".staged") { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + renameat( |
| 116 | + &esp_mount, |
| 117 | + filename, |
| 118 | + &esp_mount, |
| 119 | + // SAFETY: We won't reach here if not for the above condition |
| 120 | + filename.strip_suffix(".staged").unwrap(), |
| 121 | + ) |
| 122 | + .context("Renaming {filename}")?; |
| 123 | + } |
| 124 | + |
| 125 | + let esp_mount = esp_mount.reopen_as_ownedfd()?; |
| 126 | + fsync(esp_mount).context("fsync")?; |
| 127 | + |
| 128 | + Ok(()) |
| 129 | +} |
0 commit comments