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