-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathdelete.rs
More file actions
267 lines (212 loc) · 8.36 KB
/
Copy pathdelete.rs
File metadata and controls
267 lines (212 loc) · 8.36 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::{io::Write, path::Path};
use anyhow::{Context, Result};
use cap_std_ext::{cap_std::fs::Dir, dirext::CapStdExtDirExt};
use crate::{
bootc_composefs::{
boot::{BootType, get_efi_uuid_source},
gc::composefs_gc,
rollback::{composefs_rollback, rename_exchange_user_cfg},
status::{get_composefs_status, get_sorted_grub_uki_boot_entries},
},
composefs_consts::{
COMPOSEFS_STAGED_DEPLOYMENT_FNAME, COMPOSEFS_TRANSIENT_STATE_DIR, STATE_DIR_RELATIVE,
TYPE1_ENT_PATH, TYPE1_ENT_PATH_STAGED, USER_CFG_STAGED,
},
parsers::bls_config::{BLSConfigType, parse_bls_config},
spec::{BootEntry, Bootloader, DeploymentEntry},
status::Slot,
store::{BootedComposefs, Storage},
};
#[fn_error_context::context("Deleting Type1 Entry {}", depl.deployment.verity)]
fn delete_type1_conf_file(
depl: &DeploymentEntry,
boot_dir: &Dir,
deleting_staged: bool,
) -> Result<()> {
let entries_dir_path = if deleting_staged {
TYPE1_ENT_PATH_STAGED
} else {
TYPE1_ENT_PATH
};
let entries_dir = boot_dir
.open_dir(entries_dir_path)
.context("Opening entries dir")?;
for entry in entries_dir.entries_utf8()? {
let entry = entry?;
let file_name = entry.file_name()?;
if !file_name.ends_with(".conf") {
// We don't put any non .conf file in the entries dir
// This is here just for sanity
tracing::debug!("Found non .conf file '{file_name}' in entries dir");
continue;
}
let cfg = entries_dir
.read_to_string(&file_name)
.with_context(|| format!("Reading {file_name}"))?;
let bls_config = parse_bls_config(&cfg)?;
match &bls_config.cfg_type {
BLSConfigType::EFI { efi } => {
if !efi.as_str().contains(&depl.deployment.verity) {
continue;
}
// Boot dir in case of EFI will be the ESP
tracing::debug!("Deleting EFI .conf file: {}", file_name);
entry.remove_file().context("Removing .conf file")?;
break;
}
BLSConfigType::NonEFI { options, .. } => {
let options = options
.as_ref()
.ok_or(anyhow::anyhow!("options not found in BLS config file"))?;
if !options.contains(&depl.deployment.verity) {
continue;
}
tracing::debug!("Deleting non-EFI .conf file: {}", file_name);
entry.remove_file().context("Removing .conf file")?;
break;
}
BLSConfigType::Unknown => anyhow::bail!("Unknown BLS Config Type"),
}
}
if deleting_staged {
tracing::debug!(
"Deleting staged entries directory: {}",
TYPE1_ENT_PATH_STAGED
);
boot_dir
.remove_dir_all(TYPE1_ENT_PATH_STAGED)
.context("Removing staged entries dir")?;
}
Ok(())
}
#[fn_error_context::context("Removing Grub Menuentry")]
fn remove_grub_menucfg_entry(id: &str, boot_dir: &Dir, deleting_staged: bool) -> Result<()> {
let grub_dir = boot_dir.open_dir("grub2").context("Opening grub2")?;
if deleting_staged {
tracing::debug!("Deleting staged grub menuentry file: {}", USER_CFG_STAGED);
return grub_dir
.remove_file(USER_CFG_STAGED)
.context("Deleting staged Menuentry");
}
let mut string = String::new();
let menuentries = get_sorted_grub_uki_boot_entries(boot_dir, &mut string)?;
grub_dir
.atomic_replace_with(USER_CFG_STAGED, move |f| -> std::io::Result<_> {
f.write_all(get_efi_uuid_source().as_bytes())?;
for entry in menuentries {
if entry.body.chainloader.contains(id) {
continue;
}
f.write_all(entry.to_string().as_bytes())?;
}
Ok(())
})
.with_context(|| format!("Writing to {USER_CFG_STAGED}"))?;
rustix::fs::fsync(grub_dir.reopen_as_ownedfd().context("Reopening")?).context("fsync")?;
rename_exchange_user_cfg(&grub_dir)
}
/// Deletes the .conf files in case for systemd-boot and Type1 bootloader entries for Grub
/// or removes the corresponding menuentry from Grub's user.cfg in case for grub UKI
/// Does not delete the actual boot binaries
#[fn_error_context::context("Deleting boot entries for deployment {}", deployment.deployment.verity)]
fn delete_depl_boot_entries(
deployment: &DeploymentEntry,
storage: &Storage,
deleting_staged: bool,
) -> Result<()> {
let boot_dir = storage.require_boot_dir()?;
match deployment.deployment.bootloader {
Bootloader::Grub => match deployment.deployment.boot_type {
BootType::Bls => delete_type1_conf_file(deployment, boot_dir, deleting_staged),
BootType::Uki => {
remove_grub_menucfg_entry(&deployment.deployment.verity, boot_dir, deleting_staged)
}
},
Bootloader::Systemd => {
// For Systemd UKI as well, we use .conf files
delete_type1_conf_file(deployment, boot_dir, deleting_staged)
}
Bootloader::None => unreachable!("Checked at install time"),
}
}
#[fn_error_context::context("Deleting state directory for deployment {}", deployment_id)]
pub(crate) fn delete_state_dir(sysroot: &Dir, deployment_id: &str, dry_run: bool) -> Result<()> {
let state_dir = Path::new(STATE_DIR_RELATIVE).join(deployment_id);
tracing::debug!("Deleting state directory: {:?}", state_dir);
if dry_run {
return Ok(());
}
sysroot
.remove_dir_all(&state_dir)
.with_context(|| format!("Removing dir {state_dir:?}"))
}
#[fn_error_context::context("Deleting staged deployment")]
pub(crate) fn delete_staged(
staged: &Option<BootEntry>,
cleanup_list: &Vec<&String>,
dry_run: bool,
) -> Result<()> {
let Some(staged_depl) = staged else {
tracing::debug!("No staged deployment");
return Ok(());
};
if !cleanup_list.contains(&&staged_depl.require_composefs()?.verity) {
tracing::debug!("Staged deployment not in cleanup list");
return Ok(());
}
let file = Path::new(COMPOSEFS_TRANSIENT_STATE_DIR).join(COMPOSEFS_STAGED_DEPLOYMENT_FNAME);
if !dry_run && file.exists() {
tracing::debug!("Deleting staged deployment file: {file:?}");
std::fs::remove_file(file).context("Removing staged file")?;
}
Ok(())
}
#[fn_error_context::context("Deleting composefs deployment {}", deployment_id)]
pub(crate) async fn delete_composefs_deployment(
deployment_id: &str,
storage: &Storage,
booted_cfs: &BootedComposefs,
) -> Result<()> {
const COMPOSEFS_DELETE_JOURNAL_ID: &str = "2a1f0e9d8c7b6a5f4e3d2c1b0a9f8e7d6";
tracing::info!(
message_id = COMPOSEFS_DELETE_JOURNAL_ID,
bootc.operation = "delete",
bootc.current_deployment = booted_cfs.cmdline.digest,
bootc.target_deployment = deployment_id,
"Starting composefs deployment deletion for {}",
deployment_id
);
let host = get_composefs_status(storage, booted_cfs).await?;
let booted = host.require_composefs_booted()?;
if deployment_id == &booted.verity {
anyhow::bail!("Cannot delete currently booted deployment");
}
let all_depls = host.all_composefs_deployments()?;
let depl_to_del = all_depls
.iter()
.find(|d| d.deployment.verity == deployment_id);
let Some(depl_to_del) = depl_to_del else {
anyhow::bail!("Deployment {deployment_id} not found");
};
let deleting_staged = host
.status
.staged
.as_ref()
.and_then(|s| s.composefs.as_ref())
.map_or(false, |cfs| cfs.verity == deployment_id);
// Unqueue rollback. This makes it easier to delete boot entries later on
if matches!(depl_to_del.ty, Some(Slot::Rollback)) && host.status.rollback_queued {
composefs_rollback(storage, booted_cfs).await?;
}
let kind = if depl_to_del.pinned {
"pinned "
} else if deleting_staged {
"staged "
} else {
""
};
tracing::info!("Deleting {kind}deployment '{deployment_id}'");
delete_depl_boot_entries(&depl_to_del, &storage, deleting_staged)?;
composefs_gc(storage, booted_cfs, false, true).await?;
Ok(())
}