Linux中的NFS挂载系统调用 [英] NFS mount System Call in linux

查看:326
本文介绍了Linux中的NFS挂载系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将nfs服务器中的源目录安装到具有linux的嵌入式主板中的目标目录中.以下命令可以按预期在板中的Shell提示符中正常运行.

I am trying to mount a source directory from nfs server to a destination directory in embedded board having linux. The following command works perfectly as expected in shell prompt in the board.

mount -t nfs -o nolock 10.126.62.45:/vol/home/avinoba/Sky /mnt

在程序中用于上述命令的等效系统调用是什么? 我尝试了以下调用,但挂载失败并显示"Invalid Argument"(无效参数)

What is the equivalent system call to be used in program for the command above? I tried the below call but the mount failed with "Invalid Argument"

if(mount("10.126.62.45:/vol/home/avinoba/Sky","/mnt","nfs",MS_MGC_VAL,"nolock") == -1)
{
     printf("ERROR: mount failed: %s \n",strerror(errno));
}

请提出解决方案.

谢谢

推荐答案

在这里,关于NFS挂载的任何手册页都没有介绍这一点,我感到很惊讶.深入内核代码中,在功能 nfs_validate_text_mount_data 中,功能 nfs_parse_mount_options 负责解析作为 mount 中的第五个参数传递的多个逗号分隔的选项 strong>系统调用.

I'm quite surprised here knowing that how this is not covered by any man page regarding NFS mounts. Diving into the kernel code, in the function nfs_validate_text_mount_data, the function nfs_parse_mount_options is responsible for parsing the multiple comma separated options passed as the fifth argument in the mount system call.

struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;

if (nfs_parse_mount_options((char *)options, args) == 0)
    return -EINVAL;

if (!nfs_verify_server_address(sap))
    goto out_no_address;

在上面的代码块中,最后一个if语句检查nfs服务器地址和套接字系列是否定义为有效值.如果未在 nfs_parse_mount_options 中更新它们,则挂载最终将返回无效参数.

In the above code block, the last if statement checks whether the nfs server address and socket family is defined to valid values. If they are not updated within nfs_parse_mount_options, mount would end up returning invalid parameter.

如果执行了 nfs_parse_mount_options 的实现,则可以看到,仅对于 Opt_addr 情况,通过解析来更新nfs服务器地址和套接字系列 options 参数.

If the implementation of nfs_parse_mount_options is walked through, it can be seen that, only for the case Opt_addr, the nfs server address and the socket family is updated by parsing the options argument.

case Opt_addr:
    string = match_strdup(args);
    if (string == NULL)
        goto out_nomem;
    mnt->nfs_server.addrlen =
        rpc_pton(mnt->net, string, strlen(string),
            (struct sockaddr *)
            &mnt->nfs_server.address,
            sizeof(mnt->nfs_server.address));
    kfree(string);
    if (mnt->nfs_server.addrlen == 0)
        goto out_invalid_address;
    break;

案例 Opt_addr 对应于选项"addr = nfs server ip" .因此,要使系统调用正常工作,定义此选项是必须.据我搜索,描述nfs挂载的所有手册页中都完全没有此内容.

The case Opt_addr corresponds to the option "addr=nfs server ip". So for the system call to work, defining this option is a must. As far as I have searched, this is completely missing from all the man pages which describes nfs mounts.

因此,现在考虑问题陈述,请尝试更改为以下代码

So now considering the problem statement, please try by changing to the below code

if(mount(":/vol/home/avinoba/Sky","/mnt","nfs",0,"nolock,addr=10.126.62.45") == -1)
{
     printf("ERROR: mount failed: %s \n",strerror(errno));
}

还要注意,当在参数中放入 addr 选项时,nfs服务器路径前面的ip地址变为可选.但是,必须使用':',因为它用作解析nfs服务器路径的定界符.

Also note that when the addr option is put in the argument, the ip address in front of the nfs server path becomes optional. However the ':' is must,as this acts as the delimiter to parse the nfs server path.

这篇关于Linux中的NFS挂载系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆