计算机网络-网络文件共享协议

前言

在计算机网络中,我们经常会遇到在不同计算机网络系统之间如何共享和访问文件的场景,并且在实际项目中有这样的需求,在Linux中需要动态的mount文件,需要选择合适的网络文件共享协议以满足并发,吞吐量等需求。这就涉及今天要讲的网络文件共享协议SMB和NFS。

SMB vs NFS

什么是SMB

SMB 即 Server Message Block,最初是由IBM开发的,并被Microsoft进一步发展为CIFS(Common Internet File System)。虽然主要使用于windows,但目前也支持跨平台。该协议还在不断发展,最新的SMB版本是v.3.1.1。有时会将CIFS与SMB混淆,实际上CIFS是微软对SMB的实现。

大家可能也听说过Samba,Samba是SMB在Linux上的实现:

Samba:SMB 协议最初是由 Samba 提供 Unix 支持的。由于微软最初没有公开发布其专有协议的公共规范,Samba 的开发者不得不对其进行逆向工程。未来版本的 Samba 能够使用后来 SMB 协议的公开规范。Samba 包括对 SMB3(3.1.1)的支持。

Linux CIFS utils:这个内核软件充当 SMB 客户端,是在 Linux 上挂载现有 SMB 共享的首选方法。它最初是作为 Samba 软件的一部分包括在内的,但现在可以单独获得。Linux CIFS utils 作为大多数 Linux 发行版中的 cifs_utils 软件包提供

什么是NFS

NFS 即 Network File System,是由Sun Microsystems(现在为Oracle Corporation的一部分)开发的协议。它主要被设计用于UNIX/Linux操作系统的环境中。NFS v4是最新NFS版本。它支持并行文件访问,并且在这个版本中改进了安全性。向后兼容NFS v2和NFS v3。NFS v4支持更多的身份验证。

SMB vs NFS
SMB NFS
认证 User-based Host-based
端口 TCP 445; TCP 139, UDP 137, 138 TCP 2049, UDP 2049, TCP 111 and UDP 111; TCP 1110, UDP 1110, TCP 4045, UDP 4045.
加密 Kerberos, AES-256 Kerberos and TLS
File Lock 支持 只有高版本的支持
Performance 小文件performance更好,大文件一样。

实现

首先我们的网络文件是使用的Azure服务,我们首先来调查下Azure Blog服务都支持的协议,总结如下:

Azure Storage Service 支持的协议
Azure Blob Storage NFS 3.0
Azure File Storage (Standard) SMB
Azure File Storage (Premium) NFS 4.1, SMB

然后我们使用网络文件是为了读写Sqlite文件,而使用Sqlite文件必须得支持File Lock, 而NFS 3.0并不支持File Lock, 综合考虑,最合适的是SMB。 c#代码如下:

复制代码
class LinuxMount : IMount
{
    private const String CredFolder = "/tmp";

    private static readonly String MountFileStorageCommand = "mount -t cifs {0} {1} -o credentials={2},dir_mode=0777,file_mode=0777,uid=0,gid=0,cache=strict,mfsymlinks,nobrl";
    private static readonly String UnmountFileStorageCommand = "umount -f {0}";

    public void Mount(DeviceMountOption option)
    {
        var mountPoint = option.MountPoint;
        EnsureFolder(mountPoint);
        EnsureFolder(CredFolder);
        var credFile = Path.Combine(CredFolder, $"credentials_{Guid.NewGuid()}");
        var credContent = $"username={option.Username}{Environment.NewLine}password={option.Password}";
        try
        {
            File.WriteAllText(credFile, credContent);
            ExcuteCommand(String.Format(MountFileStorageCommand, option.FileSharePath, mountPoint, credFile));
        }
        finally
        {
            if (File.Exists(credFile))
            {
                File.Delete(credFile);
            }
        }
    }

    public void Unmount(String mountPoint)
    {
        ExcuteCommand(String.Format(UnmountFileStorageCommand, mountPoint));
    }

    private void EnsureFolder(String folder)
    {
        var dir = new DirectoryInfo(folder);
        if (!dir.Exists)
        {
            dir.Create();
        }
    }

    private void ExcuteCommand(String command)
    {
        using var proc = new Process();
        proc.StartInfo.FileName = "sh";
        proc.StartInfo.Arguments = $"-c \"{command}\"";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.Start();

        var result = proc.StandardOutput.ReadToEnd();
        result += proc.StandardError.ReadToEnd();

        proc.WaitForExit();
        if (proc.ExitCode != 0)
        {
            throw new Exception($"Command failed, {result}");
        }
    }
}
相关推荐
andxe4 小时前
安科士 AndXe 技术博客:400G QSFP112 SR4 光模块|AI 算力与超算短距互联最优方案
网络·人工智能·光模块·光通信
shiyi.十一5 小时前
第2章:应用层 — 知识要点与架构
网络·计算机网络·架构
DFT计算杂谈6 小时前
无 Root 权限在 Tesla K80 零门槛部署 DeepSeek 大模型
linux·服务器·网络·数据库·机器学习
水境传感 李兆栋6 小时前
GNSS 位移监测站 :毫米级感知,筑牢安全监测防线
网络
中微极客8 小时前
2026主流AI Agent框架技术选型与性能对比
运维·网络·人工智能
猿的天空12 小时前
机器人双手迎来全栈训练系统:灵初智能EgoSteer让灵巧手无所不能
网络·人工智能·计算机·ai·程序员·机器人·编程
Xzaveir_77712 小时前
OPPO、vivo、荣耀号码认证:多终端拨测矩阵与异常复现
大数据·网络·科技·矩阵·产品经理·ai-native
wuqingshun31415912 小时前
从网络角度来看,用户从输入网址到网页显示,期间发生了什么?
网络
牛马工作号12 小时前
Zigbee 专业详解:从协议到排障
网络·物联网·安全
牛马工作号12 小时前
Wi‑Fi 完全指南:从 802.11 协议到 Wi‑Fi 7、AC+AP 与 Mesh 工程组网
网络·网络协议·智能路由器