Overlayfs

From wikinotes

overlayfs is a fuse filesystem that allows you to merge multiple directories into one filesystem.
overlayfs mounts can be stacked together.

Documentation

official docs https://www.kernel.org/doc/html/latest/filesystems/overlayfs.html

Usage

Writable Mounts

The basic idea is that you combine one or more lower layers with a higher layer.
If a file exists in both layers, the higher layer's file is used.

# mount a single lower dir with a higher dir
mount -t overlay overlay \
  -o lowerdir=/lower,upperdir=/upper,workdir=/work \
  /merged

# mount multiple lower dirs with a higher dir (separated by ':')
# Directories stacked !!RIGHT TO LEFT!!
mount -t overlay overlay \
  -o lowerdir=/lower3:/lower2:/lower1,upperdir=/upper,workdir=/work \
  /merged

ReadOnly Mounts

If dirs are read-only, you don't need to care about lower/upper layer semantics.
Mounts are read-only automatically if upperdir is not specified.

NOTE:

mounts are mounted in order RIGHT TO LEFT.
Files in below lower1 are loaded first, and overlaid by lower3

mount -t overlay overlay \
  -o lowerdir=/lower3:/lower2:/lower1
  /merged

Identifying Source Layers

I haven't tried it yet, but the nfs_export mount option looks very promising.

Troubleshooting

mount(2) system call failed: Stale file handle

disabling index and metacopy seems to work.

mount -t overlay overlay \
  -o index=off \
  -o metacopy=off \
  -o lowerdir=/lower3:/lower2:/lower1,upperdir=/upper,workdir=/work \
  /merged