Shared Subtrees - The Linux Kernel documentation
findmnt -o SOURCE,TARGET,PROPAGATION
一个进程想拥有自己的mount namespace,但是仍然想访问最近刚挂载的挂载点。那么Shared Sub Tree就提供了这个功能。共享子树提供给了shared mount、slave mount、private mount、unbindable mount四种模式。
一个shared mount可以复制出多个不同的挂载点,这些挂载点内容都是相同的。例如下面这个例子:
// /mnt需要是一个挂载点
# mount --make-shared /mnt
# mount --bind /mnt /tmp
# ls /mnt
a b c
# ls /tmp
a b c
// /tmp目录下新创建的挂载点都会在/mnt目录下看到,反过来也是一样
Now let's say we mount a device at /tmp/a
# mount /dev/sd0 /tmp/a
#ls /tmp/a
t1 t2 t3
#ls /mnt/a
t1 t2 t3
除了不传递mount/unmount事件外,其他的和Shared mount一样,也就是说在其中一个挂载点下又挂载了新的挂载点是不会传递到bind的另外一个挂载点下的。
Let's say /mnt has a mount which is shared.
# mount --make-shared /mnt
Let's bind mount /mnt to /tmp
# mount --bind /mnt /tmp
the new mount at /tmp becomes a shared mount and it is a replica of
the mount at /mnt.
// 让/tmp变成slave,起对应的master则是/mnt
Now let's make the mount at /tmp; a slave of /mnt
# mount --make-slave /tmp
// 在master的mnt下挂载,产生的mount事件是会传递到/tmp目录下的
// 但是反之不是
let's mount /dev/sd0 on /mnt/a
# mount /dev/sd0 /mnt/a
#ls /mnt/a
t1 t2 t3
#ls /tmp/a
t1 t2 t3
Note the mount event has propagated to the mount at /tmp
However let's see what happens if we mount something on the mount at /tmp
// 在slave中进行mount是不会传递到master的
# mount /dev/sd1 /tmp/b
#ls /tmp/b
s1 s2 s3
#ls /mnt/b
Note how the mount event has not propagated to the mount at
/mnt
默认都是私有mount,不会进行传递,这个适用于mount namespace,当创建了一个新的mount namespace,会clone parent namespace下所有的mount,但是因为所有的mount默认都是private的,因此parent namespace中的挂载点内有新的挂载点,那么克隆的namespace是看不到。
let's say we have a mount at /mnt and we make it unbindable
# mount --make-unbindable /mnt
Let's try to bind mount this mount somewhere else.
# mount --bind /mnt /tmp
mount: wrong fs type, bad option, bad superblock on /mnt,
or too many mounted file systems
Binding a unbindable mount is a invalid operation.