How Velarx storage works
Table of Contents
In the last post on Velarx I gave an overview of the engine and ran a few benchmarks against Podman. Here I want to look at how the storage layer is put together. The design comes down to a few simple boundaries: untrusted fetchers bring bytes into staging over varlink, the engine verifies every file and builds the layer images itself, a storage backend (either the built-in composefs driver or an external process over varlink) stores those verified files and returns detached lower mounts, and an fs-verity chain of trust runs from the image signature down to the pages the kernel reads.
A composefs store, one layer at a time#
The built-in store layout is composefs.
The composefs driver keeps all of its files under one composefs/
directory that nothing else in the engine opens directly: regular files
live once in composefs/objects/, named by their fs-verity digest, and
each layer gets an EROFS image of metadata next to them whose regular
files are metacopy stubs pointing into objects/. Nothing is ever
unpacked into a directory tree. The engine’s own metadata sits beside
it: one SQLite database for images, tags, layers, containers and
volumes, a layers/ directory holding each layer’s compressed table of
contents and tar-split (checked by digest whenever they are read), and a
store.toml marker tying the store directory to the database so two
engines cannot write to the same store by mistake.
The difference from a single-image composefs repository is that Velarx
builds one EROFS image per layer instead of one per image. Because a
layer’s EROFS image is made from its table of contents alone, it is the
same file in every image that uses that layer, whatever sits below it.
A base layer shared by twenty images is built once and stacked as a
lower layer for all of them. On rootful kernels, mounting those EROFS
files with superblock_share lets all of those containers share the
same EROFS superblock and inode cache too, as in the
EROFS superblock sharing post.
For locking and garbage collection, Velarx uses the same flock() idea
from the composefs locking post.
Every active pull holds a shared flock on its staging directory under
staging/, and every running container keeps a shared flock on the
EROFS images of its layers. To work down to Linux 6.12 without needing
the newer FSCONFIG_SET_FD EROFS mount path, the engine passes the
monitor a lease socket holding those open, locked descriptors for as
long as the container lives. Garbage collection tries a non-blocking
exclusive lock and skips anything that fails. If a process dies, the
kernel drops its locks and there are no stale lock files to clean up.
Files inside the image, and NFS#
Keeping every file in composefs/objects/ is great for deduplication,
and it is what makes lazy pulls and peer-to-peer sharing work. For very
small files, though, having a separate object file on disk and a
metacopy redirect in EROFS is extra overhead.
Stores are configured through [[store]] in config.toml, and a
composefs store takes an inline_threshold option (or --storage-opt inline_threshold=N for one command) that puts regular files up to a
given size directly inside the layer’s EROFS image instead of objects/.
At 0 (the default) nothing is inlined and everything is a stub. At
-1, every file with content lives inside the EROFS image, so each
layer becomes a single self-contained file.
This also makes shared read-only stores simple. One machine populates a
store with inline_threshold = -1 and exports it read-only over NFS,
and the other machines attach it in their [[store]] entry:
|
|
Readers never write to or lock the export, and they can run containers
from it rootful or rootless without copying anything or running an
extra daemon. They can still pull or build new layers into their own
local store on top of base layers sitting on the export. Testing this
on NFSv3 and NFSv4 turned up a neat kernel detail: when a container
executes a binary backed by an object on NFS, overlayfs opens the
backing object for execve and NFS checks execute permission on the
server, which is why objects in the pool are stored as mode 0555
while the container still sees the permissions from the EROFS image.
Instead of checking filesystem types by name, the engine also probes
the features a store needs at startup and falls back when O_TMPFILE
or RENAME_NOREPLACE are missing, so even a writable store works on
NFS as far as the kernel allows.
Splitting the engine with varlink#
The engine runs with access to the store and the container runtime, so I do not want it parsing HTTP responses, tar streams, zstd frames or peer-to-peer traffic in the same process. Instead, every piece that talks to the outside world runs as a separate process over varlink on a Unix domain socket.
Varlink is a good fit here because it is just NUL-terminated JSON paired
with SCM_RIGHTS file descriptor passing on the same socket.
Credentials, manifests and tables of contents cross as sealed memfds;
staging directories, fetched files and detached mounts cross as open
file descriptors. Sockets live in 0700 directories and both ends
check SO_PEERCRED, so only the engine’s user or root can talk to them.
Fetchers#
Every content source implements the same org.velarx.Fetcher interface:
the registry client (velarx-registry), the local directory and archive
helper (velarx-dir), the peer-to-peer daemon (velarx-p2p), or a
small custom cache helper. The engine itself knows nothing about
libp2p or caches. It just walks [pull] sources in order:
|
|
A source can be a long-running service on a socket, or a stateless command that the engine spawns for one pull with the varlink socket on descriptor 3.
A fetcher never sees the store, and the engine doesn’t trust anything it writes. Before parsing untrusted bytes, the built-in fetchers confine themselves with Landlock and seccomp. For each layer, the engine creates a temporary staging directory and hands the fetcher a descriptor to it. The fetcher writes each file into staging, turns fs-verity on, and tells the engine which digest it thinks it staged.
The engine treats that filename as nothing more than a claim. It opens
the file without following symlinks and asks the kernel for its
fs-verity digest. Because fs-verity makes an inode immutable, if the
kernel’s measurement matches the table of contents the engine links that
inode into a private put/ directory of verified files that the fetcher
never saw. On a filesystem without fs-verity, the engine refuses to
link a file that another process could still hold writable; it copies
and hashes the bytes itself.
Lazy pulls (--lazy) work through the same pipeline. For zstd:chunked
images, the engine fetches only the table of contents up front so the
container can start right away. When the container reads files that are
not in the store yet, velarx-monitor --image-server batches those
digests per layer over org.velarx.ImageFetch. The engine fetches them
through the configured fetchers ahead of background work, measures each
open file descriptor and hands the descriptors back in the reply, and
the image server still measures the descriptor itself before serving it.
External stores over varlink#
A [[store]] can also point at a program (kind = "command") or a
running service (kind = "socket") that speaks org.velarx.Storage:
|
|
The engine owns the network, the credentials, the database, the
verification and the overlay stacking. When a pull finishes, the engine
hands the external store an open descriptor to the verified put/
directory plus the EROFS layer images as sealed memfds (Put), and
calls Sync. When a container starts, the store answers Mount with
detached lower mounts (plus any lease or server pidfd) and the digests
it measured on each layer. The engine checks those measured digests
against the manifest, makes sure no layer path escapes its mount, and
stacks the overlay itself. Only plain data and file descriptors cross
the socket.
The chain of trust#
In image sealing with composefs,
the root of trust is a digest on the kernel command line. In Velarx, it
is either a manifest digest (NAME@sha256:... or --verify-image) or a
sigstore or GPG signature accepted by policy.json.
Every layer descriptor in a manifest Velarx writes carries an
io.containers.composefs.digest annotation with the fs-verity digest of
that layer’s EROFS image. Because the signature covers the manifest, it
covers every layer’s EROFS image too.
At container start, the check is split across the engine, the container monitor and the kernel:
The engine hashes the stored manifest, opens each layer’s EROFS image
once, measures its fs-verity digest in constant time, and mounts those
exact open descriptors via EROFS (or hands them to
velarx-monitor --image-server when running rootless or lazily) before
stacking the overlay itself in one place. Whenever digests were
measured, the engine mounts the overlay with verity=require. Because
the lower layers come from the measured descriptors rather than a path
lookup, nothing can be swapped between the check and the mount.
Then velarx-monitor attaches that detached overlay mount and reads it
back from the kernel with statmount. If the kernel does not report an
overlay with verity=require (or an active FUSE image server when
running rootless on the fallback path), the monitor refuses to start the
container. From there the kernel takes over: overlayfs matches every
stub against its backing file in composefs/objects/ on open, and
fs-verity verifies every 4 KiB page against its Merkle tree on read.
Files inlined inside a layer’s EROFS image are covered by that image’s
own fs-verity tree.
What this does and does not cover#
By default (verity_chain = "auto"), Velarx turns the chain on whenever
the filesystem has fs-verity, the image is complete and the container
has a root of trust. When a manifest carries io.containers.composefs.digest
it checks against that; otherwise it falls back to the digest recorded
in SQLite when the layer was built. That catches disk corruption and
tampering with files under composefs/, though someone with write
access to both SQLite and the files could update the record and swap the
EROFS image together.
Setting composefs_digests = "require" in config.toml (or passing
--security-opt verity=enforce for one container) closes that gap.
Every layer must match the digest declared in the manifest, and the
manifest is hashed back to the signed or pinned digest on every start,
so editing the database and the EROFS files together is caught too.
Inlined layers change the EROFS bytes, so they are held to their local
record rather than strict manifest digests, and the container’s writable
upper layer is always outside the chain.