当前位置:Linux教程 - Nfs - 什么是NFS文件系统

什么是NFS文件系统

◎ What is NFS ?

Network    File    System    
NFS 是由SUN公司发展, 并於1984年推出, NFS是一个RPC service ,它使我们能够达到档案的共享, 它的设计是为了在不同的系统间使用, 所以它的通讯协定设计与主机及作业系统无关.当使用者想用远端档案时只要用"mount"就可把remote档案系统挂接在自己的档案系统之下,使得远端的档案使用上和local机器的档案没两样.

machine A machine B

/ /
bin etc usr bin etc usr
man man share local

假如我们在机器A上, 要把机器B上的 /usr/man 挂接到machine A 的/usr/man只要下mount machine_name:/usr/man /usr/home
就可mount过来.而我们不只是可以mount目录,就是一个档也是可以的.在挂接之後我们只能对档案做reading (or writing) 的动作,而不能在remote machie上把此档或目录move,delete掉 , 但须注意的是如我们mount /usr 後 , 不能再mount /usr底下的目录, 否则会发生错误



□ Servers & Clients
NFS就是促使Servers上的档案能被其他的机器mount,而达到资源共享,享用这些档案的机器就可称为Client,一个client可以从server上mount一个档或是一个层次的目录(file hierarchies) . 然而事实上任何一台机器都可以做NFS server or NFS client ,甚至同时为NFS server and NFS client 也可以.



□ Servers Exporting & Clients Mounting
NFS server 所export 出来的档案或目录都记录在 /etc/exports 这一个档中,当我们启动NFS server 时 在 /etc/rc.local 的这一个script会自动的启动 exportfs 这一个程式 , 搜寻 /etc/exports 这一个档是否存在, 并且赋予正确的权限给所有export出去的 file hierarchies .
但须注意的是,只有server所export出去的路径,NFS client才能够mount, 同样的当启动client时 , 系统会自动去mount所有server export的路径,而mount到的所有路径都会记录在 /etc/fstab 下 , 类似如下的fstab档

/dev/sd0a / 4.2 rw 1 1
/dev/sd0h /tmp 4.2 rw 1 3
/dev/sd0g /usr 4.2 rw 1 2
/dev/fd0 /pcfs pcfs rw,noauto 0 0
sparc20:/swap /swap nfs rw,intr,bg,soft 0 0
sparc17:/home /home nfs rw,intr,bg,soft 0 0
sparc17:/home3 /home3 nfs rw,intr,bg,soft 0 0
sparc14:/home4 /home4 nfs rw,intr,bg,soft 0 0
sparc20:/home2 /home2 nfs rw,intr,bg,soft 0 0
sparc20:/var/spool/mail /var/spool/mail nfs rw,intr,bg,soft 0 0
rs970:/home1 /home1 nfs rw,intr,bg,soft 0 0

★ Noted :
当client mount 到一个路径,绝对不是说copy server上的这一个路径到local的机器上,我们可以用 cd 进入这一个mount到的路径,就如同是使用local directory一样

□ Setting Up a NFS Server
1. 定义机器为 NFS file server
2. 划分servers disk ,定义哪一些partitions 是要提供出来作为
client 所共享的file system
3. 在 Client Form 上定义每一台client 的参数
4. 写出 /etc/exports (一般系统都有一个default exports)
5. 重新boot NFS server or 用指令 exportfs -a 输出所有的directories 并且用 nfsd 8 & 启动 nfsd守护程式,常驻在背景

※ ps. 一些细节
检查 /etc/exports 输出路径的权限,确定只有root能修改,
all user只能read
2. 用exportfs 去增加或删除directories
exportfs -o access=engineering,ro=dancer /usr
exportfs -u /usr
3. 假如你的机器没有NIS(YP server)的服务,当更改资料时记得修改
/etc/passwd
/etc/group
/etc/hosts
/etc/ethers
4. 为你自己的network 设置security

exportfs的语法
/usr/etc/exportfs [ -avu ] [ -o option ] [ directory ]
-a : 把 /etc/exports 中所有路径export出去
-u : 把 export出去的路径卸下 , 如 exportfs -u /usr
-o option : 如 exportfs -o ro /usr ,所有人对/usr 都为read only
option 还有 root = hostname , access = client
access = netgroup

For example :
exportfs -a 把exports中的路径全部export出去
exportfs -o access=engineering:other /usr
/usr 这路径export後只有engineering and other 这两个
group 能够 read & write
exportfs -o access=oak,ro=dancer /usr
设定dancer 这台client 对 /usr 为read only ,且只有oak这一个 group 能做read

/etc/exports 档的□例

● syntax : directory -option[,option]
(设定两个group能rw)
/usr -access=engineering:accounting
/home -access=engineering:accounting
/var/spool/mail -access=engineering:accounting
/export/exec/sun3 -access=engineering:accounting
/export/exec/sun3.sunos.4.1 -access=engineering:accounting
/export/exec/kvm/sun3.sunos.4.1 -access=engineering:accounting
/export/root/birch -access=birch,root=birch
/export/swap/birch -access=birch,root=birch
/export/root/oak -access=oak,root=oak
/export/swap/oak -access=oak,root=oak
/export/root/willow -access=willow,root=willow
/export/swap/willow -access=willow,root=willow
/export/root/pine -accsee=pine,root=pine
/export/swap/pine -accsee=pine,root=pine
(access=client , root=hostname 如此只有这一台client的superuser有权力rw)

□ Setting Up a NFS Client

1. 宣告机器为没有磁碟机或没有资料的Client,在使用SunInstall之前
2. 编辑好 /etc/fstab 这一个档,确定要mount的路径都在fstab中
3. 依照fstab所设的内容,在Client上设定好Mount points
(mount_points 就是用mkdir 设exports所输出的路径)
4. 确定我们所要mount的路径,都有出现在 /etc/exports 中
5. 可以启动mount去连结server上的directories ( mount -a )

/etc/fstab 档的□例
● syntax filesystem directory type options freq pass
oak:/export/root/boomer / nfs rw 0 0
^^^
因为档案在server上,not on client
所以client的设定为0
oak:/export/exec/sun3 /usr nfs ro 0 0
oak:/export/exec/kvm/sun3 /usr/kvm nfs ro 0 0
oak:/usr/share /usr/share nfs ro 0 0
oak:/home/oak /home/oak nfs rw,bg 0 0



§ mount 的语法

● syntax : mount -t type [-rv] -o [option] server:pathname /mount_point
MOUNT :
mount -a 把/etc/fstab 中所列的路径全部挂上
mount -o ro,soft,bg dancer:/usr/local /usr/local/dancer
把dancer server 的/usr/local mount 到 client的
/usr/local/dancer 并且是read only

-t type : 你所要mount的型别, 如 nfs or 4.2
-r : 所mount的路径定为read only
-v : mount过程的每一个动作,都有messages 传回到萤幕上
hard : 重复要求,直到server回应为止,但如server一直不回应
the server may be down !
soft : 当client的请求得不到回应,retry one time 後 传回
error message
bg : 当第一次请求不成功,第二次的mount将放到背景执行
fg : retries mount 都一直在提示符号下进行
intr : 当正在进行 NFS 请求时,允许用键盘中断

mount 成功时的message
NFS server hostname ok
hard mount fail
NFS server hostname not responding, still trying
soft . . .hostname server not reponding: RPC: Timed out

§ UNMOUNT :

umount mount_point
umount -a 卸下所有已经mount上的路径

◎ How NFS Work ?

当我们启动 NFS file server 时,/etc/rc.local 会自动启动exportfs这一个程式,指定可以export的档案或目录,而我们所能mount的也只能是其所指定的目录.



□ NFS 架设在 XDR/RPC的协定之上

XDR : (eXternal Data Representation) 外部资料表示法
XDR(eXternal Data Representation) 提供一种方法把资料从一种格式转换成另一种标准资料格式表示法,确保在不同的电脑,作业系统及电脑语言中,所有资料代表的意义都是相同的

RPC : (Remote Procedure Calls) 远端程序呼叫
RPC(Remote Procedure Calls) 远端程序呼叫, 请求远端电脑给予服务. 委托器(client)就会透过网路传送RPC到远端电脑,请求服务.
(一般 local machine : client remote machine : server )

□ NFS 如何运用 RPC 传送资料

客户端process 主服务端process
┌————┐ ┌—————┐
│ 客户端 │ │ 主服务站 │
│routines│ │ routines │
└—┬——┘ └┬————┘
本地程序呼叫 │ 《 │ 《
(1) │ │ (10) (6) │ │ (5)
》 │ 》 │
┌———┴┐ ┌————┴┐
│ 客户端 │ │ 主服务端 │
│ stub │ │ stub │
└—┬——┘ └┬————┘
系统呼叫 │ 《 │ 《
(2) │ │ (9) (7) │ │ (4)
》 │ 》 │
┌———┴┐ (8) ┌————┴┐
│network │ <——————┤ network │
│routines├——————> │ routines │
└————┘ (3) └—————┘
本地系统核心 网路通讯 远端系统核心

(1) client 送出讯息,请求服务
(2) client stub (客户株) 把client 送出的参数转换成XDR---标准资料格式并用系统呼叫(system call) 把讯息送到网路上
(3) 讯息经过网路送达远端主机系统
(4) 远端主机将接受到的讯息传给server stub (服务站株)
(5) 把XDR形式的资料,转换成符合主机端的格式,取出client发出的服务请求参数,送给server
(6) -- (10) 则是逆向而行 , server 送出服务给 client

□ rc.local 启动守护程式

一个NFS server 要 inet , portmap , nfs , mount 此四个守护程式,保持在背景执行的状态下才能运作. (if running NIS must add ypbind daemon)

◆ 当启动 NFS file server 时,

the /etc/rc.local script 会做如下的动作
1. 执行exportfs , 读取servers /etc/exports 告诉kernel
所要输出的file hierarchies 和 存取的权限
( exportfs -a )
2. 启动 rpc.mountd daemon 和 nfsd daemon (通常是 8 个)
( rpc.mountd -n nfsd 8 & echo -n nfsd )

◆ 当启动 NFS client 时

rc.local 会做如下的动作
1. 启动 boid daemons 处理读写的程序
(biod 8 echo -n biod )
2. 执行 mount -vat nfs 读取clients /etc/fstab 并且 mount 所
有属於 NFS-type 的files

□ NFS daemons (守护程式) 的功用

nfsd, biod, rpc.mountd, inetd, portmap 都可在/usr/etc 下找到

nfsd : 依client 对档案系统的需求情况, 而启动
" file system request daemon "
应付client 的需求,而一般file system request daemon 的数目
是 " 8 ", 这也就是我们在rc.local 中写 " nfsd 8 & "的原因了

biod : 此指令是在NFS client上用的 , 用来启动
" asynchronous block I/O daemon"
用来建立buffer cache , 处理在client上的读写

mountd : mountd 是一台 RPC server ,启动rpc.mountd daemon後 它会读取/etc/xtab 查看哪一台client正在mount 哪一个file system,并回应client 所要mount 的路径
(mountd处理的过程可用 showmount 来看)

inetd : inetd (Internet services daemon) , 当系统启动时rc.local会启动inetd 读取 inetd.conf 这一个 configuration-file ,
读取网路上所有 servers address , 连结启动inetd.conf中所有的server , 当client 请求服务时, inetd 就会为clinet 启动相关的server daemon 负责任务, 如user 用 telnet 时 , 则inetd 启动telnetd 迎合user telnet 的需求, 其馀像 ftp ,finger , rlogin 之类的应用程式 , inetd 也都会启动相对应的
daemons, ftpd, fingerd, rloingd
portmap : portmap 是一台server , 主要功能 转换 TCP/IP 通讯协定的port number 变成 RPC program number , 因为这样clinet才能做RPC calls
一般 RPC server 是被inetd 所启动的, 所以portmap 必须在inetd之前启动, 否则无法做 RPC call


□ NFS 的网路安全的

NFS 使server上的档案能被client所取用, 乍看之下好像server 上的档案任何人都可取用没什麽保护性, 其实不是如此的. 一开始server要 exportfs 之前在 /etc/exporrc 中就已经设定了档案的使用权限, 像/usr/src -access=engineering:accounting
就是限定只有 rcgineering 和 accounting 这两台 client 才能 mount /usr/src
/usr/src -access=oak,root=oak
这是说只有 oak 这台 cilent 能 mount这一个路径 且只有 oak clients superuser 才能行使 read & write 的权力

另外管理者为了维护 exported 和 mounted 的安全, 一定要建立公共(public)和私人(secret)的 key(password), 然而这些安全性问题是要建立在 NIS (network infomation system) 上的, 有一个/etc/publickey档 ,里面记录了public and secret keys , 而这些key是依照 machine_name 和 user_name ,以16 进位码表示出来的, 管理者可在有NIS的机器上用
newkey -u username 给予user login 权力
newkey -h hostname 造出login 此机器时所需的password

在NFS刚安装时 user 是 "nobody" 任何人都可以进入, 所以管理者一定要做修改, 以保护资料的安全.

当使用者为 nobody 时 publickey 的内容
netname users public key : users secret key
nobody 3d91f44568fbbefada5a7:7675cd9b8753b5db09dabf12

在管理者给予user权力之後, user 就可使用 chkey 修改自己的secret key 创造自己的帐号路口,

willow% chkey
Generating new key for username
password :
Sending key change request to server ...
Done.
willow%

所设定的password 是使用者位於 NIS 中的加密键 , 当我们login 时NIS 就会将此密码解开,(因为publickey中是以16进位码记录) , 存放到keyserv 里加以保管, 再将加密键传给client , 当client 发出请求时 ,此键会附在每个NFS 请求上一起送到 NFS server 上, 当加密键和server上所保存的keyserv核对无错後, 请求就会被接受



□ When fail to mount server

1. 用 rpcinfo -p server_name 去查看此server是否存在
2. 用 rpcinfo -u server_name mount 查看mountd daemon 是否有
在server上执行
3. 假如server 都没问题,检查 server 和 client 之间的
Ethernet connetions
4. 在client 上 用 ps ax 看portmap and several biod daemons
running or not !
(rpcinfo : 用来对 RPC server 做 RPC 呼叫,回传远端程序呼叫过程中的讯息)

一些mount 失败的错误讯息

1. /etc/mtab : No such file or directory
mtab 这一个路径或是档案必须存在,在mount之前
2. mount : ... Block device required
远端的机器名称可能打错了
3. mount: ... not found in /etc/fstab
fstab 一定要存在,client 上 /etc下
4. ... not in hosts database
/etc/hosts 没有这一个 hosts database , 或是
NIS 的daemon, ypbind 没有在执行
5. Must be root to use mount
一般都只有 root 才能mount ,所以mount之前先成
为superuser
6. Stale NFS file handle
当我们已经mount 上的file or directory ,在server
上突然被remove or unexport ,就会出现此讯息