]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/mount_namespaces.7
epoll.7: wfix
[thirdparty/man-pages.git] / man7 / mount_namespaces.7
CommitLineData
dc95a3a3 1.\" Copyright (c) 2016, 2019 by Michael Kerrisk <mtk.manpages@gmail.com>
98c28960
MK
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.\"
63121bd4 26.TH MOUNT_NAMESPACES 7 2019-08-02 "Linux" "Linux Programmer's Manual"
98c28960
MK
27.SH NAME
28mount_namespaces \- overview of Linux mount namespaces
29.SH DESCRIPTION
30For an overview of namespaces, see
31.BR namespaces (7).
a721e8b2 32.PP
98c28960
MK
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.
a721e8b2 37.PP
98c28960
MK
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.)
a721e8b2 50.PP
534755ee 51A new mount namespace is created using either
98c28960
MK
52.BR clone (2)
53or
54.BR unshare (2)
55with the
56.BR CLONE_NEWNS
534755ee
MK
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
98c28960
MK
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.\"
4bfc2026
MK
79.\" ============================================================
80.\"
81.SS Restrictions on mount namespaces
82Note the following points with respect to mount namespaces:
83.IP * 3
a0c97331 84Each mount namespace has an owner user namespace.
19416046
MK
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.
4d75df37 88If the new namespace and the namespace from which the mount point list
19416046 89was copied are owned by different user namespaces,
a0c97331
MK
90then the new mount namespace is considered
91.IR "less privileged" .
4bfc2026
MK
92.IP *
93When creating a less privileged mount namespace,
94shared mounts are reduced to slave mounts.
4f74d996 95(Shared and slave mounts are discussed below.)
4bfc2026
MK
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).
dc95a3a3
MK
140Consequently, the mount point is removed in the mount namespace
141where it was a mount point.
4bfc2026 142.IP
dc95a3a3
MK
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
4bfc2026
MK
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.\"
98c28960
MK
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).
a721e8b2 173.PP
98c28960
MK
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
d9cdf357 182to the other mount points that are members of the peer group.
98c28960
MK
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
d9cdf357 187peer mount points will propagate to this mount point.
98c28960
MK
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.
98c28960
MK
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.
a721e8b2 197.IP
98c28960
MK
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.
a721e8b2 211.IP
98c28960
MK
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
3dcc463a
MK
223For a discussion of the propagation type assigned to a new mount,
224see NOTES.
a721e8b2 225.PP
98c28960
MK
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).
a721e8b2 231.PP
98c28960
MK
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.
a721e8b2 242.PP
98c28960
MK
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.
46af7198 253.PP
6b49df22
MK
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
98c28960
MK
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).
a721e8b2 264.PP
98c28960
MK
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 .
d9cdf357 277Each peer group has a unique ID that is automatically
98c28960
MK
278generated by the kernel,
279and all mount points in the same peer group will show the same ID.
d9cdf357
MK
280(These IDs are assigned starting from the value 1,
281and may be recycled when a peer group ceases to have any members.)
98c28960
MK
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.
e2109196 306For further details, see below.
98c28960
MK
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 :
a721e8b2 317.PP
98c28960 318.in +4n
b8302363 319.EX
d9cdf357
MK
320sh1# \fBmount \-\-make\-shared /mntS\fP
321sh1# \fBmount \-\-make\-private /mntP\fP
f481726d 322sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
32377 61 8:17 / /mntS rw,relatime shared:1
32483 61 8:15 / /mntP rw,relatime
b8302363 325.EE
e646a1ba 326.in
a721e8b2 327.PP
98c28960
MK
328From the
329.IR /proc/self/mountinfo
330output, we see that
d9cdf357 331.IR /mntS
98c28960 332is a shared mount in peer group 1, and that
d9cdf357 333.IR /mntP
98c28960
MK
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
d9cdf357 338.IR /mntS
98c28960 339and
d9cdf357 340.IR /mntP
98c28960
MK
341is the root directory,
342.IR / ,
343which is mounted as private:
a721e8b2 344.PP
98c28960 345.in +4n
b8302363 346.EX
98c28960
MK
347sh1# \fBcat /proc/self/mountinfo | awk \(aq$1 == 61\(aq | sed \(aqs/ \- .*//\(aq\fP
34861 0 8:2 / / rw,relatime
b8302363 349.EE
e646a1ba 350.in
a721e8b2 351.PP
98c28960
MK
352On a second terminal,
353we create a new mount namespace where we run a second shell
354and inspect the mounts:
a721e8b2 355.PP
98c28960 356.in +4n
b8302363 357.EX
98c28960 358$ \fBPS1=\(aqsh2# \(aq sudo unshare \-m \-\-propagation unchanged sh\fP
f481726d 359sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
360222 145 8:17 / /mntS rw,relatime shared:1
361225 145 8:15 / /mntP rw,relatime
b8302363 362.EE
e646a1ba 363.in
a721e8b2 364.PP
98c28960
MK
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.)
a721e8b2 376.PP
98c28960 377In the second terminal, we then create submounts under each of
d9cdf357 378.IR /mntS
98c28960 379and
d9cdf357 380.IR /mntP
98c28960 381and inspect the set-up:
a721e8b2 382.PP
98c28960 383.in +4n
b8302363 384.EX
d9cdf357
MK
385sh2# \fBmkdir /mntS/a\fP
386sh2# \fBmount /dev/sdb6 /mntS/a\fP
387sh2# \fBmkdir /mntP/b\fP
388sh2# \fBmount /dev/sdb7 /mntP/b\fP
f481726d 389sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
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
b8302363 394.EE
e646a1ba 395.in
a721e8b2 396.PP
98c28960 397From the above, it can be seen that
d9cdf357 398.IR /mntS/a
98c28960 399was created as shared (inheriting this setting from its parent mount) and
d9cdf357 400.IR /mntP/b
98c28960 401was created as a private mount.
a721e8b2 402.PP
98c28960
MK
403Returning to the first terminal and inspecting the set-up,
404we see that the new mount created under the shared mount point
d9cdf357 405.IR /mntS
98c28960
MK
406propagated to its peer mount (in the initial mount namespace),
407but the new mount created under the private mount point
d9cdf357 408.IR /mntP
98c28960 409did not propagate:
a721e8b2 410.PP
98c28960 411.in +4n
b8302363 412.EX
f481726d 413sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
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
b8302363 417.EE
e646a1ba 418.in
98c28960
MK
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,
d9cdf357 423while preventing it from propagating events to that master.
98c28960
MK
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.
a721e8b2 429.PP
98c28960
MK
430We can demonstrate the effect of slaving by first marking
431two mount points as shared in the initial mount namespace:
a721e8b2 432.PP
98c28960 433.in +4n
b8302363 434.EX
98c28960
MK
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
b8302363 440.EE
e646a1ba 441.in
a721e8b2 442.PP
98c28960
MK
443On a second terminal,
444we create a new mount namespace and inspect the mount points:
a721e8b2 445.PP
98c28960 446.in +4n
b8302363 447.EX
98c28960
MK
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
b8302363 452.EE
e646a1ba 453.in
a721e8b2 454.PP
98c28960 455In the new mount namespace, we then mark one of the mount points as a slave:
a721e8b2 456.PP
98c28960 457.in +4n
b8302363 458.EX
98c28960
MK
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
b8302363 463.EE
e646a1ba 464.in
a721e8b2 465.PP
98c28960
MK
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.
a721e8b2 470.PP
98c28960
MK
471Continuing in the new namespace, we create submounts under each of
472.IR /mntX
473and
474.IR /mntY :
a721e8b2 475.PP
98c28960 476.in +4n
b8302363 477.EX
d9cdf357
MK
478sh2# \fBmkdir /mntX/a\fP
479sh2# \fBmount /dev/sda3 /mntX/a\fP
480sh2# \fBmkdir /mntY/b\fP
481sh2# \fBmount /dev/sda5 /mntY/b\fP
b8302363 482.EE
e646a1ba 483.in
a721e8b2 484.PP
98c28960
MK
485When we inspect the state of the mount points in the new mount namespace,
486we see that
d9cdf357 487.IR /mntX/a
98c28960
MK
488was created as a new shared mount
489(inheriting the "shared" setting from its parent mount) and
d9cdf357 490.IR /mntY/b
98c28960 491was created as a private mount:
a721e8b2 492.PP
98c28960 493.in +4n
b8302363 494.EX
98c28960
MK
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
d9cdf357
MK
498173 168 8:3 / /mntX/a rw,relatime shared:3
499175 169 8:5 / /mntY/b rw,relatime
b8302363 500.EE
e646a1ba 501.in
a721e8b2 502.PP
98c28960
MK
503Returning to the first terminal (in the initial mount namespace),
504we see that the mount
d9cdf357 505.IR /mntX/a
98c28960
MK
506propagated to the peer (the shared
507.IR /mntX ),
508but the mount
d9cdf357 509.IR /mntY/b
98c28960 510was not propagated:
a721e8b2 511.PP
98c28960 512.in +4n
b8302363 513.EX
98c28960
MK
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
d9cdf357 517174 132 8:3 / /mntX/a rw,relatime shared:3
b8302363 518.EE
e646a1ba 519.in
a721e8b2 520.PP
98c28960
MK
521Now we create a new mount point under
522.IR /mntY
523in the first shell:
a721e8b2 524.PP
98c28960 525.in +4n
b8302363 526.EX
d9cdf357
MK
527sh1# \fBmkdir /mntY/c\fP
528sh1# \fBmount /dev/sda1 /mntY/c\fP
98c28960
MK
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
d9cdf357
MK
532174 132 8:3 / /mntX/a rw,relatime shared:3
533178 133 8:1 / /mntY/c rw,relatime shared:4
b8302363 534.EE
e646a1ba 535.in
a721e8b2 536.PP
98c28960
MK
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):
a721e8b2 541.PP
98c28960 542.in +4n
b8302363 543.EX
98c28960
MK
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
d9cdf357
MK
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
b8302363 550.EE
e646a1ba 551.in
98c28960
MK
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.
a721e8b2 558.PP
98c28960 559Suppose we have a system with the following mount points:
a721e8b2 560.PP
98c28960 561.in +4n
b8302363 562.EX
98c28960
MK
563# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
564/dev/sda1 on /
565/dev/sdb6 on /mntX
566/dev/sdb7 on /mntY
b8302363 567.EE
e646a1ba 568.in
a721e8b2 569.PP
98c28960
MK
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:
a721e8b2 573.PP
98c28960 574.in +4n
b8302363 575.EX
98c28960
MK
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
b8302363 584.EE
e646a1ba 585.in
a721e8b2 586.PP
98c28960
MK
587When we repeat this operation for the second user,
588we start to see the explosion problem:
a721e8b2 589.PP
98c28960 590.in +4n
b8302363 591.EX
98c28960
MK
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
b8302363 606.EE
e646a1ba 607.in
a721e8b2 608.PP
98c28960
MK
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:
a721e8b2 620.PP
98c28960 621.in +4n
b8302363 622.EX
98c28960
MK
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
b8302363 649.EE
e646a1ba 650.in
a721e8b2 651.PP
98c28960
MK
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:
a721e8b2 657.PP
98c28960 658.in +4n
b8302363 659.EX
98c28960 660# \fBmount \-\-rbind \-\-make\-unbindable / /home/cecilia\fP
b8302363 661.EE
e646a1ba 662.in
a721e8b2 663.PP
98c28960 664Before going further, we show that unbindable mounts are indeed unbindable:
a721e8b2 665.PP
98c28960 666.in +4n
b8302363 667.EX
98c28960
MK
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.
b8302363 675.EE
e646a1ba 676.in
a721e8b2 677.PP
98c28960 678Now we create unbindable recursive bind mounts for the other two users:
a721e8b2 679.PP
98c28960 680.in +4n
b8302363 681.EX
98c28960
MK
682# \fBmount \-\-rbind \-\-make\-unbindable / /home/henry\fP
683# \fBmount \-\-rbind \-\-make\-unbindable / /home/otto\fP
b8302363 684.EE
e646a1ba 685.in
a721e8b2 686.PP
98c28960
MK
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:
a721e8b2 691.PP
98c28960 692.in +4n
b8302363 693.EX
98c28960
MK
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
b8302363 707.EE
e646a1ba 708.in
98c28960
MK
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
a721e8b2 729.sp 1
98c28960
MK
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:
a721e8b2 739.PP
fd6307c4
MK
740.in +4n
741.EX
742mount \-\-bind A/a B/b
743.EE
744.in
a721e8b2 745.PP
98c28960
MK
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.
a721e8b2 765.PP
98c28960
MK
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
a721e8b2 776.sp 1
98c28960
MK
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.)
a721e8b2 780.PP
98c28960 781For further details, see
1481407a 782.I Documentation/filesystems/sharedsubtree.txt
98c28960
MK
783in the kernel source tree.
784.\"
785.SS Move (MS_MOVE) semantics
786Suppose that the following command is performed:
a721e8b2 787.PP
fd6307c4
MK
788.in +4n
789.EX
790mount \-\-move A B/b
791.EE
792.in
a721e8b2 793.PP
98c28960
MK
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.
a721e8b2 809.PP
98c28960
MK
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
a721e8b2 820.sp 1
98c28960 821Note: moving a mount that resides under a shared mount is invalid.
a721e8b2 822.PP
98c28960 823For further details, see
1481407a 824.I Documentation/filesystems/sharedsubtree.txt
98c28960
MK
825in the kernel source tree.
826.\"
827.SS Mount semantics
828Suppose that we use the following command to create a mount point:
a721e8b2 829.PP
fd6307c4
MK
830.in +4n
831.EX
832mount device B/b
833.EE
834.in
a721e8b2 835.PP
98c28960
MK
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:
a721e8b2 850.PP
fd6307c4
MK
851.in +4n
852.EX
853unmount A
854.EE
855.in
a721e8b2 856.PP
98c28960
MK
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.\"
e2109196
MK
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.
a721e8b2 885.PP
e2109196
MK
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.
a721e8b2 900.PP
e2109196
MK
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.
a721e8b2 913.PP
e2109196 914.in +4n
b8302363 915.EX
e2109196
MK
916# \fBmkdir \-p /mnt/proc\fP
917# \fBmount \-\-bind / /mnt\fP
918# \fBmount \-\-bind /proc /mnt/proc\fP
b8302363 919.EE
e646a1ba 920.in
a721e8b2 921.PP
e2109196
MK
922Next, we ensure that the
923.IR /mnt
924mount is a shared mount in a new peer group (with no peers):
a721e8b2 925.PP
e2109196 926.in +4n
b8302363 927.EX
e2109196
MK
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
b8302363 933.EE
e646a1ba 934.in
a721e8b2 935.PP
e2109196
MK
936Next, we bind mount
937.IR /mnt/etc
938onto
939.IR /tmp/etc :
a721e8b2 940.PP
e2109196 941.in +4n
b8302363 942.EX
e2109196
MK
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
b8302363 949.EE
e646a1ba 950.in
a721e8b2 951.PP
e2109196
MK
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:
a721e8b2 961.PP
e2109196 962.in +4n
b8302363 963.EX
e2109196
MK
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
b8302363 970.EE
e646a1ba 971.in
a721e8b2 972.PP
e2109196
MK
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 :
a721e8b2 982.PP
e2109196 983.in +4n
b8302363 984.EX
e2109196
MK
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
e646a1ba 993.EE
e2109196 994.in
e646a1ba 995.PP
e2109196
MK
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 .
a721e8b2 1002.PP
e2109196
MK
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:
a721e8b2 1009.PP
e2109196 1010.in +4n
b8302363 1011.EX
e2109196 1012# \fBchroot /mnt\fP
b8302363 1013.EE
e646a1ba 1014.in
a721e8b2 1015.PP
e2109196
MK
1016When we examine the state of the mounts inside the chroot-ed environment,
1017we see the following:
a721e8b2 1018.PP
e2109196 1019.in +4n
b8302363 1020.EX
e2109196
MK
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
b8302363 1025.EE
e646a1ba 1026.in
a721e8b2 1027.PP
e2109196
MK
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.\"
c307aecd
MK
1040.SH VERSIONS
1041Mount namespaces first appeared in Linux 2.4.19.
1042.SH CONFORMING TO
1043Namespaces are a Linux-specific feature.
1044.\"
98c28960 1045.SH NOTES
3dcc463a 1046The propagation type assigned to a new mount point depends
4954c465 1047on the propagation type of the parent mount.
3dcc463a
MK
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
98c28960 1054.BR MS_PRIVATE .
a721e8b2 1055.PP
3dcc463a
MK
1056Notwithstanding the fact that the default propagation type
1057for new mount points is in many cases
1058.BR MS_PRIVATE ,
98c28960 1059.BR MS_SHARED
3dcc463a
MK
1060is typically more useful.
1061For this reason,
98c28960
MK
1062.BR systemd (1)
1063automatically remounts all mount points as
1064.BR MS_SHARED
1065on system startup.
3dcc463a
MK
1066Thus, on most modern systems, the default propagation type is in practice
1067.BR MS_SHARED .
a721e8b2 1068.PP
98c28960
MK
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:
a721e8b2 1083.PP
fd6307c4
MK
1084.in +4n
1085.EX
1086mount \-\-make\-rprivate /
1087.EE
1088.in
a721e8b2 1089.PP
98c28960
MK
1090To prevent this, one can use the
1091.IR "\-\-propagation\ unchanged"
1092option to
1093.BR unshare (1).
a721e8b2 1094.PP
a2fc45a9
MK
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
bce2cf98 1100(as is done by
a2fc45a9
MK
1101.BR unshare (1)).
1102This can be done by changing the propagation type of
ed425459 1103mount points in the new namespace to either
a2fc45a9
MK
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
3dcc463a
MK
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 .
43d438e2
MK
1121.SH EXAMPLE
1122See
1123.BR pivot_root (2).
98c28960
MK
1124.SH SEE ALSO
1125.BR unshare (1),
1126.BR clone (2),
1127.BR mount (2),
e70abf48 1128.BR pivot_root (2),
98c28960
MK
1129.BR setns (2),
1130.BR umount (2),
1131.BR unshare (2),
1132.BR proc (5),
466247eb 1133.BR namespaces (7),
93f5b0f8 1134.BR user_namespaces (7),
e70abf48
MK
1135.BR findmnt (8),
1136.BR pivot_root (8)
a721e8b2 1137.PP
98c28960
MK
1138.IR Documentation/filesystems/sharedsubtree.txt
1139in the kernel source tree.