C++NFS挂载

3.1k words

挂载NFS

挂载命令

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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有:

1
2
3
4
5
#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进行强制卸载:

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

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

检查是否挂载

检查命令

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

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

    1
    2
    3
    4
    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

    1
    2
    3
    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

    1
    2
    3
    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

    1
    2
    #打印文件系统的主设备号和次设备号
    0:21

功能实现

功能需求:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
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服务

Windows下安装

Windows下常见的NFS服务软件可以使用haneWIN NFS Server

1. 下载地址https://r.hanewin.net/nfs1261.zip

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

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

Linux下安装

  1. 安装:sudo apt-get install nfs-kernel-server
  2. 配置方法:编辑文件 /etc/exports
1
/mnt/nfstest *(rw,sync,no_root_squash,no_subtree_check)
  • rw:可读可写
  • no_root_squash:root的权限
  • sync:资料同步写入存储器中
  • no_subtree_check:输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率

编辑成功之后运行命令exportfs -au刷新

Comments