Linux 获取挂载点的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9280759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:34:37  来源:igfitidea点击:

Linux function to get mount points

clinuxmountlibc

提问by tMC

Is there a function (or interface; ioctl, netlink etc) in the standard Linux libs that will return the current mounts directly from the kernel without parsing /proc? straceing the mount command, it looks like it parses files in /proc

标准 Linux 库中是否有函数(或接口;ioctl、netlink 等)可以直接从内核返回当前挂载而不解析 /proc? strace在 mount 命令中,它看起来像是在解析 /proc 中的文件

回答by Aaron Digulla

There is no syscall to list this information; instead, you can find it in the file /etc/mtab

没有系统调用来列出这些信息;相反,您可以在文件中找到它/etc/mtab

回答by Petesh

Please see the clarification at the bottom of the answer for the reasoning being used in this answer.

有关此答案中使用的推理,请参阅答案底部的说明。

Is there any reason that you would not use the getmntentlibc library call? I do realize that it's not the same as an 'all in one' system call, but it should allow you to get the relevant information.

有什么理由不使用getmntentlibc 库调用吗?我确实意识到它与“多合一”系统调用不同,但它应该允许您获取相关信息。

#include <stdio.h>
#include <stdlib.h>
#include <mntent.h>

int main(void)
{
  struct mntent *ent;
  FILE *aFile;

  aFile = setmntent("/proc/mounts", "r");
  if (aFile == NULL) {
    perror("setmntent");
    exit(1);
  }
  while (NULL != (ent = getmntent(aFile))) {
    printf("%s %s\n", ent->mnt_fsname, ent->mnt_dir);
  }
  endmntent(aFile);
}

Clarification

澄清

Considering that the OP clarified about trying to do this withouthaving /procmounted, I'm going to clarify:

考虑到澄清有关试图做到这一点的OP具有/proc安装,我要澄清:

There is nofacility outside of /procfor getting the fully qualified list of mounted file systems from the linux kernel. There is no system call, there is no ioctl. The /procinterface is the agreed upon interface.

除了从 linux 内核获取已安装文件系统的完全限定列表之外,没有其他工具/proc。没有系统调用,没有ioctl。该/proc接口在商定接口。

With that said, if you don't have /procmounted, you will have to parse the /etc/mtabfile - pass in /etc/mtabinstead of /proc/mountsto the initial setmntentcall.

话虽如此,如果您没有/proc安装,则必须解析/etc/mtab文件 - 传入/etc/mtab而不是/proc/mounts初始setmntent调用。

It is an agreed upon protocol that the mountand unmountcommandswill maintain a list of currently mounted filesystems in the file /etc/mtab. This is detailed in almost all linux/unix/bsdmanual pages for these commands. So if you don't have /procyou can sort ofrely on the contents of this file. It's not guaranteed to be a source of truth, but conventions are conventions for these things.

mountunmount命令在文件 /etc/mtab 中维护当前安装的文件系统列表,这是一个商定的协议。这在几乎所有linux/ unix/ bsd手册页中都有关于这些命令的详细说明。所以,如果你没有/proc,你可以排序的靠这个文件的内容。它不能保证是事实的来源,但约定是这些事情的约定。

So, if you don't have /proc, you would use /etc/mtabin the getmntentlibc library call below to get the list of file systems; otherwise you could use one of /proc/mountsor /proc/self/mountinfo(which is recommended nowadays over /proc/mounts).

所以,如果你没有/proc,你可以使用/etc/mtabgetmntent以下libc库调用来获取文件系统的列表; 否则,您可以使用/proc/mounts或之一/proc/self/mountinfo(现在推荐使用/proc/mounts)。