5

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?

  • 1
    I guess that nothing would happen. At least according to my understanding of namespaces(7) – Basile Starynkevitch Jun 25 '15 at 16:16
  • According to the manpage you refer, network interfaces in a names space will be handed back to the origin namespace. What happens to mounts in mount namespaces? – abraXxl Jun 26 '15 at 12:00
  • I really don't know; study the kernel's source code. – Basile Starynkevitch Jun 26 '15 at 12:42
2

It seems that the mounts remain, but become unaccessible.

I did the following as a test:

  1. Entered a new namespace and mounted tmpfs:
root@localhost:~# mkdir tmp
root@localhost:~# unshare -m bash
root@localhost:~# mount -t tmpfs tmpfs tmp
  1. Checked memory usage before and after creating a 200 megabyte file on the tmpfs. You can notice the "shared" usage going from 404 megabytes to 604 megabytes:
root@localhost:~# free -m
              total        used        free      shared  buff/cache   available
Mem:           9885        2966         681         404        6237        6148
Swap:          8191         293        7898
root@localhost:~# dd if=/dev/urandom of=tmp/dummy bs=1M count=200
200+0 records in
200+0 records out
209715200 bytes (210 MB, 200 MiB) copied, 12.0075 s, 17.5 MB/s
root@localhost:~# free -m
              total        used        free      shared  buff/cache   available
Mem:           9885        2966         481         604        6437        5948
Swap:          8191         293        7898
  1. Exited the unshare shell, the memory did not get reclaimed:
root@localhost:~# exit
root@localhost:~# free -m
              total        used        free      shared  buff/cache   available
Mem:           9885        2963         484         604        6437        5951
Swap:          8191         293        7898

Whereas if I unmount the tmpfs, the shared count goes back to initial value.

1

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.

0

Cannot add a comment, because not enough rep, so here it goes: I did exactly the same test as jpa on a unmodified debian stretch and the memory gets back to the initial value, even though I did NOT unmount. So it seems this behaviour has changed.

http://man7.org/linux/man-pages/man7/namespaces.7.html

implies this at least by saying:

Bind mounting (see mount(2)) one of the files in this directory to somewhere else in the filesystem keeps the corresponding namespace of the process specified by pid alive even if all processes currently in the namespace terminate.

So not doing that destroys the namespace (?).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.