C++ NFS挂载

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++ NFS挂载

Yaronzz   2022-05-23 我要评论

挂载NFS

挂载命令

挂载NFS时,常用的命令比如:

#将远程目录挂载到本地/home/share目录下
mount -t nfs -o nolock 192.168.1.10:/tmp /home/share

在Linux 下可以用mount函数进行挂载:

bool Mount()
{
    string remotePath = m_remoteIP + ":" + m_remotePath;
    string localPath = m_localPath;
    string params = "nolock,soft,fg,tcp,timeo=5,retrans=1,rsize=4096,vers=3,addr=" + m_remoteIP;

    int ret = mount(remotePath.c_str(),
                    localPath.c_str(),
                    "nfs",
                    MS_SYNCHRONOUS,
                    params.c_str());

    if (ret != 0)
    {
        auto errInfo = errno;
        return false;
    }

    return true;
}

错误码

挂载失败时,常见错误码errno有:

#define EINVAL 22 /* Invalid argument 挂载参数问题,可能是没加addr*/

#define EACCES 13 /* Permission denied 权限问题*/

#define EPERM 1 /* Operation not permitted 权限问题*/

#define EBUSY 16 /* Device or resource busy 挂载目录正被使用*/

#define ENOENT 2 /* No such file or directory 挂载目录错误*/

挂载参数

参数 说明
rsize=n 读时最大字节数
wsize=n 写时最大字节数
timeo=n 客户端重传请求前等待时间,默认等待重传时间为60s
retrans=n 客户端返回错误前的重传次数。默认为重传3次。retrans与soft参数一起使用时才有效。
soft/hard 软挂载方式挂载系统,若NFS请求超时,则客户端向调用程序返回错误;
如果使用硬连接方式则客户端一直重新请求直至成功。默认为hard
bg/fg 设置挂载失败后的行为方式。
默认的fg方式将立刻退出返回错误状态,bg方式是退出前将产生一个子进程在后台继续尝试挂载
vers=xxx 设置版本
addr=xxx 设置服务地址
tcp/udp 设置协议,一般使用tcp,网络不稳定可以换udp
port=n 设置服务端口号
lock/nolock 选择是否使用NLM协议在服务器上锁文件。当选择nolock选项时,锁对于同一主机的应用有效,对不同主机不受锁的影响。默认为lock。

卸载

卸载函数有mountmount2,第二个可以加参数,所以一般情况下使用mount2进行强制卸载:

bool UnMount()
{
    return umount2(m_localPath.c_str(), MNT_FORCE) == 0);
}

如果m_localPath目录没有被挂载,则会返回错误

检查是否挂载

检查命令

Linux下使用命令检查nfs是否挂载成功的方式有很多,比如:

检查文件系统的磁盘空间占用情况 df -h

Filesystem                Size      Used Available Use% Mounted on
ubi0:rootfs              54.1M     50.8M      3.3M  94% /
devtmpfs                215.8M         0    215.8M   0% /dev
192.168.0.249:/nfs       59.6G      1.6G     57.9G   3% /mnt/udisk

查看挂载信息文件 cat /proc/mounts

ubi0:rootfs / ubifs rw,sync,relatime 0 0
devtmpfs /dev devtmpfs rw,relatime,size=220936k,nr_inodes=55234,mode=755 0 0
192.168.0.249:/nfs /mnt/udisk nfs rw,sync,relatime,vers=3,rsize=4096,wsize=8192,namlen=255,soft,nolock,proto=tcp,timeo=5,retrans=1,sec=sys,mountaddr=192.168.0.249,mountvers=3,mountproto=tcp,local_lock=all,addr=192.168.0.249 0 0

直接使用挂载命令 mount

ubi0:rootfs on / type ubifs (rw,sync,relatime)
devtmpfs on /dev type devtmpfs (rw,relatime,size=220936k,nr_inodes=55234,mode=755)
192.168.0.249:/nfs on /mnt/udisk type nfs (rw,sync,relatime,vers=3,rsize=4096,wsize=8192,namlen=255,soft,nolock,proto=tcp,timeo=5,retrans=1,sec=sys,mountaddr=192.168.0.249,mountvers=3,mountproto=tcp,local_lock=all,addr=192.168.0.249)

查看指定目录是否挂载点 mountpoint -d /mnt/udisk

#打印文件系统的主设备号和次设备号

0:21

功能实现

功能需求:

  • 检查目录是否挂载成功
  • 当NFS服务关闭时,检查结果也需要同步

经过测试,只有mountpoint 能完成第二点功能需求,所以直接使用shell命令来进行检查:

bool CheckMount()
{
    string shell = "mountpoint -d " + m_localPath;

    QProcess proc;
    proc.start(shell.c_str());
    if (!proc.waitForFinished(10000))
        return false;
    
    auto output = proc.readAll();
    return !output.isEmpty())
}

NFS服务

Linux下的NFS服务安装比较方便,Windows下常见的NFS服务软件可以使用haneWIN NFS Server

1. 下载地址:http://xiazai.jb51.net/202112/yuanma/nfsd_jb51.rar

2. 配置方法:打开目录下的exports文件,配置NFS服务目录,比如共享D盘下的NFSData目录:

D:\NFSData  -name:nfs -exec -maproot:0 -alldirs

  • name:设置挂载的目录名给客户端使用,比如mount -t nfs -o nolock 192.168.1.10:/nfs /home/share
  • maproot:将root权限开放给客户端
  • alldirs:将全部路径开放给客户端

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们