Skip to content

Commit f896d93

Browse files
authored
vetu clone: opportunistically zero-copy of files on Linux (#46)
1 parent 5bdaa13 commit f896d93

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

internal/storage/temporary/temporary.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"github.com/cirruslabs/vetu/internal/sparseio"
88
"github.com/cirruslabs/vetu/internal/vmconfig"
99
"github.com/cirruslabs/vetu/internal/vmdirectory"
10+
"github.com/cirruslabs/vetu/internal/zerocopy"
1011
"github.com/google/uuid"
12+
"golang.org/x/sys/unix"
1113
"os"
1214
"path/filepath"
1315
)
@@ -61,8 +63,15 @@ func CreateFrom(srcDir string) (*vmdirectory.VMDirectory, error) {
6163
return nil, err
6264
}
6365

64-
if err := sparseio.Copy(dstFile, srcFile); err != nil {
65-
return nil, err
66+
if err := zerocopy.Clone(int(dstFile.Fd()), int(srcFile.Fd())); err != nil {
67+
if !errors.Is(err, unix.ENOTSUP) {
68+
return nil, err
69+
}
70+
71+
// Fall back to slower sparse I/O copying if zero-copy is not supported
72+
if err := sparseio.Copy(dstFile, srcFile); err != nil {
73+
return nil, err
74+
}
6675
}
6776

6877
if err := srcFile.Close(); err != nil {

internal/zerocopy/zerocopy_linux.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package zerocopy
2+
3+
import (
4+
"golang.org/x/sys/unix"
5+
)
6+
7+
func Clone(destFd int, srcFd int) error {
8+
return unix.IoctlFileClone(destFd, srcFd)
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !linux
2+
3+
package zerocopy
4+
5+
import "golang.org/x/sys/unix"
6+
7+
func Clone(destFd int, srcFd int) error {
8+
return unix.ENOTSUP
9+
}

0 commit comments

Comments
 (0)