Skip to content

mkcomposefs: Add --hardlinks - #446

Open
cgwalters wants to merge 3 commits into
composefs:mainfrom
cgwalters:mkcomposefs-hardlinks
Open

cgwalters wants to merge 3 commits into
composefs:mainfrom
cgwalters:mkcomposefs-hardlinks

Conversation

@cgwalters

Copy link
Copy Markdown
Contributor

Closes: #444

Comment thread libcomposefs/lcfs-writer.c Resolved
lcfs_build() is a recursive directory walker, but the only state it
threads through recursive calls today is the plain buildflags int.
To support detecting hardlinks across an entire directory tree (a
link's two names can live in arbitrary subdirectories), we need a
lookup table that persists across the whole walk rather than just a
single readdir() loop.

Split the existing body into a static lcfs_build_internal() that
takes a struct lcfs_build_ctx * instead of a bare buildflags int, and
turn the public lcfs_build() into a thin wrapper that sets up the
context and delegates to it. For now the context only holds
buildflags, so this is a pure refactor with no behavior change; a
follow-up commit adds the hardlink tracking state.

Assisted-by: AI
Signed-off-by: Colin Walters <[email protected]>
lcfs_build() walks a directory tree via readdir()/fstatat() and never
looks at st_ino/st_dev, so two paths that are hardlinked to the same
inode on disk silently become two independent inodes with nlink=1 in
the composefs image. This causes fs-verity digest mismatches against
hardlink-aware tools and a mounted image that misreports nlink/size
(composefs#444).

Add an opt-in LCFS_BUILD_TRACK_HARDLINKS flag that keeps a (dev,ino)
-> node hash table alive for the whole recursive walk (a link's two
names can be anywhere under the root, not just siblings) and, on a
repeat inode, wires up a hardlink via the existing
lcfs_node_make_hardlink() instead of re-reading the file content.

This is opt-in and not the default, matching composefs-rs's own
mkcomposefs-compatible implementation, which requires the same
--hardlinks flag to enable deduplication. Directories from
scanning a real filesystem today rely on every path becoming an
independent inode, and changing that by default would change digests
for existing consumers (RHEL9, ostree, etc.) that build composefs
images from directories.

Tracking is also unconditional on st_nlink, i.e. it doesn't skip
looking up a file just because its st_nlink report is 1: some
filesystems (e.g. fuse-overlayfs, see
containers/fuse-overlayfs#435) can misreport that field.

Every non-directory type is tracked (regular files, symlinks, FIFOs,
device nodes, sockets), since all of them are hardlinkable on disk
(e.g. via `ln -P`) and composefs-rs's own scanner doesn't special-case
regular files either. The one deliberate exception is a chardev with
rdev=0: composefs represents an overlayfs whiteout this way, and the
EROFS writer already rejects such an inode outright if it ends up
with nlink>1 (a whiteout marks the absence of a file, so hardlinking
two of them together is semantically invalid) -- but only when
*loading* an image, not when writing one. Leave those untracked so a
directory scan can't silently produce an image composefs itself would
refuse to read back.

Assisted-by: AI
Signed-off-by: Colin Walters <[email protected]>
Wire up LCFS_BUILD_TRACK_HARDLINKS behind a new --hardlinks CLI
option. The name matches composefs-rs's own drop-in mkcomposefs
reimplementation, which likewise defaults to not tracking hardlinks
(bit-for-bit compatible with this tool) and requires --hardlinks to
opt into deduplication.

The flag only affects the directory-scan path; combined with
--from-file it is a harmless no-op since dumpfiles already encode
hardlinks explicitly via the '@' prefix.

Assisted-by: AI
Closes: composefs#444
Signed-off-by: Colin Walters <[email protected]>
@cgwalters
cgwalters force-pushed the mkcomposefs-hardlinks branch from 49dfcd3 to a819a1b Compare July 30, 2026 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mkcomposefs: building from a directory silently loses hardlinks

2 participants