【问题标题】:how to use mount function from c?如何使用c中的mount功能?
【发布时间】:2017-06-28 17:09:29
【问题描述】:

我想使用挂载函数来实现 NFS。

int mount(const char *source, const char *target,
                 const char *filesystemtype, unsigned long mountflags,
                 const void *data);

我可以使用 mount 命令 来实现它,例如 mount 172.16.0.144:/tmp/test /tmp/test。但是当我使用 mount() 函数时,它不起作用。这是我的代码。

#include<sys/mount.h> 
#include<iostream>
#include<errno.h>
#include<fstream>
#include<string.h>
using namespace std;

int main(int argc, char**argv) {
    const char* srcPath = "/tmp/watchman";
    const char* targetPath = "172.16.0.144:/tmp/watchman";
    if (argc == 3) {
        srcPath = argv[1];
        targetPath = argv[2];
        cerr << "reset the src && target path\n";
    } else {
        if (argc != 1) {
            cerr << "wrong input argument!\n";
            return 0;
        }
    }
    cerr << "srcPath = " << srcPath << endl;
    cerr << "target = " << targetPath << endl;
    int ret_val = mount(srcPath, targetPath, "", MS_SHARED, "");
    if (ret_val == 0) {
        cerr << "mount succeed\n";
        string filename = string(srcPath) + "/" + "tmp.txt";
        fstream fin(filename.c_str(), ios::out);
        fin << "there is a write test from client\n";
        fin.close();
        ret_val = umount(srcPath);
        if (ret_val == 0) {
            cerr << "umount succeed \n";
        } else {
            cerr << "umount failed \n";
            printf("%s/n", strerror(errno));
        }
    } else {
        cout<<"ret_val = "<<ret_val<<endl;
        cerr << "mount failed \n";
        cerr << strerror(errno) << endl;
    }
    return 0;
}

它 printf 挂载失败,没有这样的文件或目录。任何人都可以帮助我吗?请 !!!

【问题讨论】:

  • 使用正确的标签。这是 C++,不是 C。

标签: c++ mount


【解决方案1】:

如果你read the mount manual page 你会看到

mount()source 指定的文件系统(通常是指设备的路径名,但也可以是目录或文件的路径名,或虚拟字符串)附加到target 中的路径名指定的位置(目录或文件)。

您已在应用程序中切换了源和目标。

【讨论】:

  • 我试了一下,还是不行。你能给我一个例子吗?我很困惑呢。
  • @CloriaD 目录/tmp/watchman 存在吗?它必须存在才能用作挂载的目标。
  • 是的,当然存在。在 NFS 服务器和本地都存在 /tmp/watchman 文件夹。那么您是否有示例可以运行并获得良好的结果?请帮帮我。
  • @CloriaD 您要求的“示例”就是您现在拥有的,但是切换了srcPathtargetPath
  • @CloriaD 哦,也许尝试提供一个文件系统类型?喜欢"nfs"?和 no 挂载标志。
猜你喜欢
  • 2012-06-04
  • 2011-03-29
  • 1970-01-01
  • 2012-05-11
  • 2019-03-15
  • 2014-12-07
  • 2015-10-29
  • 1970-01-01
相关资源
最近更新 更多