window下挂载linux环境的NFS的共享目录

各位好,小猿现在有个头大的问题。
问题描述:在Linux环境下部署NFS的服务,并添加一个共享目录。例如:linux机器的IP:192.168.58.16 NFS的共享目录为:/home/nfs_test。
在linux环境部署成功后,可以通过


showmount -e 192.168.58.16  //命令能成功查询到该目录

现在需要通过windows的机器,IP:192.168.58.84去挂载linux机器的NFS的共享目录到Q:盘 。
我本人当前使用了

CreateProcess   net use Q: \\\\192.168.58.16\\home\\nfs_test
WNetAddConnection2

以上的方式。但是都不成功。

现在需要有大猿帮忙实现这个功能,提供可用的源码和demo,谢谢。

以下是一个实现在 Windows 系统中挂载 Linux NFS 共享目录的 C# 示例代码:

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace MountNfsShare
{
    class Program
    {
        [DllImport("mpr.dll", CharSet = CharSet.Auto)]
        public static extern int WNetAddConnection2(
            ref NETRESOURCE lpNetResource,
            string lpPassword,
            string lpUsername,
            int dwFlags);

        [StructLayout(LayoutKind.Sequential)]
        public struct NETRESOURCE
        {
            public int dwScope;
            public int dwType;
            public int dwDisplayType;
            public int dwUsage;
            public string lpLocalName;
            public string lpRemoteName;
            public string lpComment;
            public string lpProvider;
        }

        static void Main(string[] args)
        {
            // Linux NFS 共享目录的 IP 和路径
            string nfsServerIp = "192.168.58.16";
            string nfsSharePath = "/home/nfs_test";

            // 本地 Windows 目录名,可以自定义
            string localDriveName = "Q:";

            // 挂载 Linux NFS 共享目录到本地 Windows 目录
            if (MountNfsShare(nfsServerIp, nfsSharePath, localDriveName))
            {
                Console.WriteLine("Linux NFS share mounted successfully.");
            }
            else
            {
                Console.WriteLine("Failed to mount Linux NFS share.");
            }

            Console.ReadKey();
        }

        static bool MountNfsShare(string nfsServerIp, string nfsSharePath, string localDriveName)
        {
            // 构造 NETRESOURCE 对象,用于映射网络驱动器
            NETRESOURCE netResource = new NETRESOURCE();
            netResource.dwScope = 2;
            netResource.dwType = 1;
            netResource.dwDisplayType = 0;
            netResource.dwUsage = 0;
            netResource.lpLocalName = localDriveName;
            netResource.lpRemoteName = $"\\\\{nfsServerIp}\\{nfsSharePath}";
            netResource.lpComment = null;
            netResource.lpProvider = null;

            // 调用 WNetAddConnection2 函数,映射网络驱动器
            int result = WNetAddConnection2(ref netResource, null, null, 0);
            if (result == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

上述代码中,我们使用了 WNetAddConnection2 函数来挂载 Linux NFS 共享目录到本地的 Windows 目录。

可以将上述代码保存为一个 C# 项目,然后在 Visual Studio 中编译并运行,即可实现在 Windows 系统中挂载 Linux NFS 共享目录的功能。

需要注意的是,为了能够成功挂载 NFS 共享目录,需要确保 Windows 系统上已经安装了 NFS 客户端。另外,如果 Linux 系统的 NFS 服务使用了访问控制列表(ACL),需要在 exports 文件中将 Windows 系统的 IP 地址添加到 ACL 中。

该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下,希望采纳。
你可以使用libnfs库来实现在C++代码中访问NFS共享文件系统。下面是一个简单的示例代码:

#include <stdio.h>
#include <nfs/nfs.h>

int main() {
    // 定义NFS服务器的IP地址和共享目录
    const char* server = "192.168.58.16";
    const char* share = "/home/nfs_test";
    
    // 连接NFS服务器
    struct nfs_context* context = nfs_init_context();
    struct nfs_mount_handle* mount_handle = nfs_mount(context, server, share, NULL);
    
    // 挂载共享目录到本地
    const char* mount_point = "Q:";
    int ret = nfs_mount_mount(mount_handle, mount_point);
    if (ret != NFS_OK) {
        printf("Failed to mount %s\n", mount_point);
    } else {
        printf("Mount %s success!\n", mount_point);
    }
    
    // 断开与NFS服务器的连接
    nfs_umount(mount_handle);
    nfs_destroy_context(context);
    
    return 0;
}

你需要在编译代码时链接libnfs库。在Ubuntu下,你可以使用以下命令安装libnfs:

sudo apt-get install libnfs-dev

然后,你可以使用以下命令编译上述代码:

g++ -o nfs_mount nfs_mount.cpp -lnfs

最后,你可以在Linux环境下运行编译后的二进制文件,它将挂载NFS共享目录到Windows的Q:盘。


如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

该回答引用ChatGPT GPT-4

首先,需要明确一点,C++ 代码在 Windows 系统上无法直接操作 Linux 系统上的 NFS 服务。不过,你可以在 Windows 系统上运行 C++ 代码,调用 Windows 命令行工具来实现这个需求。

为了实现这个功能,你需要确保 Windows 上已经安装了 NFS 客户端。你可以按照以下步骤来安装 NFS 客户端:

1、打开 "控制面板" > "程序" > "启用或关闭 Windows 功能"。
2、在弹出的窗口中找到 "适用于 NFS 的服务",勾选 "NFS 客户端",然后点击 "确定"。
3、等待安装完成。

在确认安装了 NFS 客户端之后,你可以使用以下 C++ 代码来挂载 Linux 上的 NFS 共享目录:

#include <iostream>
#include <string>
#include <windows.h>

int main() {
    std::string command = "mount -o anon \\\\192.168.58.16\\home\\nfs_test Q:";
    int result = system(command.c_str());

    if (result == 0) {
        std::cout << "挂载成功" << std::endl;
    } else {
        std::cout << "挂载失败" << std::endl;
    }

    return 0;
}


这段代码将会调用 Windows 命令行工具 "mount" 来挂载 Linux 上的 NFS 共享目录。如果挂载成功,程序会输出 "挂载成功",否则会输出 "挂载失败"。

如果你遇到问题,请确保你的防火墙设置允许 NFS 通信。同时,你也需要检查 Linux NFS 服务器的配置,确保 Windows 客户端可以访问共享目录。

如果你想在 Windows 系统下挂载 Linux 系统下的 NFS 共享目录,你可以参考以下步骤:

确保 Windows 系统安装了 NFS 客户端软件
在 Windows 系统下,需要安装 NFS 客户端软件才能挂载 Linux 系统下的 NFS 共享目录。可以选择安装开源的 NFS 客户端软件,例如:WinNFSd 或 NFSClient。

在 Windows 系统下挂载 Linux 系统下的 NFS 共享目录
在 Windows 命令行中,使用以下命令可以挂载 Linux 系统下的 NFS 共享目录到本地的一个驱动器:

arduino
Copy code
mount -o anon \192.168.58.16\home\nfs_test Q:
如果需要设置用户名和密码,则可以使用以下命令:

arduino
Copy code
mount -o user=username,password=yourpassword \192.168.58.16\home\nfs_test Q:
在 C++ 程序中实现挂载
你可以在 C++ 程序中使用 Win32 API 中的 WNetAddConnection2 函数来挂载 NFS 共享目录。以下是一个示例代码:

#include <Windows.h>
#include <winnetwk.h>

#pragma comment(lib, "Mpr.lib")

int main() {
    LPCTSTR pszDrive = L"Q:";
    LPCTSTR pszServer = L"192.168.58.16";
    LPCTSTR pszShare = L"home\\nfs_test";
    LPCTSTR pszUser = L"username";
    LPCTSTR pszPassword = L"yourpassword";

    NETRESOURCE netRes = {0};
    netRes.dwType = RESOURCETYPE_DISK;
    netRes.lpLocalName = pszDrive;
    netRes.lpRemoteName = pszServer;
    netRes.lpProvider = NULL;

    DWORD dwFlags = 0;
    if (pszUser && pszPassword) {
        dwFlags |= CONNECT_PROMPT;
        netRes.lpUserName = pszUser;
        netRes.lpPassword = pszPassword;
    }

    DWORD dwErrCode = WNetAddConnection2(&netRes, pszPassword, pszUser, dwFlags);
    if (dwErrCode == NO_ERROR) {
        printf("NFS share mounted successfully.\n");
    } else {
        printf("Error mounting NFS share. Error code: %d\n", dwErrCode);
    }

    return 0;
}

注意:需要将示例代码中的 pszServer 和 pszShare 修改为你实际的 NFS 服务器 IP 和共享目录名称,如果需要设置用户名和密码,也需要修改对应的参数。在编译时需要链接 Mpr.lib 库。

  • 这篇文章:Linux NFS挂载时候解决的记录 也许有你想要的答案,你可以看看
  • 除此之外, 这篇博客: nfs实现文件共享中的 第1步:为了检验NFS服务配置的效果,我们需要使用两台Linux主机(一台充当NFS服务器,一台充当NFS客户端),并设置它们所使用的IP地址。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  •                       两台Linux主机所使用的操作系统以及IP地址
    

    主机名称 操作系统 IP地址
    NFS服务端 RHEL 7 192.168.132.10
    NFS客户端 RHEL 7 192.168.132.20

    清空NFS服务器上面iptables防火墙的默认策略,以免默认的防火墙策略禁止正常的NFS共享服务。

    [root@localhost ~]# iptables -F
    [root@localhost ~]# service iptables save
    iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
    

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
首先,需要确认以下几个事项:

  1. 确认Windows机器上已安装了NFS的客户端程序,例如“Windows Services for UNIX”或者“NFS Client for Windows”,并且开启了对应的服务。
  2. 确认Linux机器上已配置好NFS的服务,并且已开启对应的共享目录的访问权限。
  3. 确认Windows机器和Linux机器在同一个局域网内,并且可以互相通信。

接下来可以使用以下两种方式之一进行挂载:

  1. 通过命令行方式挂载

在Windows机器上打开命令行,执行以下命令:

mount -o anon \\192.168.58.16\home\nfs_test Q:

其中,-o anon表示匿名访问,也可以使用username和password参数设置访问权限。Q:为需要挂载的本地盘符。

  1. 通过程序方式挂载

可以使用Python语言的os模块,调用系统命令进行挂载。代码示例:

import os

os.system("mount -o anon \\\\192.168.58.16\\home\\nfs_test Q:")

其中,需要注意转义符号的使用,双反斜杠表示转义。

如果以上两种方式均无法实现挂载,可以尝试检查网络通信和权限设置等问题。
如果我的回答解决了您的问题,请采纳!

你可以尝试以下步骤在 Windows 上挂载 Linux 的 NFS 共享目录:

确保 Windows 上已经安装了 Microsoft Services for NFS。

在 Windows 上打开命令提示符或 PowerShell。

执行以下命令以挂载 Linux 的 NFS 共享目录:
mount -o anon \\192.168.58.16\home\nfs_test Q:
这个命令将会挂载 \\192.168.58.16\home\nfs_test 到本地的 Q: 盘。

如果出现错误,可能需要检查 Linux 上的 NFS 服务是否已正确配置并正在运行。你可以使用以下命令检查:

systemctl status nfs
希望这能帮助到你。

该回答引用ChatGPT
首先,需要确认Linux环境下NFS服务已经正确部署,并且已经添加了共享目录。可以通过以下命令来确认:

showmount -e 192.168.58.16

如果能够成功查询到该目录,则说明NFS服务已经正确部署。
接下来,需要在Windows机器上安装NFS客户端。可以从以下链接下载并安装:
https://github.com/winnfsd/winnfsd/releases
安装完成后,需要在Windows机器上打开命令提示符,并使用以下命令来挂载Linux机器的NFS共享目录:

winnfsd.exe 192.168.58.16:/home/nfs_test Q:

其中,192.168.58.16是Linux机器的IP地址,/home/nfs_test是共享目录的路径,Q:是Windows机器上挂载的盘符。
如果一切正常,命令提示符会输出“Mounted 192.168.58.16:/home/nfs_test to Q:”的信息,表示挂载成功。
如果需要在程序中实现挂载NFS共享目录的功能,可以使用以下代码:
c++
#include
#include
#include
#include
#include
int main()
{
HANDLE hDevice;
DWORD dwBytesReturned;
WCHAR wszDeviceName[] = L"\\\\\\\\.\\\\NfsClient";
WCHAR wszServerName[] = L"192.168.58.16";
WCHAR wszExportName[] = L"/home/nfs_test";
WCHAR wszDriveLetter[] = L"Q:";
WCHAR wszMountPoint[] = L"\\\\\\\\?\\\\Q:";
WCHAR wszUserName[] = L"";
WCHAR wszPassword[] = L"";
WCHAR wszDomain[] = L"";
hDevice = CreateFileW(wszDeviceName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
printf("Failed to open NFS client device.\
");
return 1;
}
NFS_MOUNT_REQUEST mountRequest = { 0 };
mountRequest.Version = NFS_MOUNT_REQUEST_VERSION;
mountRequest.ServerName = wszServerName;
mountRequest.ExportName = wszExportName;
mountRequest.DriveLetter = wszDriveLetter;
mountRequest.MountPoint = wszMountPoint;
mountRequest.UserName = wszUserName;
mountRequest.Password = wszPassword;
mountRequest.Domain = wszDomain;
if (!DeviceIoControl(hDevice, IOCTL_NFS_MOUNT, &mountRequest, sizeof(mountRequest), NULL, 0, &dwBytesReturned, NULL))
{
printf("Failed to mount NFS share.\
");
CloseHandle(hDevice);
return 1;
}
printf("NFS share mounted successfully.\
");
CloseHandle(hDevice);
return 0;
}

该代码使用了Windows的NFS客户端驱动程序,通过调用设备IO控制码IOCTL_NFS_MOUNT来挂载NFS共享目录。需要注意的是,该代码需要以管理员权限运行才能正常工作。

基于最新版ChatGPT4的回答,望采纳!!!有其他问题也可以询问我哦💕(最新版更智能,功能更加强大):
好的,以下是一个示例代码实现在Windows机器上挂载Linux机器的NFS共享目录:

java

import java.io.IOException;
import java.io.InputStream;

public class MountNfsDemo {
    public static void main(String[] args) {
        String nfsShare = "\\\\192.168.58.16\\home\\nfs_test";
        String driveLetter = "Q:";
        try {
            // Mount NFS share using "net use" command
            Process process = Runtime.getRuntime().exec("net use " + driveLetter + " " + nfsShare);
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("Mounted " + nfsShare + " to " + driveLetter + " successfully.");
                
                // Use the mounted drive here...
                
                // Unmount NFS share using "net use" command
                process = Runtime.getRuntime().exec("net use " + driveLetter + " /delete");
                exitCode = process.waitFor();
                if (exitCode == 0) {
                    System.out.println("Unmounted " + driveLetter + " successfully.");
                } else {
                    printError(process.getErrorStream());
                }
            } else {
                printError(process.getErrorStream());
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void printError(InputStream err) throws IOException {
        byte[] buffer = new byte[1024];
        int length;
        while ((length = err.read(buffer)) != -1) {
            System.err.print(new String(buffer, 0, length));
        }
    }
}

这个代码将使用Java的Runtime类来执行命令行命令,在Windows机器上挂载Linux机器的NFS共享目录。在此示例中,我们使用"net use"命令来挂载和卸载共享目录。

请注意,在上面的示例中,您需要将实际的nfsShare路径和要分配的驱动器号替换为适当的值。

希望这个示例对您有所帮助。