Network File System (NFS)
NFS is the original *nix file sharing protocol. Using NFS, you can share the file system resources (as well as printers) across a network. Hence, other hosts on the network can access the remote storage, just like their local storage.
NFS was developed in 1970 by Sun Microsystems, and has evolved hugely since then. Nowadays NFS is an integrated part of every *nix operating system.
NFS is a client/server protocol, which means a server shares its storage resources and other client(s) can access the shared resources on the server. The clients and servers should be configured separately using FreeBSD's built-in NFS utilities.
NFS server has three main daemons that should be running in order to serve storage resources across the network:
- The nfsd(8) daemon services NFS requests from client machines.
- The rpcbind(8) daemon maps Remote Procedure Call (RPC) program numbers to universal addresses and lets NFS clients know which ports the NFS server is using to serve requests.
- The mountd(8) daemon processes incoming mount requests.
Enabling the NFS server is as simple as adding appropriate variables to /etc/ rc.conf, and starting the daemons (or restarting the server):
Running NFS server is the easiest part. Now all you need to do is to specify which resources should be shared (exported), as well as access policies for each export.
This is done using the /etc/exports file. The /etc/exports file contains information about every resource you want to share, as well as some export options and the hosts you want to permit to have access to each resource.
The above example shows the /etc/exports file that specifies three exported directories.
/usr/ports/distfiles -network 192.168.11.0 -mask=255.255.255.0 /cdrom -ro 192.168.0.14 /public -ro -mapall=nobody
The first line exports the /usr/ports/distfiles directory. The share is only accessible for 192.168.11.0/24 subnet, and is exported with read/write permission (which is available by default).
The second line in the above code, exports the /cdrom directory with read-only permission (hence the -ro parameter), only for the host 192.168.0.14.
And the third line exports /public directory as read-only for any host, and everyone can access the contents of this export with user access set to nobody. The -mapall=nobody maps all users to local user nobody.
FreeBSD can mount NFS shares using mount(8) or mount_nfs(8) utility. All you need to do is to prepare the host to be able to mount NFS shares, by enabling NFS client modules:
Adding the above variable to the /etc/rc.conf file ensures that the current operating environment is prepared to mount NFS shares.
You also need to run the NFS client rc script manually, for the first time:
# /etc/rc.d/nfsclient start
You can now mount the NFS shares using the mount(8) utility. The following example shows how to mount one of the exports we have done recently:
# mount 192.168.0.5:/usr/ports/distfiles /mnt
This will mount the export /usr/ports/distfiles from host 192.168.0.5 on the /mnt mount point. You can then verify if the share has been mounted:
# mount /dev/da0s1a on / (ufs, local) devfs on /dev (devfs, local) /dev/da0s1e on /tmp (ufs, local, soft-updates) /dev/da0s1f on /usr (ufs, local, soft-updates) /dev/da0s1d on /var (ufs, local, soft-updates) fdescfs on /dev/fd (fdescfs) 192.168.0.5:/usr/ports/distfiles on /mnt (nfs)
You can then unmount the mounted share using the umount(8) command:
# umount /mnt
Mounting a NFS share manually is quite straightforward. This is useful when you want to mount a share temporarily, any time. But it is highly likely that you may want to have your mounts activated automatically, during system boot. Just as in every other file system you can achieve this by adding necessary mounting information to the /etc/fstab file. However, this might be a little bit tricky. The following example shows mounting the same share we used in the recent example via the /etc/fstab file. To achieve this, the following line should be added to the fstab file:
192.168.0.5:/usr/ports/distfiles /mnt nfs -b 0 0
This line in the fstab file will mount the share /usr/ports/distfiles from 192.168.0.5 on /mnt, during system boot up.
Normally your system will fail to boot if the specified share is not available and therefore cannot be mounted. This is the reason why -b flag is used. This flag tells mount_nfs(8) to fork a background process if it fails to mount the share for any reason, and retry the mount in the background. This will avoid any boot problem if your share is being mounted from the fstab file during the boot-up process.
Locking files over NFS protocol is not enabled by the default configuration. There may be certain circumstances when you may need to have the file locking feature on your NFS mounts, just as in the local file system. To achieve this, you have to enable rpc.lockd(8) and rpc.statd(8) on both NFS server and its clients. The rpc. lockd(8) daemon in conjunction with rpc.statd(8) takes care of file and record locking in an NFS environment.
The following lines should be added to the /etc/rc.conf file on NFS server and its clients in order to enable file locking over NFS:
And then start the rpc.lockd daemon manually, if you do not want to reboot your host:
# /etc/rc.d/lockd start