-
Notifications
You must be signed in to change notification settings - Fork 26
forkvm: clone fork files via FICLONE reflink with sparse-copy fallback #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f750584
forkvm: clone fork files via FICLONE reflink with sparse-copy fallback
sjmiller609 ff86aba
test: force sparse path in seek-unsupported test
sjmiller609 a8c9e07
test: assert FICLONE actually reflinked in snapshot scenario
sjmiller609 9bfce24
Merge branch 'main' into hypeship/fork-reflink
sjmiller609 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| //go:build linux | ||
|
|
||
| package forkvm | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // copyRegularFileReflink attempts to clone srcPath to dstPath via FICLONE | ||
| // (reflink). On filesystems that support copy-on-write at the block layer | ||
| // (btrfs, xfs with reflink=1, zfs, bcachefs), this is effectively | ||
| // instantaneous and consumes no additional space until pages diverge. | ||
| // | ||
| // Returns ErrReflinkUnsupported when the filesystem or kernel rejects the | ||
| // operation; callers should fall back to a full-copy path. | ||
| func copyRegularFileReflink(srcPath, dstPath string, perms fs.FileMode) (retErr error) { | ||
| src, err := os.Open(srcPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer src.Close() | ||
|
|
||
| dst, err := os.OpenFile(dstPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, perms) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { | ||
| if cerr := dst.Close(); retErr == nil && cerr != nil { | ||
| retErr = cerr | ||
| } | ||
| if retErr != nil { | ||
| _ = os.Remove(dstPath) | ||
| } | ||
| }() | ||
|
|
||
| if err := unix.IoctlFileClone(int(dst.Fd()), int(src.Fd())); err != nil { | ||
| if isReflinkUnsupportedError(err) { | ||
| return fmt.Errorf("%w: FICLONE rejected for %s: %v", ErrReflinkUnsupported, srcPath, err) | ||
| } | ||
| return fmt.Errorf("FICLONE %s -> %s: %w", srcPath, dstPath, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // isReflinkUnsupportedError returns true when an FICLONE failure indicates the | ||
| // operation cannot be served by the filesystem and the caller should fall | ||
| // back. Real errors (EIO, ENOSPC) propagate as-is. | ||
| func isReflinkUnsupportedError(err error) bool { | ||
| switch { | ||
| case errors.Is(err, unix.EINVAL), | ||
| errors.Is(err, unix.ENOTSUP), | ||
| errors.Is(err, unix.EOPNOTSUPP), | ||
| errors.Is(err, unix.EXDEV), | ||
| errors.Is(err, unix.ETXTBSY), | ||
| errors.Is(err, unix.EISDIR), | ||
| errors.Is(err, unix.ENOTTY): | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //go:build !linux | ||
|
|
||
| package forkvm | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/fs" | ||
| ) | ||
|
|
||
| // copyRegularFileReflink is unavailable on non-Linux platforms. On macOS APFS | ||
| // supports clonefile(2) and could be wired up here, but we currently only | ||
| // rely on the sparse-copy fallback off-Linux. | ||
| func copyRegularFileReflink(srcPath, dstPath string, perms fs.FileMode) error { | ||
| _ = dstPath | ||
| _ = perms | ||
| return fmt.Errorf("%w: reflink unsupported on this platform: %s", ErrReflinkUnsupported, srcPath) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package forkvm | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestCopyGuestDirectory_ReflinkFallback exercises the sparse-copy fallback | ||
| // path. The reflink fast path is fs-dependent and not portable across CI | ||
| // runners; this test forces it off and verifies copy correctness. | ||
| func TestCopyGuestDirectory_ReflinkFallback(t *testing.T) { | ||
| SetReflinkDisabled(true) | ||
| t.Cleanup(func() { SetReflinkDisabled(false) }) | ||
|
|
||
| src := filepath.Join(t.TempDir(), "src") | ||
| dst := filepath.Join(t.TempDir(), "dst") | ||
|
|
||
| require.NoError(t, os.MkdirAll(src, 0755)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(src, "rootfs.ext4"), []byte("rootfs-bytes"), 0644)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(src, "config.json"), []byte(`{"x":1}`), 0644)) | ||
|
|
||
| require.NoError(t, CopyGuestDirectory(src, dst)) | ||
|
|
||
| got, err := os.ReadFile(filepath.Join(dst, "rootfs.ext4")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "rootfs-bytes", string(got)) | ||
|
|
||
| got, err = os.ReadFile(filepath.Join(dst, "config.json")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, `{"x":1}`, string(got)) | ||
| } | ||
|
|
||
| // TestCopyGuestDirectory_ReflinkAttempted verifies that with reflink enabled | ||
| // (the default), the copy still produces a correct destination on filesystems | ||
| // where FICLONE either succeeds or falls back transparently. This is the | ||
| // happy-path smoke test for the new fast path; on filesystems that don't | ||
| // support FICLONE the fallback handles correctness. | ||
| func TestCopyGuestDirectory_ReflinkAttempted(t *testing.T) { | ||
| SetReflinkDisabled(false) | ||
|
|
||
| src := filepath.Join(t.TempDir(), "src") | ||
| dst := filepath.Join(t.TempDir(), "dst") | ||
|
|
||
| require.NoError(t, os.MkdirAll(src, 0755)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(src, "rootfs.ext4"), []byte("rootfs-bytes"), 0644)) | ||
|
|
||
| require.NoError(t, CopyGuestDirectory(src, dst)) | ||
|
|
||
| got, err := os.ReadFile(filepath.Join(dst, "rootfs.ext4")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "rootfs-bytes", string(got)) | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.