Linux – What happens if the last process in a namespace exits

linuxnamespacenetwork-namespaces

I am running Linux.

I have a single process in a mount namespace.
I did in this process a mount -t tmpfs tmpfs /mountpoint.

What happens if the process exits and there are no more processes in that mount namespace?
Will the filesystem be automatically unmounted?
Will the mount namespace be destroyed?
If the namespaces and the mount are still active how do I access it?

What happens to tun/tap/macvtap interfaces if a network namespace has no more processes?

Best Answer

I was wondering the same thing, and so I ran a little test (on kernel 4.20.0, using unshare from util-linux 2.33; the manpage for unshare in that version has some notes on shared/private mounts that are worth reading, and YMMV if you are using an older version).

TL;DR: Yes, the filesystem is unmounted when the last process in the namespace exits.


In my case, the device I'm testing with is dm-6, and it is not mounted in the "outer" namespace.

Window 1:

cd /sys/fs/ext4
ls -d dm-6
# No such file or directory
Window 2:
unshare -m
mount /dev/dm-6 /mnt/tmp
# don't exit yet, keep the namespace active
Window 3: Do the same thing as window 1.

Window 1:

ls -d dm-6
# exists now

Window 2: Exit the unshare environment

Window 1: Check again, dm-6 is still there

Window 3: Exit the unshare environment

Window 1: Check again, dm-6 is gone again


Another useful demo / test: Similar idea, but instead of having 3 windows, enter and exit Window 2 twice. Check dmesg or logs, and verified that the kernel message that it mounted the filesystem appears twice in this case.

Related Question