-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathfinalize.rs
More file actions
160 lines (126 loc) · 5.38 KB
/
Copy pathfinalize.rs
File metadata and controls
160 lines (126 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::path::Path;
use crate::bootc_composefs::boot::BootType;
use crate::bootc_composefs::gc::composefs_gc;
use crate::bootc_composefs::rollback::{rename_exchange_bls_entries, rename_exchange_user_cfg};
use crate::bootc_composefs::status::get_composefs_status;
use crate::composefs_consts::STATE_DIR_ABS;
use crate::spec::Bootloader;
use crate::store::{BootedComposefs, Storage};
use anyhow::{Context, Result};
use bootc_initramfs_setup::mount_composefs_image;
use bootc_mount::tempmount::TempMount;
use cap_std_ext::cap_std::{ambient_authority, fs::Dir};
use cap_std_ext::dirext::CapStdExtDirExt;
use composefs::generic_tree::{FileSystem, Stat};
use composefs_ctl::composefs;
use etc_merge::{compute_diff, merge, print_diff, traverse_etc};
use rustix::fs::fsync;
use fn_error_context::context;
pub(crate) async fn get_etc_diff(storage: &Storage, booted_cfs: &BootedComposefs) -> Result<()> {
let host = get_composefs_status(storage, booted_cfs).await?;
let booted_composefs = host.require_composefs_booted()?;
// Mount the booted EROFS image to get pristine etc
let sysroot_fd = storage.physical_root.reopen_as_ownedfd()?;
let composefs_fd = mount_composefs_image(
&sysroot_fd,
&booted_composefs.verity,
booted_cfs.cmdline.allow_missing_fsverity,
)?;
let erofs_tmp_mnt = TempMount::mount_fd(&composefs_fd)?;
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 (pristine_files, current_files, _) = traverse_etc(&pristine_etc, ¤t_etc, None)?;
let diff = compute_diff(
&pristine_files,
¤t_files,
&FileSystem::new(Stat::uninitialized()),
)?;
print_diff(&diff, &mut std::io::stdout());
Ok(())
}
pub(crate) async fn composefs_backend_finalize(
storage: &Storage,
booted_cfs: &BootedComposefs,
) -> Result<()> {
const COMPOSEFS_FINALIZE_JOURNAL_ID: &str = "0e9d8c7b6a5f4e3d2c1b0a9f8e7d6c5b4";
tracing::info!(
message_id = COMPOSEFS_FINALIZE_JOURNAL_ID,
bootc.operation = "finalize",
bootc.current_deployment = booted_cfs.cmdline.digest,
"Starting composefs staged deployment finalization"
);
let host = get_composefs_status(storage, booted_cfs).await?;
let booted_composefs = host.require_composefs_booted()?;
let Some(staged_depl) = host.status.staged.as_ref() else {
tracing::info!(
message_id = COMPOSEFS_FINALIZE_JOURNAL_ID,
bootc.operation = "finalize",
"No staged deployment found"
);
return Ok(());
};
if staged_depl.download_only {
tracing::info!(
message_id = COMPOSEFS_FINALIZE_JOURNAL_ID,
bootc.operation = "finalize",
bootc.download_only = "true",
"Staged deployment is marked download only. Won't finalize"
);
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_fd = storage.physical_root.reopen_as_ownedfd()?;
let composefs_fd = mount_composefs_image(
&sysroot_fd,
&booted_composefs.verity,
booted_cfs.cmdline.allow_missing_fsverity,
)?;
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, Some(&new_etc))?;
let new_files =
new_files.ok_or_else(|| anyhow::anyhow!("Failed to get dirtree for new etc"))?;
let diff = compute_diff(&pristine_files, ¤t_files, &new_files)?;
merge(¤t_etc, ¤t_files, &new_etc, &new_files, &diff)?;
// Unmount EROFS
drop(erofs_tmp_mnt);
let boot_dir = storage.require_boot_dir()?;
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(boot_dir)?,
},
Bootloader::Systemd => {
let entries_dir = boot_dir.open_dir("loader")?;
rename_exchange_bls_entries(&entries_dir)?;
}
Bootloader::None => unreachable!("Checked at install time"),
};
// Now that we have successfully updated bootloader entires, we can GC the unreferenced ones
// We do not prune the composefs repository here though
composefs_gc(storage, booted_cfs, false, false).await?;
Ok(())
}
#[context("Grub: Finalizing staged UKI")]
fn finalize_staged_grub_uki(boot_fd: &Dir) -> Result<()> {
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(())
}