1 .\" Copyright (c) 2016, 2019, 2021 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH MOUNT_NAMESPACES 7 2021-08-27 "Linux" "Linux Programmer's Manual"
28 mount_namespaces \- overview of Linux mount namespaces
30 For an overview of namespaces, see
33 Mount namespaces provide isolation of the list of mounts seen
34 by the processes in each namespace instance.
35 Thus, the processes in each of the mount namespace instances
36 will see distinct single-directory hierarchies.
38 The views provided by the
39 .IR /proc/[pid]/mounts ,
40 .IR /proc/[pid]/mountinfo ,
42 .IR /proc/[pid]/mountstats
43 files (all described in
45 correspond to the mount namespace in which the process with the PID
48 (All of the processes that reside in the same mount namespace
49 will see the same view in these files.)
51 A new mount namespace is created using either
58 When a new mount namespace is created,
59 its mount list is initialized as follows:
61 If the namespace is created using
63 the mount list of the child's namespace is a copy
64 of the mount list in the parent process's mount namespace.
66 If the namespace is created using
68 the mount list of the new namespace is a copy of
69 the mount list in the caller's previous mount namespace.
71 Subsequent modifications to the mount list
75 in either mount namespace will not (by default) affect the
76 mount list seen in the other namespace
77 (but see the following discussion of shared subtrees).
80 After the implementation of mount namespaces was completed,
81 experience showed that the isolation that they provided was,
82 in some cases, too great.
83 For example, in order to make a newly loaded optical disk
84 available in all mount namespaces,
85 a mount operation was required in each namespace.
86 For this use case, and others,
87 the shared subtree feature was introduced in Linux 2.6.15.
88 This feature allows for automatic, controlled propagation of mount and unmount
91 (or, more precisely, between the mounts that are members of a
93 that are propagating events to one another).
95 Each mount is marked (via
97 as having one of the following
98 .IR "propagation types" :
101 This mount shares events with members of a peer group.
102 Mount and unmount events immediately under this mount will propagate
103 to the other mounts that are members of the peer group.
105 here means that the same mount or unmount will automatically occur
106 under all of the other mounts in the peer group.
107 Conversely, mount and unmount events that take place under
108 peer mounts will propagate to this mount.
111 This mount is private; it does not have a peer group.
112 Mount and unmount events do not propagate into or out of this mount.
115 Mount and unmount events propagate into this mount from
116 a (master) shared peer group.
117 Mount and unmount events under this mount do not propagate to any peer.
119 Note that a mount can be the slave of another peer group
120 while at the same time sharing mount and unmount events
121 with a peer group of which it is a member.
122 (More precisely, one peer group can be the slave of another peer group.)
125 This is like a private mount,
126 and in addition this mount can't be bind mounted.
127 Attempts to bind mount this mount
133 When a recursive bind mount
139 flags) is performed on a directory subtree,
140 any bind mounts within the subtree are automatically pruned
141 (i.e., not replicated)
142 when replicating that subtree to produce the target subtree.
144 For a discussion of the propagation type assigned to a new mount,
147 The propagation type is a per-mount-point setting;
148 some mounts may be marked as shared
149 (with each shared mount being a member of a distinct peer group),
150 while others are private
151 (or slaved or unbindable).
153 Note that a mount's propagation type determines whether
154 mounts and unmounts of mounts
155 .I "immediately under"
156 the mount are propagated.
157 Thus, the propagation type does not affect propagation of events for
158 grandchildren and further removed descendant mounts.
159 What happens if the mount itself is unmounted is determined by
160 the propagation type that is in effect for the
164 Members are added to a
166 when a mount is marked as shared and either:
168 the mount is replicated during the creation of a new mount namespace; or
170 a new bind mount is created from the mount.
172 In both of these cases, the new mount joins the peer group
173 of which the existing mount is a member.
175 A new peer group is also created when a child mount is created under
176 an existing mount that is marked as shared.
177 In this case, the new child mount is also marked as shared and
178 the resulting peer group consists of all the mounts
179 that are replicated under the peers of parent mounts.
181 A mount ceases to be a member of a peer group when either
182 the mount is explicitly unmounted,
183 or when the mount is implicitly unmounted because a mount namespace is removed
184 (because it has no more member processes).
186 The propagation type of the mounts in a mount namespace
187 can be discovered via the "optional fields" exposed in
188 .IR /proc/[pid]/mountinfo .
191 for details of this file.)
192 The following tags can appear in the optional fields
193 for a record in that file:
196 This mount is shared in peer group
198 Each peer group has a unique ID that is automatically
199 generated by the kernel,
200 and all mounts in the same peer group will show the same ID.
201 (These IDs are assigned starting from the value 1,
202 and may be recycled when a peer group ceases to have any members.)
205 This mount is a slave to shared peer group
208 .IR propagate_from:X " (since Linux 2.6.26)"
209 .\" commit 97e7e0f71d6d948c25f11f0a33878d9356d9579e
210 This mount is a slave and receives propagation from shared peer group
212 This tag will always appear in conjunction with a
217 is the closest dominant peer group under the process's root directory.
220 is the immediate master of the mount,
221 or if there is no dominant peer group under the same root,
224 field is present and not the
227 For further details, see below.
230 This is an unbindable mount.
232 If none of the above tags is present, then this is a private mount.
233 .SS MS_SHARED and MS_PRIVATE example
234 Suppose that on a terminal in the initial mount namespace,
235 we mark one mount as shared and another as private,
236 and then view the mounts in
237 .IR /proc/self/mountinfo :
241 sh1# \fBmount \-\-make\-shared /mntS\fP
242 sh1# \fBmount \-\-make\-private /mntP\fP
243 sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
244 77 61 8:17 / /mntS rw,relatime shared:1
245 83 61 8:15 / /mntP rw,relatime
250 .IR /proc/self/mountinfo
253 is a shared mount in peer group 1, and that
255 has no optional tags, indicating that it is a private mount.
256 The first two fields in each record in this file are the unique
257 ID for this mount, and the mount ID of the parent mount.
258 We can further inspect this file to see that the parent mount of
262 is the root directory,
264 which is mounted as private:
268 sh1# \fBcat /proc/self/mountinfo | awk \(aq$1 == 61\(aq | sed \(aqs/ \- .*//\(aq\fP
269 61 0 8:2 / / rw,relatime
273 On a second terminal,
274 we create a new mount namespace where we run a second shell
275 and inspect the mounts:
279 $ \fBPS1=\(aqsh2# \(aq sudo unshare \-m \-\-propagation unchanged sh\fP
280 sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
281 222 145 8:17 / /mntS rw,relatime shared:1
282 225 145 8:15 / /mntP rw,relatime
286 The new mount namespace received a copy of the initial mount namespace's
288 These new mounts maintain the same propagation types,
289 but have unique mount IDs.
291 .IR \-\-propagation\ unchanged
294 from marking all mounts as private when creating a new mount namespace,
295 .\" Since util-linux 2.27
296 which it does by default.)
298 In the second terminal, we then create submounts under each of
302 and inspect the set-up:
306 sh2# \fBmkdir /mntS/a\fP
307 sh2# \fBmount /dev/sdb6 /mntS/a\fP
308 sh2# \fBmkdir /mntP/b\fP
309 sh2# \fBmount /dev/sdb7 /mntP/b\fP
310 sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
311 222 145 8:17 / /mntS rw,relatime shared:1
312 225 145 8:15 / /mntP rw,relatime
313 178 222 8:22 / /mntS/a rw,relatime shared:2
314 230 225 8:23 / /mntP/b rw,relatime
318 From the above, it can be seen that
320 was created as shared (inheriting this setting from its parent mount) and
322 was created as a private mount.
324 Returning to the first terminal and inspecting the set-up,
325 we see that the new mount created under the shared mount
327 propagated to its peer mount (in the initial mount namespace),
328 but the new mount created under the private mount
334 sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
335 77 61 8:17 / /mntS rw,relatime shared:1
336 83 61 8:15 / /mntP rw,relatime
337 179 77 8:22 / /mntS/a rw,relatime shared:2
342 Making a mount a slave allows it to receive propagated
343 mount and unmount events from a master shared peer group,
344 while preventing it from propagating events to that master.
345 This is useful if we want to (say) receive a mount event when
346 an optical disk is mounted in the master shared peer group
347 (in another mount namespace),
348 but want to prevent mount and unmount events under the slave mount
349 from having side effects in other namespaces.
351 We can demonstrate the effect of slaving by first marking
352 two mounts as shared in the initial mount namespace:
356 sh1# \fBmount \-\-make\-shared /mntX\fP
357 sh1# \fBmount \-\-make\-shared /mntY\fP
358 sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
359 132 83 8:23 / /mntX rw,relatime shared:1
360 133 83 8:22 / /mntY rw,relatime shared:2
364 On a second terminal,
365 we create a new mount namespace and inspect the mounts:
369 sh2# \fBunshare \-m \-\-propagation unchanged sh\fP
370 sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
371 168 167 8:23 / /mntX rw,relatime shared:1
372 169 167 8:22 / /mntY rw,relatime shared:2
376 In the new mount namespace, we then mark one of the mounts as a slave:
380 sh2# \fBmount \-\-make\-slave /mntY\fP
381 sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
382 168 167 8:23 / /mntX rw,relatime shared:1
383 169 167 8:22 / /mntY rw,relatime master:2
387 From the above output, we see that
389 is now a slave mount that is receiving propagation events from
390 the shared peer group with the ID 2.
392 Continuing in the new namespace, we create submounts under each of
399 sh2# \fBmkdir /mntX/a\fP
400 sh2# \fBmount /dev/sda3 /mntX/a\fP
401 sh2# \fBmkdir /mntY/b\fP
402 sh2# \fBmount /dev/sda5 /mntY/b\fP
406 When we inspect the state of the mounts in the new mount namespace,
409 was created as a new shared mount
410 (inheriting the "shared" setting from its parent mount) and
412 was created as a private mount:
416 sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
417 168 167 8:23 / /mntX rw,relatime shared:1
418 169 167 8:22 / /mntY rw,relatime master:2
419 173 168 8:3 / /mntX/a rw,relatime shared:3
420 175 169 8:5 / /mntY/b rw,relatime
424 Returning to the first terminal (in the initial mount namespace),
425 we see that the mount
427 propagated to the peer (the shared
435 sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
436 132 83 8:23 / /mntX rw,relatime shared:1
437 133 83 8:22 / /mntY rw,relatime shared:2
438 174 132 8:3 / /mntX/a rw,relatime shared:3
442 Now we create a new mount under
448 sh1# \fBmkdir /mntY/c\fP
449 sh1# \fBmount /dev/sda1 /mntY/c\fP
450 sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
451 132 83 8:23 / /mntX rw,relatime shared:1
452 133 83 8:22 / /mntY rw,relatime shared:2
453 174 132 8:3 / /mntX/a rw,relatime shared:3
454 178 133 8:1 / /mntY/c rw,relatime shared:4
458 When we examine the mounts in the second mount namespace,
459 we see that in this case the new mount has been propagated
461 and that the new mount is itself a slave mount (to peer group 4):
465 sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
466 168 167 8:23 / /mntX rw,relatime shared:1
467 169 167 8:22 / /mntY rw,relatime master:2
468 173 168 8:3 / /mntX/a rw,relatime shared:3
469 175 169 8:5 / /mntY/b rw,relatime
470 179 169 8:1 / /mntY/c rw,relatime master:4
474 .SS MS_UNBINDABLE example
475 One of the primary purposes of unbindable mounts is to avoid
476 the "mount explosion" problem when repeatedly performing bind mounts
477 of a higher-level subtree at a lower-level mount.
478 The problem is illustrated by the following shell session.
480 Suppose we have a system with the following mounts:
484 # \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
491 Suppose furthermore that we wish to recursively bind mount
492 the root directory under several users' home directories.
493 We do this for the first user, and inspect the mounts:
497 # \fBmount \-\-rbind / /home/cecilia/\fP
498 # \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
502 /dev/sda1 on /home/cecilia
503 /dev/sdb6 on /home/cecilia/mntX
504 /dev/sdb7 on /home/cecilia/mntY
508 When we repeat this operation for the second user,
509 we start to see the explosion problem:
513 # \fBmount \-\-rbind / /home/henry\fP
514 # \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
518 /dev/sda1 on /home/cecilia
519 /dev/sdb6 on /home/cecilia/mntX
520 /dev/sdb7 on /home/cecilia/mntY
521 /dev/sda1 on /home/henry
522 /dev/sdb6 on /home/henry/mntX
523 /dev/sdb7 on /home/henry/mntY
524 /dev/sda1 on /home/henry/home/cecilia
525 /dev/sdb6 on /home/henry/home/cecilia/mntX
526 /dev/sdb7 on /home/henry/home/cecilia/mntY
532 we have not only recursively added the
536 mounts, but also the recursive mounts of those directories under
538 that were created in the previous step.
539 Upon repeating the step for a third user,
540 it becomes obvious that the explosion is exponential in nature:
544 # \fBmount \-\-rbind / /home/otto\fP
545 # \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
549 /dev/sda1 on /home/cecilia
550 /dev/sdb6 on /home/cecilia/mntX
551 /dev/sdb7 on /home/cecilia/mntY
552 /dev/sda1 on /home/henry
553 /dev/sdb6 on /home/henry/mntX
554 /dev/sdb7 on /home/henry/mntY
555 /dev/sda1 on /home/henry/home/cecilia
556 /dev/sdb6 on /home/henry/home/cecilia/mntX
557 /dev/sdb7 on /home/henry/home/cecilia/mntY
558 /dev/sda1 on /home/otto
559 /dev/sdb6 on /home/otto/mntX
560 /dev/sdb7 on /home/otto/mntY
561 /dev/sda1 on /home/otto/home/cecilia
562 /dev/sdb6 on /home/otto/home/cecilia/mntX
563 /dev/sdb7 on /home/otto/home/cecilia/mntY
564 /dev/sda1 on /home/otto/home/henry
565 /dev/sdb6 on /home/otto/home/henry/mntX
566 /dev/sdb7 on /home/otto/home/henry/mntY
567 /dev/sda1 on /home/otto/home/henry/home/cecilia
568 /dev/sdb6 on /home/otto/home/henry/home/cecilia/mntX
569 /dev/sdb7 on /home/otto/home/henry/home/cecilia/mntY
573 The mount explosion problem in the above scenario can be avoided
574 by making each of the new mounts unbindable.
575 The effect of doing this is that recursive mounts of the root
576 directory will not replicate the unbindable mounts.
577 We make such a mount for the first user:
581 # \fBmount \-\-rbind \-\-make\-unbindable / /home/cecilia\fP
585 Before going further, we show that unbindable mounts are indeed unbindable:
590 # \fBmount \-\-bind /home/cecilia /mntZ\fP
591 mount: wrong fs type, bad option, bad superblock on /home/cecilia,
592 missing codepage or helper program, or other error
594 In some cases useful info is found in syslog \- try
599 Now we create unbindable recursive bind mounts for the other two users:
603 # \fBmount \-\-rbind \-\-make\-unbindable / /home/henry\fP
604 # \fBmount \-\-rbind \-\-make\-unbindable / /home/otto\fP
608 Upon examining the list of mounts,
609 we see there has been no explosion of mounts,
610 because the unbindable mounts were not replicated
611 under each user's directory:
615 # \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
619 /dev/sda1 on /home/cecilia
620 /dev/sdb6 on /home/cecilia/mntX
621 /dev/sdb7 on /home/cecilia/mntY
622 /dev/sda1 on /home/henry
623 /dev/sdb6 on /home/henry/mntX
624 /dev/sdb7 on /home/henry/mntY
625 /dev/sda1 on /home/otto
626 /dev/sdb6 on /home/otto/mntX
627 /dev/sdb7 on /home/otto/mntY
631 .SS Propagation type transitions
632 The following table shows the effect that applying a new propagation type
634 .IR "mount \-\-make\-xxxx")
635 has on the existing propagation type of a mount.
636 The rows correspond to existing propagation types,
637 and the columns are the new propagation settings.
638 For reasons of space, "private" is abbreviated as "priv" and
639 "unbindable" as "unbind".
643 make-shared make-slave make-priv make-unbind
645 shared shared slave/priv [1] priv unbind
646 slave slave+shared slave [2] priv unbind
647 slave+shared slave+shared slave priv unbind
648 private shared priv [2] priv unbind
649 unbindable shared unbind [2] priv unbind
652 Note the following details to the table:
654 If a shared mount is the only mount in its peer group,
655 making it a slave automatically makes it private.
657 Slaving a nonshared mount has no effect on the mount.
659 .SS Bind (MS_BIND) semantics
660 Suppose that the following command is performed:
664 mount \-\-bind A/a B/b
672 is the destination mount,
674 is a subdirectory path under the mount point
678 is a subdirectory path under the mount point
680 The propagation type of the resulting mount,
682 depends on the propagation types of the mounts
686 and is summarized in the following table.
689 lb2 lb1 lb2 lb2 lb2 lb0
690 lb2 lb1 lb2 lb2 lb2 lb0
693 shared private slave unbind
695 dest(B) shared shared shared slave+shared invalid
696 nonshared shared private slave invalid
699 Note that a recursive bind of a subtree follows the same semantics
700 as for a bind operation on each mount in the subtree.
701 (Unbindable mounts are automatically pruned at the target mount point.)
703 For further details, see
704 .I Documentation/filesystems/sharedsubtree.txt
705 in the kernel source tree.
707 .SS Move (MS_MOVE) semantics
708 Suppose that the following command is performed:
720 is the destination mount, and
722 is a subdirectory path under the mount point
724 The propagation type of the resulting mount,
726 depends on the propagation types of the mounts
730 and is summarized in the following table.
733 lb2 lb1 lb2 lb2 lb2 lb0
734 lb2 lb1 lb2 lb2 lb2 lb0
737 shared private slave unbind
739 dest(B) shared shared shared slave+shared invalid
740 nonshared shared private slave unbindable
743 Note: moving a mount that resides under a shared mount is invalid.
745 For further details, see
746 .I Documentation/filesystems/sharedsubtree.txt
747 in the kernel source tree.
750 Suppose that we use the following command to create a mount:
760 is the destination mount, and
762 is a subdirectory path under the mount point
764 The propagation type of the resulting mount,
766 follows the same rules as for a bind mount,
767 where the propagation type of the source mount
768 is considered always to be private.
770 .SS Unmount semantics
771 Suppose that we use the following command to tear down a mount:
785 is the parent mount and
787 is a subdirectory path under the mount point
791 is shared, then all most-recently-mounted mounts at
793 on mounts that receive propagation from mount
795 and do not have submounts under them are unmounted.
797 .SS The /proc/[pid]/mountinfo "propagate_from" tag
800 tag is shown in the optional fields of a
801 .IR /proc/[pid]/mountinfo
802 record in cases where a process can't see a slave's immediate master
803 (i.e., the pathname of the master is not reachable from
804 the filesystem root directory)
805 and so cannot determine the
806 chain of propagation between the mounts it can see.
808 In the following example, we first create a two-link master-slave chain
816 command is used to make the
818 mount point unreachable from the root directory,
819 creating a situation where the master of
821 is not reachable from the (new) root directory of the process.
823 First, we bind mount the root directory onto
829 so that after the later
833 filesystem remains visible at the correct location
834 in the chroot-ed environment.
838 # \fBmkdir \-p /mnt/proc\fP
839 # \fBmount \-\-bind / /mnt\fP
840 # \fBmount \-\-bind /proc /mnt/proc\fP
844 Next, we ensure that the
846 mount is a shared mount in a new peer group (with no peers):
850 # \fBmount \-\-make\-private /mnt\fP # Isolate from any previous peer group
851 # \fBmount \-\-make\-shared /mnt\fP
852 # \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
853 239 61 8:2 / /mnt ... shared:102
854 248 239 0:4 / /mnt/proc ... shared:5
865 # \fBmkdir \-p /tmp/etc\fP
866 # \fBmount \-\-bind /mnt/etc /tmp/etc\fP
867 # \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
868 239 61 8:2 / /mnt ... shared:102
869 248 239 0:4 / /mnt/proc ... shared:5
870 267 40 8:2 /etc /tmp/etc ... shared:102
874 Initially, these two mounts are in the same peer group,
882 so that it can propagate events to the next slave in the chain:
886 # \fBmount \-\-make\-slave /tmp/etc\fP
887 # \fBmount \-\-make\-shared /tmp/etc\fP
888 # \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
889 239 61 8:2 / /mnt ... shared:102
890 248 239 0:4 / /mnt/proc ... shared:5
891 267 40 8:2 /etc /tmp/etc ... shared:105 master:102
899 Again, the two mounts are initially in the same peer group,
907 # \fBmkdir \-p /mnt/tmp/etc\fP
908 # \fBmount \-\-bind /tmp/etc /mnt/tmp/etc\fP
909 # \fBmount \-\-make\-slave /mnt/tmp/etc\fP
910 # \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
911 239 61 8:2 / /mnt ... shared:102
912 248 239 0:4 / /mnt/proc ... shared:5
913 267 40 8:2 /etc /tmp/etc ... shared:105 master:102
914 273 239 8:2 /etc /mnt/tmp/etc ... master:105
918 From the above, we see that
920 is the master of the slave
922 which in turn is the master of the slave
929 directory, which renders the mount with ID 267 unreachable
930 from the (new) root directory:
938 When we examine the state of the mounts inside the chroot-ed environment,
939 we see the following:
943 # \fBcat /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
944 239 61 8:2 / / ... shared:102
945 248 239 0:4 / /proc ... shared:5
946 273 239 8:2 /etc /tmp/etc ... master:105 propagate_from:102
950 Above, we see that the mount with ID 273
951 is a slave whose master is the peer group 105.
952 The mount point for that master is unreachable, and so a
954 tag is displayed, indicating that the closest dominant peer group
955 (i.e., the nearest reachable mount in the slave chain)
956 is the peer group with the ID 102 (corresponding to the
958 mount point before the
963 Mount namespaces first appeared in Linux 2.4.19.
965 Namespaces are a Linux-specific feature.
968 The propagation type assigned to a new mount depends
969 on the propagation type of the parent mount.
970 If the mount has a parent (i.e., it is a non-root mount
971 point) and the propagation type of the parent is
973 then the propagation type of the new mount is also
975 Otherwise, the propagation type of the new mount is
978 Notwithstanding the fact that the default propagation type
979 for new mount is in many cases
982 is typically more useful.
985 automatically remounts all mounts as
988 Thus, on most modern systems, the default propagation type is in practice
993 to create a mount namespace,
994 the goal is commonly to provide full isolation of the mounts
995 in the new namespace,
999 version 2.27) in turn reverses the step performed by
1001 by making all mounts private in the new namespace.
1004 performs the equivalent of the following in the new mount namespace:
1008 mount \-\-make\-rprivate /
1012 To prevent this, one can use the
1013 .IR "\-\-propagation\ unchanged"
1017 An application that creates a new mount namespace directly using
1021 may desire to prevent propagation of mount events to other mount namespaces
1024 This can be done by changing the propagation type of
1025 mounts in the new namespace to either
1029 using a call such as the following:
1033 mount(NULL, "/", MS_SLAVE | MS_REC, NULL);
1037 For a discussion of propagation types when moving mounts
1039 and creating bind mounts
1042 .IR Documentation/filesystems/sharedsubtree.txt .
1044 .\" ============================================================
1046 .SS Restrictions on mount namespaces
1047 Note the following points with respect to mount namespaces:
1049 Each mount namespace has an owner user namespace.
1050 As explained above, when a new mount namespace is created,
1051 its mount list is initialized as a copy of the mount list
1052 of another mount namespace.
1053 If the new namespace and the namespace from which the mount list
1054 was copied are owned by different user namespaces,
1055 then the new mount namespace is considered
1056 .IR "less privileged" .
1058 When creating a less privileged mount namespace,
1059 shared mounts are reduced to slave mounts.
1060 This ensures that mappings performed in less
1061 privileged mount namespaces will not propagate to more privileged
1064 Mounts that come as a single unit from a more privileged mount namespace are
1065 locked together and may not be separated in a less privileged mount
1070 operation brings across all of the mounts from the original
1071 mount namespace as a single unit,
1072 and recursive mounts that propagate between
1073 mount namespaces propagate as a single unit.)
1075 In this context, "may not be separated" means that the mounts
1076 are locked so that they may not be individually unmounted.
1077 Consider the following example:
1083 # \fBmount \-\-bind /dev/null /etc/shadow\fP
1084 # \fBcat /etc/shadow\fP # Produces no output
1089 The above steps, performed in a more privileged mount namespace,
1090 have created a bind mount that
1091 obscures the contents of the shadow password file,
1093 For security reasons, it should not be possible to unmount
1094 that mount in a less privileged mount namespace,
1095 since that would reveal the contents of
1098 Suppose we now create a new mount namespace
1099 owned by a new user namespace.
1100 The new mount namespace will inherit copies of all of the mounts
1101 from the previous mount namespace.
1102 However, those mounts will be locked because the new mount namespace
1104 Consequently, an attempt to unmount the mount fails as show
1105 in the following step:
1110 # \fBunshare \-\-user \-\-map\-root\-user \-\-mount \e\fP
1111 \fBstrace \-o /tmp/log \e\fP
1112 \fBumount /mnt/dir\fP
1113 umount: /etc/shadow: not mounted.
1114 # \fBgrep \(aq^umount\(aq /tmp/log\fP
1115 umount2("/etc/shadow", 0) = \-1 EINVAL (Invalid argument)
1120 The error message from
1122 is a little confusing, but the
1124 output reveals that the underlying
1126 system call failed with the error
1128 which is the error that the kernel returns to indicate that
1129 the mount is locked.
1131 Note, however, that it is possible to stack (and unstack) a
1132 mount on top of one of the inherited locked mounts in a
1133 less privileged mount namespace:
1137 # \fBecho \(aqaaaaa\(aq > /tmp/a\fP # File to mount onto /etc/shadow
1138 # \fBunshare \-\-user \-\-map\-root\-user \-\-mount \e\fP
1139 \fBsh \-c \(aqmount \-\-bind /tmp/a /etc/shadow; cat /etc/shadow\(aq\fP
1141 # \fBumount /etc/shadow\fP
1147 command above, which is performed in the initial mount namespace,
1150 file once more visible in that namespace.
1152 Following on from point [3],
1153 note that it is possible to unmount an entire subtree of mounts that
1154 propagated as a unit into a less privileged mount namespace,
1155 as illustrated in the following example.
1157 First, we create new user and mount namespaces using
1159 In the new mount namespace,
1160 the propagation type of all mounts is set to private.
1161 We then create a shared bind mount at
1163 and a small hierarchy of mounts underneath that mount.
1167 $ \fBPS1=\(aqns1# \(aq sudo unshare \-\-user \-\-map\-root\-user \e\fP
1168 \fB\-\-mount \-\-propagation private bash\fP
1169 ns1# \fBecho $$\fP # We need the PID of this shell later
1171 ns1# \fBmount \-\-make\-shared \-\-bind /mnt /mnt\fP
1172 ns1# \fBmkdir /mnt/x\fP
1173 ns1# \fBmount \-\-make\-private \-t tmpfs none /mnt/x\fP
1174 ns1# \fBmkdir /mnt/x/y\fP
1175 ns1# \fBmount \-\-make\-private \-t tmpfs none /mnt/x/y\fP
1176 ns1# \fBgrep /mnt /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
1177 986 83 8:5 /mnt /mnt rw,relatime shared:344
1178 989 986 0:56 / /mnt/x rw,relatime
1179 990 989 0:57 / /mnt/x/y rw,relatime
1183 Continuing in the same shell session,
1184 we then create a second shell in a new user namespace and a new
1185 (less privileged) mount namespace and
1186 check the state of the propagated mounts rooted at
1191 ns1# \fBPS1=\(aqns2# \(aq unshare \-\-user \-\-map\-root\-user \e\fP
1192 \fB\-\-mount \-\-propagation unchanged bash\fP
1193 ns2# \fBgrep /mnt /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
1194 1239 1204 8:5 /mnt /mnt rw,relatime master:344
1195 1240 1239 0:56 / /mnt/x rw,relatime
1196 1241 1240 0:57 / /mnt/x/y rw,relatime
1200 Of note in the above output is that the propagation type of the mount
1202 has been reduced to slave, as explained in point [2].
1203 This means that submount events will propagate from the master
1205 in "ns1", but propagation will not occur in the opposite direction.
1207 From a separate terminal window, we then use
1209 to enter the mount and user namespaces corresponding to "ns1".
1210 In that terminal window, we then recursively bind mount
1217 $ \fBPS1=\(aqns3# \(aq sudo nsenter \-t 778501 \-\-user \-\-mount\fP
1218 ns3# \fBmount \-\-rbind \-\-make\-private /mnt/x /mnt/ppp\fP
1219 ns3# \fBgrep /mnt /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
1220 986 83 8:5 /mnt /mnt rw,relatime shared:344
1221 989 986 0:56 / /mnt/x rw,relatime
1222 990 989 0:57 / /mnt/x/y rw,relatime
1223 1242 986 0:56 / /mnt/ppp rw,relatime
1224 1243 1242 0:57 / /mnt/ppp/y rw,relatime shared:518
1228 Because the propagation type of the parent mount,
1230 was shared, the recursive bind mount propagated a small subtree of
1231 mounts under the slave mount
1234 as can be verified by executing the following command in that shell session:
1238 ns2# \fBgrep /mnt /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
1239 1239 1204 8:5 /mnt /mnt rw,relatime master:344
1240 1240 1239 0:56 / /mnt/x rw,relatime
1241 1241 1240 0:57 / /mnt/x/y rw,relatime
1242 1244 1239 0:56 / /mnt/ppp rw,relatime
1243 1245 1244 0:57 / /mnt/ppp/y rw,relatime master:518
1247 While it is not possible to unmount a part of the propagated subtree
1250 it is possible to unmount the entire subtree,
1251 as shown by the following commands:
1255 ns2# \fBumount /mnt/ppp/y\fP
1256 umount: /mnt/ppp/y: not mounted.
1257 ns2# \fBumount \-l /mnt/ppp | sed \(aqs/ \- .*//\(aq\fP # Succeeds...
1258 ns2# \fBgrep /mnt /proc/self/mountinfo\fP
1259 1239 1204 8:5 /mnt /mnt rw,relatime master:344
1260 1240 1239 0:56 / /mnt/x rw,relatime
1261 1241 1240 0:57 / /mnt/x/y rw,relatime
1271 and the "atime" flags
1275 settings become locked
1276 .\" commit 9566d6742852c527bf5af38af5cbb878dad75705
1277 .\" Author: Eric W. Biederman <ebiederm@xmission.com>
1278 .\" Date: Mon Jul 28 17:26:07 2014 -0700
1280 .\" mnt: Correct permission checks in do_remount
1282 when propagated from a more privileged to
1283 a less privileged mount namespace,
1284 and may not be changed in the less privileged mount namespace.
1286 This point is illustrated in the following example where,
1287 in a more privileged mount namespace,
1288 we create a bind mount that is marked as read-only.
1289 For security reasons,
1290 it should not be possible to make the mount writable in
1291 a less privileged mount namespace, and indeed the kernel prevents this:
1296 $ \fBsudo mkdir /mnt/dir\fP
1297 $ \fBsudo mount \-\-bind \-o ro /some/path /mnt/dir\fP
1298 $ \fBsudo unshare \-\-user \-\-map\-root\-user \-\-mount \e\fP
1299 \fBmount \-o remount,rw /mnt/dir\fP
1300 mount: /mnt/dir: permission denied.
1305 .\" (As of 3.18-rc1 (in Al Viro's 2014-08-30 vfs.git#for-next tree))
1306 A file or directory that is a mount point in one namespace that is not
1307 a mount point in another namespace, may be renamed, unlinked, or removed
1309 in the mount namespace in which it is not a mount point
1310 (subject to the usual permission checks).
1311 Consequently, the mount point is removed in the mount namespace
1312 where it was a mount point.
1314 Previously (before Linux 3.18),
1315 .\" mtk: The change was in Linux 3.18, I think, with this commit:
1316 .\" commit 8ed936b5671bfb33d89bc60bdcc7cf0470ba52fe
1317 .\" Author: Eric W. Biederman <ebiederman@twitter.com>
1318 .\" Date: Tue Oct 1 18:33:48 2013 -0700
1320 .\" vfs: Lazily remove mounts on unlinked files and directories.
1321 attempting to unlink, rename, or remove a file or directory
1322 that was a mount point in another mount namespace would result in the error
1324 That behavior had technical problems of enforcement (e.g., for NFS)
1325 and permitted denial-of-service attacks against more privileged users
1326 (i.e., preventing individual files from being updated
1327 by bind mounting on top of them).
1335 .BR mount_setattr (2),
1342 .BR user_namespaces (7),
1345 .BR pam_namespace (8),
1349 .IR Documentation/filesystems/sharedsubtree.txt
1350 in the kernel source tree.