ILD

bind mount 将子目录挂载到一个挂载点
作者:Yuan Jianpeng 邮箱:yuanjianpeng@xiaomi.com
发布时间:2021-5-7 站点:Inside Linux Development

可以设备或者分区,可以先将其挂载到一个挂载点。子目录可以通过bind方式挂载到另外的挂载点。

1
2
3
4
5
# mkdir tmpfs
# mount -t tmpfs none tmpfs
# mkdir tmpfs/123
# mkdir 2
# mount --bind tmpfs/123/ 2/


这样123子目录就挂载到2了。使用mount显示为:

1
2
3
$ mount
none on /work/mnt/tmpfs type tmpfs (rw,relatime)
none on /work/mnt/2 type tmpfs (rw,relatime)


查看/proc/mounts,显示为:

1
2
3
$ cat /proct/mounts
none /work/mnt/tmpfs tmpfs rw,relatime 0 0
none /work/mnt/2 tmpfs rw,relatime 0 0


以上两种方法是无法查看是子目录还是根目录挂载的,可以通过进程的挂载信息获得:

1
2
3
$ cat /proc/self/mountinfo
838 29 0:50 / /work/mnt/tmpfs rw,relatime shared:465 - tmpfs none rw
859 29 0:50 /123 /work/mnt/2 rw,relatime shared:465 - tmpfs none rw

可以看到,前者是/挂载到tmpfs,后者是/123挂载到2。


如果删除了123这个目录,那么2目录,也不能读写了:

$ touch 2/abc

touch: cannot touch '2/abc': No such file or directory


此时/proc/pid/mountinfo,显示为deleted

859 29 0:50 /123//deleted /work/mnt/2 rw,relatime shared:465 - tmpfs none rw


子目录bind挂载后,是可以umount根目录的挂载的,不影响子目录的使用:

$ sudo umount tmpfs

$ touch 2/abc

$ df

none             4075332        0   4075332   0% /work/mnt/2


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.