mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-19 07:49:17 +00:00
runtime: Copy shared files recursively
This patch enables recursive file copying when filesystem sharing is not used. Signed-off-by: Yohei Ueda <yohei@jp.ibm.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -239,23 +240,43 @@ func (f *FilesystemShare) ShareFile(ctx context.Context, c *Container, m *Mount)
|
|||||||
if !caps.IsFsSharingSupported() {
|
if !caps.IsFsSharingSupported() {
|
||||||
f.Logger().Debug("filesystem sharing is not supported, files will be copied")
|
f.Logger().Debug("filesystem sharing is not supported, files will be copied")
|
||||||
|
|
||||||
fileInfo, err := os.Stat(m.Source)
|
var ignored bool
|
||||||
if err != nil {
|
srcRoot := filepath.Clean(m.Source)
|
||||||
return nil, err
|
|
||||||
|
walk := func(srcPath string, d fs.DirEntry, err error) error {
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := d.Info()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !(info.Mode().IsRegular() || info.Mode().IsDir() || (info.Mode()&os.ModeSymlink) == os.ModeSymlink) {
|
||||||
|
f.Logger().WithField("ignored-file", srcPath).Debug("Ignoring non-regular file as FS sharing not supported")
|
||||||
|
if srcPath == srcRoot {
|
||||||
|
// Ignore the mount if this is not a regular file (excludes socket, device, ...) as it cannot be handled by
|
||||||
|
// a simple copy. But this should not be treated as an error, only as a limitation.
|
||||||
|
ignored = true
|
||||||
|
return filepath.SkipDir
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
dstPath := filepath.Join(guestPath, srcPath[len(srcRoot):])
|
||||||
|
|
||||||
|
return f.sandbox.agent.copyFile(ctx, srcPath, dstPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore the mount if this is not a regular file (excludes
|
if err := filepath.WalkDir(srcRoot, walk); err != nil {
|
||||||
// directory, socket, device, ...) as it cannot be handled by
|
c.Logger().WithField("failed-file", m.Source).Debugf("failed to copy file to sandbox: %v", err)
|
||||||
// a simple copy. But this should not be treated as an error,
|
return nil, err
|
||||||
// only as a limitation.
|
}
|
||||||
if !fileInfo.Mode().IsRegular() {
|
if ignored {
|
||||||
f.Logger().WithField("ignored-file", m.Source).Debug("Ignoring non-regular file as FS sharing not supported")
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := f.sandbox.agent.copyFile(ctx, m.Source, guestPath); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// These mounts are created in the shared dir
|
// These mounts are created in the shared dir
|
||||||
mountDest := filepath.Join(getMountPath(f.sandbox.ID()), filename)
|
mountDest := filepath.Join(getMountPath(f.sandbox.ID()), filename)
|
||||||
|
@@ -10,6 +10,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -2119,40 +2120,57 @@ func (k *kataAgent) setGuestDateTime(ctx context.Context, tv time.Time) error {
|
|||||||
func (k *kataAgent) copyFile(ctx context.Context, src, dst string) error {
|
func (k *kataAgent) copyFile(ctx context.Context, src, dst string) error {
|
||||||
var st unix.Stat_t
|
var st unix.Stat_t
|
||||||
|
|
||||||
err := unix.Stat(src, &st)
|
err := unix.Lstat(src, &st)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Could not get file %s information: %v", src, err)
|
return fmt.Errorf("Could not get file %s information: %v", src, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := os.ReadFile(src)
|
cpReq := &grpc.CopyFileRequest{
|
||||||
if err != nil {
|
Path: dst,
|
||||||
return fmt.Errorf("Could not read file %s: %v", src, err)
|
DirMode: uint32(DirMode),
|
||||||
|
FileMode: st.Mode,
|
||||||
|
Uid: int32(st.Uid),
|
||||||
|
Gid: int32(st.Gid),
|
||||||
}
|
}
|
||||||
|
|
||||||
fileSize := int64(len(b))
|
var b []byte
|
||||||
|
|
||||||
|
switch sflag := st.Mode & unix.S_IFMT; sflag {
|
||||||
|
case unix.S_IFREG:
|
||||||
|
var err error
|
||||||
|
// TODO: Support incrementail file copying instead of loading whole file into memory
|
||||||
|
b, err = ioutil.ReadFile(src)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Could not read file %s: %v", src, err)
|
||||||
|
}
|
||||||
|
cpReq.FileSize = int64(len(b))
|
||||||
|
|
||||||
|
case unix.S_IFDIR:
|
||||||
|
|
||||||
|
case unix.S_IFLNK:
|
||||||
|
symlink, err := os.Readlink(src)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Could not read symlink %s: %v", src, err)
|
||||||
|
}
|
||||||
|
cpReq.Data = []byte(symlink)
|
||||||
|
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("Unsupported file type: %o", sflag)
|
||||||
|
}
|
||||||
|
|
||||||
k.Logger().WithFields(logrus.Fields{
|
k.Logger().WithFields(logrus.Fields{
|
||||||
"source": src,
|
"source": src,
|
||||||
"dest": dst,
|
"dest": dst,
|
||||||
}).Debugf("Copying file from host to guest")
|
}).Debugf("Copying file from host to guest")
|
||||||
|
|
||||||
cpReq := &grpc.CopyFileRequest{
|
|
||||||
Path: dst,
|
|
||||||
DirMode: uint32(DirMode),
|
|
||||||
FileMode: uint32(st.Mode),
|
|
||||||
FileSize: fileSize,
|
|
||||||
Uid: int32(st.Uid),
|
|
||||||
Gid: int32(st.Gid),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle the special case where the file is empty
|
// Handle the special case where the file is empty
|
||||||
if fileSize == 0 {
|
if cpReq.FileSize == 0 {
|
||||||
_, err = k.sendReq(ctx, cpReq)
|
_, err := k.sendReq(ctx, cpReq)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy file by parts if it's needed
|
// Copy file by parts if it's needed
|
||||||
remainingBytes := fileSize
|
remainingBytes := cpReq.FileSize
|
||||||
offset := int64(0)
|
offset := int64(0)
|
||||||
for remainingBytes > 0 {
|
for remainingBytes > 0 {
|
||||||
bytesToCopy := int64(len(b))
|
bytesToCopy := int64(len(b))
|
||||||
|
Reference in New Issue
Block a user