Skip to content

Commit c73d2d2

Browse files
committed
efi: unmount only if bootupd did the mount
Assisted-by: Claude Code (Claude Sonnet 4)
1 parent 175f2fa commit c73d2d2

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/efi.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ pub(crate) fn is_efi_booted() -> Result<bool> {
6868
#[derive(Default)]
6969
pub(crate) struct Efi {
7070
mountpoint: RefCell<Option<PathBuf>>,
71+
/// Track whether we mounted the ESP ourselves (true) or found it pre-mounted (false)
72+
did_mount: RefCell<bool>,
7173
}
7274

7375
impl Efi {
@@ -93,6 +95,7 @@ impl Efi {
9395
if let Some(mnt) = found_mount {
9496
log::debug!("Reusing existing mount point {mnt:?}");
9597
*self.mountpoint.borrow_mut() = Some(mnt.clone());
98+
*self.did_mount.borrow_mut() = false; // We didn't mount it
9699
Ok(Some(mnt))
97100
} else {
98101
Ok(None)
@@ -111,14 +114,15 @@ impl Efi {
111114
std::process::Command::new("mount")
112115
.arg(&esp_device)
113116
.arg(&mnt)
114-
.run()
117+
.run_inherited()
115118
.with_context(|| format!("Failed to mount {:?}", esp_device))?;
116119
log::debug!("Mounted at {mnt:?}");
117120
mountpoint = Some(mnt);
118121
break;
119122
}
120123
let mnt = mountpoint.ok_or_else(|| anyhow::anyhow!("No mount point found"))?;
121124
*self.mountpoint.borrow_mut() = Some(mnt.clone());
125+
*self.did_mount.borrow_mut() = true; // We mounted it ourselves
122126
Ok(mnt)
123127
}
124128

@@ -136,12 +140,16 @@ impl Efi {
136140
}
137141

138142
fn unmount(&self) -> Result<()> {
139-
if let Some(mount) = self.mountpoint.borrow_mut().take() {
140-
Command::new("umount")
141-
.arg(&mount)
142-
.run()
143-
.with_context(|| format!("Failed to unmount {mount:?}"))?;
144-
log::trace!("Unmounted");
143+
// Only unmount if we mounted it ourselves
144+
if *self.did_mount.borrow() {
145+
if let Some(mount) = self.mountpoint.borrow_mut().take() {
146+
Command::new("umount")
147+
.arg(&mount)
148+
.run_inherited()
149+
.with_context(|| format!("Failed to unmount {mount:?}"))?;
150+
log::trace!("Unmounted");
151+
*self.did_mount.borrow_mut() = false;
152+
}
145153
}
146154
Ok(())
147155
}
@@ -775,7 +783,7 @@ pub(crate) fn clear_efi_target(target: &str) -> Result<()> {
775783
let mut cmd = Command::new(EFIBOOTMGR);
776784
cmd.args(["-b", entry.id.as_str(), "-B"]);
777785
println!("Executing: {cmd:?}");
778-
cmd.run_with_cmd_context()?;
786+
cmd.run_inherited_with_cmd_context()?;
779787
}
780788
}
781789

@@ -803,7 +811,7 @@ pub(crate) fn create_efi_boot_entry(
803811
target,
804812
]);
805813
println!("Executing: {cmd:?}");
806-
cmd.run_with_cmd_context()
814+
cmd.run_inherited_with_cmd_context()
807815
}
808816

809817
#[context("Find target file recursively")]

0 commit comments

Comments
 (0)