前言
在計(jì)算機(jī)網(wǎng)絡(luò)中,我們經(jīng)常會(huì)遇到在不同計(jì)算機(jī)網(wǎng)絡(luò)系統(tǒng)之間如何共享和訪問文件的場(chǎng)景,并且在實(shí)際項(xiàng)目中有這樣的需求,在Linux中需要?jiǎng)討B(tài)的mount文件,需要選擇合適的網(wǎng)絡(luò)文件共享協(xié)議以滿足并發(fā),吞吐量等需求。這就涉及今天要講的網(wǎng)絡(luò)文件共享協(xié)議SMB和NFS。
SMB vs NFS
什么是SMB
SMB 即 Server Message Block,最初是由IBM開發(fā)的,并被Microsoft進(jìn)一步發(fā)展為CIFS(Common Internet File System)。雖然主要使用于windows,但目前也支持跨平臺(tái)。該協(xié)議還在不斷發(fā)展,最新的SMB版本是v.3.1.1。有時(shí)會(huì)將CIFS與SMB混淆,實(shí)際上CIFS是微軟對(duì)SMB的實(shí)現(xiàn)。
大家可能也聽說過Samba,Samba是SMB在Linux上的實(shí)現(xiàn):
Samba:SMB 協(xié)議最初是由 Samba 提供 Unix 支持的。由于微軟最初沒有公開發(fā)布其專有協(xié)議的公共規(guī)范,Samba 的開發(fā)者不得不對(duì)其進(jìn)行逆向工程。未來版本的 Samba 能夠使用后來 SMB 協(xié)議的公開規(guī)范。Samba 包括對(duì) SMB3(3.1.1)的支持。
Linux CIFS utils:這個(gè)內(nèi)核軟件充當(dāng) SMB 客戶端,是在 Linux 上掛載現(xiàn)有 SMB 共享的首選方法。它最初是作為 Samba 軟件的一部分包括在內(nèi)的,但現(xiàn)在可以單獨(dú)獲得。Linux CIFS utils 作為大多數(shù) Linux 發(fā)行版中的 cifs_utils 軟件包提供
什么是NFS
NFS 即 Network File System,是由Sun Microsystems(現(xiàn)在為Oracle Corporation的一部分)開發(fā)的協(xié)議。它主要被設(shè)計(jì)用于UNIX/Linux操作系統(tǒng)的環(huán)境中。NFS v4是最新NFS版本。它支持并行文件訪問,并且在這個(gè)版本中改進(jìn)了安全性。向后兼容NFS v2和NFS v3。NFS v4支持更多的身份驗(yàn)證。
SMB vs NFS
SMB | NFS | |
---|---|---|
認(rèn)證 | 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更好,大文件一樣。 |
實(shí)現(xiàn)
首先我們的網(wǎng)絡(luò)文件是使用的Azure服務(wù),我們首先來調(diào)查下Azure Blog服務(wù)都支持的協(xié)議,總結(jié)如下:文章來源:http://www.zghlxwxcb.cn/news/detail-861504.html
Azure Storage Service | 支持的協(xié)議 |
---|---|
Azure Blob Storage | NFS 3.0 |
Azure File Storage (Standard) | SMB |
Azure File Storage (Premium) | NFS 4.1, SMB |
然后我們使用網(wǎng)絡(luò)文件是為了讀寫Sqlite文件,而使用Sqlite文件必須得支持File Lock, 而NFS 3.0并不支持File Lock, 綜合考慮,最合適的是SMB。 c#代碼如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-861504.html
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}");
}
}
}
到了這里,關(guān)于計(jì)算機(jī)網(wǎng)絡(luò)-網(wǎng)絡(luò)文件共享協(xié)議的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!