Skip to content

Commit dd0db6a

Browse files
Do not clean tempdir if we fail to unmount
Ran into this issue by chance in bootupd where a the following drop ordering ```rust drop(tempdir) drop(mount) // unmounts thing mounted at tempdir ``` was causing all the contents of the mounted device to be deleted because the tempdir was being deleted. We might run into the same issue with our Tempdir impl if we fail to unount the ESP and tempdir is dropped deleting everything the ESP. To preven that, explicitly set `disable_cleanup` to false on the Tempdir and only enable cleanup after we've successfully unmounted whatever was mounted Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
1 parent 60d5461 commit dd0db6a

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

crates/mount/src/tempmount.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ impl TempMount {
6767
flags: MountFlags,
6868
data: Option<&std::ffi::CStr>,
6969
) -> Result<Self> {
70-
let tempdir = tempfile::TempDir::new()?;
70+
let mut tempdir = tempfile::TempDir::new()?;
71+
tempdir.disable_cleanup(true);
7172

7273
let utf8path = Utf8Path::from_path(tempdir.path())
7374
.ok_or(anyhow::anyhow!("Failed to convert path to UTF-8 Path"))?;
@@ -81,7 +82,8 @@ impl TempMount {
8182
Ok(fd) => fd,
8283
Err(e) => {
8384
unmount(tempdir.path(), UnmountFlags::DETACH)?;
84-
Err(e)?
85+
tempdir.disable_cleanup(false);
86+
return Err(e)?;
8587
}
8688
};
8789

@@ -91,7 +93,8 @@ impl TempMount {
9193
/// Mount and fd acquired with `open_tree` like syscall
9294
#[context("Mounting fd")]
9395
pub fn mount_fd(mnt_fd: impl AsFd) -> Result<Self> {
94-
let tempdir = tempfile::TempDir::new()?;
96+
let mut tempdir = tempfile::TempDir::new()?;
97+
tempdir.disable_cleanup(true);
9598

9699
move_mount(
97100
mnt_fd.as_fd(),
@@ -109,7 +112,8 @@ impl TempMount {
109112
Ok(fd) => fd,
110113
Err(e) => {
111114
unmount(tempdir.path(), UnmountFlags::DETACH)?;
112-
Err(e)?
115+
tempdir.disable_cleanup(false);
116+
return Err(e)?;
113117
}
114118
};
115119

@@ -120,7 +124,9 @@ impl TempMount {
120124
impl Drop for TempMount {
121125
fn drop(&mut self) {
122126
match unmount(self.dir.path(), UnmountFlags::DETACH) {
123-
Ok(_) => {}
127+
Ok(_) => self.dir.disable_cleanup(false),
128+
// DO NOT clean the tempdir if we fail to unmount
129+
// else it would delete the entire ESP
124130
Err(e) => tracing::warn!("Failed to unmount tempdir: {e:?}"),
125131
}
126132
}

0 commit comments

Comments
 (0)