2594: preserve hardlinks during COPY --from - part 1 - #630
Merged
Conversation
mzihlmann
force-pushed
the
2594-hardlinks-part-1
branch
3 times, most recently
from
April 7, 2026 19:53
9848e5d to
98e46ac
Compare
mzihlmann
marked this pull request as ready for review
April 7, 2026 20:35
Collaborator
Author
|
to be continued here #626 |
mzihlmann
force-pushed
the
2594-hardlinks-part-1
branch
4 times, most recently
from
April 13, 2026 09:07
9dcb168 to
9dc0e6a
Compare
CopyDir passed all non-symlink, non-directory files to CopyFile, which always created a new independent file. Hardlinks are indistinguishable from regular files via os.Lstat, so the shared inode was silently lost. At snapshot time, checkHardlink found Nlink==1 on each copied file and emitted regular tar entries, the TypeLink relationship was gone. CopyDir now tracks inodes in a map[uint64]string across the walk. On the first occurrence of an inode the file is copied normally and the destination path is recorded. On subsequent occurrences os.Link is used instead of CopyFile, preserving the hardlink in the destination tree. checkHardlink then finds Nlink>1 at snapshot time and emits the correct tar.TypeLink entries, matching Docker/BuildKit behaviour.
mzihlmann
force-pushed
the
2594-hardlinks-part-1
branch
from
April 15, 2026 06:17
9dc0e6a to
a3d0140
Compare
mzihlmann
force-pushed
the
2594-hardlinks-part-1
branch
from
April 15, 2026 06:20
a3d0140 to
a4d23c4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When copying a directory via
COPY --from=<image>, kaniko passed every file toCopyFile, which creates a new independent inode for each one. Hardlinks in the source stage were silently broken, causing files that shared a single inode to be duplicated in the output image. For images that rely heavily on hardlinks (e.g. git installations), this inflates image size significantly, one reporter saw 83 MB grow to 720 MB.CopyDirnow tracks inodes across the directory walk. On the first occurrence of an inode the file is copied normally. On subsequent occurrencesos.Linkis used instead, preserving the hardlink. At snapshot time we already handle hardlinks correctly.This is a first part in a series of fixes, here we are concerned with copying from a remote image, in a later PR we will address copying from local stages.
Gated behind
FF_KANIKO_PRESERVE_HARDLINKS=falseto preserve backwards compatibility. Becomes default in v1.28.0.