-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathfinalize.rs
More file actions
177 lines (137 loc) · 5.64 KB
/
finalize.rs
File metadata and controls
177 lines (137 loc) · 5.64 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::path::Path;
use crate::bootc_composefs::boot::BootType;
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::{Directory, Stat};
use etc_merge::{compute_diff, merge, print_diff, traverse_etc};
use rustix::fs::{fsync, renameat};
use rustix::path::Arg;
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,
&Directory::new(Stat::uninitialized()),
)?;
print_diff(&diff, &mut std::io::stdout());
Ok(())
}
pub(crate) async fn composefs_backend_finalize(
storage: &Storage,
booted_cfs: &BootedComposefs,
) -> Result<()> {
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::debug!("No staged deployment found");
return Ok(());
};
if staged_depl.download_only {
tracing::debug!("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()?;
let esp_mount = storage
.esp
.as_ref()
.ok_or_else(|| anyhow::anyhow!("ESP not found"))?;
// 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 => {
if matches!(staged_composefs.boot_type, BootType::Uki) {
rename_staged_uki_entries(&esp_mount.fd)?;
}
let entries_dir = boot_dir.open_dir("loader")?;
rename_exchange_bls_entries(&entries_dir)?;
}
Bootloader::None => unreachable!("Checked at install time"),
};
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(())
}