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


    前言

    计算机网络中,我们经常会遇到在不同计算机网络系统之间如何共享和访问文件的场景,并且在实际项目中有这样的需求,在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
    SMBNFS
    认证User-basedHost-based
    端口TCP 445; TCP 139, UDP 137, 138TCP 2049, UDP 2049, TCP 111 and UDP 111; TCP 1110, UDP 1110, TCP 4045, UDP 4045.
    加密Kerberos, AES-256Kerberos and TLS
    File Lock支持只有高版本的支持
    Performance小文件performance更好,大文件一样。

    实现

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

    Azure Storage Service支持的协议
    Azure Blob StorageNFS 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#代码如下:

    1. class LinuxMount : IMount
    2. {
    3. private const String CredFolder = "/tmp";
    4. 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";
    5. private static readonly String UnmountFileStorageCommand = "umount -f {0}";
    6. public void Mount(DeviceMountOption option)
    7. {
    8. var mountPoint = option.MountPoint;
    9. EnsureFolder(mountPoint);
    10. EnsureFolder(CredFolder);
    11. var credFile = Path.Combine(CredFolder, $"credentials_{Guid.NewGuid()}");
    12. var credContent = $"username={option.Username}{Environment.NewLine}password={option.Password}";
    13. try
    14. {
    15. File.WriteAllText(credFile, credContent);
    16. ExcuteCommand(String.Format(MountFileStorageCommand, option.FileSharePath, mountPoint, credFile));
    17. }
    18. finally
    19. {
    20. if (File.Exists(credFile))
    21. {
    22. File.Delete(credFile);
    23. }
    24. }
    25. }
    26. public void Unmount(String mountPoint)
    27. {
    28. ExcuteCommand(String.Format(UnmountFileStorageCommand, mountPoint));
    29. }
    30. private void EnsureFolder(String folder)
    31. {
    32. var dir = new DirectoryInfo(folder);
    33. if (!dir.Exists)
    34. {
    35. dir.Create();
    36. }
    37. }
    38. private void ExcuteCommand(String command)
    39. {
    40. using var proc = new Process();
    41. proc.StartInfo.FileName = "sh";
    42. proc.StartInfo.Arguments = $"-c \"{command}\"";
    43. proc.StartInfo.UseShellExecute = false;
    44. proc.StartInfo.CreateNoWindow = true;
    45. proc.StartInfo.RedirectStandardOutput = true;
    46. proc.StartInfo.RedirectStandardError = true;
    47. proc.Start();
    48. var result = proc.StandardOutput.ReadToEnd();
    49. result += proc.StandardError.ReadToEnd();
    50. proc.WaitForExit();
    51. if (proc.ExitCode != 0)
    52. {
    53. throw new Exception($"Command failed, {result}");
    54. }
    55. }
    56. }
  • 相关阅读:
    java发送企业微信群
    String 字符串的使用方法 -- JavaScript
    springboot+vue学生信息管理系统学籍 成绩 选课 奖惩,奖学金缴费idea maven mysql
    SQL Server简介
    java面试题之 int和Integer的区别
    Linux开发工具---->yum/gcc/g++/gdb/makefile
    客户收入企稳、亏损收窄,有赞在盛夏“回春”?
    Markem imaje马肯依玛士喷码机维修9450E打码机维修
    做交互设计都有哪些需要掌握的思维方式
    【自动化测试】——robotframework实战(三)编写测试用例
  • 原文地址:https://blog.csdn.net/douluo998/article/details/138083903