关于 /proc/mounts

现在的 Linux 系统里一般都有这么三个文件:/etc/fstab,/etc/mtab,和 /proc/mounts,比较容易让人迷惑。简单解释一下。

This file provides a list of all mounts in use by the system:

rootfs / rootfs rw 0 0 
/proc /proc proc rw,nodiratime 0 0 none 
/dev ramfs rw 0 0 
/dev/mapper/VolGroup00-LogVol00 / ext3 rw 0 0 
none /dev ramfs rw 0 0 
/proc /proc proc rw,nodiratime 0 0 
/sys /sys sysfs rw 0 0 
none /dev/pts devpts rw 0 0 
usbdevfs /proc/bus/usb usbdevfs rw 0 0 
/dev/hda1 /boot ext3 rw 0 0 
none /dev/shm tmpfs rw 0 0 
none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0 
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw 0 0

The output found here is similar to the contents of /etc/mtab, except that /proc/mount is more up-to-date.

The first column specifies the device that is mounted, the second column reveals the mount point, and the third column tells the file system type, and the fourth column tells you if it is mounted read-only (ro) or read-write (rw). The fifth and sixth columns are dummy values designed to match the format used in/etc/mtab.

1、/etc/fstab 是只读不写的,它仅仅是开机时的配置文件,它提供的是系统上挂载设备的静态信息,比如 mount -a 就会挂载 /etc/fstab 里面指定的文件系统。


2、/etc/mtab 是供 mount/umount 进行读写的,是相对动态的。读的话,比如你在挂载一个文件系统时缺少一个参数,它就会自动去/etc/mtab 或者 /etc/fstab 里去查,如果找到的话,只要一个参数也够。写的话,比如你umount了一个文件系统,umount 就会删掉/etc/mtab 里面的相关记录。

With linux >= 2.6.26, /proc/mounts contains all of the information in 
/etc/mtab, plus more.  The mount system call can now pass all of the mount 
options to the kernel, so no information is missing in /proc/mounts.  This 
has obviously useful benefits such as read-only root, and the state in 
/etc/mtab never gets out of sync with reality (there are a number of open 
bugs against mount where this occurs).


Additionally, with the addition of per-process namespaces with CLONE_NEWNS to 
clone(2), each process has its own set of mounts, and as such a system-wide 
/etc/mtab is useless: it's only valid in one of the potentially many 
namespaces and can quickly get into a horrible mess.  At this point, 
/etc/mtab *must* be a symlink to avoid breakage.  Note that /proc/mounts is 
now a symlink to /proc/self/mounts for this reason: each process has
potentially different mounts.

3、看似上面的这两个文件已经够用了,但是新的情况出现了。Linux 内核引入了一个 mount namespace,是给container用的。因为这个的出现,Linux 不得不引入 /proc/mounts。为什么呢?因为记录 mount 信息的 /etc/mtab 是全局的,也就是说,就算你的某个进程有自己的 namespace,但只要还和外面共享同一个 /etc/mtab,那么,里面进行umount/mount操作的信息也会被记录到/etc/mtab里,外面也会看到!凌乱了!由此可见,我们不能有全局的mtab,肿么办呢?/proc/mounts 出来了,有人可能觉得它也是全局的啊!可你仔细看一下的话会发现,它其实是到 /proc/self/mounts 的一个符号链接!如此以来,container 里面的 /proc/mounts 和外面的当然就不会一样了!聪明啊!


所以,/etc/mtab 已经过时了,应该被抛弃,或者直接符号链接到/proc/mounts。同理,查看系统上挂载的文件系统的话,直接调用无参数的mount也是不妥的,因为那样也是读 /etc/mtab。我们应该使用 util-linux-ng 提供的一个新命令: findmnt,它是读的 /proc/self/mountinfo。

你可能感兴趣的:(Linux)