]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/mount_namespaces.7
Many pages: Fix style issues reported by `make lint-groff`
[thirdparty/man-pages.git] / man7 / mount_namespaces.7
CommitLineData
ee54e5d5 1.\" Copyright (c) 2016, 2019, 2021 by Michael Kerrisk <mtk.manpages@gmail.com>
98c28960 2.\"
5fbde956 3.\" SPDX-License-Identifier: Linux-man-pages-copyleft
98c28960
MK
4.\"
5.\"
6e00b7a8 6.TH MOUNT_NAMESPACES 7 2021-08-27 "Linux" "Linux Programmer's Manual"
98c28960
MK
7.SH NAME
8mount_namespaces \- overview of Linux mount namespaces
9.SH DESCRIPTION
10For an overview of namespaces, see
11.BR namespaces (7).
a721e8b2 12.PP
8c9a8274 13Mount namespaces provide isolation of the list of mounts seen
98c28960
MK
14by the processes in each namespace instance.
15Thus, the processes in each of the mount namespace instances
16will see distinct single-directory hierarchies.
a721e8b2 17.PP
98c28960 18The views provided by the
1ae6b2c7
AC
19.IR /proc/ pid /mounts ,
20.IR /proc/ pid /mountinfo ,
98c28960 21and
1ae6b2c7 22.IR /proc/ pid /mountstats
98c28960
MK
23files (all described in
24.BR proc (5))
25correspond to the mount namespace in which the process with the PID
1ae6b2c7 26.I pid
98c28960
MK
27resides.
28(All of the processes that reside in the same mount namespace
29will see the same view in these files.)
a721e8b2 30.PP
534755ee 31A new mount namespace is created using either
98c28960
MK
32.BR clone (2)
33or
34.BR unshare (2)
35with the
1ae6b2c7 36.B CLONE_NEWNS
534755ee
MK
37flag.
38When a new mount namespace is created,
8c9a8274 39its mount list is initialized as follows:
534755ee
MK
40.IP * 3
41If the namespace is created using
42.BR clone (2),
8c9a8274 43the mount list of the child's namespace is a copy
82357e60 44of the mount list in the parent process's mount namespace.
534755ee
MK
45.IP *
46If the namespace is created using
47.BR unshare (2),
8c9a8274
MK
48the mount list of the new namespace is a copy of
49the mount list in the caller's previous mount namespace.
534755ee 50.PP
8c9a8274 51Subsequent modifications to the mount list
98c28960
MK
52.RB ( mount (2)
53and
54.BR umount (2))
55in either mount namespace will not (by default) affect the
8c9a8274 56mount list seen in the other namespace
98c28960
MK
57(but see the following discussion of shared subtrees).
58.\"
59.SH SHARED SUBTREES
60After the implementation of mount namespaces was completed,
61experience showed that the isolation that they provided was,
62in some cases, too great.
63For example, in order to make a newly loaded optical disk
64available in all mount namespaces,
65a mount operation was required in each namespace.
66For this use case, and others,
67the shared subtree feature was introduced in Linux 2.6.15.
68This feature allows for automatic, controlled propagation of mount and unmount
69.I events
70between namespaces
24483c27 71(or, more precisely, between the mounts that are members of a
1ae6b2c7 72.I peer group
98c28960 73that are propagating events to one another).
a721e8b2 74.PP
8c9a8274 75Each mount is marked (via
98c28960
MK
76.BR mount (2))
77as having one of the following
78.IR "propagation types" :
79.TP
1ae6b2c7 80.B MS_SHARED
8c9a8274
MK
81This mount shares events with members of a peer group.
82Mount and unmount events immediately under this mount will propagate
83to the other mounts that are members of the peer group.
98c28960
MK
84.I Propagation
85here means that the same mount or unmount will automatically occur
8c9a8274 86under all of the other mounts in the peer group.
98c28960 87Conversely, mount and unmount events that take place under
8c9a8274 88peer mounts will propagate to this mount.
98c28960 89.TP
1ae6b2c7 90.B MS_PRIVATE
8c9a8274
MK
91This mount is private; it does not have a peer group.
92Mount and unmount events do not propagate into or out of this mount.
98c28960 93.TP
1ae6b2c7 94.B MS_SLAVE
8c9a8274 95Mount and unmount events propagate into this mount from
98c28960 96a (master) shared peer group.
8c9a8274 97Mount and unmount events under this mount do not propagate to any peer.
a721e8b2 98.IP
8c9a8274 99Note that a mount can be the slave of another peer group
98c28960
MK
100while at the same time sharing mount and unmount events
101with a peer group of which it is a member.
102(More precisely, one peer group can be the slave of another peer group.)
103.TP
1ae6b2c7 104.B MS_UNBINDABLE
98c28960
MK
105This is like a private mount,
106and in addition this mount can't be bind mounted.
107Attempts to bind mount this mount
108.RB ( mount (2)
109with the
1ae6b2c7 110.B MS_BIND
98c28960 111flag) will fail.
a721e8b2 112.IP
98c28960
MK
113When a recursive bind mount
114.RB ( mount (2)
115with the
1ae6b2c7 116.B MS_BIND
98c28960 117and
1ae6b2c7 118.B MS_REC
98c28960
MK
119flags) is performed on a directory subtree,
120any bind mounts within the subtree are automatically pruned
121(i.e., not replicated)
122when replicating that subtree to produce the target subtree.
123.PP
3dcc463a
MK
124For a discussion of the propagation type assigned to a new mount,
125see NOTES.
a721e8b2 126.PP
98c28960 127The propagation type is a per-mount-point setting;
8c9a8274
MK
128some mounts may be marked as shared
129(with each shared mount being a member of a distinct peer group),
98c28960
MK
130while others are private
131(or slaved or unbindable).
a721e8b2 132.PP
98c28960 133Note that a mount's propagation type determines whether
8c9a8274 134mounts and unmounts of mounts
1ae6b2c7 135.I immediately under
8c9a8274 136the mount are propagated.
98c28960 137Thus, the propagation type does not affect propagation of events for
8c9a8274
MK
138grandchildren and further removed descendant mounts.
139What happens if the mount itself is unmounted is determined by
98c28960
MK
140the propagation type that is in effect for the
141.I parent
8c9a8274 142of the mount.
a721e8b2 143.PP
98c28960 144Members are added to a
1ae6b2c7 145.I peer group
8c9a8274 146when a mount is marked as shared and either:
98c28960 147.IP * 3
8c9a8274 148the mount is replicated during the creation of a new mount namespace; or
98c28960 149.IP *
8c9a8274 150a new bind mount is created from the mount.
98c28960 151.PP
8c9a8274
MK
152In both of these cases, the new mount joins the peer group
153of which the existing mount is a member.
46af7198 154.PP
8c9a8274
MK
155A new peer group is also created when a child mount is created under
156an existing mount that is marked as shared.
157In this case, the new child mount is also marked as shared and
158the resulting peer group consists of all the mounts
9428bb9d 159that are replicated under the peers of parent mounts.
6b49df22 160.PP
98c28960
MK
161A mount ceases to be a member of a peer group when either
162the mount is explicitly unmounted,
163or when the mount is implicitly unmounted because a mount namespace is removed
164(because it has no more member processes).
a721e8b2 165.PP
8c9a8274 166The propagation type of the mounts in a mount namespace
98c28960 167can be discovered via the "optional fields" exposed in
1ae6b2c7 168.IR /proc/ pid /mountinfo .
98c28960
MK
169(See
170.BR proc (5)
171for details of this file.)
172The following tags can appear in the optional fields
173for a record in that file:
174.TP
175.I shared:X
8c9a8274 176This mount is shared in peer group
98c28960 177.IR X .
d9cdf357 178Each peer group has a unique ID that is automatically
98c28960 179generated by the kernel,
8c9a8274 180and all mounts in the same peer group will show the same ID.
d9cdf357
MK
181(These IDs are assigned starting from the value 1,
182and may be recycled when a peer group ceases to have any members.)
98c28960
MK
183.TP
184.I master:X
185This mount is a slave to shared peer group
186.IR X .
187.TP
188.IR propagate_from:X " (since Linux 2.6.26)"
189.\" commit 97e7e0f71d6d948c25f11f0a33878d9356d9579e
190This mount is a slave and receives propagation from shared peer group
191.IR X .
192This tag will always appear in conjunction with a
1ae6b2c7 193.I master:X
98c28960
MK
194tag.
195Here,
1ae6b2c7 196.I X
98c28960
MK
197is the closest dominant peer group under the process's root directory.
198If
1ae6b2c7 199.I X
98c28960
MK
200is the immediate master of the mount,
201or if there is no dominant peer group under the same root,
202then only the
1ae6b2c7 203.I master:X
98c28960 204field is present and not the
1ae6b2c7 205.I propagate_from:X
98c28960 206field.
e2109196 207For further details, see below.
98c28960 208.TP
1ae6b2c7 209.I unbindable
98c28960
MK
210This is an unbindable mount.
211.PP
212If none of the above tags is present, then this is a private mount.
213.SS MS_SHARED and MS_PRIVATE example
214Suppose that on a terminal in the initial mount namespace,
8c9a8274 215we mark one mount as shared and another as private,
98c28960
MK
216and then view the mounts in
217.IR /proc/self/mountinfo :
a721e8b2 218.PP
98c28960 219.in +4n
b8302363 220.EX
d9cdf357
MK
221sh1# \fBmount \-\-make\-shared /mntS\fP
222sh1# \fBmount \-\-make\-private /mntP\fP
f481726d 223sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
22477 61 8:17 / /mntS rw,relatime shared:1
22583 61 8:15 / /mntP rw,relatime
b8302363 226.EE
e646a1ba 227.in
a721e8b2 228.PP
98c28960 229From the
1ae6b2c7 230.I /proc/self/mountinfo
98c28960 231output, we see that
1ae6b2c7 232.I /mntS
98c28960 233is a shared mount in peer group 1, and that
1ae6b2c7 234.I /mntP
98c28960
MK
235has no optional tags, indicating that it is a private mount.
236The first two fields in each record in this file are the unique
237ID for this mount, and the mount ID of the parent mount.
8c9a8274 238We can further inspect this file to see that the parent mount of
1ae6b2c7 239.I /mntS
98c28960 240and
1ae6b2c7 241.I /mntP
98c28960
MK
242is the root directory,
243.IR / ,
244which is mounted as private:
a721e8b2 245.PP
98c28960 246.in +4n
b8302363 247.EX
98c28960
MK
248sh1# \fBcat /proc/self/mountinfo | awk \(aq$1 == 61\(aq | sed \(aqs/ \- .*//\(aq\fP
24961 0 8:2 / / rw,relatime
b8302363 250.EE
e646a1ba 251.in
a721e8b2 252.PP
98c28960
MK
253On a second terminal,
254we create a new mount namespace where we run a second shell
255and inspect the mounts:
a721e8b2 256.PP
98c28960 257.in +4n
b8302363 258.EX
98c28960 259$ \fBPS1=\(aqsh2# \(aq sudo unshare \-m \-\-propagation unchanged sh\fP
f481726d 260sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
261222 145 8:17 / /mntS rw,relatime shared:1
262225 145 8:15 / /mntP rw,relatime
b8302363 263.EE
e646a1ba 264.in
a721e8b2 265.PP
98c28960 266The new mount namespace received a copy of the initial mount namespace's
8c9a8274
MK
267mounts.
268These new mounts maintain the same propagation types,
98c28960
MK
269but have unique mount IDs.
270(The
1ae6b2c7 271.I \-\-propagation\~unchanged
98c28960
MK
272option prevents
273.BR unshare (1)
274from marking all mounts as private when creating a new mount namespace,
275.\" Since util-linux 2.27
276which it does by default.)
a721e8b2 277.PP
98c28960 278In the second terminal, we then create submounts under each of
1ae6b2c7 279.I /mntS
98c28960 280and
1ae6b2c7 281.I /mntP
98c28960 282and inspect the set-up:
a721e8b2 283.PP
98c28960 284.in +4n
b8302363 285.EX
d9cdf357
MK
286sh2# \fBmkdir /mntS/a\fP
287sh2# \fBmount /dev/sdb6 /mntS/a\fP
288sh2# \fBmkdir /mntP/b\fP
289sh2# \fBmount /dev/sdb7 /mntP/b\fP
f481726d 290sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
291222 145 8:17 / /mntS rw,relatime shared:1
292225 145 8:15 / /mntP rw,relatime
293178 222 8:22 / /mntS/a rw,relatime shared:2
294230 225 8:23 / /mntP/b rw,relatime
b8302363 295.EE
e646a1ba 296.in
a721e8b2 297.PP
98c28960 298From the above, it can be seen that
1ae6b2c7 299.I /mntS/a
98c28960 300was created as shared (inheriting this setting from its parent mount) and
1ae6b2c7 301.I /mntP/b
98c28960 302was created as a private mount.
a721e8b2 303.PP
98c28960 304Returning to the first terminal and inspecting the set-up,
8c9a8274 305we see that the new mount created under the shared mount
1ae6b2c7 306.I /mntS
98c28960 307propagated to its peer mount (in the initial mount namespace),
8c9a8274 308but the new mount created under the private mount
1ae6b2c7 309.I /mntP
98c28960 310did not propagate:
a721e8b2 311.PP
98c28960 312.in +4n
b8302363 313.EX
f481726d 314sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
d9cdf357
MK
31577 61 8:17 / /mntS rw,relatime shared:1
31683 61 8:15 / /mntP rw,relatime
317179 77 8:22 / /mntS/a rw,relatime shared:2
b8302363 318.EE
e646a1ba 319.in
98c28960
MK
320.\"
321.SS MS_SLAVE example
8c9a8274 322Making a mount a slave allows it to receive propagated
98c28960 323mount and unmount events from a master shared peer group,
d9cdf357 324while preventing it from propagating events to that master.
98c28960
MK
325This is useful if we want to (say) receive a mount event when
326an optical disk is mounted in the master shared peer group
327(in another mount namespace),
328but want to prevent mount and unmount events under the slave mount
329from having side effects in other namespaces.
a721e8b2 330.PP
98c28960 331We can demonstrate the effect of slaving by first marking
8c9a8274 332two mounts as shared in the initial mount namespace:
a721e8b2 333.PP
98c28960 334.in +4n
b8302363 335.EX
98c28960
MK
336sh1# \fBmount \-\-make\-shared /mntX\fP
337sh1# \fBmount \-\-make\-shared /mntY\fP
338sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
339132 83 8:23 / /mntX rw,relatime shared:1
340133 83 8:22 / /mntY rw,relatime shared:2
b8302363 341.EE
e646a1ba 342.in
a721e8b2 343.PP
98c28960 344On a second terminal,
8c9a8274 345we create a new mount namespace and inspect the mounts:
a721e8b2 346.PP
98c28960 347.in +4n
b8302363 348.EX
98c28960
MK
349sh2# \fBunshare \-m \-\-propagation unchanged sh\fP
350sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
351168 167 8:23 / /mntX rw,relatime shared:1
352169 167 8:22 / /mntY rw,relatime shared:2
b8302363 353.EE
e646a1ba 354.in
a721e8b2 355.PP
8c9a8274 356In the new mount namespace, we then mark one of the mounts as a slave:
a721e8b2 357.PP
98c28960 358.in +4n
b8302363 359.EX
98c28960
MK
360sh2# \fBmount \-\-make\-slave /mntY\fP
361sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
362168 167 8:23 / /mntX rw,relatime shared:1
363169 167 8:22 / /mntY rw,relatime master:2
b8302363 364.EE
e646a1ba 365.in
a721e8b2 366.PP
98c28960 367From the above output, we see that
1ae6b2c7 368.I /mntY
98c28960
MK
369is now a slave mount that is receiving propagation events from
370the shared peer group with the ID 2.
a721e8b2 371.PP
98c28960 372Continuing in the new namespace, we create submounts under each of
1ae6b2c7 373.I /mntX
98c28960
MK
374and
375.IR /mntY :
a721e8b2 376.PP
98c28960 377.in +4n
b8302363 378.EX
d9cdf357
MK
379sh2# \fBmkdir /mntX/a\fP
380sh2# \fBmount /dev/sda3 /mntX/a\fP
381sh2# \fBmkdir /mntY/b\fP
382sh2# \fBmount /dev/sda5 /mntY/b\fP
b8302363 383.EE
e646a1ba 384.in
a721e8b2 385.PP
8c9a8274 386When we inspect the state of the mounts in the new mount namespace,
98c28960 387we see that
1ae6b2c7 388.I /mntX/a
98c28960
MK
389was created as a new shared mount
390(inheriting the "shared" setting from its parent mount) and
1ae6b2c7 391.I /mntY/b
98c28960 392was created as a private mount:
a721e8b2 393.PP
98c28960 394.in +4n
b8302363 395.EX
98c28960
MK
396sh2# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
397168 167 8:23 / /mntX rw,relatime shared:1
398169 167 8:22 / /mntY rw,relatime master:2
d9cdf357
MK
399173 168 8:3 / /mntX/a rw,relatime shared:3
400175 169 8:5 / /mntY/b rw,relatime
b8302363 401.EE
e646a1ba 402.in
a721e8b2 403.PP
98c28960
MK
404Returning to the first terminal (in the initial mount namespace),
405we see that the mount
1ae6b2c7 406.I /mntX/a
98c28960
MK
407propagated to the peer (the shared
408.IR /mntX ),
409but the mount
1ae6b2c7 410.I /mntY/b
98c28960 411was not propagated:
a721e8b2 412.PP
98c28960 413.in +4n
b8302363 414.EX
98c28960
MK
415sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
416132 83 8:23 / /mntX rw,relatime shared:1
417133 83 8:22 / /mntY rw,relatime shared:2
d9cdf357 418174 132 8:3 / /mntX/a rw,relatime shared:3
b8302363 419.EE
e646a1ba 420.in
a721e8b2 421.PP
8c9a8274 422Now we create a new mount under
1ae6b2c7 423.I /mntY
98c28960 424in the first shell:
a721e8b2 425.PP
98c28960 426.in +4n
b8302363 427.EX
d9cdf357
MK
428sh1# \fBmkdir /mntY/c\fP
429sh1# \fBmount /dev/sda1 /mntY/c\fP
861d36ba 430sh1# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
98c28960
MK
431132 83 8:23 / /mntX rw,relatime shared:1
432133 83 8:22 / /mntY rw,relatime shared:2
d9cdf357
MK
433174 132 8:3 / /mntX/a rw,relatime shared:3
434178 133 8:1 / /mntY/c rw,relatime shared:4
b8302363 435.EE
e646a1ba 436.in
a721e8b2 437.PP
8c9a8274 438When we examine the mounts in the second mount namespace,
98c28960 439we see that in this case the new mount has been propagated
8c9a8274 440to the slave mount,
98c28960 441and that the new mount is itself a slave mount (to peer group 4):
a721e8b2 442.PP
98c28960 443.in +4n
b8302363 444.EX
98c28960
MK
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
d9cdf357
MK
448173 168 8:3 / /mntX/a rw,relatime shared:3
449175 169 8:5 / /mntY/b rw,relatime
450179 169 8:1 / /mntY/c rw,relatime master:4
b8302363 451.EE
e646a1ba 452.in
98c28960
MK
453.\"
454.SS MS_UNBINDABLE example
455One of the primary purposes of unbindable mounts is to avoid
8c9a8274
MK
456the "mount explosion" problem when repeatedly performing bind mounts
457of a higher-level subtree at a lower-level mount.
98c28960 458The problem is illustrated by the following shell session.
a721e8b2 459.PP
8c9a8274 460Suppose we have a system with the following mounts:
a721e8b2 461.PP
98c28960 462.in +4n
b8302363 463.EX
98c28960
MK
464# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
465/dev/sda1 on /
466/dev/sdb6 on /mntX
467/dev/sdb7 on /mntY
b8302363 468.EE
e646a1ba 469.in
a721e8b2 470.PP
98c28960
MK
471Suppose furthermore that we wish to recursively bind mount
472the root directory under several users' home directories.
8c9a8274 473We do this for the first user, and inspect the mounts:
a721e8b2 474.PP
98c28960 475.in +4n
b8302363 476.EX
98c28960
MK
477# \fBmount \-\-rbind / /home/cecilia/\fP
478# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
479/dev/sda1 on /
480/dev/sdb6 on /mntX
481/dev/sdb7 on /mntY
482/dev/sda1 on /home/cecilia
483/dev/sdb6 on /home/cecilia/mntX
484/dev/sdb7 on /home/cecilia/mntY
b8302363 485.EE
e646a1ba 486.in
a721e8b2 487.PP
98c28960
MK
488When we repeat this operation for the second user,
489we start to see the explosion problem:
a721e8b2 490.PP
98c28960 491.in +4n
b8302363 492.EX
98c28960
MK
493# \fBmount \-\-rbind / /home/henry\fP
494# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
495/dev/sda1 on /
496/dev/sdb6 on /mntX
497/dev/sdb7 on /mntY
498/dev/sda1 on /home/cecilia
499/dev/sdb6 on /home/cecilia/mntX
500/dev/sdb7 on /home/cecilia/mntY
501/dev/sda1 on /home/henry
502/dev/sdb6 on /home/henry/mntX
503/dev/sdb7 on /home/henry/mntY
504/dev/sda1 on /home/henry/home/cecilia
505/dev/sdb6 on /home/henry/home/cecilia/mntX
506/dev/sdb7 on /home/henry/home/cecilia/mntY
b8302363 507.EE
e646a1ba 508.in
a721e8b2 509.PP
98c28960
MK
510Under
511.IR /home/henry ,
512we have not only recursively added the
1ae6b2c7 513.I /mntX
98c28960 514and
1ae6b2c7 515.I /mntY
98c28960 516mounts, but also the recursive mounts of those directories under
1ae6b2c7 517.I /home/cecilia
98c28960
MK
518that were created in the previous step.
519Upon repeating the step for a third user,
520it becomes obvious that the explosion is exponential in nature:
a721e8b2 521.PP
98c28960 522.in +4n
b8302363 523.EX
98c28960
MK
524# \fBmount \-\-rbind / /home/otto\fP
525# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
526/dev/sda1 on /
527/dev/sdb6 on /mntX
528/dev/sdb7 on /mntY
529/dev/sda1 on /home/cecilia
530/dev/sdb6 on /home/cecilia/mntX
531/dev/sdb7 on /home/cecilia/mntY
532/dev/sda1 on /home/henry
533/dev/sdb6 on /home/henry/mntX
534/dev/sdb7 on /home/henry/mntY
535/dev/sda1 on /home/henry/home/cecilia
536/dev/sdb6 on /home/henry/home/cecilia/mntX
537/dev/sdb7 on /home/henry/home/cecilia/mntY
538/dev/sda1 on /home/otto
539/dev/sdb6 on /home/otto/mntX
540/dev/sdb7 on /home/otto/mntY
541/dev/sda1 on /home/otto/home/cecilia
542/dev/sdb6 on /home/otto/home/cecilia/mntX
543/dev/sdb7 on /home/otto/home/cecilia/mntY
544/dev/sda1 on /home/otto/home/henry
545/dev/sdb6 on /home/otto/home/henry/mntX
546/dev/sdb7 on /home/otto/home/henry/mntY
547/dev/sda1 on /home/otto/home/henry/home/cecilia
548/dev/sdb6 on /home/otto/home/henry/home/cecilia/mntX
549/dev/sdb7 on /home/otto/home/henry/home/cecilia/mntY
b8302363 550.EE
e646a1ba 551.in
a721e8b2 552.PP
98c28960
MK
553The mount explosion problem in the above scenario can be avoided
554by making each of the new mounts unbindable.
555The effect of doing this is that recursive mounts of the root
556directory will not replicate the unbindable mounts.
557We make such a mount for the first user:
a721e8b2 558.PP
98c28960 559.in +4n
b8302363 560.EX
98c28960 561# \fBmount \-\-rbind \-\-make\-unbindable / /home/cecilia\fP
b8302363 562.EE
e646a1ba 563.in
a721e8b2 564.PP
98c28960 565Before going further, we show that unbindable mounts are indeed unbindable:
a721e8b2 566.PP
98c28960 567.in +4n
b8302363 568.EX
98c28960
MK
569# \fBmkdir /mntZ\fP
570# \fBmount \-\-bind /home/cecilia /mntZ\fP
571mount: wrong fs type, bad option, bad superblock on /home/cecilia,
572 missing codepage or helper program, or other error
573
574 In some cases useful info is found in syslog \- try
575 dmesg | tail or so.
b8302363 576.EE
e646a1ba 577.in
a721e8b2 578.PP
98c28960 579Now we create unbindable recursive bind mounts for the other two users:
a721e8b2 580.PP
98c28960 581.in +4n
b8302363 582.EX
98c28960
MK
583# \fBmount \-\-rbind \-\-make\-unbindable / /home/henry\fP
584# \fBmount \-\-rbind \-\-make\-unbindable / /home/otto\fP
b8302363 585.EE
e646a1ba 586.in
a721e8b2 587.PP
8c9a8274
MK
588Upon examining the list of mounts,
589we see there has been no explosion of mounts,
98c28960
MK
590because the unbindable mounts were not replicated
591under each user's directory:
a721e8b2 592.PP
98c28960 593.in +4n
b8302363 594.EX
98c28960
MK
595# \fBmount | awk \(aq{print $1, $2, $3}\(aq\fP
596/dev/sda1 on /
597/dev/sdb6 on /mntX
598/dev/sdb7 on /mntY
599/dev/sda1 on /home/cecilia
600/dev/sdb6 on /home/cecilia/mntX
601/dev/sdb7 on /home/cecilia/mntY
602/dev/sda1 on /home/henry
603/dev/sdb6 on /home/henry/mntX
604/dev/sdb7 on /home/henry/mntY
605/dev/sda1 on /home/otto
606/dev/sdb6 on /home/otto/mntX
607/dev/sdb7 on /home/otto/mntY
b8302363 608.EE
e646a1ba 609.in
98c28960
MK
610.\"
611.SS Propagation type transitions
612The following table shows the effect that applying a new propagation type
613(i.e.,
1ae6b2c7 614.IR mount\~\-\-make\-xxxx )
8c9a8274 615has on the existing propagation type of a mount.
98c28960
MK
616The rows correspond to existing propagation types,
617and the columns are the new propagation settings.
618For reasons of space, "private" is abbreviated as "priv" and
619"unbindable" as "unbind".
620.TS
621lb2 lb2 lb2 lb2 lb1
4cfdaa53 622lb | l l l l l.
98c28960 623 make-shared make-slave make-priv make-unbind
4cfdaa53 624_
98c28960
MK
625shared shared slave/priv [1] priv unbind
626slave slave+shared slave [2] priv unbind
627slave+shared slave+shared slave priv unbind
628private shared priv [2] priv unbind
629unbindable shared unbind [2] priv unbind
630.TE
a721e8b2 631.sp 1
98c28960
MK
632Note the following details to the table:
633.IP [1] 4
634If a shared mount is the only mount in its peer group,
635making it a slave automatically makes it private.
636.IP [2]
637Slaving a nonshared mount has no effect on the mount.
638.\"
639.SS Bind (MS_BIND) semantics
640Suppose that the following command is performed:
a721e8b2 641.PP
fd6307c4
MK
642.in +4n
643.EX
644mount \-\-bind A/a B/b
645.EE
646.in
a721e8b2 647.PP
98c28960
MK
648Here,
649.I A
8c9a8274 650is the source mount,
98c28960 651.I B
8c9a8274 652is the destination mount,
98c28960
MK
653.I a
654is a subdirectory path under the mount point
655.IR A ,
656and
657.I b
658is a subdirectory path under the mount point
659.IR B .
660The propagation type of the resulting mount,
661.IR B/b ,
8c9a8274 662depends on the propagation types of the mounts
1ae6b2c7 663.I A
98c28960
MK
664and
665.IR B ,
666and is summarized in the following table.
a721e8b2 667.PP
98c28960
MK
668.TS
669lb2 lb1 lb2 lb2 lb2 lb0
670lb2 lb1 lb2 lb2 lb2 lb0
4cfdaa53 671lb lb | l l l l l.
98c28960
MK
672 source(A)
673 shared private slave unbind
674_
4cfdaa53
MK
675dest(B) shared shared shared slave+shared invalid
676 nonshared shared private slave invalid
98c28960 677.TE
a721e8b2 678.sp 1
98c28960
MK
679Note that a recursive bind of a subtree follows the same semantics
680as for a bind operation on each mount in the subtree.
681(Unbindable mounts are automatically pruned at the target mount point.)
a721e8b2 682.PP
98c28960 683For further details, see
77a4c232 684.I Documentation/filesystems/sharedsubtree.rst
98c28960
MK
685in the kernel source tree.
686.\"
687.SS Move (MS_MOVE) semantics
688Suppose that the following command is performed:
a721e8b2 689.PP
fd6307c4
MK
690.in +4n
691.EX
692mount \-\-move A B/b
693.EE
694.in
a721e8b2 695.PP
98c28960
MK
696Here,
697.I A
8c9a8274 698is the source mount,
98c28960 699.I B
8c9a8274 700is the destination mount, and
98c28960
MK
701.I b
702is a subdirectory path under the mount point
703.IR B .
704The propagation type of the resulting mount,
705.IR B/b ,
8c9a8274 706depends on the propagation types of the mounts
1ae6b2c7 707.I A
98c28960
MK
708and
709.IR B ,
710and is summarized in the following table.
a721e8b2 711.PP
98c28960
MK
712.TS
713lb2 lb1 lb2 lb2 lb2 lb0
714lb2 lb1 lb2 lb2 lb2 lb0
4cfdaa53 715lb lb | l l l l l.
98c28960
MK
716 source(A)
717 shared private slave unbind
718_
4cfdaa53
MK
719dest(B) shared shared shared slave+shared invalid
720 nonshared shared private slave unbindable
98c28960 721.TE
a721e8b2 722.sp 1
98c28960 723Note: moving a mount that resides under a shared mount is invalid.
a721e8b2 724.PP
98c28960 725For further details, see
77a4c232 726.I Documentation/filesystems/sharedsubtree.rst
98c28960
MK
727in the kernel source tree.
728.\"
729.SS Mount semantics
8c9a8274 730Suppose that we use the following command to create a mount:
a721e8b2 731.PP
fd6307c4
MK
732.in +4n
733.EX
734mount device B/b
735.EE
736.in
a721e8b2 737.PP
a66648bb
MK
738Here,
739.I B
8c9a8274 740is the destination mount, and
a66648bb
MK
741.I b
742is a subdirectory path under the mount point
743.IR B .
744The propagation type of the resulting mount,
745.IR B/b ,
746follows the same rules as for a bind mount,
747where the propagation type of the source mount
748is considered always to be private.
749.\"
750.SS Unmount semantics
8c9a8274 751Suppose that we use the following command to tear down a mount:
a66648bb
MK
752.PP
753.in +4n
754.EX
755unmount A
756.EE
757.in
758.PP
759Here,
760.I A
8c9a8274 761is a mount on
a66648bb
MK
762.IR B/b ,
763where
764.I B
765is the parent mount and
766.I b
767is a subdirectory path under the mount point
768.IR B .
769If
770.B B
771is shared, then all most-recently-mounted mounts at
772.I b
773on mounts that receive propagation from mount
774.I B
775and do not have submounts under them are unmounted.
776.\"
1ae6b2c7 777.SS The /proc/ pid /mountinfo "propagate_from" tag
a66648bb
MK
778The
779.I propagate_from:X
780tag is shown in the optional fields of a
1ae6b2c7 781.IR /proc/ pid /mountinfo
a66648bb
MK
782record in cases where a process can't see a slave's immediate master
783(i.e., the pathname of the master is not reachable from
784the filesystem root directory)
785and so cannot determine the
786chain of propagation between the mounts it can see.
787.PP
788In the following example, we first create a two-link master-slave chain
789between the mounts
790.IR /mnt ,
791.IR /tmp/etc ,
792and
793.IR /mnt/tmp/etc .
794Then the
795.BR chroot (1)
796command is used to make the
1ae6b2c7 797.I /tmp/etc
a66648bb
MK
798mount point unreachable from the root directory,
799creating a situation where the master of
1ae6b2c7 800.I /mnt/tmp/etc
a66648bb
MK
801is not reachable from the (new) root directory of the process.
802.PP
803First, we bind mount the root directory onto
1ae6b2c7 804.I /mnt
a66648bb 805and then bind mount
1ae6b2c7 806.I /proc
a66648bb 807at
1ae6b2c7 808.I /mnt/proc
a66648bb
MK
809so that after the later
810.BR chroot (1)
811the
812.BR proc (5)
813filesystem remains visible at the correct location
814in the chroot-ed environment.
815.PP
816.in +4n
817.EX
818# \fBmkdir \-p /mnt/proc\fP
819# \fBmount \-\-bind / /mnt\fP
820# \fBmount \-\-bind /proc /mnt/proc\fP
821.EE
822.in
823.PP
824Next, we ensure that the
1ae6b2c7 825.I /mnt
a66648bb
MK
826mount is a shared mount in a new peer group (with no peers):
827.PP
828.in +4n
829.EX
830# \fBmount \-\-make\-private /mnt\fP # Isolate from any previous peer group
831# \fBmount \-\-make\-shared /mnt\fP
832# \fBcat /proc/self/mountinfo | grep \(aq/mnt\(aq | sed \(aqs/ \- .*//\(aq\fP
833239 61 8:2 / /mnt ... shared:102
834248 239 0:4 / /mnt/proc ... shared:5
835.EE
836.in
837.PP
838Next, we bind mount
1ae6b2c7 839.I /mnt/etc
a66648bb
MK
840onto
841.IR /tmp/etc :
842.PP
843.in +4n
844.EX
845# \fBmkdir \-p /tmp/etc\fP
846# \fBmount \-\-bind /mnt/etc /tmp/etc\fP
847# \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
848239 61 8:2 / /mnt ... shared:102
849248 239 0:4 / /mnt/proc ... shared:5
850267 40 8:2 /etc /tmp/etc ... shared:102
851.EE
852.in
853.PP
8c9a8274 854Initially, these two mounts are in the same peer group,
a66648bb 855but we then make the
1ae6b2c7 856.I /tmp/etc
a66648bb
MK
857a slave of
858.IR /mnt/etc ,
859and then make
1ae6b2c7 860.I /tmp/etc
a66648bb
MK
861shared as well,
862so that it can propagate events to the next slave in the chain:
863.PP
864.in +4n
865.EX
866# \fBmount \-\-make\-slave /tmp/etc\fP
867# \fBmount \-\-make\-shared /tmp/etc\fP
868# \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
869239 61 8:2 / /mnt ... shared:102
870248 239 0:4 / /mnt/proc ... shared:5
871267 40 8:2 /etc /tmp/etc ... shared:105 master:102
872.EE
873.in
874.PP
875Then we bind mount
1ae6b2c7 876.I /tmp/etc
a66648bb
MK
877onto
878.IR /mnt/tmp/etc .
8c9a8274 879Again, the two mounts are initially in the same peer group,
a66648bb 880but we then make
1ae6b2c7 881.I /mnt/tmp/etc
a66648bb
MK
882a slave of
883.IR /tmp/etc :
884.PP
885.in +4n
886.EX
887# \fBmkdir \-p /mnt/tmp/etc\fP
888# \fBmount \-\-bind /tmp/etc /mnt/tmp/etc\fP
889# \fBmount \-\-make\-slave /mnt/tmp/etc\fP
890# \fBcat /proc/self/mountinfo | egrep \(aq/mnt|/tmp/\(aq | sed \(aqs/ \- .*//\(aq\fP
891239 61 8:2 / /mnt ... shared:102
892248 239 0:4 / /mnt/proc ... shared:5
893267 40 8:2 /etc /tmp/etc ... shared:105 master:102
894273 239 8:2 /etc /mnt/tmp/etc ... master:105
895.EE
896.in
897.PP
898From the above, we see that
1ae6b2c7 899.I /mnt
a66648bb
MK
900is the master of the slave
901.IR /tmp/etc ,
902which in turn is the master of the slave
903.IR /mnt/tmp/etc .
904.PP
905We then
906.BR chroot (1)
907to the
1ae6b2c7 908.I /mnt
a66648bb
MK
909directory, which renders the mount with ID 267 unreachable
910from the (new) root directory:
911.PP
912.in +4n
913.EX
914# \fBchroot /mnt\fP
915.EE
916.in
917.PP
918When we examine the state of the mounts inside the chroot-ed environment,
919we see the following:
920.PP
921.in +4n
922.EX
923# \fBcat /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
924239 61 8:2 / / ... shared:102
925248 239 0:4 / /proc ... shared:5
926273 239 8:2 /etc /tmp/etc ... master:105 propagate_from:102
927.EE
928.in
929.PP
930Above, we see that the mount with ID 273
931is a slave whose master is the peer group 105.
932The mount point for that master is unreachable, and so a
1ae6b2c7 933.I propagate_from
a66648bb
MK
934tag is displayed, indicating that the closest dominant peer group
935(i.e., the nearest reachable mount in the slave chain)
936is the peer group with the ID 102 (corresponding to the
1ae6b2c7 937.I /mnt
a66648bb
MK
938mount point before the
939.BR chroot (1)
940was performed.
941.\"
942.SH VERSIONS
943Mount namespaces first appeared in Linux 2.4.19.
944.SH CONFORMING TO
945Namespaces are a Linux-specific feature.
946.\"
947.SH NOTES
8c9a8274 948The propagation type assigned to a new mount depends
a66648bb 949on the propagation type of the parent mount.
8c9a8274 950If the mount has a parent (i.e., it is a non-root mount
a66648bb
MK
951point) and the propagation type of the parent is
952.BR MS_SHARED ,
953then the propagation type of the new mount is also
954.BR MS_SHARED .
955Otherwise, the propagation type of the new mount is
956.BR MS_PRIVATE .
957.PP
958Notwithstanding the fact that the default propagation type
8c9a8274 959for new mount is in many cases
a66648bb 960.BR MS_PRIVATE ,
1ae6b2c7 961.B MS_SHARED
a66648bb
MK
962is typically more useful.
963For this reason,
964.BR systemd (1)
8c9a8274 965automatically remounts all mounts as
1ae6b2c7 966.B MS_SHARED
a66648bb
MK
967on system startup.
968Thus, on most modern systems, the default propagation type is in practice
969.BR MS_SHARED .
970.PP
971Since, when one uses
972.BR unshare (1)
973to create a mount namespace,
8c9a8274 974the goal is commonly to provide full isolation of the mounts
a66648bb
MK
975in the new namespace,
976.BR unshare (1)
977(since
1ae6b2c7 978.I util\-linux
a66648bb
MK
979version 2.27) in turn reverses the step performed by
980.BR systemd (1),
8c9a8274 981by making all mounts private in the new namespace.
a66648bb
MK
982That is,
983.BR unshare (1)
984performs the equivalent of the following in the new mount namespace:
a721e8b2 985.PP
fd6307c4
MK
986.in +4n
987.EX
a66648bb 988mount \-\-make\-rprivate /
fd6307c4
MK
989.EE
990.in
a721e8b2 991.PP
a66648bb 992To prevent this, one can use the
1ae6b2c7 993.I \-\-propagation\~unchanged
a66648bb
MK
994option to
995.BR unshare (1).
a721e8b2 996.PP
a66648bb
MK
997An application that creates a new mount namespace directly using
998.BR clone (2)
999or
1000.BR unshare (2)
1001may desire to prevent propagation of mount events to other mount namespaces
1002(as is done by
1003.BR unshare (1)).
1004This can be done by changing the propagation type of
8c9a8274 1005mounts in the new namespace to either
a66648bb
MK
1006.B MS_SLAVE
1007or
1008.BR MS_PRIVATE ,
1009using a call such as the following:
a721e8b2 1010.PP
e2109196 1011.in +4n
b8302363 1012.EX
a66648bb 1013mount(NULL, "/", MS_SLAVE | MS_REC, NULL);
b8302363 1014.EE
e646a1ba 1015.in
a721e8b2 1016.PP
a66648bb
MK
1017For a discussion of propagation types when moving mounts
1018.RB ( MS_MOVE )
1019and creating bind mounts
1020.RB ( MS_BIND ),
1021see
77a4c232 1022.IR Documentation/filesystems/sharedsubtree.rst .
a66648bb
MK
1023.\"
1024.\" ============================================================
1025.\"
1026.SS Restrictions on mount namespaces
1027Note the following points with respect to mount namespaces:
ababc346 1028.IP [1] 4
a66648bb
MK
1029Each mount namespace has an owner user namespace.
1030As explained above, when a new mount namespace is created,
8c9a8274 1031its mount list is initialized as a copy of the mount list
a66648bb 1032of another mount namespace.
8c9a8274 1033If the new namespace and the namespace from which the mount list
a66648bb
MK
1034was copied are owned by different user namespaces,
1035then the new mount namespace is considered
1036.IR "less privileged" .
ababc346 1037.IP [2]
a66648bb
MK
1038When creating a less privileged mount namespace,
1039shared mounts are reduced to slave mounts.
1040This ensures that mappings performed in less
1041privileged mount namespaces will not propagate to more privileged
1042mount namespaces.
ababc346 1043.IP [3]
a66648bb
MK
1044Mounts that come as a single unit from a more privileged mount namespace are
1045locked together and may not be separated in a less privileged mount
1046namespace.
1047(The
1048.BR unshare (2)
1049.B CLONE_NEWNS
1050operation brings across all of the mounts from the original
1051mount namespace as a single unit,
1052and recursive mounts that propagate between
1053mount namespaces propagate as a single unit.)
1054.IP
1055In this context, "may not be separated" means that the mounts
1056are locked so that they may not be individually unmounted.
1057Consider the following example:
1058.IP
1059.RS
e2109196 1060.in +4n
b8302363 1061.EX
906ab494
MK
1062$ \fBsudo sh\fP
1063# \fBmount \-\-bind /dev/null /etc/shadow\fP
1064# \fBcat /etc/shadow\fP # Produces no output
b8302363 1065.EE
e646a1ba 1066.in
a66648bb
MK
1067.RE
1068.IP
aa62e72d 1069The above steps, performed in a more privileged mount namespace,
2433a20c 1070have created a bind mount that
906ab494
MK
1071obscures the contents of the shadow password file,
1072.IR /etc/shadow .
a66648bb 1073For security reasons, it should not be possible to unmount
aa62e72d 1074that mount in a less privileged mount namespace,
906ab494
MK
1075since that would reveal the contents of
1076.IR /etc/shadow .
a66648bb
MK
1077.IP
1078Suppose we now create a new mount namespace
2433a20c 1079owned by a new user namespace.
a66648bb
MK
1080The new mount namespace will inherit copies of all of the mounts
1081from the previous mount namespace.
1082However, those mounts will be locked because the new mount namespace
2433a20c
MK
1083is less privileged.
1084Consequently, an attempt to unmount the mount fails as show
1085in the following step:
a66648bb
MK
1086.IP
1087.RS
e2109196 1088.in +4n
b8302363 1089.EX
906ab494 1090# \fBunshare \-\-user \-\-map\-root\-user \-\-mount \e\fP
a66648bb
MK
1091 \fBstrace \-o /tmp/log \e\fP
1092 \fBumount /mnt/dir\fP
906ab494
MK
1093umount: /etc/shadow: not mounted.
1094# \fBgrep \(aq^umount\(aq /tmp/log\fP
1095umount2("/etc/shadow", 0) = \-1 EINVAL (Invalid argument)
b8302363 1096.EE
e646a1ba 1097.in
a66648bb
MK
1098.RE
1099.IP
1100The error message from
1101.BR mount (8)
1102is a little confusing, but the
1103.BR strace (1)
1104output reveals that the underlying
1105.BR umount2 (2)
1106system call failed with the error
1107.BR EINVAL ,
1108which is the error that the kernel returns to indicate that
1109the mount is locked.
ebc82e00
MK
1110.IP
1111Note, however, that it is possible to stack (and unstack) a
1112mount on top of one of the inherited locked mounts in a
1113less privileged mount namespace:
1114.IP
1115.in +4n
1116.EX
906ab494
MK
1117# \fBecho \(aqaaaaa\(aq > /tmp/a\fP # File to mount onto /etc/shadow
1118# \fBunshare \-\-user \-\-map\-root\-user \-\-mount \e\fP
1119 \fBsh \-c \(aqmount \-\-bind /tmp/a /etc/shadow; cat /etc/shadow\(aq\fP
1120aaaaa
1121# \fBumount /etc/shadow\fP
ebc82e00
MK
1122.EE
1123.in
906ab494
MK
1124.IP
1125The final
1126.BR umount (8)
1127command above, which is performed in the initial mount namespace,
1128makes the original
1129.I /etc/shadow
1130file once more visible in that namespace.
ababc346
MK
1131.IP [4]
1132Following on from point [3],
f6aaf493 1133note that it is possible to unmount an entire subtree of mounts that
aa62e72d 1134propagated as a unit into a less privileged mount namespace,
a66648bb
MK
1135as illustrated in the following example.
1136.IP
1137First, we create new user and mount namespaces using
1138.BR unshare (1).
1139In the new mount namespace,
1140the propagation type of all mounts is set to private.
1141We then create a shared bind mount at
1142.IR /mnt ,
8c9a8274 1143and a small hierarchy of mounts underneath that mount.
a66648bb 1144.IP
e2109196 1145.in +4n
b8302363 1146.EX
a66648bb
MK
1147$ \fBPS1=\(aqns1# \(aq sudo unshare \-\-user \-\-map\-root\-user \e\fP
1148 \fB\-\-mount \-\-propagation private bash\fP
1149ns1# \fBecho $$\fP # We need the PID of this shell later
1150778501
1151ns1# \fBmount \-\-make\-shared \-\-bind /mnt /mnt\fP
1152ns1# \fBmkdir /mnt/x\fP
1153ns1# \fBmount \-\-make\-private \-t tmpfs none /mnt/x\fP
1154ns1# \fBmkdir /mnt/x/y\fP
1155ns1# \fBmount \-\-make\-private \-t tmpfs none /mnt/x/y\fP
1156ns1# \fBgrep /mnt /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
1157986 83 8:5 /mnt /mnt rw,relatime shared:344
1158989 986 0:56 / /mnt/x rw,relatime
1159990 989 0:57 / /mnt/x/y rw,relatime
b8302363 1160.EE
e646a1ba 1161.in
a66648bb
MK
1162.IP
1163Continuing in the same shell session,
aa62e72d
MK
1164we then create a second shell in a new user namespace and a new
1165(less privileged) mount namespace and
8c9a8274 1166check the state of the propagated mounts rooted at
a66648bb
MK
1167.IR /mnt .
1168.IP
e2109196 1169.in +4n
b8302363 1170.EX
2433a20c 1171ns1# \fBPS1=\(aqns2# \(aq unshare \-\-user \-\-map\-root\-user \e\fP
a66648bb
MK
1172 \fB\-\-mount \-\-propagation unchanged bash\fP
1173ns2# \fBgrep /mnt /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
11741239 1204 8:5 /mnt /mnt rw,relatime master:344
11751240 1239 0:56 / /mnt/x rw,relatime
11761241 1240 0:57 / /mnt/x/y rw,relatime
e646a1ba 1177.EE
e2109196 1178.in
a66648bb 1179.IP
8c9a8274 1180Of note in the above output is that the propagation type of the mount
a66648bb 1181.I /mnt
ababc346 1182has been reduced to slave, as explained in point [2].
a66648bb
MK
1183This means that submount events will propagate from the master
1184.I /mnt
1185in "ns1", but propagation will not occur in the opposite direction.
1186.IP
1187From a separate terminal window, we then use
1188.BR nsenter (1)
1189to enter the mount and user namespaces corresponding to "ns1".
1190In that terminal window, we then recursively bind mount
1ae6b2c7 1191.I /mnt/x
a66648bb
MK
1192at the location
1193.IR /mnt/ppp .
1194.IP
e2109196 1195.in +4n
b8302363 1196.EX
a66648bb
MK
1197$ \fBPS1=\(aqns3# \(aq sudo nsenter \-t 778501 \-\-user \-\-mount\fP
1198ns3# \fBmount \-\-rbind \-\-make\-private /mnt/x /mnt/ppp\fP
1199ns3# \fBgrep /mnt /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
1200986 83 8:5 /mnt /mnt rw,relatime shared:344
1201989 986 0:56 / /mnt/x rw,relatime
1202990 989 0:57 / /mnt/x/y rw,relatime
12031242 986 0:56 / /mnt/ppp rw,relatime
12041243 1242 0:57 / /mnt/ppp/y rw,relatime shared:518
b8302363 1205.EE
e646a1ba 1206.in
a66648bb
MK
1207.IP
1208Because the propagation type of the parent mount,
1209.IR /mnt ,
f6aaf493 1210was shared, the recursive bind mount propagated a small subtree of
a66648bb
MK
1211mounts under the slave mount
1212.I /mnt
1213into "ns2",
1214as can be verified by executing the following command in that shell session:
1215.IP
e2109196 1216.in +4n
b8302363 1217.EX
a66648bb
MK
1218ns2# \fBgrep /mnt /proc/self/mountinfo | sed \(aqs/ \- .*//\(aq\fP
12191239 1204 8:5 /mnt /mnt rw,relatime master:344
12201240 1239 0:56 / /mnt/x rw,relatime
12211241 1240 0:57 / /mnt/x/y rw,relatime
12221244 1239 0:56 / /mnt/ppp rw,relatime
12231245 1244 0:57 / /mnt/ppp/y rw,relatime master:518
b8302363 1224.EE
e646a1ba 1225.in
a66648bb 1226.IP
2433a20c 1227While it is not possible to unmount a part of the propagated subtree
5aea19ed
MK
1228.RI ( /mnt/ppp/y )
1229in "ns2",
f6aaf493 1230it is possible to unmount the entire subtree,
a66648bb
MK
1231as shown by the following commands:
1232.IP
fd6307c4
MK
1233.in +4n
1234.EX
a66648bb
MK
1235ns2# \fBumount /mnt/ppp/y\fP
1236umount: /mnt/ppp/y: not mounted.
1237ns2# \fBumount \-l /mnt/ppp | sed \(aqs/ \- .*//\(aq\fP # Succeeds...
1238ns2# \fBgrep /mnt /proc/self/mountinfo\fP
12391239 1204 8:5 /mnt /mnt rw,relatime master:344
12401240 1239 0:56 / /mnt/x rw,relatime
12411241 1240 0:57 / /mnt/x/y rw,relatime
fd6307c4
MK
1242.EE
1243.in
ababc346 1244.IP [5]
a66648bb
MK
1245The
1246.BR mount (2)
1247flags
1248.BR MS_RDONLY ,
1249.BR MS_NOSUID ,
1250.BR MS_NOEXEC ,
1251and the "atime" flags
1252.RB ( MS_NOATIME ,
1253.BR MS_NODIRATIME ,
1254.BR MS_RELATIME )
1255settings become locked
1256.\" commit 9566d6742852c527bf5af38af5cbb878dad75705
1257.\" Author: Eric W. Biederman <ebiederm@xmission.com>
1258.\" Date: Mon Jul 28 17:26:07 2014 -0700
1259.\"
1260.\" mnt: Correct permission checks in do_remount
1261.\"
1262when propagated from a more privileged to
1263a less privileged mount namespace,
1264and may not be changed in the less privileged mount namespace.
1265.IP
2433a20c
MK
1266This point is illustrated in the following example where,
1267in a more privileged mount namespace,
1268we create a bind mount that is marked as read-only.
a66648bb
MK
1269For security reasons,
1270it should not be possible to make the mount writable in
2433a20c 1271a less privileged mount namespace, and indeed the kernel prevents this:
a66648bb
MK
1272.IP
1273.RS
a2fc45a9
MK
1274.in +4n
1275.EX
a66648bb 1276$ \fBsudo mkdir /mnt/dir\fP
a66648bb
MK
1277$ \fBsudo mount \-\-bind \-o ro /some/path /mnt/dir\fP
1278$ \fBsudo unshare \-\-user \-\-map\-root\-user \-\-mount \e\fP
1279 \fBmount \-o remount,rw /mnt/dir\fP
1280mount: /mnt/dir: permission denied.
a2fc45a9
MK
1281.EE
1282.in
a66648bb 1283.RE
ababc346 1284.IP [6]
a66648bb
MK
1285.\" (As of 3.18-rc1 (in Al Viro's 2014-08-30 vfs.git#for-next tree))
1286A file or directory that is a mount point in one namespace that is not
1287a mount point in another namespace, may be renamed, unlinked, or removed
1288.RB ( rmdir (2))
1289in the mount namespace in which it is not a mount point
1290(subject to the usual permission checks).
1291Consequently, the mount point is removed in the mount namespace
1292where it was a mount point.
1293.IP
1294Previously (before Linux 3.18),
1295.\" mtk: The change was in Linux 3.18, I think, with this commit:
1296.\" commit 8ed936b5671bfb33d89bc60bdcc7cf0470ba52fe
1297.\" Author: Eric W. Biederman <ebiederman@twitter.com>
1298.\" Date: Tue Oct 1 18:33:48 2013 -0700
1299.\"
1300.\" vfs: Lazily remove mounts on unlinked files and directories.
1301attempting to unlink, rename, or remove a file or directory
1302that was a mount point in another mount namespace would result in the error
1303.BR EBUSY .
1304That behavior had technical problems of enforcement (e.g., for NFS)
1305and permitted denial-of-service attacks against more privileged users
1306(i.e., preventing individual files from being updated
1307by bind mounting on top of them).
a14af333 1308.SH EXAMPLES
43d438e2
MK
1309See
1310.BR pivot_root (2).
98c28960
MK
1311.SH SEE ALSO
1312.BR unshare (1),
1313.BR clone (2),
1314.BR mount (2),
4d7a6485 1315.BR mount_setattr (2),
e70abf48 1316.BR pivot_root (2),
98c28960
MK
1317.BR setns (2),
1318.BR umount (2),
1319.BR unshare (2),
1320.BR proc (5),
466247eb 1321.BR namespaces (7),
93f5b0f8 1322.BR user_namespaces (7),
e70abf48 1323.BR findmnt (8),
e9832dc0 1324.BR mount (8),
88b0e0e0 1325.BR pam_namespace (8),
e9832dc0
MK
1326.BR pivot_root (8),
1327.BR umount (8)
a721e8b2 1328.PP
1ae6b2c7 1329.I Documentation/filesystems/sharedsubtree.rst
98c28960 1330in the kernel source tree.