]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/mount_namespaces.7
user_namespaces.7: Minor rewordings of recently added text
[thirdparty/man-pages.git] / man7 / mount_namespaces.7
CommitLineData
98c28960
MK
1.\" Copyright (c) 2016 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.\"
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).
125.IP
126Previously, attempting to unlink, rename, or remove a file or directory
127that was a mount point in another mount namespace would result in the error
128.BR EBUSY .
129That behavior had technical problems of enforcement (e.g., for NFS)
130and permitted denial-of-service attacks against more privileged users.
131(i.e., preventing individual files from being updated
132by bind mounting on top of them).
133.\"
98c28960
MK
134.SH SHARED SUBTREES
135After the implementation of mount namespaces was completed,
136experience showed that the isolation that they provided was,
137in some cases, too great.
138For example, in order to make a newly loaded optical disk
139available in all mount namespaces,
140a mount operation was required in each namespace.
141For this use case, and others,
142the shared subtree feature was introduced in Linux 2.6.15.
143This feature allows for automatic, controlled propagation of mount and unmount
144.I events
145between namespaces
146(or, more precisely, between the members of a
147.IR "peer group"
148that are propagating events to one another).
a721e8b2 149.PP
98c28960
MK
150Each mount point is marked (via
151.BR mount (2))
152as having one of the following
153.IR "propagation types" :
154.TP
155.BR MS_SHARED
156This mount point shares events with members of a peer group.
157Mount and unmount events immediately under this mount point will propagate
d9cdf357 158to the other mount points that are members of the peer group.
98c28960
MK
159.I Propagation
160here means that the same mount or unmount will automatically occur
161under all of the other mount points in the peer group.
162Conversely, mount and unmount events that take place under
d9cdf357 163peer mount points will propagate to this mount point.
98c28960
MK
164.TP
165.BR MS_PRIVATE
166This mount point is private; it does not have a peer group.
167Mount and unmount events do not propagate into or out of this mount point.
98c28960
MK
168.TP
169.BR MS_SLAVE
170Mount and unmount events propagate into this mount point from
171a (master) shared peer group.
172Mount and unmount events under this mount point do not propagate to any peer.
a721e8b2 173.IP
98c28960
MK
174Note that a mount point can be the slave of another peer group
175while at the same time sharing mount and unmount events
176with a peer group of which it is a member.
177(More precisely, one peer group can be the slave of another peer group.)
178.TP
179.BR MS_UNBINDABLE
180This is like a private mount,
181and in addition this mount can't be bind mounted.
182Attempts to bind mount this mount
183.RB ( mount (2)
184with the
185.BR MS_BIND
186flag) will fail.
a721e8b2 187.IP
98c28960
MK
188When a recursive bind mount
189.RB ( mount (2)
190with the
191.BR MS_BIND
192and
193.BR MS_REC
194flags) is performed on a directory subtree,
195any bind mounts within the subtree are automatically pruned
196(i.e., not replicated)
197when replicating that subtree to produce the target subtree.
198.PP
3dcc463a
MK
199For a discussion of the propagation type assigned to a new mount,
200see NOTES.
a721e8b2 201.PP
98c28960
MK
202The propagation type is a per-mount-point setting;
203some mount points may be marked as shared
204(with each shared mount point being a member of a distinct peer group),
205while others are private
206(or slaved or unbindable).
a721e8b2 207.PP
98c28960
MK
208Note that a mount's propagation type determines whether
209mounts and unmounts of mount points
210.I "immediately under"
211the mount point are propagated.
212Thus, the propagation type does not affect propagation of events for
213grandchildren and further removed descendant mount points.
214What happens if the mount point itself is unmounted is determined by
215the propagation type that is in effect for the
216.I parent
217of the mount point.
a721e8b2 218.PP
98c28960
MK
219Members are added to a
220.IR "peer group"
221when a mount point is marked as shared and either:
222.IP * 3
223the mount point is replicated during the creation of a new mount namespace; or
224.IP *
225a new bind mount is created from the mount point.
226.PP
227In both of these cases, the new mount point joins the peer group
228of which the existing mount point is a member.
46af7198 229.PP
6b49df22
MK
230A new peer group is also created when a child mount point is created under
231an existing mount point that is marked as shared.
232In this case, the new child mount point is also marked as shared and
233the resulting peer group consists of all the mount points
234that are replicated under the peers of parent mount.
235.PP
98c28960
MK
236A mount ceases to be a member of a peer group when either
237the mount is explicitly unmounted,
238or when the mount is implicitly unmounted because a mount namespace is removed
239(because it has no more member processes).
a721e8b2 240.PP
98c28960
MK
241The propagation type of the mount points in a mount namespace
242can be discovered via the "optional fields" exposed in
243.IR /proc/[pid]/mountinfo .
244(See
245.BR proc (5)
246for details of this file.)
247The following tags can appear in the optional fields
248for a record in that file:
249.TP
250.I shared:X
251This mount point is shared in peer group
252.IR X .
d9cdf357 253Each peer group has a unique ID that is automatically
98c28960
MK
254generated by the kernel,
255and all mount points in the same peer group will show the same ID.
d9cdf357
MK
256(These IDs are assigned starting from the value 1,
257and may be recycled when a peer group ceases to have any members.)
98c28960
MK
258.TP
259.I master:X
260This mount is a slave to shared peer group
261.IR X .
262.TP
263.IR propagate_from:X " (since Linux 2.6.26)"
264.\" commit 97e7e0f71d6d948c25f11f0a33878d9356d9579e
265This mount is a slave and receives propagation from shared peer group
266.IR X .
267This tag will always appear in conjunction with a
268.IR master:X
269tag.
270Here,
271.IR X
272is the closest dominant peer group under the process's root directory.
273If
274.IR X
275is the immediate master of the mount,
276or if there is no dominant peer group under the same root,
277then only the
278.IR master:X
279field is present and not the
280.IR propagate_from:X
281field.
e2109196 282For further details, see below.
98c28960
MK
283.TP
284.IR unbindable
285This is an unbindable mount.
286.PP
287If none of the above tags is present, then this is a private mount.
288.SS MS_SHARED and MS_PRIVATE example
289Suppose that on a terminal in the initial mount namespace,
290we mark one mount point as shared and another as private,
291and then view the mounts in
292.IR /proc/self/mountinfo :
a721e8b2 293.PP
98c28960 294.in +4n
b8302363 295.EX
d9cdf357
MK
296sh1# \fBmount \-\-make\-shared /mntS\fP
297sh1# \fBmount \-\-make\-private /mntP\fP
f481726d 298sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
29977 61 8:17 / /mntS rw,relatime shared:1
30083 61 8:15 / /mntP rw,relatime
b8302363 301.EE
e646a1ba 302.in
a721e8b2 303.PP
98c28960
MK
304From the
305.IR /proc/self/mountinfo
306output, we see that
d9cdf357 307.IR /mntS
98c28960 308is a shared mount in peer group 1, and that
d9cdf357 309.IR /mntP
98c28960
MK
310has no optional tags, indicating that it is a private mount.
311The first two fields in each record in this file are the unique
312ID for this mount, and the mount ID of the parent mount.
313We can further inspect this file to see that the parent mount point of
d9cdf357 314.IR /mntS
98c28960 315and
d9cdf357 316.IR /mntP
98c28960
MK
317is the root directory,
318.IR / ,
319which is mounted as private:
a721e8b2 320.PP
98c28960 321.in +4n
b8302363 322.EX
98c28960
MK
323sh1# \fBcat /proc/self/mountinfo | awk \(aq$1 == 61\(aq | sed \(aqs/ \- .*//\(aq\fP
32461 0 8:2 / / rw,relatime
b8302363 325.EE
e646a1ba 326.in
a721e8b2 327.PP
98c28960
MK
328On a second terminal,
329we create a new mount namespace where we run a second shell
330and inspect the mounts:
a721e8b2 331.PP
98c28960 332.in +4n
b8302363 333.EX
98c28960 334$ \fBPS1=\(aqsh2# \(aq sudo unshare \-m \-\-propagation unchanged sh\fP
f481726d 335sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
336222 145 8:17 / /mntS rw,relatime shared:1
337225 145 8:15 / /mntP rw,relatime
b8302363 338.EE
e646a1ba 339.in
a721e8b2 340.PP
98c28960
MK
341The new mount namespace received a copy of the initial mount namespace's
342mount points.
343These new mount points maintain the same propagation types,
344but have unique mount IDs.
345(The
346.IR \-\-propagation\ unchanged
347option prevents
348.BR unshare (1)
349from marking all mounts as private when creating a new mount namespace,
350.\" Since util-linux 2.27
351which it does by default.)
a721e8b2 352.PP
98c28960 353In the second terminal, we then create submounts under each of
d9cdf357 354.IR /mntS
98c28960 355and
d9cdf357 356.IR /mntP
98c28960 357and inspect the set-up:
a721e8b2 358.PP
98c28960 359.in +4n
b8302363 360.EX
d9cdf357
MK
361sh2# \fBmkdir /mntS/a\fP
362sh2# \fBmount /dev/sdb6 /mntS/a\fP
363sh2# \fBmkdir /mntP/b\fP
364sh2# \fBmount /dev/sdb7 /mntP/b\fP
f481726d 365sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
366222 145 8:17 / /mntS rw,relatime shared:1
367225 145 8:15 / /mntP rw,relatime
368178 222 8:22 / /mntS/a rw,relatime shared:2
369230 225 8:23 / /mntP/b rw,relatime
b8302363 370.EE
e646a1ba 371.in
a721e8b2 372.PP
98c28960 373From the above, it can be seen that
d9cdf357 374.IR /mntS/a
98c28960 375was created as shared (inheriting this setting from its parent mount) and
d9cdf357 376.IR /mntP/b
98c28960 377was created as a private mount.
a721e8b2 378.PP
98c28960
MK
379Returning to the first terminal and inspecting the set-up,
380we see that the new mount created under the shared mount point
d9cdf357 381.IR /mntS
98c28960
MK
382propagated to its peer mount (in the initial mount namespace),
383but the new mount created under the private mount point
d9cdf357 384.IR /mntP
98c28960 385did not propagate:
a721e8b2 386.PP
98c28960 387.in +4n
b8302363 388.EX
f481726d 389sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
39077 61 8:17 / /mntS rw,relatime shared:1
39183 61 8:15 / /mntP rw,relatime
392179 77 8:22 / /mntS/a rw,relatime shared:2
b8302363 393.EE
e646a1ba 394.in
98c28960
MK
395.\"
396.SS MS_SLAVE example
397Making a mount point a slave allows it to receive propagated
398mount and unmount events from a master shared peer group,
d9cdf357 399while preventing it from propagating events to that master.
98c28960
MK
400This is useful if we want to (say) receive a mount event when
401an optical disk is mounted in the master shared peer group
402(in another mount namespace),
403but want to prevent mount and unmount events under the slave mount
404from having side effects in other namespaces.
a721e8b2 405.PP
98c28960
MK
406We can demonstrate the effect of slaving by first marking
407two mount points as shared in the initial mount namespace:
a721e8b2 408.PP
98c28960 409.in +4n
b8302363 410.EX
98c28960
MK
411sh1# \fBmount \-\-make\-shared /mntX\fP
412sh1# \fBmount \-\-make\-shared /mntY\fP
413sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
414132 83 8:23 / /mntX rw,relatime shared:1
415133 83 8:22 / /mntY rw,relatime shared:2
b8302363 416.EE
e646a1ba 417.in
a721e8b2 418.PP
98c28960
MK
419On a second terminal,
420we create a new mount namespace and inspect the mount points:
a721e8b2 421.PP
98c28960 422.in +4n
b8302363 423.EX
98c28960
MK
424sh2# \fBunshare \-m \-\-propagation unchanged sh\fP
425sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
426168 167 8:23 / /mntX rw,relatime shared:1
427169 167 8:22 / /mntY rw,relatime shared:2
b8302363 428.EE
e646a1ba 429.in
a721e8b2 430.PP
98c28960 431In the new mount namespace, we then mark one of the mount points as a slave:
a721e8b2 432.PP
98c28960 433.in +4n
b8302363 434.EX
98c28960
MK
435sh2# \fBmount \-\-make\-slave /mntY\fP
436sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
437168 167 8:23 / /mntX rw,relatime shared:1
438169 167 8:22 / /mntY rw,relatime master:2
b8302363 439.EE
e646a1ba 440.in
a721e8b2 441.PP
98c28960
MK
442From the above output, we see that
443.IR /mntY
444is now a slave mount that is receiving propagation events from
445the shared peer group with the ID 2.
a721e8b2 446.PP
98c28960
MK
447Continuing in the new namespace, we create submounts under each of
448.IR /mntX
449and
450.IR /mntY :
a721e8b2 451.PP
98c28960 452.in +4n
b8302363 453.EX
d9cdf357
MK
454sh2# \fBmkdir /mntX/a\fP
455sh2# \fBmount /dev/sda3 /mntX/a\fP
456sh2# \fBmkdir /mntY/b\fP
457sh2# \fBmount /dev/sda5 /mntY/b\fP
b8302363 458.EE
e646a1ba 459.in
a721e8b2 460.PP
98c28960
MK
461When we inspect the state of the mount points in the new mount namespace,
462we see that
d9cdf357 463.IR /mntX/a
98c28960
MK
464was created as a new shared mount
465(inheriting the "shared" setting from its parent mount) and
d9cdf357 466.IR /mntY/b
98c28960 467was created as a private mount:
a721e8b2 468.PP
98c28960 469.in +4n
b8302363 470.EX
98c28960
MK
471sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
472168 167 8:23 / /mntX rw,relatime shared:1
473169 167 8:22 / /mntY rw,relatime master:2
d9cdf357
MK
474173 168 8:3 / /mntX/a rw,relatime shared:3
475175 169 8:5 / /mntY/b rw,relatime
b8302363 476.EE
e646a1ba 477.in
a721e8b2 478.PP
98c28960
MK
479Returning to the first terminal (in the initial mount namespace),
480we see that the mount
d9cdf357 481.IR /mntX/a
98c28960
MK
482propagated to the peer (the shared
483.IR /mntX ),
484but the mount
d9cdf357 485.IR /mntY/b
98c28960 486was not propagated:
a721e8b2 487.PP
98c28960 488.in +4n
b8302363 489.EX
98c28960
MK
490sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
491132 83 8:23 / /mntX rw,relatime shared:1
492133 83 8:22 / /mntY rw,relatime shared:2
d9cdf357 493174 132 8:3 / /mntX/a rw,relatime shared:3
b8302363 494.EE
e646a1ba 495.in
a721e8b2 496.PP
98c28960
MK
497Now we create a new mount point under
498.IR /mntY
499in the first shell:
a721e8b2 500.PP
98c28960 501.in +4n
b8302363 502.EX
d9cdf357
MK
503sh1# \fBmkdir /mntY/c\fP
504sh1# \fBmount /dev/sda1 /mntY/c\fP
98c28960
MK
505sh1# \fBcat /proc/self/mountinfo | grep '/mnt' | sed 's/ \- .*//'\fP
506132 83 8:23 / /mntX rw,relatime shared:1
507133 83 8:22 / /mntY rw,relatime shared:2
d9cdf357
MK
508174 132 8:3 / /mntX/a rw,relatime shared:3
509178 133 8:1 / /mntY/c rw,relatime shared:4
b8302363 510.EE
e646a1ba 511.in
a721e8b2 512.PP
98c28960
MK
513When we examine the mount points in the second mount namespace,
514we see that in this case the new mount has been propagated
515to the slave mount point,
516and that the new mount is itself a slave mount (to peer group 4):
a721e8b2 517.PP
98c28960 518.in +4n
b8302363 519.EX
98c28960
MK
520sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
521168 167 8:23 / /mntX rw,relatime shared:1
522169 167 8:22 / /mntY rw,relatime master:2
d9cdf357
MK
523173 168 8:3 / /mntX/a rw,relatime shared:3
524175 169 8:5 / /mntY/b rw,relatime
525179 169 8:1 / /mntY/c rw,relatime master:4
b8302363 526.EE
e646a1ba 527.in
98c28960
MK
528.\"
529.SS MS_UNBINDABLE example
530One of the primary purposes of unbindable mounts is to avoid
531the "mount point explosion" problem when repeatedly performing bind mounts
532of a higher-level subtree at a lower-level mount point.
533The problem is illustrated by the following shell session.
a721e8b2 534.PP
98c28960 535Suppose we have a system with the following mount points:
a721e8b2 536.PP
98c28960 537.in +4n
b8302363 538.EX
98c28960
MK
539# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
540/dev/sda1 on /
541/dev/sdb6 on /mntX
542/dev/sdb7 on /mntY
b8302363 543.EE
e646a1ba 544.in
a721e8b2 545.PP
98c28960
MK
546Suppose furthermore that we wish to recursively bind mount
547the root directory under several users' home directories.
548We do this for the first user, and inspect the mount points:
a721e8b2 549.PP
98c28960 550.in +4n
b8302363 551.EX
98c28960
MK
552# \fBmount \-\-rbind / /home/cecilia/\fP
553# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
554/dev/sda1 on /
555/dev/sdb6 on /mntX
556/dev/sdb7 on /mntY
557/dev/sda1 on /home/cecilia
558/dev/sdb6 on /home/cecilia/mntX
559/dev/sdb7 on /home/cecilia/mntY
b8302363 560.EE
e646a1ba 561.in
a721e8b2 562.PP
98c28960
MK
563When we repeat this operation for the second user,
564we start to see the explosion problem:
a721e8b2 565.PP
98c28960 566.in +4n
b8302363 567.EX
98c28960
MK
568# \fBmount \-\-rbind / /home/henry\fP
569# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
570/dev/sda1 on /
571/dev/sdb6 on /mntX
572/dev/sdb7 on /mntY
573/dev/sda1 on /home/cecilia
574/dev/sdb6 on /home/cecilia/mntX
575/dev/sdb7 on /home/cecilia/mntY
576/dev/sda1 on /home/henry
577/dev/sdb6 on /home/henry/mntX
578/dev/sdb7 on /home/henry/mntY
579/dev/sda1 on /home/henry/home/cecilia
580/dev/sdb6 on /home/henry/home/cecilia/mntX
581/dev/sdb7 on /home/henry/home/cecilia/mntY
b8302363 582.EE
e646a1ba 583.in
a721e8b2 584.PP
98c28960
MK
585Under
586.IR /home/henry ,
587we have not only recursively added the
588.IR /mntX
589and
590.IR /mntY
591mounts, but also the recursive mounts of those directories under
592.IR /home/cecilia
593that were created in the previous step.
594Upon repeating the step for a third user,
595it becomes obvious that the explosion is exponential in nature:
a721e8b2 596.PP
98c28960 597.in +4n
b8302363 598.EX
98c28960
MK
599# \fBmount \-\-rbind / /home/otto\fP
600# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
601/dev/sda1 on /
602/dev/sdb6 on /mntX
603/dev/sdb7 on /mntY
604/dev/sda1 on /home/cecilia
605/dev/sdb6 on /home/cecilia/mntX
606/dev/sdb7 on /home/cecilia/mntY
607/dev/sda1 on /home/henry
608/dev/sdb6 on /home/henry/mntX
609/dev/sdb7 on /home/henry/mntY
610/dev/sda1 on /home/henry/home/cecilia
611/dev/sdb6 on /home/henry/home/cecilia/mntX
612/dev/sdb7 on /home/henry/home/cecilia/mntY
613/dev/sda1 on /home/otto
614/dev/sdb6 on /home/otto/mntX
615/dev/sdb7 on /home/otto/mntY
616/dev/sda1 on /home/otto/home/cecilia
617/dev/sdb6 on /home/otto/home/cecilia/mntX
618/dev/sdb7 on /home/otto/home/cecilia/mntY
619/dev/sda1 on /home/otto/home/henry
620/dev/sdb6 on /home/otto/home/henry/mntX
621/dev/sdb7 on /home/otto/home/henry/mntY
622/dev/sda1 on /home/otto/home/henry/home/cecilia
623/dev/sdb6 on /home/otto/home/henry/home/cecilia/mntX
624/dev/sdb7 on /home/otto/home/henry/home/cecilia/mntY
b8302363 625.EE
e646a1ba 626.in
a721e8b2 627.PP
98c28960
MK
628The mount explosion problem in the above scenario can be avoided
629by making each of the new mounts unbindable.
630The effect of doing this is that recursive mounts of the root
631directory will not replicate the unbindable mounts.
632We make such a mount for the first user:
a721e8b2 633.PP
98c28960 634.in +4n
b8302363 635.EX
98c28960 636# \fBmount \-\-rbind \-\-make\-unbindable / /home/cecilia\fP
b8302363 637.EE
e646a1ba 638.in
a721e8b2 639.PP
98c28960 640Before going further, we show that unbindable mounts are indeed unbindable:
a721e8b2 641.PP
98c28960 642.in +4n
b8302363 643.EX
98c28960
MK
644# \fBmkdir /mntZ\fP
645# \fBmount \-\-bind /home/cecilia /mntZ\fP
646mount: wrong fs type, bad option, bad superblock on /home/cecilia,
647 missing codepage or helper program, or other error
648
649 In some cases useful info is found in syslog \- try
650 dmesg | tail or so.
b8302363 651.EE
e646a1ba 652.in
a721e8b2 653.PP
98c28960 654Now we create unbindable recursive bind mounts for the other two users:
a721e8b2 655.PP
98c28960 656.in +4n
b8302363 657.EX
98c28960
MK
658# \fBmount \-\-rbind \-\-make\-unbindable / /home/henry\fP
659# \fBmount \-\-rbind \-\-make\-unbindable / /home/otto\fP
b8302363 660.EE
e646a1ba 661.in
a721e8b2 662.PP
98c28960
MK
663Upon examining the list of mount points,
664we see there has been no explosion of mount points,
665because the unbindable mounts were not replicated
666under each user's directory:
a721e8b2 667.PP
98c28960 668.in +4n
b8302363 669.EX
98c28960
MK
670# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
671/dev/sda1 on /
672/dev/sdb6 on /mntX
673/dev/sdb7 on /mntY
674/dev/sda1 on /home/cecilia
675/dev/sdb6 on /home/cecilia/mntX
676/dev/sdb7 on /home/cecilia/mntY
677/dev/sda1 on /home/henry
678/dev/sdb6 on /home/henry/mntX
679/dev/sdb7 on /home/henry/mntY
680/dev/sda1 on /home/otto
681/dev/sdb6 on /home/otto/mntX
682/dev/sdb7 on /home/otto/mntY
b8302363 683.EE
e646a1ba 684.in
98c28960
MK
685.\"
686.SS Propagation type transitions
687The following table shows the effect that applying a new propagation type
688(i.e.,
689.IR "mount \-\-make\-xxxx")
690has on the existing propagation type of a mount point.
691The rows correspond to existing propagation types,
692and the columns are the new propagation settings.
693For reasons of space, "private" is abbreviated as "priv" and
694"unbindable" as "unbind".
695.TS
696lb2 lb2 lb2 lb2 lb1
697lb l l l l l.
698 make-shared make-slave make-priv make-unbind
699shared shared slave/priv [1] priv unbind
700slave slave+shared slave [2] priv unbind
701slave+shared slave+shared slave priv unbind
702private shared priv [2] priv unbind
703unbindable shared unbind [2] priv unbind
704.TE
a721e8b2 705.sp 1
98c28960
MK
706Note the following details to the table:
707.IP [1] 4
708If a shared mount is the only mount in its peer group,
709making it a slave automatically makes it private.
710.IP [2]
711Slaving a nonshared mount has no effect on the mount.
712.\"
713.SS Bind (MS_BIND) semantics
714Suppose that the following command is performed:
a721e8b2 715.PP
98c28960 716 mount \-\-bind A/a B/b
a721e8b2 717.PP
98c28960
MK
718Here,
719.I A
720is the source mount point,
721.I B
722is the destination mount point,
723.I a
724is a subdirectory path under the mount point
725.IR A ,
726and
727.I b
728is a subdirectory path under the mount point
729.IR B .
730The propagation type of the resulting mount,
731.IR B/b ,
732depends on the propagation types of the mount points
733.IR A
734and
735.IR B ,
736and is summarized in the following table.
a721e8b2 737.PP
98c28960
MK
738.TS
739lb2 lb1 lb2 lb2 lb2 lb0
740lb2 lb1 lb2 lb2 lb2 lb0
741lb lb l l l l l.
742 source(A)
743 shared private slave unbind
744_
745dest(B) shared | shared shared slave+shared invalid
746 nonshared | shared private slave invalid
747.TE
a721e8b2 748.sp 1
98c28960
MK
749Note that a recursive bind of a subtree follows the same semantics
750as for a bind operation on each mount in the subtree.
751(Unbindable mounts are automatically pruned at the target mount point.)
a721e8b2 752.PP
98c28960 753For further details, see
1481407a 754.I Documentation/filesystems/sharedsubtree.txt
98c28960
MK
755in the kernel source tree.
756.\"
757.SS Move (MS_MOVE) semantics
758Suppose that the following command is performed:
a721e8b2 759.PP
98c28960 760 mount \-\-move A B/b
a721e8b2 761.PP
98c28960
MK
762Here,
763.I A
764is the source mount point,
765.I B
766is the destination mount point, and
767.I b
768is a subdirectory path under the mount point
769.IR B .
770The propagation type of the resulting mount,
771.IR B/b ,
772depends on the propagation types of the mount points
773.IR A
774and
775.IR B ,
776and is summarized in the following table.
a721e8b2 777.PP
98c28960
MK
778.TS
779lb2 lb1 lb2 lb2 lb2 lb0
780lb2 lb1 lb2 lb2 lb2 lb0
781lb lb l l l l l.
782 source(A)
783 shared private slave unbind
784_
785dest(B) shared | shared shared slave+shared invalid
786 nonshared | shared private slave unbindable
787.TE
a721e8b2 788.sp 1
98c28960 789Note: moving a mount that resides under a shared mount is invalid.
a721e8b2 790.PP
98c28960 791For further details, see
1481407a 792.I Documentation/filesystems/sharedsubtree.txt
98c28960
MK
793in the kernel source tree.
794.\"
795.SS Mount semantics
796Suppose that we use the following command to create a mount point:
a721e8b2 797.PP
98c28960 798 mount device B/b
a721e8b2 799.PP
98c28960
MK
800Here,
801.I B
802is the destination mount point, and
803.I b
804is a subdirectory path under the mount point
805.IR B .
806The propagation type of the resulting mount,
807.IR B/b ,
808follows the same rules as for a bind mount,
809where the propagation type of the source mount
810is considered always to be private.
811.\"
812.SS Unmount semantics
813Suppose that we use the following command to tear down a mount point:
a721e8b2 814.PP
98c28960 815 unmount A
a721e8b2 816.PP
98c28960
MK
817Here,
818.I A
819is a mount point on
820.IR B/b ,
821where
822.I B
823is the parent mount and
824.I b
825is a subdirectory path under the mount point
826.IR B .
827If
828.B B
829is shared, then all most-recently-mounted mounts at
830.I b
831on mounts that receive propagation from mount
832.I B
833and do not have submounts under them are unmounted.
834.\"
e2109196
MK
835.SS The /proc/[pid]/mountinfo "propagate_from" tag
836The
837.I propagate_from:X
838tag is shown in the optional fields of a
839.IR /proc/[pid]/mountinfo
840record in cases where a process can't see a slave's immediate master
841(i.e., the pathname of the master is not reachable from
842the filesystem root directory)
843and so cannot determine the
844chain of propagation between the mounts it can see.
a721e8b2 845.PP
e2109196
MK
846In the following example, we first create a two-link master-slave chain
847between the mounts
848.IR /mnt ,
849.IR /tmp/etc ,
850and
851.IR /mnt/tmp/etc .
852Then the
853.BR chroot (1)
854command is used to make the
855.IR /tmp/etc
856mount point unreachable from the root directory,
857creating a situation where the master of
858.IR /mnt/tmp/etc
859is not reachable from the (new) root directory of the process.
a721e8b2 860.PP
e2109196
MK
861First, we bind mount the root directory onto
862.IR /mnt
863and then bind mount
864.IR /proc
865at
866.IR /mnt/proc
867so that after the later
868.BR chroot (1)
869the
870.BR proc (5)
871filesystem remains visible at the correct location
872in the chroot-ed environment.
a721e8b2 873.PP
e2109196 874.in +4n
b8302363 875.EX
e2109196
MK
876# \fBmkdir \-p /mnt/proc\fP
877# \fBmount \-\-bind / /mnt\fP
878# \fBmount \-\-bind /proc /mnt/proc\fP
b8302363 879.EE
e646a1ba 880.in
a721e8b2 881.PP
e2109196
MK
882Next, we ensure that the
883.IR /mnt
884mount is a shared mount in a new peer group (with no peers):
a721e8b2 885.PP
e2109196 886.in +4n
b8302363 887.EX
e2109196
MK
888# \fBmount \-\-make\-private /mnt\fP # Isolate from any previous peer group
889# \fBmount \-\-make\-shared /mnt\fP
890# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
891239 61 8:2 / /mnt ... shared:102
892248 239 0:4 / /mnt/proc ... shared:5
b8302363 893.EE
e646a1ba 894.in
a721e8b2 895.PP
e2109196
MK
896Next, we bind mount
897.IR /mnt/etc
898onto
899.IR /tmp/etc :
a721e8b2 900.PP
e2109196 901.in +4n
b8302363 902.EX
e2109196
MK
903# \fBmkdir \-p /tmp/etc\fP
904# \fBmount \-\-bind /mnt/etc /tmp/etc\fP
905# \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
906239 61 8:2 / /mnt ... shared:102
907248 239 0:4 / /mnt/proc ... shared:5
908267 40 8:2 /etc /tmp/etc ... shared:102
b8302363 909.EE
e646a1ba 910.in
a721e8b2 911.PP
e2109196
MK
912Initially, these two mount points are in the same peer group,
913but we then make the
914.IR /tmp/etc
915a slave of
916.IR /mnt/etc ,
917and then make
918.IR /tmp/etc
919shared as well,
920so that it can propagate events to the next slave in the chain:
a721e8b2 921.PP
e2109196 922.in +4n
b8302363 923.EX
e2109196
MK
924# \fBmount \-\-make\-slave /tmp/etc\fP
925# \fBmount \-\-make\-shared /tmp/etc\fP
926# \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
927239 61 8:2 / /mnt ... shared:102
928248 239 0:4 / /mnt/proc ... shared:5
929267 40 8:2 /etc /tmp/etc ... shared:105 master:102
b8302363 930.EE
e646a1ba 931.in
a721e8b2 932.PP
e2109196
MK
933Then we bind mount
934.IR /tmp/etc
935onto
936.IR /mnt/tmp/etc .
937Again, the two mount points are initially in the same peer group,
938but we then make
939.IR /mnt/tmp/etc
940a slave of
941.IR /tmp/etc :
a721e8b2 942.PP
e2109196 943.in +4n
b8302363 944.EX
e2109196
MK
945# \fBmkdir \-p /mnt/tmp/etc\fP
946# \fBmount \-\-bind /tmp/etc /mnt/tmp/etc\fP
947# \fBmount \-\-make\-slave /mnt/tmp/etc\fP
948# \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
949239 61 8:2 / /mnt ... shared:102
950248 239 0:4 / /mnt/proc ... shared:5
951267 40 8:2 /etc /tmp/etc ... shared:105 master:102
952273 239 8:2 /etc /mnt/tmp/etc ... master:105
e646a1ba 953.EE
e2109196 954.in
e646a1ba 955.PP
e2109196
MK
956From the above, we see that
957.IR /mnt
958is the master of the slave
959.IR /tmp/etc ,
960which in turn is the master of the slave
961.IR /mnt/tmp/etc .
a721e8b2 962.PP
e2109196
MK
963We then
964.BR chroot (1)
965to the
966.IR /mnt
967directory, which renders the mount with ID 267 unreachable
968from the (new) root directory:
a721e8b2 969.PP
e2109196 970.in +4n
b8302363 971.EX
e2109196 972# \fBchroot /mnt\fP
b8302363 973.EE
e646a1ba 974.in
a721e8b2 975.PP
e2109196
MK
976When we examine the state of the mounts inside the chroot-ed environment,
977we see the following:
a721e8b2 978.PP
e2109196 979.in +4n
b8302363 980.EX
e2109196
MK
981# \fBcat /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
982239 61 8:2 / / ... shared:102
983248 239 0:4 / /proc ... shared:5
984273 239 8:2 /etc /tmp/etc ... master:105 propagate_from:102
b8302363 985.EE
e646a1ba 986.in
a721e8b2 987.PP
e2109196
MK
988Above, we see that the mount with ID 273
989is a slave whose master is the peer group 105.
990The mount point for that master is unreachable, and so a
991.IR propagate_from
992tag is displayed, indicating that the closest dominant peer group
993(i.e., the nearest reachable mount in the slave chain)
994is the peer group with the ID 102 (corresponding to the
995.IR /mnt
996mount point before the
997.BR chroot (1)
998was performed.
999.\"
c307aecd
MK
1000.SH VERSIONS
1001Mount namespaces first appeared in Linux 2.4.19.
1002.SH CONFORMING TO
1003Namespaces are a Linux-specific feature.
1004.\"
98c28960 1005.SH NOTES
3dcc463a 1006The propagation type assigned to a new mount point depends
4954c465 1007on the propagation type of the parent mount.
3dcc463a
MK
1008If the mount point has a parent (i.e., it is a non-root mount
1009point) and the propagation type of the parent is
1010.BR MS_SHARED ,
1011then the propagation type of the new mount is also
1012.BR MS_SHARED .
1013Otherwise, the propagation type of the new mount is
98c28960 1014.BR MS_PRIVATE .
a721e8b2 1015.PP
3dcc463a
MK
1016Notwithstanding the fact that the default propagation type
1017for new mount points is in many cases
1018.BR MS_PRIVATE ,
98c28960 1019.BR MS_SHARED
3dcc463a
MK
1020is typically more useful.
1021For this reason,
98c28960
MK
1022.BR systemd (1)
1023automatically remounts all mount points as
1024.BR MS_SHARED
1025on system startup.
3dcc463a
MK
1026Thus, on most modern systems, the default propagation type is in practice
1027.BR MS_SHARED .
a721e8b2 1028.PP
98c28960
MK
1029Since, when one uses
1030.BR unshare (1)
1031to create a mount namespace,
1032the goal is commonly to provide full isolation of the mount points
1033in the new namespace,
1034.BR unshare (1)
1035(since
1036.IR util-linux
1037version 2.27) in turn reverses the step performed by
1038.BR systemd (1),
1039by making all mount points private in the new namespace.
1040That is,
1041.BR unshare (1)
1042performs the equivalent of the following in the new mount namespace:
a721e8b2 1043.PP
98c28960 1044 mount \-\-make\-rprivate /
a721e8b2 1045.PP
98c28960
MK
1046To prevent this, one can use the
1047.IR "\-\-propagation\ unchanged"
1048option to
1049.BR unshare (1).
a721e8b2 1050.PP
3dcc463a
MK
1051For a discussion of propagation types when moving mounts
1052.RB ( MS_MOVE )
1053and creating bind mounts
1054.RB ( MS_BIND ),
1055see
1056.IR Documentation/filesystems/sharedsubtree.txt .
98c28960
MK
1057.SH SEE ALSO
1058.BR unshare (1),
1059.BR clone (2),
1060.BR mount (2),
1061.BR setns (2),
1062.BR umount (2),
1063.BR unshare (2),
1064.BR proc (5),
466247eb 1065.BR namespaces (7),
93f5b0f8
MK
1066.BR user_namespaces (7),
1067.BR findmnt (8)
a721e8b2 1068.PP
98c28960
MK
1069.IR Documentation/filesystems/sharedsubtree.txt
1070in the kernel source tree.