Somebody always wants the same directory visible inside a jail without copying it. Ports tree, a shared distfiles cache, a log directory the host wants to read. The answer on FreeBSD has been nullfs for a very long time, and it is about as complicated as a hard link that works on directories.
The mount
mount -t nullfs -o ro /usr/ports /jails/web/usr/ports
That is the whole thing. /usr/ports on the host now also appears at /jails/web/usr/ports. nullfs is a pass-through layer: it does not copy anything and it does not have its own storage. Whatever is below shows through. Write to the lower directory and the jail sees it on the next read.
-o ro makes the nullfs view read-only. The lower filesystem stays writable from wherever it is mounted normally, so the host can update the ports tree while the jail can only read it. That one flag is most of the reason to use this instead of a copy.
Check what you built with mount -t nullfs. Remove it with umount /jails/web/usr/ports.
Make it survive a reboot
For a jail, the cleanest place is an fstab file that the jail config points at, not the host’s /etc/fstab:
# /jails/web.fstab
/usr/ports /jails/web/usr/ports nullfs ro 0 0
Then in jail.conf(5):
web {
mount.fstab = "/jails/web.fstab";
...
}
The jail framework mounts these on start and unmounts them on stop, so the mounts live and die with the jail. Put them in the host fstab and you get a mount that outlives its jail, and a jail that fails to start when the host boots in the wrong order. Related reading: jails on ZFS covers the dataset side of the same setup.
Where it bites
- The mount point must exist inside the jail. nullfs mounts over a directory, it does not create one.
mkdir -pit first, or the mount fails with a plain error and you stare at fstab for ten minutes. - Mount order matters. Mount
/jails/web/usr/portsbefore the jail’s own filesystems are in place and you are mounting onto something that is about to be covered. Order fstab lines parent first, and unmount in reverse. - Read-only is per mount. A jail that can also see the lower path some other way can still write there.
roconstrains the nullfs view, not the data. If you need the data frozen, that is a different layer. - It is not a security boundary by itself. A writable nullfs of a directory the jail does not own is a hole you dug yourself. Mount the narrowest directory that does the job, and
rounless you have a reason. Pair it with rctl limits if the jail is untrusted. - Busy unmounts. If
umountsays the filesystem is busy, something inside has a file open. fstat tells you who. Reach forumount -fafter you know, not before. - Device numbers differ. A file seen through nullfs reports a different
st_devthan the same file on the lower filesystem. Tools that compare device and inode to detect “same file” can be fooled. Rare, but it explains the odd backup-tool complaint.
When not to bother
If the jail needs its own copy because it will modify it, use a ZFS clone and be done. nullfs is for sharing one real directory, not for making cheap writable copies. The man page, nullfs(5), is short. Read the one on your release, since the behaviour around caching and locking has been reworked over the years and I will not pretend my memory beats your man page. More in the FreeBSD and OpenBSD hub.
