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