找回密码
 立即注册

QQ登录

只需一步,快速开始

工控课堂 首页 工控文库 上位机编程 查看内容

C++ NFS挂载

2022-6-27 22:57| 发布者: gkket| 查看: 1655| 评论: 0

摘要: 挂载NFS#挂载命令#挂载NFS时,常用的命令比如:#将远程目录挂载到本地/home/share目录下 mount -t nfs -o nolock 192.168.1.10:/tmp /home/share 在Linux 下可以用mount函数进行挂载:bool Mount() { string remote ...

挂载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是否挂载成功的方式有很多,比如:

  1. 检查文件系统的磁盘空间占用情况 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
    
  2. 查看挂载信息文件 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
    
  3. 直接使用挂载命令 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)
    
  4. 查看指定目录是否挂载点 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())
}



关注公众号,加入500人微信群,下载100G免费资料!

最新评论

热门文章
关闭

站长推荐上一条 /1 下一条

QQ|手机版|免责声明|本站介绍|工控课堂 ( 沪ICP备20008691号-1 )

GMT+8, 2025-12-22 18:29 , Processed in 0.079151 second(s), 23 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.