Notes on unionfs and nullfs.

unionfs is a type of filesystem that allows you to combine two directory trees into one, where the contents of both are visible from the mount point of the filesystem. The filesystem can be mounted either with mount or mount_unionfs:

mount -t unionfs [directory] [mountpoint]
mount_unionfs [directory] [mountpoint]

Running either of the above will union mount [directory] onto [mountpoint], making the contents of both visible in the latter. Important to note is that [directory] becomes the upper layer, and [mountpoint], the lower layer. The upper layer is essentially where the changes made at the [mountpoint] persist after umount. Adding -o below, makes [directory] the lower layer and [mountpoint] the upper layer.

For files with the same name in both layers, the upper layer file is visible from [mountpoint]. This may be an issue if you still need to access the lower layer file (i.e. if your lower layer is [mountpoint], as in the default behavior of unionfs mount). One way to keep access to the lower layer file is to use the nullfs loopback filesystem to duplicate the mountpoint’s tree elsewhere before employing unionfs:

mount -t nullfs [unionfs mountpoint] [copy mountpoint] # or use mount_nullfs
mount -t unionfs [directory] [unionfs mountpoint]

The above will let you access just the original files in [unionfs mountpoint] from [copy mountpoint]. Additionally, whatever changes you make in [copy mountpoint] will take effect and persist in [directory].

Another usage model might look like this:

mount -t nullfs [shared directory] [copy mountpoint]
mount -t unionfs [directory] [copy mountpoint]

In this case, if the two commands are repeated for different [directory]’s and [copy mountpoint]’s, each [directory] will have access to identical copies of [shared directory]. Having access to multiple identical copies of the same directory is useful when, for example, setting up the ports tree for multiple jails in a space-efficient way.

This contrasts with the previous case, where each [directory] will have access to the incremental results of previous unionfs invocations.

As a side-note, nullfs’s functions might be compared to using the --bind option for mount on Linux 2.4.0 and later.