Skip to content

Commit b7956de

Browse files
committed
feat: Clean up LUKS decrypted volumes after use
1 parent c3c3081 commit b7956de

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/backend/install.rs

+17
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,23 @@ impl InstallationState {
245245
Self::dd_submarine(blockdev)?;
246246
InstallationType::set_cgpt_flags(blockdev)?;
247247
}
248+
249+
tracing::info!("Cleaning up state...");
250+
251+
if let Some(_key) = &self.encryption_key {
252+
std::fs::remove_file(keyfile) // don't care if it fails
253+
.unwrap_or_else(|e| tracing::warn!("Failed to remove keyfile: {e}"));
254+
255+
// Close all mapped LUKS devices if exists
256+
257+
if let Ok(mut cache) = super::repart_output::MAPPER_CACHE.try_write() {
258+
if let Some(cache) = std::sync::Arc::get_mut(&mut cache) {
259+
cache.clear();
260+
}
261+
}
262+
263+
}
264+
248265
tracing::info!("install() finished");
249266
Ok(())
250267
}

src/backend/repart_output.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use bytesize::ByteSize;
22
use color_eyre::eyre::{Context, OptionExt};
3-
use lsblk::Populate;
43
use std::path::PathBuf;
54
use std::{fmt::Write, sync::Arc};
65
use sys_mount::MountFlags;
76
use tiffin::{Container, MountTarget};
87

9-
use crate::INSTALLATION_STATE;
108
use crate::{
119
backend::repartcfg::{FileSystem, RepartConfig},
1210
util::sys::check_uefi,
@@ -48,15 +46,15 @@ impl MapperCache {
4846
}
4947
}
5048

51-
fn get(&self, node: &str) -> Option<&PathBuf> {
49+
pub(crate) fn get(&self, node: &str) -> Option<&PathBuf> {
5250
self.cache.get(node)
5351
}
5452

55-
fn insert(&mut self, node: String, path: PathBuf) {
53+
pub(crate) fn insert(&mut self, node: String, path: PathBuf) {
5654
self.cache.insert(node, path);
5755
}
5856

59-
fn clear(&mut self) {
57+
pub(crate) fn clear(&mut self) {
6058
for (node, path) in self.cache.drain() {
6159
if let Err(e) = cryptsetup_close(&path.to_string_lossy()) {
6260
tracing::error!(?node, ?e, "Failed to close mapper device");

0 commit comments

Comments
 (0)