简体   繁体   English

如何获得已挂载文件系统的列表

[英]How do I get list of mounted filesystems

I need to parse table of mounted filesystems without using /proc/mounts since I'm parsing it to determine where proc file system is mounted. 我需要在不使用/ proc / mounts的情况下解析已安装文件系统的表,因为我正在对其进行分析以确定proc文件系统的安装位置。 How can I do it? 我该怎么做?

I've googled it, but all answers was to use /proc 我已经用谷歌搜索了,但是所有答案都是使用/ proc

And why people are so sure that procfs is mounted to default place? 以及为什么人们如此肯定procfs已安装到默认位置?


I'm asking out of curiosity. 我出于好奇而问。 I understand that /proc is a standard de-facto, many tools use it and so /proc is anchored pretty good. 我知道/ proc是事实上的标准,许多工具都使用它,因此/ proc锚定的非常好。

Linux API is consistent towards filesystem paths, all really needed information is passed via environmental variables, making possible to alter library and executable files, configuration files and so on. Linux API与文件系统路径保持一致,所有真正需要的信息都是通过环境变量传递的,从而可以更改库和可执行文件,配置文件等。

I'm curios is it possible to detec PROC_PATH with no prior knowledge about it? 我很想知道是否有可能在没有先验知识的情况下检测PROC_PATH I may do ftw with statvfs used as callback, but this is so ungraceful. 我可能会将statvfs用作回调,但是这太不合常规了。 There definetly should be more straight way. 绝对应该有更直接的方法。

查看/ etc / mtab,它会跟踪已挂载的文件系统。

How about df -k ? df -k怎么样? google leads me to this too. 谷歌也把我引向了一点。

on mac you have diskutil list . 在Mac上,您有diskutil list

if you want to figure out what syscalls you need to perform in your c code use strace to find it out. 如果您想弄清楚需要在c代码中执行哪些系统调用,请使用strace找出来。


Another approach assuming you have access to / root directory used Petesh code and llolydm code : 假设您可以访问/ root目录的另一种方法是使用Petesh 代码和llolydm 代码

#include <stdlib.h>
#include <mntent.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>


   void listdir(const char *name, int level)
{
    DIR *dir;
    struct dirent *entry;
    struct mntent *ent;
    FILE *aFile;

    if (!(dir = opendir(name)))
        return;
    if (!(entry = readdir(dir)))
        return;

    do {
        if (entry->d_type == DT_DIR) {
            char path[1024];
            int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
            path[len] = 0;
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            printf("%*s[%s]\n", level*2, "", entry->d_name);
            listdir(path, level + 1);

        }
        else {
            printf("%*s- %s\n", level*2, "", entry->d_name);
            aFile = setmntent(entry);
            while (NULL != (ent = getmntent(aFile))) {
                 printf("%s %s\n", ent->mnt_fsname, ent->mnt_dir);
            }
            endmntent(aFile);

        }
    } while (entry = readdir(dir));
    closedir(dir);
}

int main(void)
{
    listdir("/", 0);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM