Tuesday, July 27, 2010

NFS Server


Introduction

I use NFS to provide simple NAS services to my home network. I'll be exporting the directories '/home' and '/srv/media' on the NFS server so they are accessible toNFS clients on my home network.
This is a really simple setup, which is not super secure, but ideal for my needs at home. I want the '/srv/media' export to be read/write to all clients on my home network so we can share a common source of Shared Documents, Music, Pictures, Videos, etc. Each user also mounts their own home directory, on the NFS server, for personal data.

NFS Server

 sudo aptitude install nfs-kernel-server

Creating the exports

NFS clients typically access an NFS share as the user 'nobody'. However, the '/home' directory isn't owned by 'nobody', it is owned by the user. In order to enable read and write access to the '/home' export via NFS we need to instruct the NFS server that all accesses should be made as 'root' to the '/home' export.
However, the '/srv/media' directory doesn't exist, so we can create it and change ownership to 'nobody' and 'nogroup'.
 mkdir -p /srv/media/Backups
 mkdir -p /srv/media/Documents
 mkdir -p /srv/media/Music
 mkdir -p /srv/media/Podcasts
 mkdir -p /srv/media/Pictures
 mkdir -p /srv/media/Videos
 chown -R nobody:nogroup /srv/media
 chmod 2775 /srv/media
Setup the NFS exports.
 sudo nano /etc/exports
Sample
 /home        192.168.1.0/24(rw,sync,no_root_squash,no_subtree_check)
 /srv/media   192.168.1.0/24(rw,sync,all_squash,no_subtree_check)
Whenever '/etc/exports' is modified, you need to run...
  exportfs -r -v
...to re-export the new file systems.

Tune the NFS server

I am running my NFS server on an NLSU2 with just 32mb RAM, therefore I tweak the NFS server a little to try and use less resources.
 nano /etc/default/nfs-kernel-server 
Sample
 # Number of servers to start up
 RPCNFSDCOUNT=2

 # Do you want to start the svcgssd daemon? It is only required for Kerberos
 # exports. Valid alternatives are "yes" and "no"; the default is "no".
 NEED_SVCGSSD=no
More NFS server tuning.
 vi /etc/default/nfs-common
Sample
 # Do you want to start the idmapd daemon? It is only needed for NFSv4.
 NEED_IDMAPD=no

 # Do you want to start the gssd daemon? It is required for Kerberos mounts.
 NEED_GSSD=no

NFS Clients

 sudo aptitude install nfs-common
Make the mount points
 mkdir -p ~/Backups
 mkdir -p ~/Documents
 mkdir -p ~/Public
 mkdir -p ~/Music
 mkdir -p ~/Podcasts
 mkdir -p ~/Pictures
 mkdir -p ~/Videos
Setup the fstab, so that the exports will be automatically mounted at boot time.
 sudo nano /etc/fstab
Sample
 slug:/home/username               /home/username/Documents                  nfs     defaults        0       0
 slug:/srv/media/Backups         /home/username/Backups                    nfs     defaults        0       0
 slug:/srv/media/Music           /home/username/Music                      nfs     defaults        0       0
 slug:/srv/media/Podcasts        /home/username/Podcasts                   nfs     defaults        0       0
 slug:/srv/media/Pictures        /home/username/Pictures                   nfs     defaults        0       0
 slug:/srv/media/shared-Documents       /home/username/Public        nfs     defaults        0       0
 slug:/srv/media/Videos          /home/username/Videos                     nfs     defaults        0       0
You will notice that one of my mount points has a space in the name. From the fstab man page...
The second field, (fs_file), describes the mount point for the filesystem. For swap partitions, this field should be specified as ‘none’. If the name of the mount point contains spaces these can be escaped as ‘\040’.
You could mount each NFS export manually now they are in fstab, such as...
  sudo mount /home/username/Documents
Or, you can just mount them all...
  sudo mount -a

Useful NFS Related Commands

Query RPC services

 rpcinfo -p slug

Show NFS exports

 showmount -e slug

NFS statistics

 nfsstat

Show detailed mount infomartion

 cat /proc/mounts

Simple Performance Testing

The default Debian NFS mount options are optimal, you can measure the performance as follows.

Write test

Where '/mnt/test' is a NFS mounted filesystem.
 time dd if=/dev/zero of=/mnt/test/benchmark bs=1024k count=1024
Output
 1024+0 records in
 1024+0 records out
 1073741824 bytes (1.1 GB) copied, 230.803 seconds, 4.7 MB/s
 
 real    3m51.018s
 user    0m0.008s
 sys     0m13.097s

Read test

Where '/mnt/test' is a NFS mounted filesystem.
 time dd if=/mnt/test/benchmark of=/dev/null bs=1024k
Output
 1024+0 records in
 1024+0 records out
 1073741824 bytes (1.1 GB) copied, 150.198 seconds, 7.1 MB/s

 real    2m30.283s
 user    0m0.016s
 sys     0m1.472s
Make sure you clean up afterwards ;-)
 rm /mnt/test/benchmark
References