Skip to content

Commit 19b1573

Browse files
committed
Add mount hiding (umount --detach) after bind mounts
After each bind mount, perform a lazy unmount (MNT_DETACH) to hide the mount point from other namespaces while keeping it active in the current namespace. This matches how Hybrid Mount hides its mounts via KSU's TryUmount API.
1 parent 5016322 commit 19b1573

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

apd/src/magic_mount.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ fn collect_module_files() -> Result<Option<Node>> {
251251
}
252252
}
253253

254+
fn hide_mount(target: &Path) -> Result<()> {
255+
log::debug!("hiding mount {}", target.display());
256+
unmount(target, UnmountFlags::DETACH).ok();
257+
Ok(())
258+
}
259+
254260
fn clone_symlink<Src: AsRef<Path>, Dst: AsRef<Path>>(src: Src, dst: Dst) -> Result<()> {
255261
let src_symlink = read_link(src.as_ref())?;
256262
symlink(&src_symlink, dst.as_ref())?;
@@ -281,6 +287,7 @@ fn mount_mirror<P: AsRef<Path>, WP: AsRef<Path>>(
281287
);
282288
fs::File::create(&work_dir_path)?;
283289
mount_bind(&path, &work_dir_path)?;
290+
hide_mount(&work_dir_path)?;
284291
} else if file_type.is_dir() {
285292
log::debug!(
286293
"mount mirror dir {} -> {}",
@@ -299,6 +306,7 @@ fn mount_mirror<P: AsRef<Path>, WP: AsRef<Path>>(
299306
for entry in read_dir(&path)?.flatten() {
300307
mount_mirror(&path, &work_dir_path, &entry)?;
301308
}
309+
hide_mount(&work_dir_path)?;
302310
} else if file_type.is_symlink() {
303311
log::debug!(
304312
"create mirror symlink {} -> {}",
@@ -447,6 +455,7 @@ fn do_magic_mount<P: AsRef<Path>, WP: AsRef<Path>>(
447455
work_dir_path.display()
448456
);
449457
mount_bind(module_path, target_path)?;
458+
hide_mount(target_path)?;
450459
} else {
451460
bail!("cannot mount root file {}!", path.display());
452461
}
@@ -477,6 +486,7 @@ fn do_magic_mount<P: AsRef<Path>, WP: AsRef<Path>>(
477486
work_dir_path.display()
478487
);
479488
mount_bind(&work_dir_path, &work_dir_path).context("bind self")?;
489+
hide_mount(&work_dir_path)?;
480490
}
481491
if path.exists() && !current.replace {
482492
process_existing_entries(
@@ -495,6 +505,7 @@ fn do_magic_mount<P: AsRef<Path>, WP: AsRef<Path>>(
495505
process_remaining_children(&path, &work_dir_path, current.children, has_tmpfs)?;
496506
if create_tmpfs {
497507
move_tmpfs_to_target(&work_dir_path, &path)?;
508+
hide_mount(&path)?;
498509
}
499510
}
500511
Whiteout => {

0 commit comments

Comments
 (0)