]> git.ipfire.org Git - thirdparty/man-pages.git/blame_incremental - man7/mount_namespaces.7
epoll.7: wfix
[thirdparty/man-pages.git] / man7 / mount_namespaces.7
... / ...
CommitLineData
1.\" Copyright (c) 2016, 2019 by Michael Kerrisk <mtk.manpages@gmail.com>
2.\"
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.
7.\"
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.
12.\"
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
19.\" professionally.
20.\"
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
23.\" %%%LICENSE_END
24.\"
25.\"
26.TH MOUNT_NAMESPACES 7 2019-08-02 "Linux" "Linux Programmer's Manual"
27.SH NAME
28mount_namespaces \- overview of Linux mount namespaces
29.SH DESCRIPTION
30For an overview of namespaces, see
31.BR namespaces (7).
32.PP
33Mount namespaces provide isolation of the list of mount points seen
34by the processes in each namespace instance.
35Thus, the processes in each of the mount namespace instances
36will see distinct single-directory hierarchies.
37.PP
38The views provided by the
39.IR /proc/[pid]/mounts ,
40.IR /proc/[pid]/mountinfo ,
41and
42.IR /proc/[pid]/mountstats
43files (all described in
44.BR proc (5))
45correspond to the mount namespace in which the process with the PID
46.IR [pid]
47resides.
48(All of the processes that reside in the same mount namespace
49will see the same view in these files.)
50.PP
51A new mount namespace is created using either
52.BR clone (2)
53or
54.BR unshare (2)
55with the
56.BR CLONE_NEWNS
57flag.
58When a new mount namespace is created,
59its mount point list is initialized as follows:
60.IP * 3
61If the namespace is created using
62.BR clone (2),
63the mount point list of the child's namespace is a copy
64of the mount point list in the parent's namespace.
65.IP *
66If the namespace is created using
67.BR unshare (2),
68the mount point list of the new namespace is a copy of
69the mount point list in the caller's previous mount namespace.
70.PP
71Subsequent modifications to the mount point list
72.RB ( mount (2)
73and
74.BR umount (2))
75in either mount namespace will not (by default) affect the
76mount point list seen in the other namespace
77(but see the following discussion of shared subtrees).
78.\"
79.\" ============================================================
80.\"
81.SS Restrictions on mount namespaces
82Note the following points with respect to mount namespaces:
83.IP * 3
84Each mount namespace has an owner user namespace.
85As explained above, when a new mount namespace is created,
86its mount point list is initialized as a copy of the mount point list
87of another mount namespace.
88If the new namespace and the namespace from which the mount point list
89was copied are owned by different user namespaces,
90then the new mount namespace is considered
91.IR "less privileged" .
92.IP *
93When creating a less privileged mount namespace,
94shared mounts are reduced to slave mounts.
95(Shared and slave mounts are discussed below.)
96This ensures that mappings performed in less
97privileged mount namespaces will not propagate to more privileged
98mount namespaces.
99.IP *
100.\" FIXME .
101.\" What does "come as a single unit from more privileged mount" mean?
102Mounts that come as a single unit from more privileged mount are
103locked together and may not be separated in a less privileged mount
104namespace.
105(The
106.BR unshare (2)
107.B CLONE_NEWNS
108operation brings across all of the mounts from the original
109mount namespace as a single unit,
110and recursive mounts that propagate between
111mount namespaces propagate as a single unit.)
112.IP *
113The
114.BR mount (2)
115flags
116.BR MS_RDONLY ,
117.BR MS_NOSUID ,
118.BR MS_NOEXEC ,
119and the "atime" flags
120.RB ( MS_NOATIME ,
121.BR MS_NODIRATIME ,
122.BR MS_RELATIME )
123settings become locked
124.\" commit 9566d6742852c527bf5af38af5cbb878dad75705
125.\" Author: Eric W. Biederman <ebiederm@xmission.com>
126.\" Date: Mon Jul 28 17:26:07 2014 -0700
127.\"
128.\" mnt: Correct permission checks in do_remount
129.\"
130when propagated from a more privileged to
131a less privileged mount namespace,
132and may not be changed in the less privileged mount namespace.
133.IP *
134.\" (As of 3.18-rc1 (in Al Viro's 2014-08-30 vfs.git#for-next tree))
135A file or directory that is a mount point in one namespace that is not
136a mount point in another namespace, may be renamed, unlinked, or removed
137.RB ( rmdir (2))
138in the mount namespace in which it is not a mount point
139(subject to the usual permission checks).
140Consequently, the mount point is removed in the mount namespace
141where it was a mount point.
142.IP
143Previously (before Linux 3.18),
144.\" mtk: The change was in Linux 3.18, I think, with this commit:
145.\" commit 8ed936b5671bfb33d89bc60bdcc7cf0470ba52fe
146.\" Author: Eric W. Biederman <ebiederman@twitter.com>
147.\" Date: Tue Oct 1 18:33:48 2013 -0700
148.\"
149.\" vfs: Lazily remove mounts on unlinked files and directories.
150attempting to unlink, rename, or remove a file or directory
151that was a mount point in another mount namespace would result in the error
152.BR EBUSY .
153That behavior had technical problems of enforcement (e.g., for NFS)
154and permitted denial-of-service attacks against more privileged users.
155(i.e., preventing individual files from being updated
156by bind mounting on top of them).
157.\"
158.SH SHARED SUBTREES
159After the implementation of mount namespaces was completed,
160experience showed that the isolation that they provided was,
161in some cases, too great.
162For example, in order to make a newly loaded optical disk
163available in all mount namespaces,
164a mount operation was required in each namespace.
165For this use case, and others,
166the shared subtree feature was introduced in Linux 2.6.15.
167This feature allows for automatic, controlled propagation of mount and unmount
168.I events
169between namespaces
170(or, more precisely, between the members of a
171.IR "peer group"
172that are propagating events to one another).
173.PP
174Each mount point is marked (via
175.BR mount (2))
176as having one of the following
177.IR "propagation types" :
178.TP
179.BR MS_SHARED
180This mount point shares events with members of a peer group.
181Mount and unmount events immediately under this mount point will propagate
182to the other mount points that are members of the peer group.
183.I Propagation
184here means that the same mount or unmount will automatically occur
185under all of the other mount points in the peer group.
186Conversely, mount and unmount events that take place under
187peer mount points will propagate to this mount point.
188.TP
189.BR MS_PRIVATE
190This mount point is private; it does not have a peer group.
191Mount and unmount events do not propagate into or out of this mount point.
192.TP
193.BR MS_SLAVE
194Mount and unmount events propagate into this mount point from
195a (master) shared peer group.
196Mount and unmount events under this mount point do not propagate to any peer.
197.IP
198Note that a mount point can be the slave of another peer group
199while at the same time sharing mount and unmount events
200with a peer group of which it is a member.
201(More precisely, one peer group can be the slave of another peer group.)
202.TP
203.BR MS_UNBINDABLE
204This is like a private mount,
205and in addition this mount can't be bind mounted.
206Attempts to bind mount this mount
207.RB ( mount (2)
208with the
209.BR MS_BIND
210flag) will fail.
211.IP
212When a recursive bind mount
213.RB ( mount (2)
214with the
215.BR MS_BIND
216and
217.BR MS_REC
218flags) is performed on a directory subtree,
219any bind mounts within the subtree are automatically pruned
220(i.e., not replicated)
221when replicating that subtree to produce the target subtree.
222.PP
223For a discussion of the propagation type assigned to a new mount,
224see NOTES.
225.PP
226The propagation type is a per-mount-point setting;
227some mount points may be marked as shared
228(with each shared mount point being a member of a distinct peer group),
229while others are private
230(or slaved or unbindable).
231.PP
232Note that a mount's propagation type determines whether
233mounts and unmounts of mount points
234.I "immediately under"
235the mount point are propagated.
236Thus, the propagation type does not affect propagation of events for
237grandchildren and further removed descendant mount points.
238What happens if the mount point itself is unmounted is determined by
239the propagation type that is in effect for the
240.I parent
241of the mount point.
242.PP
243Members are added to a
244.IR "peer group"
245when a mount point is marked as shared and either:
246.IP * 3
247the mount point is replicated during the creation of a new mount namespace; or
248.IP *
249a new bind mount is created from the mount point.
250.PP
251In both of these cases, the new mount point joins the peer group
252of which the existing mount point is a member.
253.PP
254A new peer group is also created when a child mount point is created under
255an existing mount point that is marked as shared.
256In this case, the new child mount point is also marked as shared and
257the resulting peer group consists of all the mount points
258that are replicated under the peers of parent mount.
259.PP
260A mount ceases to be a member of a peer group when either
261the mount is explicitly unmounted,
262or when the mount is implicitly unmounted because a mount namespace is removed
263(because it has no more member processes).
264.PP
265The propagation type of the mount points in a mount namespace
266can be discovered via the "optional fields" exposed in
267.IR /proc/[pid]/mountinfo .
268(See
269.BR proc (5)
270for details of this file.)
271The following tags can appear in the optional fields
272for a record in that file:
273.TP
274.I shared:X
275This mount point is shared in peer group
276.IR X .
277Each peer group has a unique ID that is automatically
278generated by the kernel,
279and all mount points in the same peer group will show the same ID.
280(These IDs are assigned starting from the value 1,
281and may be recycled when a peer group ceases to have any members.)
282.TP
283.I master:X
284This mount is a slave to shared peer group
285.IR X .
286.TP
287.IR propagate_from:X " (since Linux 2.6.26)"
288.\" commit 97e7e0f71d6d948c25f11f0a33878d9356d9579e
289This mount is a slave and receives propagation from shared peer group
290.IR X .
291This tag will always appear in conjunction with a
292.IR master:X
293tag.
294Here,
295.IR X
296is the closest dominant peer group under the process's root directory.
297If
298.IR X
299is the immediate master of the mount,
300or if there is no dominant peer group under the same root,
301then only the
302.IR master:X
303field is present and not the
304.IR propagate_from:X
305field.
306For further details, see below.
307.TP
308.IR unbindable
309This is an unbindable mount.
310.PP
311If none of the above tags is present, then this is a private mount.
312.SS MS_SHARED and MS_PRIVATE example
313Suppose that on a terminal in the initial mount namespace,
314we mark one mount point as shared and another as private,
315and then view the mounts in
316.IR /proc/self/mountinfo :
317.PP
318.in +4n
319.EX
320sh1# \fBmount \-\-make\-shared /mntS\fP
321sh1# \fBmount \-\-make\-private /mntP\fP
322sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
32377 61 8:17 / /mntS rw,relatime shared:1
32483 61 8:15 / /mntP rw,relatime
325.EE
326.in
327.PP
328From the
329.IR /proc/self/mountinfo
330output, we see that
331.IR /mntS
332is a shared mount in peer group 1, and that
333.IR /mntP
334has no optional tags, indicating that it is a private mount.
335The first two fields in each record in this file are the unique
336ID for this mount, and the mount ID of the parent mount.
337We can further inspect this file to see that the parent mount point of
338.IR /mntS
339and
340.IR /mntP
341is the root directory,
342.IR / ,
343which is mounted as private:
344.PP
345.in +4n
346.EX
347sh1# \fBcat /proc/self/mountinfo | awk \(aq$1 == 61\(aq | sed \(aqs/ \- .*//\(aq\fP
34861 0 8:2 / / rw,relatime
349.EE
350.in
351.PP
352On a second terminal,
353we create a new mount namespace where we run a second shell
354and inspect the mounts:
355.PP
356.in +4n
357.EX
358$ \fBPS1=\(aqsh2# \(aq sudo unshare \-m \-\-propagation unchanged sh\fP
359sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
360222 145 8:17 / /mntS rw,relatime shared:1
361225 145 8:15 / /mntP rw,relatime
362.EE
363.in
364.PP
365The new mount namespace received a copy of the initial mount namespace's
366mount points.
367These new mount points maintain the same propagation types,
368but have unique mount IDs.
369(The
370.IR \-\-propagation\ unchanged
371option prevents
372.BR unshare (1)
373from marking all mounts as private when creating a new mount namespace,
374.\" Since util-linux 2.27
375which it does by default.)
376.PP
377In the second terminal, we then create submounts under each of
378.IR /mntS
379and
380.IR /mntP
381and inspect the set-up:
382.PP
383.in +4n
384.EX
385sh2# \fBmkdir /mntS/a\fP
386sh2# \fBmount /dev/sdb6 /mntS/a\fP
387sh2# \fBmkdir /mntP/b\fP
388sh2# \fBmount /dev/sdb7 /mntP/b\fP
389sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
390222 145 8:17 / /mntS rw,relatime shared:1
391225 145 8:15 / /mntP rw,relatime
392178 222 8:22 / /mntS/a rw,relatime shared:2
393230 225 8:23 / /mntP/b rw,relatime
394.EE
395.in
396.PP
397From the above, it can be seen that
398.IR /mntS/a
399was created as shared (inheriting this setting from its parent mount) and
400.IR /mntP/b
401was created as a private mount.
402.PP
403Returning to the first terminal and inspecting the set-up,
404we see that the new mount created under the shared mount point
405.IR /mntS
406propagated to its peer mount (in the initial mount namespace),
407but the new mount created under the private mount point
408.IR /mntP
409did not propagate:
410.PP
411.in +4n
412.EX
413sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
41477 61 8:17 / /mntS rw,relatime shared:1
41583 61 8:15 / /mntP rw,relatime
416179 77 8:22 / /mntS/a rw,relatime shared:2
417.EE
418.in
419.\"
420.SS MS_SLAVE example
421Making a mount point a slave allows it to receive propagated
422mount and unmount events from a master shared peer group,
423while preventing it from propagating events to that master.
424This is useful if we want to (say) receive a mount event when
425an optical disk is mounted in the master shared peer group
426(in another mount namespace),
427but want to prevent mount and unmount events under the slave mount
428from having side effects in other namespaces.
429.PP
430We can demonstrate the effect of slaving by first marking
431two mount points as shared in the initial mount namespace:
432.PP
433.in +4n
434.EX
435sh1# \fBmount \-\-make\-shared /mntX\fP
436sh1# \fBmount \-\-make\-shared /mntY\fP
437sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
438132 83 8:23 / /mntX rw,relatime shared:1
439133 83 8:22 / /mntY rw,relatime shared:2
440.EE
441.in
442.PP
443On a second terminal,
444we create a new mount namespace and inspect the mount points:
445.PP
446.in +4n
447.EX
448sh2# \fBunshare \-m \-\-propagation unchanged sh\fP
449sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
450168 167 8:23 / /mntX rw,relatime shared:1
451169 167 8:22 / /mntY rw,relatime shared:2
452.EE
453.in
454.PP
455In the new mount namespace, we then mark one of the mount points as a slave:
456.PP
457.in +4n
458.EX
459sh2# \fBmount \-\-make\-slave /mntY\fP
460sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
461168 167 8:23 / /mntX rw,relatime shared:1
462169 167 8:22 / /mntY rw,relatime master:2
463.EE
464.in
465.PP
466From the above output, we see that
467.IR /mntY
468is now a slave mount that is receiving propagation events from
469the shared peer group with the ID 2.
470.PP
471Continuing in the new namespace, we create submounts under each of
472.IR /mntX
473and
474.IR /mntY :
475.PP
476.in +4n
477.EX
478sh2# \fBmkdir /mntX/a\fP
479sh2# \fBmount /dev/sda3 /mntX/a\fP
480sh2# \fBmkdir /mntY/b\fP
481sh2# \fBmount /dev/sda5 /mntY/b\fP
482.EE
483.in
484.PP
485When we inspect the state of the mount points in the new mount namespace,
486we see that
487.IR /mntX/a
488was created as a new shared mount
489(inheriting the "shared" setting from its parent mount) and
490.IR /mntY/b
491was created as a private mount:
492.PP
493.in +4n
494.EX
495sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
496168 167 8:23 / /mntX rw,relatime shared:1
497169 167 8:22 / /mntY rw,relatime master:2
498173 168 8:3 / /mntX/a rw,relatime shared:3
499175 169 8:5 / /mntY/b rw,relatime
500.EE
501.in
502.PP
503Returning to the first terminal (in the initial mount namespace),
504we see that the mount
505.IR /mntX/a
506propagated to the peer (the shared
507.IR /mntX ),
508but the mount
509.IR /mntY/b
510was not propagated:
511.PP
512.in +4n
513.EX
514sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
515132 83 8:23 / /mntX rw,relatime shared:1
516133 83 8:22 / /mntY rw,relatime shared:2
517174 132 8:3 / /mntX/a rw,relatime shared:3
518.EE
519.in
520.PP
521Now we create a new mount point under
522.IR /mntY
523in the first shell:
524.PP
525.in +4n
526.EX
527sh1# \fBmkdir /mntY/c\fP
528sh1# \fBmount /dev/sda1 /mntY/c\fP
529sh1# \fBcat /proc/self/mountinfo | grep '/mnt' | sed 's/ \- .*//'\fP
530132 83 8:23 / /mntX rw,relatime shared:1
531133 83 8:22 / /mntY rw,relatime shared:2
532174 132 8:3 / /mntX/a rw,relatime shared:3
533178 133 8:1 / /mntY/c rw,relatime shared:4
534.EE
535.in
536.PP
537When we examine the mount points in the second mount namespace,
538we see that in this case the new mount has been propagated
539to the slave mount point,
540and that the new mount is itself a slave mount (to peer group 4):
541.PP
542.in +4n
543.EX
544sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
545168 167 8:23 / /mntX rw,relatime shared:1
546169 167 8:22 / /mntY rw,relatime master:2
547173 168 8:3 / /mntX/a rw,relatime shared:3
548175 169 8:5 / /mntY/b rw,relatime
549179 169 8:1 / /mntY/c rw,relatime master:4
550.EE
551.in
552.\"
553.SS MS_UNBINDABLE example
554One of the primary purposes of unbindable mounts is to avoid
555the "mount point explosion" problem when repeatedly performing bind mounts
556of a higher-level subtree at a lower-level mount point.
557The problem is illustrated by the following shell session.
558.PP
559Suppose we have a system with the following mount points:
560.PP
561.in +4n
562.EX
563# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
564/dev/sda1 on /
565/dev/sdb6 on /mntX
566/dev/sdb7 on /mntY
567.EE
568.in
569.PP
570Suppose furthermore that we wish to recursively bind mount
571the root directory under several users' home directories.
572We do this for the first user, and inspect the mount points:
573.PP
574.in +4n
575.EX
576# \fBmount \-\-rbind / /home/cecilia/\fP
577# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
578/dev/sda1 on /
579/dev/sdb6 on /mntX
580/dev/sdb7 on /mntY
581/dev/sda1 on /home/cecilia
582/dev/sdb6 on /home/cecilia/mntX
583/dev/sdb7 on /home/cecilia/mntY
584.EE
585.in
586.PP
587When we repeat this operation for the second user,
588we start to see the explosion problem:
589.PP
590.in +4n
591.EX
592# \fBmount \-\-rbind / /home/henry\fP
593# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
594/dev/sda1 on /
595/dev/sdb6 on /mntX
596/dev/sdb7 on /mntY
597/dev/sda1 on /home/cecilia
598/dev/sdb6 on /home/cecilia/mntX
599/dev/sdb7 on /home/cecilia/mntY
600/dev/sda1 on /home/henry
601/dev/sdb6 on /home/henry/mntX
602/dev/sdb7 on /home/henry/mntY
603/dev/sda1 on /home/henry/home/cecilia
604/dev/sdb6 on /home/henry/home/cecilia/mntX
605/dev/sdb7 on /home/henry/home/cecilia/mntY
606.EE
607.in
608.PP
609Under
610.IR /home/henry ,
611we have not only recursively added the
612.IR /mntX
613and
614.IR /mntY
615mounts, but also the recursive mounts of those directories under
616.IR /home/cecilia
617that were created in the previous step.
618Upon repeating the step for a third user,
619it becomes obvious that the explosion is exponential in nature:
620.PP
621.in +4n
622.EX
623# \fBmount \-\-rbind / /home/otto\fP
624# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
625/dev/sda1 on /
626/dev/sdb6 on /mntX
627/dev/sdb7 on /mntY
628/dev/sda1 on /home/cecilia
629/dev/sdb6 on /home/cecilia/mntX
630/dev/sdb7 on /home/cecilia/mntY
631/dev/sda1 on /home/henry
632/dev/sdb6 on /home/henry/mntX
633/dev/sdb7 on /home/henry/mntY
634/dev/sda1 on /home/henry/home/cecilia
635/dev/sdb6 on /home/henry/home/cecilia/mntX
636/dev/sdb7 on /home/henry/home/cecilia/mntY
637/dev/sda1 on /home/otto
638/dev/sdb6 on /home/otto/mntX
639/dev/sdb7 on /home/otto/mntY
640/dev/sda1 on /home/otto/home/cecilia
641/dev/sdb6 on /home/otto/home/cecilia/mntX
642/dev/sdb7 on /home/otto/home/cecilia/mntY
643/dev/sda1 on /home/otto/home/henry
644/dev/sdb6 on /home/otto/home/henry/mntX
645/dev/sdb7 on /home/otto/home/henry/mntY
646/dev/sda1 on /home/otto/home/henry/home/cecilia
647/dev/sdb6 on /home/otto/home/henry/home/cecilia/mntX
648/dev/sdb7 on /home/otto/home/henry/home/cecilia/mntY
649.EE
650.in
651.PP
652The mount explosion problem in the above scenario can be avoided
653by making each of the new mounts unbindable.
654The effect of doing this is that recursive mounts of the root
655directory will not replicate the unbindable mounts.
656We make such a mount for the first user:
657.PP
658.in +4n
659.EX
660# \fBmount \-\-rbind \-\-make\-unbindable / /home/cecilia\fP
661.EE
662.in
663.PP
664Before going further, we show that unbindable mounts are indeed unbindable:
665.PP
666.in +4n
667.EX
668# \fBmkdir /mntZ\fP
669# \fBmount \-\-bind /home/cecilia /mntZ\fP
670mount: wrong fs type, bad option, bad superblock on /home/cecilia,
671 missing codepage or helper program, or other error
672
673 In some cases useful info is found in syslog \- try
674 dmesg | tail or so.
675.EE
676.in
677.PP
678Now we create unbindable recursive bind mounts for the other two users:
679.PP
680.in +4n
681.EX
682# \fBmount \-\-rbind \-\-make\-unbindable / /home/henry\fP
683# \fBmount \-\-rbind \-\-make\-unbindable / /home/otto\fP
684.EE
685.in
686.PP
687Upon examining the list of mount points,
688we see there has been no explosion of mount points,
689because the unbindable mounts were not replicated
690under each user's directory:
691.PP
692.in +4n
693.EX
694# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
695/dev/sda1 on /
696/dev/sdb6 on /mntX
697/dev/sdb7 on /mntY
698/dev/sda1 on /home/cecilia
699/dev/sdb6 on /home/cecilia/mntX
700/dev/sdb7 on /home/cecilia/mntY
701/dev/sda1 on /home/henry
702/dev/sdb6 on /home/henry/mntX
703/dev/sdb7 on /home/henry/mntY
704/dev/sda1 on /home/otto
705/dev/sdb6 on /home/otto/mntX
706/dev/sdb7 on /home/otto/mntY
707.EE
708.in
709.\"
710.SS Propagation type transitions
711The following table shows the effect that applying a new propagation type
712(i.e.,
713.IR "mount \-\-make\-xxxx")
714has on the existing propagation type of a mount point.
715The rows correspond to existing propagation types,
716and the columns are the new propagation settings.
717For reasons of space, "private" is abbreviated as "priv" and
718"unbindable" as "unbind".
719.TS
720lb2 lb2 lb2 lb2 lb1
721lb l l l l l.
722 make-shared make-slave make-priv make-unbind
723shared shared slave/priv [1] priv unbind
724slave slave+shared slave [2] priv unbind
725slave+shared slave+shared slave priv unbind
726private shared priv [2] priv unbind
727unbindable shared unbind [2] priv unbind
728.TE
729.sp 1
730Note the following details to the table:
731.IP [1] 4
732If a shared mount is the only mount in its peer group,
733making it a slave automatically makes it private.
734.IP [2]
735Slaving a nonshared mount has no effect on the mount.
736.\"
737.SS Bind (MS_BIND) semantics
738Suppose that the following command is performed:
739.PP
740.in +4n
741.EX
742mount \-\-bind A/a B/b
743.EE
744.in
745.PP
746Here,
747.I A
748is the source mount point,
749.I B
750is the destination mount point,
751.I a
752is a subdirectory path under the mount point
753.IR A ,
754and
755.I b
756is a subdirectory path under the mount point
757.IR B .
758The propagation type of the resulting mount,
759.IR B/b ,
760depends on the propagation types of the mount points
761.IR A
762and
763.IR B ,
764and is summarized in the following table.
765.PP
766.TS
767lb2 lb1 lb2 lb2 lb2 lb0
768lb2 lb1 lb2 lb2 lb2 lb0
769lb lb l l l l l.
770 source(A)
771 shared private slave unbind
772_
773dest(B) shared | shared shared slave+shared invalid
774 nonshared | shared private slave invalid
775.TE
776.sp 1
777Note that a recursive bind of a subtree follows the same semantics
778as for a bind operation on each mount in the subtree.
779(Unbindable mounts are automatically pruned at the target mount point.)
780.PP
781For further details, see
782.I Documentation/filesystems/sharedsubtree.txt
783in the kernel source tree.
784.\"
785.SS Move (MS_MOVE) semantics
786Suppose that the following command is performed:
787.PP
788.in +4n
789.EX
790mount \-\-move A B/b
791.EE
792.in
793.PP
794Here,
795.I A
796is the source mount point,
797.I B
798is the destination mount point, and
799.I b
800is a subdirectory path under the mount point
801.IR B .
802The propagation type of the resulting mount,
803.IR B/b ,
804depends on the propagation types of the mount points
805.IR A
806and
807.IR B ,
808and is summarized in the following table.
809.PP
810.TS
811lb2 lb1 lb2 lb2 lb2 lb0
812lb2 lb1 lb2 lb2 lb2 lb0
813lb lb l l l l l.
814 source(A)
815 shared private slave unbind
816_
817dest(B) shared | shared shared slave+shared invalid
818 nonshared | shared private slave unbindable
819.TE
820.sp 1
821Note: moving a mount that resides under a shared mount is invalid.
822.PP
823For further details, see
824.I Documentation/filesystems/sharedsubtree.txt
825in the kernel source tree.
826.\"
827.SS Mount semantics
828Suppose that we use the following command to create a mount point:
829.PP
830.in +4n
831.EX
832mount device B/b
833.EE
834.in
835.PP
836Here,
837.I B
838is the destination mount point, and
839.I b
840is a subdirectory path under the mount point
841.IR B .
842The propagation type of the resulting mount,
843.IR B/b ,
844follows the same rules as for a bind mount,
845where the propagation type of the source mount
846is considered always to be private.
847.\"
848.SS Unmount semantics
849Suppose that we use the following command to tear down a mount point:
850.PP
851.in +4n
852.EX
853unmount A
854.EE
855.in
856.PP
857Here,
858.I A
859is a mount point on
860.IR B/b ,
861where
862.I B
863is the parent mount and
864.I b
865is a subdirectory path under the mount point
866.IR B .
867If
868.B B
869is shared, then all most-recently-mounted mounts at
870.I b
871on mounts that receive propagation from mount
872.I B
873and do not have submounts under them are unmounted.
874.\"
875.SS The /proc/[pid]/mountinfo "propagate_from" tag
876The
877.I propagate_from:X
878tag is shown in the optional fields of a
879.IR /proc/[pid]/mountinfo
880record in cases where a process can't see a slave's immediate master
881(i.e., the pathname of the master is not reachable from
882the filesystem root directory)
883and so cannot determine the
884chain of propagation between the mounts it can see.
885.PP
886In the following example, we first create a two-link master-slave chain
887between the mounts
888.IR /mnt ,
889.IR /tmp/etc ,
890and
891.IR /mnt/tmp/etc .
892Then the
893.BR chroot (1)
894command is used to make the
895.IR /tmp/etc
896mount point unreachable from the root directory,
897creating a situation where the master of
898.IR /mnt/tmp/etc
899is not reachable from the (new) root directory of the process.
900.PP
901First, we bind mount the root directory onto
902.IR /mnt
903and then bind mount
904.IR /proc
905at
906.IR /mnt/proc
907so that after the later
908.BR chroot (1)
909the
910.BR proc (5)
911filesystem remains visible at the correct location
912in the chroot-ed environment.
913.PP
914.in +4n
915.EX
916# \fBmkdir \-p /mnt/proc\fP
917# \fBmount \-\-bind / /mnt\fP
918# \fBmount \-\-bind /proc /mnt/proc\fP
919.EE
920.in
921.PP
922Next, we ensure that the
923.IR /mnt
924mount is a shared mount in a new peer group (with no peers):
925.PP
926.in +4n
927.EX
928# \fBmount \-\-make\-private /mnt\fP # Isolate from any previous peer group
929# \fBmount \-\-make\-shared /mnt\fP
930# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
931239 61 8:2 / /mnt ... shared:102
932248 239 0:4 / /mnt/proc ... shared:5
933.EE
934.in
935.PP
936Next, we bind mount
937.IR /mnt/etc
938onto
939.IR /tmp/etc :
940.PP
941.in +4n
942.EX
943# \fBmkdir \-p /tmp/etc\fP
944# \fBmount \-\-bind /mnt/etc /tmp/etc\fP
945# \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
946239 61 8:2 / /mnt ... shared:102
947248 239 0:4 / /mnt/proc ... shared:5
948267 40 8:2 /etc /tmp/etc ... shared:102
949.EE
950.in
951.PP
952Initially, these two mount points are in the same peer group,
953but we then make the
954.IR /tmp/etc
955a slave of
956.IR /mnt/etc ,
957and then make
958.IR /tmp/etc
959shared as well,
960so that it can propagate events to the next slave in the chain:
961.PP
962.in +4n
963.EX
964# \fBmount \-\-make\-slave /tmp/etc\fP
965# \fBmount \-\-make\-shared /tmp/etc\fP
966# \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
967239 61 8:2 / /mnt ... shared:102
968248 239 0:4 / /mnt/proc ... shared:5
969267 40 8:2 /etc /tmp/etc ... shared:105 master:102
970.EE
971.in
972.PP
973Then we bind mount
974.IR /tmp/etc
975onto
976.IR /mnt/tmp/etc .
977Again, the two mount points are initially in the same peer group,
978but we then make
979.IR /mnt/tmp/etc
980a slave of
981.IR /tmp/etc :
982.PP
983.in +4n
984.EX
985# \fBmkdir \-p /mnt/tmp/etc\fP
986# \fBmount \-\-bind /tmp/etc /mnt/tmp/etc\fP
987# \fBmount \-\-make\-slave /mnt/tmp/etc\fP
988# \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
989239 61 8:2 / /mnt ... shared:102
990248 239 0:4 / /mnt/proc ... shared:5
991267 40 8:2 /etc /tmp/etc ... shared:105 master:102
992273 239 8:2 /etc /mnt/tmp/etc ... master:105
993.EE
994.in
995.PP
996From the above, we see that
997.IR /mnt
998is the master of the slave
999.IR /tmp/etc ,
1000which in turn is the master of the slave
1001.IR /mnt/tmp/etc .
1002.PP
1003We then
1004.BR chroot (1)
1005to the
1006.IR /mnt
1007directory, which renders the mount with ID 267 unreachable
1008from the (new) root directory:
1009.PP
1010.in +4n
1011.EX
1012# \fBchroot /mnt\fP
1013.EE
1014.in
1015.PP
1016When we examine the state of the mounts inside the chroot-ed environment,
1017we see the following:
1018.PP
1019.in +4n
1020.EX
1021# \fBcat /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
1022239 61 8:2 / / ... shared:102
1023248 239 0:4 / /proc ... shared:5
1024273 239 8:2 /etc /tmp/etc ... master:105 propagate_from:102
1025.EE
1026.in
1027.PP
1028Above, we see that the mount with ID 273
1029is a slave whose master is the peer group 105.
1030The mount point for that master is unreachable, and so a
1031.IR propagate_from
1032tag is displayed, indicating that the closest dominant peer group
1033(i.e., the nearest reachable mount in the slave chain)
1034is the peer group with the ID 102 (corresponding to the
1035.IR /mnt
1036mount point before the
1037.BR chroot (1)
1038was performed.
1039.\"
1040.SH VERSIONS
1041Mount namespaces first appeared in Linux 2.4.19.
1042.SH CONFORMING TO
1043Namespaces are a Linux-specific feature.
1044.\"
1045.SH NOTES
1046The propagation type assigned to a new mount point depends
1047on the propagation type of the parent mount.
1048If the mount point has a parent (i.e., it is a non-root mount
1049point) and the propagation type of the parent is
1050.BR MS_SHARED ,
1051then the propagation type of the new mount is also
1052.BR MS_SHARED .
1053Otherwise, the propagation type of the new mount is
1054.BR MS_PRIVATE .
1055.PP
1056Notwithstanding the fact that the default propagation type
1057for new mount points is in many cases
1058.BR MS_PRIVATE ,
1059.BR MS_SHARED
1060is typically more useful.
1061For this reason,
1062.BR systemd (1)
1063automatically remounts all mount points as
1064.BR MS_SHARED
1065on system startup.
1066Thus, on most modern systems, the default propagation type is in practice
1067.BR MS_SHARED .
1068.PP
1069Since, when one uses
1070.BR unshare (1)
1071to create a mount namespace,
1072the goal is commonly to provide full isolation of the mount points
1073in the new namespace,
1074.BR unshare (1)
1075(since
1076.IR util-linux
1077version 2.27) in turn reverses the step performed by
1078.BR systemd (1),
1079by making all mount points private in the new namespace.
1080That is,
1081.BR unshare (1)
1082performs the equivalent of the following in the new mount namespace:
1083.PP
1084.in +4n
1085.EX
1086mount \-\-make\-rprivate /
1087.EE
1088.in
1089.PP
1090To prevent this, one can use the
1091.IR "\-\-propagation\ unchanged"
1092option to
1093.BR unshare (1).
1094.PP
1095An application that creates a new mount namespace directly using
1096.BR clone (2)
1097or
1098.BR unshare (2)
1099may desire to prevent propagation of mount events to other mount namespaces
1100(as is done by
1101.BR unshare (1)).
1102This can be done by changing the propagation type of
1103mount points in the new namespace to either
1104.BR MS_SLAVE
1105or
1106.BR MS_PRIVATE .
1107using a call such as the following:
1108.IP
1109.in +4n
1110.EX
1111mount(NULL, "/", MS_SLAVE | MS_REC, NULL);
1112.EE
1113.in
1114.PP
1115For a discussion of propagation types when moving mounts
1116.RB ( MS_MOVE )
1117and creating bind mounts
1118.RB ( MS_BIND ),
1119see
1120.IR Documentation/filesystems/sharedsubtree.txt .
1121.SH EXAMPLE
1122See
1123.BR pivot_root (2).
1124.SH SEE ALSO
1125.BR unshare (1),
1126.BR clone (2),
1127.BR mount (2),
1128.BR pivot_root (2),
1129.BR setns (2),
1130.BR umount (2),
1131.BR unshare (2),
1132.BR proc (5),
1133.BR namespaces (7),
1134.BR user_namespaces (7),
1135.BR findmnt (8),
1136.BR pivot_root (8)
1137.PP
1138.IR Documentation/filesystems/sharedsubtree.txt
1139in the kernel source tree.