What Linux bind mounts are really doing

Lots of Unixes have some form of ‘loopback‘ mounts, where you can mount a bit of an existing filesystem somewhere else; they‘re called loopback mounts by analogy with the loopback interface.

The general idea behind them is that they are a more efficient (and easier to use) version of doing an NFS mount from localhost.

Linux‘s bind mounts (so called because they are done with mount --bind, or by specifying bind as the filesystem type in /etc/fstab) look like any other sort of loopback mounting. However, they actually operate in a way quite different from the usual idea of loopback mounting, and the difference has some important consequences.

What bind mounts are really doing is more or less mounting the filesystem again with a different inode as the root inode. Thus, if you do:

mount /dev/md1 /foo
mount --bind /foo/bar /bar

what you really have is /dev/md1 mounted twice, once with the root inode of the filesystem on md1 as the root of the mount point, and once with the inode for ‘bar‘ in the root of the filesystem on md1 as the root of the mount point.

The mount command makes this hard to see by being misleading in its output, reporting things like‘/data/home on /home type none (rw,bind)‘.

Because they use /etc/mtab, which mount maintains, things like df also report like this. More of the real state of affairs is visible in /proc/mounts, where the kernel itself reports:

/dev/md5 /data ext3 rw,data=ordered 0 0
/dev/md5 /home ext3 rw,data=ordered 0 0

Unfortunately the kernel doesn‘t report that what root inode /home is mounted with, which generally makes mount‘s output more useful once you know what is really going on.

One consequence of this is that once you‘ve set up your bind mounts, you can unmount the original mount point, something which I believe is not true of things like Solaris‘s loopback mounts (and which definitely wouldn‘t be true of NFS mounts from localhost). There might be a use for this in obscure situations.

Sidebar: Deeper under the hood

Disclaimer: I am not sure I understand this correctly.

Under the hood, there are two things: actual mounts of filesystems from devices (or the network), and namespace-based views of such filesystems. Rather than create new copies of both, bind mounts create new views (‘mounts‘ or ‘vfsmounts‘) of the same underlying mounted filesystem.

This explains one limitation of bind mounts, which is that you can‘t change mount flags when you do a bind mount (so you can‘t have a bind mount that is a read-only version of part of a read-write filesystem). Currently, all mount flags are associated with the filesystem, not with the view, so all views have to have the same mount flags.

时间: 2024-10-01 22:41:22

What Linux bind mounts are really doing的相关文章

docker从零开始 存储(三)bind mounts

使用bind mounts 自Docker早期以来bind mounts 一直存在.与volumes相比,绑定挂载具有有限的功能.使用bind mounts时,主机上的文件或目录将装入容器中.文件或目录由其在主机上的完整路径或相对路径引用.相反,当您使用卷时,会在主机上的Docker存储目录中创建一个新目录,Docker会管理该目录的内容. 该文件或目录不需要已存在于Docker主机上.如果它尚不存在,则按需创建.绑定挂载非常高效,但它们依赖于具有特定目录结构的主机文件系统.如果您正在开发新的D

Bind Mounts and File System Mount Order

     When you use the bind option of the mount command, you must be sure that the file systems are mounted in the correct order. In the following example, the /var/log directory must be mounted before executing the bind mount on the /tmp directory: #

Linux bind 安装问题01

Linux bind 安装完成无法提供查询转发解析 需要将主配置文件/etc/named.conf文件中的dns安全扩展"dnssec-enable"与"dnssec-validation" 改为"no" 原文地址:http://blog.51cto.com/2937761/2323048

docker 应用数据的管理之bind mounts

创建容器使用bind mounts 挂载文件系统.宿主机文件系统会覆盖掉容器里初始数据 [root@localhost ~]# mkdir /www/htpm -pv mkdir: 已创建目录 "/www" mkdir: 已创建目录 "/www/htpm" [root@localhost ~]# docker run -d --name web-7 -p 82:80 --mount type=bind,src=/www/htpm,dst=/usr/share/ngi

docker存储bind mounts用法

docker之前磁盘挂载做持久化使用 -v 使用-v /root/jenkins_home:/var/jenkins_home 时.如果宿主机上没有这个文件/root/jenkins_home,也会自动创建. 但是如果使用mount时,宿主机中没有这个文件会报错找不到这个文件,并创建失败. docker container run -itd --name=jenkins-8088 -p 8088:8080 --mount type=bind,src=/root/jenkins_home,dst=

【Linux】一步一步学Linux——bind命令

目录文章目录 目录 命令概述 命令格式 常用选项 参考示例 附录 命令概述bind命令用于显示和设置命令行的键盘序列绑定功能.通过这一命令,可以提高命令行中操作效率.您可以利用bind命令了解有哪些按键组合与其功能,也可以自行指定要用哪些按键组合. 命令格式用法:bind [选项] [参数]1 常用选项-m 键映射 在此命令执行过程中使用指定的键映射.可以被接受的键映射名字有 emacs.emacs-standard.emacs-meta.emacs-ctlx.vi.vi-move.vi-com

Docker的数据管理(volume/bind mount/tmpfs)

Docker提供了三种不同的方式用于将宿主的数据挂载到容器中:volumes,bind mounts,tmpfs volumes.当你不知道该选择哪种方式时,记住,volumes总是正确的选择. volumes是Docker数据持久化机制.bind mounts依赖主机目录结构,volumes完全由Docker管理.Volumes有以下优点: Volumes更容易备份和移植. 可以通过Docker CLI或API进行管理 Volumes可以无区别的工作中Windows和Linux下. 多个容器共

Linux学习:rc.sysinit

下面贴出的是centos6.5 /etc/rc.sysinit文件内容 #!/bin/bash # # /etc/rc.d/rc.sysinit - run once at boot time # # Taken in part from Miquel van Smoorenburg's bcheckrc. # HOSTNAME=$(/bin/hostname) set -m if [ -f /etc/sysconfig/network ]; then . /etc/sysconfig/netw

Bind系统调用

Bind含义 一条tcp/udp 连接一般是有五元组进行标识的 {<proto>, <src addr>, <src port>, <dst addr>, <dst port>} Bind一般是在server端调用,通过bind,会把本端的地址和端口号与socket描述符进行绑定,而目的地址和端口在connect的时候才能确定:对于client端是不需要bind(但是可以bind),client只要在connect的时候指定目的地址和端口,远端的