这篇文章主要介绍了C++ NFS挂载,文中给大家提到了挂载NFS时常用的命令,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
挂载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。 | 
卸载
卸载函数有mount和mount2,第二个可以加参数,所以一般情况下使用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.mobange.com/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:将全部路径开放给客户端
到此这篇关于C++ NFS挂载的文章就介绍到这了,更多相关C++ NFS挂载内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:C++ NFS挂载及挂载命令
 
				
         
 
            
        基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
						 
						 
						 
						 
						 
				 
				 
				 
				