Transform Your Raspberry Pi Home Lab into a Network File System

Transform Your Raspberry Pi Home Lab into a Network File System
Introduction: Add a shared file system to your home lab using NFS server.
Word count: 8554, estimated reading time: 12 minutes
https://linux.cn/article-12413-1.htmlAuthor: Chris CollinsTranslator: Xingyu.Wang

Add a shared file system to your home lab using NFS server.

A shared file system is a great way to add versatility and functionality to your home lab. Sharing a centralized file system for clients in the lab makes organizing data, backing up, and sharing data quite easy. This is especially useful for web applications running on multiple servers and persistent volumes used in Kubernetes, as it allows for rotating pods with persistent data across any number of nodes.

Whether your home lab consists of regular computers, spare enterprise servers, or Raspberry Pi or other single-board computers (SBCs), a shared file system is a valuable asset, and an NFS server is a great way to create a shared file system.

I previously wrote about building a “home private cloud,” which is a home lab consisting of Raspberry Pi or other SBCs, possibly along with some other consumer-grade hardware or desktop PCs. The NFS server is an ideal way to share data between these components. Since most SBCs run their operating systems from SD cards, there are some challenges. In particular, the failure rate of SD cards increases when used as the operating system disk for computers, as they are not designed for constant read/write operations. What you really need is a real hard drive: they are usually cheaper per GB than SD cards, especially for larger disks, and are less likely to fail continuously. The Raspberry Pi 4 now has USB 3.0 ports, and USB 3.0 hard drives are widely available and affordable. This is a perfect match. In this project, I will use a 2TB USB 3.0 external hard drive plugged into the Raspberry Pi 4 running the NFS server.

Transform Your Raspberry Pi Home Lab into a Network File System

Raspberry Pi with a USB hard disk

Transform Your Raspberry Pi Home Lab into a Network File System

Install NFS server software

I run Fedora server on my Raspberry Pi, but this project can also run on other distributions. To run the NFS server on Fedora, you need the nfs-utils package, and fortunately, it is already installed (at least in Fedora 31). If you plan to run NFSv3 service, you also need the rpcbind package, but it is not strictly required for NFSv4.

If these packages are not already on your system, install them using the dnf command.

# Install nfs-utils and rpcbind
$ sudo dnf install nfs-utils rpcbind

Raspbian is another popular operating system used with Raspberry Pi, and the setup is almost identical. The package names are different, but that is the only major difference. To install the NFS server on a system running Raspbian, you need the following packages.

nfs-common: These files are common files for NFS servers and clients.
nfs-kernel-server: The main NFS server package.

Raspbian uses apt-get to manage packages (instead of using dnf like Fedora), so use that to install the packages.

# For Raspbian system, use apt-get to install NFS packages
$ sudo apt-get install nfs-common nfs-kernel-server

Transform Your Raspberry Pi Home Lab into a Network File System

Prepare a USB hard drive as storage device

As I mentioned above, USB hard drives are a good choice for providing storage for Raspberry Pi or other SBCs, especially since SD cards used for operating system disk images are not suitable for this purpose. For a home private cloud, you can use inexpensive USB 3.0 hard drives for large-scale storage. Insert the disk and use fdisk to find the device ID assigned to it, and you can start working with it.

# Use fdisk to find your hard drive
# Irrelevant disk information has been omitted
$ sudo fdisk -l

Disk /dev/sda: 1.84 TiB, 2000398933504 bytes, 3907029167 sectors
Disk model: BUP Slim BK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe3345ae9

Device     Boot Start        End    Sectors  Size Id Type
/dev/sda1        2048 3907028991 3907026944  1.8T 83 Linux

To be clear, in the output above, I omitted all other disk information except for the one I am interested in. You can see that the USB disk I want to use has been assigned the device /dev/sda, and you can see some information about the model (Disk model: BUP Slim BK), which helps me identify the correct disk. The disk already has a partition, and its size confirms that it is the disk I am looking for.

Note: Make sure to correctly identify the disk and partition of your device. It may differ from the example above.

Each partition created on the drive has a special universally unique identifier (UUID). The computer uses UUIDs to ensure that it mounts the correct partition to the correct location using the /etc/fstab configuration file. You can retrieve the UUID of the partition using the blkid command.

# Get the block device attributes of the partition
# Make sure you are using the correct partition, it should differ.
$ sudo blkid /dev/sda1

/dev/sda1: LABEL="backup" UUID="bd44867c-447c-4f85-8dbf-dc6b9bc65c91" TYPE="xfs" PARTUUID="e3345ae9-01"

Here, the UUID of /dev/sda1 is bd44867c-447c-4f85-8dbf-dc6b9bc65c91. Your UUID will differ, so make sure to note it down.

Transform Your Raspberry Pi Home Lab into a Network File System

Configure Raspberry Pi to mount this disk at boot and mount it

Now that you have identified the disk and partition you want to use, you need to tell the computer how to mount it, and to do so at every boot. Let’s go ahead and mount it. Since this is a USB disk, it may be removed, so you also want to configure the Raspberry Pi not to wait if the disk is not inserted or there are other unavailability issues at boot time.

In Linux, you include the partition in the /etc/fstab configuration file to tell the computer how to handle it by specifying where you want it mounted and some options. This example will mount the partition at /srv/nfs, so first create that path:

# Create the mount point for the disk partition
$ sudo mkdir -p /srv/nfs

Next, modify the /etc/fstab file using the following syntax:

<disk id>     <mountpoint>      <filesystem type>     <options>     <fs_freq> <fs_passno>

Use the UUID you identified earlier as the disk ID. As I mentioned in the previous step, the mount point is /srv/nfs. For the filesystem type, it is generally best to choose its actual filesystem, but since this is a USB disk, use auto.

For the options value, use nosuid,nodev,nofail.

Transform Your Raspberry Pi Home Lab into a Network File System

Sidebar on manual pages

Actually, there are many possible options, and the manual pages (man) are the best way to view them. Checking the manual page for fstab is a great place to start.

# Open the manual page for fstab
$ man fstab

This will open the manual/documentation related to the fstab command. In the manual page, each option is broken down into different content to show its purpose and common choices. For example, the “fourth field (fs_mntopts)” gives some basic information about the options available in that field and directs you to man 8 mount for a deeper description of mount options. This makes sense because the /etc/fstab file essentially tells the computer how to automatically mount disks, just like you would manually use the mount command.

You can get more information about the options you will use from the manual page for mount. The number 8 indicates the section of the manual page. Here, section 8 is system management tools and daemons.

You can get a list of standard sections from the manual page for man.

Back to mounting disks, let’s take a look at man 8 mount.

# Open the mount manual page in section 8
$ man 8 mount

In this manual page, you can see the purpose of the options listed above.

nosuid: Ignore suid/guid bits. No files on the USB drive are allowed to execute as root. This is a good security practice.
nodev: Do not recognize character or block special devices in the filesystem, i.e., ignore any device nodes on the USB drive. Another good security practice.
nofail: Do not log any errors if the device does not exist. This is a USB drive that may not be inserted, so in that case, it will be ignored.

Back to the line you are adding to the /etc/fstab file, there are two last options: fs_freq and fs_passno. Their values relate to some obsolete options, and most modern systems use 0 for both options, especially for filesystems on USB disks. The value of fs_freq relates to the dump command and filesystem dumps. The value of fs_passno defines the filesystem to be fsck at boot time and its order, with the root partition typically being 1 and other filesystems being 2, setting this value to 0 to skip using fsck on that partition.

Using your favorite editor, open the /etc/fstab file and add the entry for the partition on the USB drive, replacing the values here with the ones obtained in the previous steps.

# With sudo, or as root, add the partition info to the /etc/fstab file
UUID="bd44867c-447c-4f85-8dbf-dc6b9bc65c91"    /srv/nfs    auto    nosuid,nodev,nofail,noatime 0 0

Transform Your Raspberry Pi Home Lab into a Network File System

Enable and start the NFS server

With the packages installed and the partition added to your /etc/fstab file, you can now start the NFS server. On Fedora systems, you need to enable and start two services: rpcbind and nfs-server. Use the systemctl command to do this.

# Start NFS server and rpcbind
$ sudo systemctl enable rpcbind.service
$ sudo systemctl enable nfs-server.service
$ sudo systemctl start rpcbind.service
$ sudo systemctl start nfs-server.service

On Raspbian or other Debian-based distributions, you only need to use the systemctl command to enable and start the nfs-kernel-server service, the same way as above.

Transform Your Raspberry Pi Home Lab into a Network File System

RPCBind

The rpcbind tool is used to map remote procedure call (RPC) services to the ports they listen on. According to the rpcbind manual page:

“When an RPC service starts, it tells rpcbind the address it is listening on and the RPC program number it is ready to serve. When a client wants to make an RPC call to a given program number, it first contacts rpcbind on the server machine to determine where the RPC request should be sent.”

In the case of the NFS server, rpcbind maps the NFS protocol number to the port that the NFS server is listening on. However, NFSv4 does not require rpcbind. If you only use NFSv4 (by removing versions 2 and 3 from the configuration), you do not need to use rpcbind. I include it here for backward compatibility with NFSv3.

Transform Your Raspberry Pi Home Lab into a Network File System

Export the mounted filesystem

The NFS server uses another configuration file, /etc/exports, to decide which filesystems to share (export) with which remote clients. This file is simply a mapping of IP addresses (or subnets) to the filesystems to share, along with some options (read-only or read-write, root squash, etc.). The format of the file is:

<directory>     <host>(options)

In this example, you will export the partition mounted at /srv/nfs. This is the “directory” part.

The second part, the host, includes the host you want to export this partition to. These hosts can be a single host: specify using a fully qualified domain name (FQDN) or hostname, or the host’s IP address; or a group of hosts: use wildcard characters to match domains (like *.example.org), IP networks (like classless inter-domain routing CIDR notation), or netgroups.

The third part includes the options applied to the export.

ro/rw: Export the filesystem as read-only or read-write.
wdelay: Delay writing to disk if another write is imminent to improve performance (this may not be as useful if you are using solid-state USB disks).
root_squash: Prevent any root user on the client from having root privileges on the host and set the root UID to nfsnobody as a security precaution.

Test exporting the partition mounted at /srv/nfs to a client — for example, a laptop. Determine the IP address of your client (my laptop is 192.168.2.64, but yours may differ). You can share it to a large subnet, but for testing, limit it to a single IP address. The CIDR notation for this IP is 192.168.2.64/32, where /32 represents a single IP.

Edit the /etc/exports file with your favorite editor, writing your directory, host CIDR, and rw and root_squash options.

# Edit your /etc/exports file like this, replacing with information from your system
/srv/nfs    192.168.2.64/32(rw,root_squash)

Note: If you copied the /etc/exports file from somewhere else or overwrote the original file with a copy, you may need to restore the SELinux context of the file. You can use the restorecon command to restore it.

# Restore the SELinux context of the /etc/exports file
$ sudo restorecon /etc/exports

Once done, restart the NFS server to receive the changes to the /etc/exports file.

# Restart the NFS server
$ sudo systemctl restart nfs-server.service

Transform Your Raspberry Pi Home Lab into a Network File System

Open the firewall for NFS service

Some systems do not run firewall services by default. For example, Raspbian has open iptables rules by default, and ports opened for different services are immediately available from outside the machine. In contrast, Fedora server runs the firewalld service by default, so you must open the ports for the NFS server (and rpcbind, if you will use NFSv3). You can do this using the firewall-cmd command.

Check the zone used by firewalld and get the default zone. For Fedora server, this is the FedoraServer zone.

# List zones
# Omitted some output for brevity
$ sudo firewall-cmd --list-all-zones

# Get default zone info
# Note the default zone
$ sudo firewall-cmd --get-default-zone

# Permanently add nfs service to allowed ports list
$ sudo firewall-cmd --add-service=nfs --permanent

# For NFSv3, we need to add some more ports: nfsv3, rpc-mountd, rpc-bind
$ sudo firewall-cmd --add-service=(nfs3,mountd,rpc-bind)

# View services of the default zone, replacing with the default zone used in your system
$ sudo firewall-cmd --list-services --zone=FedoraServer

# If all is good, reload firewalld
$ sudo firewall-cmd --reload

And there you have it, you have successfully configured the NFS server with your mounted USB disk partition and exported it to your test system for sharing. Now you can test mounting it on the system you added to the export list.

Transform Your Raspberry Pi Home Lab into a Network File System

Test NFS export

First, create a file to read in the /srv/nfs directory on the NFS server.

# Create a test file to share
echo "Can you see this?" >> /srv/nfs/nfs_test

Now, on the client system you added to the export list, first ensure the NFS client package is installed. On Fedora systems, it is the nfs-utils package, which can be installed with dnf. The Raspbian system has the libnfs-utils package, which can be installed with apt-get.

Install the NFS client package:

# Install nfs-utils package with dnf
$ sudo dnf install nfs-utils

Once the client package is installed, you can test the NFS export. Similarly on the client, use the mount command with the NFS server IP and export path, and mount it to a location on the client, in this test, the /mnt directory. In this example, my NFS server’s IP is 192.168.2.109, but yours may differ.

# Mount the NFS server output to the client host
# Make sure to replace with your host's corresponding information
$ sudo mount 192.168.2.109:/srv/nfs /mnt

# Check if nfs_test file is visible
$ cat /mnt/nfs_test
yes, I can see this!

Success! You now have a working NFS server that can share files with multiple hosts, allowing multiple read/write access and providing centralized storage and backup for your data. There are many options for shared storage in a home lab, but NFS is an old, efficient choice that can be added to your “home private cloud” home lab. Future articles in this series will expand on how to automatically mount NFS shares on clients and how to use NFS as a storage class for Kubernetes persistent volumes.

via: https://opensource.com/article/20/5/nfs-raspberry-pi

Author: Chris Collins Topic: lujun9972 Translator: wxy Proofreader: wxy

This article is originally compiled by LCTT and proudly presented by Linux China

Transform Your Raspberry Pi Home Lab into a Network File System

Transform Your Raspberry Pi Home Lab into a Network File System

Leave a Comment

Your email address will not be published. Required fields are marked *