NFS的搭建和使用(挂载+libnfs)

项目中前端向后端传递文件,除了直接传递文件的base64编码(请求体很大,很不友好),还可以传递NFS的文件路径,方便请求的进行。

NFS(Network File System),简单的来说一台主机可以通过网络直接的访问另一台主机的文件,并且忽略操作系统之间的差异。

NFS是基于C/S架构,搭建好NFS服务器后主要有两种使用方式: 1.直接客户端挂载服务器 2.使用python的libnfs库无需挂载直接进行访问

文章目录

一、NFS服务器的搭建二、访问NFS文件1.挂载1.1 windows挂载1.2 linux挂载

2.libnfs库直接访问nfs文件

一、NFS服务器的搭建

#安装nfs服务器(ubuntu)

sudo apt-get install nfs-kernel-server

#安装nfs服务器(centos)

yum -y install nfs-utils

#修改配置,指定共享文件夹的相关设置

vim /etc/exports

#加上下面这行配置,将/home/rits/wantao/nfs文件夹共享,开放读写权限

/home/rits/wantao/nfs *(rw,sync,no_subtree_check,no_root_squash)

#配置生效

exportfs -r

#查看配置

exportfs -v

nfs配置详解:

rw # 客户端对共享的目录可读写ro # 客户端对共享的目录只读不可写sync # 同步模式,也就是把内存的数据实时写入硬盘,但这样会降低磁盘效率async # 非同步模式,也就是每隔一段时间才会把内存的数据写入硬盘,能保证磁盘效率,但当异常宕机/断电时,会丢失内存里的数据no_root_squash # 客户端挂载NFS共享目录后,客户端上的root用户不受这些挂载选项的限制,权限很大root_squash # 跟no_root_squash相反,客户端上的root用户受到这些挂载选项的限制,被当成普通用户all_squash # 客户端上的所有用户在使用NFS共享目录时都被限定为一个普通用户anonuid # 上面的几个squash用于把客户端的用户限定为普通用户,而anouid用于限定这个普通用户的uid,这个uid与服务端的/etc/passwd文件相对应,如:anouid=1000 # 比如我客户端用xiaoming这个用户去创建文件,那么服务端同步这个文件的时候,文件的属主会变成服务端的uid(1000)所对应的用户anongid # 同上,用于限定这个普通用户的gid

二、访问NFS文件

1.挂载

1.1 windows挂载

开启NFS功能 启动或关闭windows功能–>NFS服务 查看nfs服务器

showmount -e 172.25.78.130

将本地文件夹挂载到nfs服务器共享文件夹

mount 172.25.78.130:/home/rits/wantao/nfs Z:

1.2 linux挂载

安装nfs

yum -y install nfs-utils

启动rpcbind

#开机启动

# systemctl enable rpcbind

#启动

# systemctl start rpcbind

将本地文件夹挂载到nfs服务器共享文件夹

#创建本地文件夹

mkdir /nfs

#将本地文件夹挂载到nfs服务器共享文件夹

mount -t nfs -o vers=4 172.25.78.130:/home/rits/wantao/nfs /nfs

#查看挂载结果

df -hT | grep /nfs

2.libnfs库直接访问nfs文件

项目中没有使用挂载的方式,因为不够灵活。而通过python的libnfs库可以直接通过nfs文件路径直接访问nfs文件。

之前以为直接通过pip install libnfs就搞定了,没想到很多的坑,花了我一天的时间,才在linux上安装成功。

安装libnfs

按照https://github.com/sahlberg/libnfs-python的README,安装libnfs这个库之前还得先安装https://github.com/sahlberg/libnfs这个模块。

#下载模块源码

git clone https://github.com/sahlberg/libnfs

#安装编译必须依赖

apt-get install autoconf

apt-get install automake

apt-get install libtool

cd libnfs

#编译和安装

./bootstrap

./configure

make && make install

然后pip install libnfs,发现还会报一个错ImportError: libnfs.so.13: cannot open shared object file: No such file or directory whereis libnfs.so.13.原因是没有找到libnfs.so.13这个库。

#查看这个库在哪里,可以看到在/usr/lib/local下

whereis libnfs.so.13

#指定库所在的目录

vim /etc/profile

export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"

#使配置文件生效

source /etc/profile

#这样就能安装成功了

pip install libnfs

libnfs使用示例

nfs = libnfs.open('nfs://172.25.78.130/home/rits/wantao/nfs/1.txt',mode='r')

print(nfs.read())

[an error occurred while processing the directive]
Copyright © 2088 迷你世界杯_竞猜世界杯 - xhfzmy.com All Rights Reserved.
友情链接