Welcome to this guide on how to cache NFS Share Data with FS-Cache on Linux. Caching the NFS share data helps improve file access performance. FS-Cache is one of the tools that play an important role in caching data retrieved over the network to a local disk. This helps minimize the network traffic when accessing the files over the network.

FS-Cache is designed with a very clear approach, making it easy to use for system admins and other users. It allows a file system on the server to interact with the client’s local cache directly without the need to create another filesystem. When used together with NFS, a mount point is used to instruct the client ot mount the NFS share with FS-cache enabled. Here, the mount point uploads two kernel modules i.e fscache and cachefiles. The cache is then implemented through the communication between the cachefilesd daemon and the kernel modules.

The image below will help you understand how FS-Cache works.

Cache NFS Share Data with FS Cache 1

One amazing feature about FS-Cache is that it does not alter the basic file operation of the filesystem on the network. It only provides the filesystem with local storage to cache its data. NFS shares can still be mounted whether FS-Cache is enabled on not.

Moreso, NFS can still handle the files that cannot fit into the cache individually or collectively since files can be partially cached and do not need to be read completely upfront.

In order for caching to occur, a cache backend is required by FS-Cache. This is usually a storage driver configured to provide the caching services. Normally a block-based filesystem is required with any of the following filesystems:

  • ext3 (with extended attributes enabled)
  • ext4
  • BTRFS
  • XFS

By following this guide to the latter, you should be able to cache NFS Share Data with FS-Cache on Linux.

1. Install FS-Cache on Linux.

FS-Cache is available in default repositories of several Linux distributions. It can be installed using the command:

##On Debian/Ubuntu##
sudo apt update && sudo apt install cachefilesd

##On CentOS/Fedora/RedHat/Rocky Linux/AlmaLinux
sudo yum install cachefilesd

Once installed, start and enable the service:

sudo systemctl enable --now cachefilesd

Check if the service is running:

$ systemctl status cachefilesd
● cachefilesd.service - LSB: CacheFiles daemon
     Loaded: loaded (/etc/init.d/cachefilesd; generated)
     Active: active (exited) since Sun 2025-01-05 20:52:08 UTC; 16s ago
       Docs: man:systemd-sysv-generator(8)
      Tasks: 0 (limit: 4606)
     Memory: 0B
     CGroup: /system.slice/cachefilesd.service

Jul 08 10:07:22 ubuntu systemd[1]: Starting LSB: CacheFiles daemon...
Jul 08 10:07:22 ubuntu cachefilesd[68355]:  * cachefilesd disabled, please set>
Jul 08 10:07:22 ubuntu systemd[1]: Started LSB: CacheFiles daemon.

2. Configure FS-Cache on Linux

Once installed, you need to configure FS-Cache and set the backend directory to be used as the cache.

Open the configuration file for editing:

sudo vim /etc/cachefilesd.conf

In the file, add the backend directory:

dir /var/cache/fscache

/var/cache/fscache is the default path, however, you can use another desired and configure SELinux on Rhel-based systems using the command:

sudo semanage fcontext -a -e /var/cache/fscache /path/to/cache
sudo restorecon -Rv /path/to/cache

Replace /path/to/cache with the new cache backend directory. If the above commands fail to work, you can also use:

sudo semanage permissive -a cachefilesd_t
sudo semanage permissive -a cachefiles_kernel_t

You can also mount and use a disk partition for caching. But you need to enable user-defined extended attributes for ext3 file systems using the command:

sudo tune2fs -o user_xattr /dev/device

Replace /dev/device with the disk path. Alternatively, you can enable the extended attributes during the mount time:

sudo mount /dev/device /path/to/cache -o user_xattr

Configure Cache culling

The FS-cache daemon uses the specified backend file/disk to store remote data shared. This can pile and consume all the available free space on the disk and result in problems especially when you are using the same disk for boot.

cachefilesd tries to maintain the amount of free space on the disk by relinquishing the old objects cached. This characteristic is referred to as cache culling. This configuration can be done in the /etc/cachefilesd.conf using six control limits:

  • brun N% (percentage of blocks) & frun N% (percentage of files): this describes the amount of free space and the number of available files. If these values cache rise above the set percentage, then culling is turned off
  • bcull N% (percentage of blocks) & fcull N% (percentage of files): describes the amount of available space or the number of files and if these values fall below the set limit, then cull is started.
  • bstop N% (percentage of blocks) & fstop N% (percentage of files): here if the amount of available space or the number of available files in the cache falls below either of these limits then the allocation of disk space stops until the limits are raised above the set percentage.

This can be set in the config file by specifying the values of N as below:

....
tag mycache
brun 10%
bcull 7%
bstop 3%
frun 10%
fcull 7%
fstop 3%

The tag mycache variable is used to specify FS-Cache to use when you have multiple caches.

More configuration information can be obtained using the command:

man cachefilesd.conf

Once the desired configurations have been made, restart the service:

sudo systemctl restart cachefilesd

3. Retrieve statistical information from the FS-Cache

FS-Cache track of the general system statistics. This can information can be retrieved on Rhel-based systems using the command:

cat /proc/fs/fscache/stats

Sample output:

Cache NFS Share Data with FS Cache 1 1

4. Cache NFS Share Data with FS-Cache

NFS cannot use FS-Cache unless it is instructed to do so. The below steps are used to configure NFS Share to use FS-Cache.

I assume you already have an NFS server up and accessible. The below guides can be used to set up NFS shares on Linux:

Proceed and mount the NFS share on this client using the command with the below syntax:

# mount -t nfs <nfs-share:/ > </mount/point> -o fsc

Replace the nfs-share:/ and /mount/point appropriately. For example, mounting the shared /home path to a mount point /mnt/home:

sudo mkdir /mnt/home
sudo mount -t nfs nfs.computingforgeeks.com:/home /mnt/home -o fsc

The above command performs a temporary mounting and the settings will not survive a system reboot, To make these settings persistent, you need to make a permanent mounting by editing the /etc/fstab file:

$ sudo vim /etc/fstab
nfs.computingforgeeks.com:/home               /mnt/home      nfs fsc,auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

Once mounted, verify caching using the command:

cd /var/cache/fscache
ls -Z

Sample Output:

system_u:object_r:cachefiles_var_t:s0 cache
system_u:object_r:cachefiles_var_t:s0 graveyard

Check the state of the individual NFS shares;

$ cat /proc/fs/nfsfs/servers
NV SERVER   PORT USE HOSTNAME
v4 c0a8cd02  801   1 nfs.computingforgeeks.com

Check the state of the NFS Share volumes:

$ cat /proc/fs/nfsfs/volumes
NV SERVER   PORT DEV          FSID                              FSC
v4 c0a8cd02  801 0:91         87517e2c67364d4e:8f046303c16eb5ea yes

5. Test the FS-Cache Setup

To test if FS-Cache is working, we will try copying files from the share to the local drive. With Fs-cache configures, we will run the copy command twice and examine the time taken:

##First time
time cp /path/to/nfs/mnt/point/bigfile.gz /tmp

#### this should speed up as bigfile.gz is in cache now #####
time cp /path/to/nfs/mnt/point/bigfile.gz /dev/null

For example:

time cp /mnt/home/admin/Downloads/docker-desktop-4.9.1-amd64.deb  /tmp

Repeat the copying of the file to another location and examine the time taken this time:

time cp /mnt/home/admin/Downloads/docker-desktop-4.9.1-amd64.deb /dev/null

Sample Output:

Cache NFS Share Data with FS Cache 2

From the above output, we can all agree that the copy command was faster the second time since caching for the file has happened. That marks the end of this guide on how to cache NFS Share Data with FS-Cache on Linux. I hope this was significant.

See more:

Enable NFS File Service in VMware vSAN Storage Cluster

Configure NFS Filesystem as OpenNebula Datastores

How To Configure Samba Share on Debian

Install and Configure Samba Share on Ubuntu

LEAVE A REPLY

Please enter your comment!
Please enter your name here