Unmount a rbind mount without affecting the original mount

Asked on serverfault but didn’t get enough attention, so reposted here, with the hope some people here know the answer.

There is another question discussing about umounting rbind mounts, but the solution has unwanted effect. Consider the following directory layout:

.
├── A_dir
│   └── mount_b
├── B_dir
│   └── mount_c
└── C_dir

Now I bind C_dir to B_dir/mount_c and rbind B_dir to A_dir/mount_b:

[hidden]$ sudo mount --bind C_dir B_dir/mount_c
[hidden]$ sudo mount --rbind B_dir A_dir/mount_b
[hidden]$ mount | grep _dir | wc -l
3

Now umount A_dir/mount_b will fail, which is not surprising. According to the answers everywhere on the web, we need to umount A_dir/mount_b/mount_c first then umount A_dir/mount_b. However, umount A_dir/mount_b/mount_c will also unmount B_dir/mount_c, which is unwanted:

[hidden]$ sudo umount A_dir/mount_b/mount_c
[hidden]$ mount | grep _dir | wc -l
1

Now my question is, how do I unmount A_dir/mount_b but leaving B_dir unaffected, i.e. there is still a bind B_dir/mount_c to C_dir?

EDIT: this problem doesn’t seem to appear in Ubuntu. More specifically, it works fine on my Ubuntu 14.04 but not working on Fedora 23 and CentOS 7. Why there is the difference and what’s the work around for Fedora and CentOS?

EDIT: some more information on the actual problem that I am trying to solve. I tried to create a sandbox and used --rbind to mount the /dev and /proc to the sandbox. When destroying the sandbox, it seems I can’t cleanly destroy it because unmounting <sandbox-root>/dev/pts in FC23 and CentOS7 will unmount /dev/pts, after which my shell and SSH connections hang and I have to reboot the machine. That’s why I am asking if there is a way to unmount the –rbind mounts without affecting submounts.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I found the solution myself. I simply just need to use --make-rslave to make any changes in A_dir/mount_b not propagate back to B_dir:

sudo mount --make-rslave A_dir/mount_b
sudo umount -R A_dir/mount_b

See mount man page section The shared subtree operations.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x