标签:ati 实现 $$ 方式 共享 挂载点 隔离 share dock
mount namespace 通过隔离文件系统挂载点对隔离文件系统提供支持。使用 unshare 构造 mount namespace 如下:
root@chunqiu:~/chunqiu/docker/mount# dd if=/dev/zero bs=1M count=32 of=./disk1.img
root@chunqiu:~/chunqiu/docker/mount# dd if=/dev/zero bs=1M count=32 of=./disk2.img
root@chunqiu:~/chunqiu/docker/mount# mkfs.ext4 ./disk1.img
root@chunqiu:~/chunqiu/docker/mount# mkfs.ext4 ./disk2.img
root@chunqiu:~/chunqiu/docker/mount# ls
disk1 disk1.img disk2 disk2.img
root@chunqiu:~/chunqiu/docker/mount# mount disk1.img disk1
root@chunqiu:~/chunqiu/docker/mount# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/loop0 ext4 27M 395K 25M 2% /root/chunqiu/docker/mount/disk1
root@chunqiu:~/chunqiu/docker/mount# readlink /proc/$$/ns/mnt
mnt:[4026531840]
开启另一个 shell 窗口,并创建 mount namespace:
root@chunqiu:~# unshare --mount /bin/bash
root@chunqiu:~# readlink /proc/$$/ns/mnt
mnt:[4026532206]
root@chunqiu:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/loop0 ext4 27M 395K 25M 2% /root/chunqiu/docker/mount/disk1
root@chunqiu:~/chunqiu/docker/mount# mount disk2.img disk2
root@chunqiu:~/chunqiu/docker/mount# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/loop0 ext4 27M 395K 25M 2% /root/chunqiu/docker/mount/disk1
/dev/loop1 ext4 27M 395K 25M 2% /root/chunqiu/docker/mount/disk2
root@chunqiu:~/chunqiu/docker/mount# touch disk1/demo; touch disk2/demo
回到原 mount namespace:
root@chunqiu:~/chunqiu/docker/mount# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/loop0 ext4 27M 395K 25M 2% /root/chunqiu/docker/mount/disk1
root@chunqiu:~/chunqiu/docker/mount# ls disk1
demo
root@chunqiu:~/chunqiu/docker/mount# ls disk2
可以看到,通过 mount namespace 实现了文件系统的隔离:
上例中介绍了默认情况下所有挂载状态是私有的。那么,如果父 mount namespace 挂载了一张硬盘,在子 mount namespace 是不可见的,需要在子 mount namespace 中重新 mount。而挂载传播(mount propagation) 解决了这个问题。它定义了挂载对象之间的关系,系统用这些关系决定了挂载对象中的挂载事件如何传播到其它挂载对象。
挂载传播的详细信息可参阅 man mount,这里以共享和私有挂载为例查看这两种挂载方式是如何影响子 mount namespace 的:
root@chunqiu:~/chunqiu/docker/mount# ls
disk1 disk1.img disk2 disk2.img
root@chunqiu:~/chunqiu/docker/mount# mount --make-shared disk1.img ./disk1
root@chunqiu:~/chunqiu/docker/mount# mount --make-private disk2.img ./disk2
root@chunqiu:~/chunqiu/docker/mount# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/loop0 ext4 27M 383K 25M 2% /root/chunqiu/docker/mount/disk1
/dev/loop1 ext4 27M 395K 25M 2% /root/chunqiu/docker/mount/disk2
root@chunqiu:~/chunqiu/docker/mount# cat /proc/self/mountinfo |grep disk| sed ‘s/ - .*//‘
54 25 7:0 / /root/chunqiu/docker/mount/disk1 rw,relatime shared:40
55 25 7:1 / /root/chunqiu/docker/mount/disk2 rw,relatime
开启另一个 shell 窗口,并创建 mount namespace:
root@chunqiu:~# unshare --mount --propagation unchanged /bin/bash
root@chunqiu:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/loop0 ext4 27M 383K 25M 2% /root/chunqiu/docker/mount/disk1
/dev/loop1 ext4 27M 395K 25M 2% /root/chunqiu/docker/mount/disk2
root@chunqiu:~# cat /proc/self/mountinfo |grep disk| sed ‘s/ - .*//‘
/* disk1 是 shared 是因为 unshare 指明了 propagation unchanged,如果未指定,默认是私有的 */
116 63 7:0 / /root/chunqiu/docker/mount/disk1 rw,relatime shared:40
118 63 7:1 / /root/chunqiu/docker/mount/disk2 rw,relatime
在原 mount namespace 中创建“硬盘”并模仿挂载事件:
root@chunqiu:~/chunqiu/docker/mount# dd if=/dev/zero bs=1M count=32 of=./disk3.img
root@chunqiu:~/chunqiu/docker/mount# dd if=/dev/zero bs=1M count=32 of=./disk4.img
root@chunqiu:~/chunqiu/docker/mount# mkfs.ext4 ./disk3.img
root@chunqiu:~/chunqiu/docker/mount# mkfs.ext4 ./disk4.img
root@chunqiu:~/chunqiu/docker/mount# mkdir disk2/disk4
root@chunqiu:~/chunqiu/docker/mount# mkdir disk1/disk3
root@chunqiu:~/chunqiu/docker/mount# mount disk3.img disk1/disk3/
root@chunqiu:~/chunqiu/docker/mount# mount disk4.img disk2/disk4/
查看子 mount namespace 的挂载信息:
root@chunqiu:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/loop0 ext4 27M 384K 25M 2% /root/chunqiu/docker/mount/disk1
/dev/loop1 ext4 27M 396K 25M 2% /root/chunqiu/docker/mount/disk2
/dev/loop2 ext4 27M 395K 25M 2% /root/chunqiu/docker/mount/disk1/disk3
可以看到共享的挂载传播方式,文件系统内的挂载事件会映射到子 mount namespace 而私有的挂载传播方式则不会。
标签:ati 实现 $$ 方式 共享 挂载点 隔离 share dock
原文地址:https://www.cnblogs.com/xingzheanan/p/14727405.html