2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2015-07-16 21:14:58 +00:00
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2015-07-16 21:14:58 +00:00
|
|
|
|
2016-01-20 23:32:02 +00:00
|
|
|
import (
|
2022-10-12 23:51:20 +00:00
|
|
|
"context"
|
2022-10-07 22:28:45 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2023-08-26 13:24:46 +00:00
|
|
|
"github.com/docker/docker/api/types/events"
|
2016-04-27 02:26:12 +00:00
|
|
|
"github.com/docker/docker/container"
|
2022-09-27 20:17:27 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2022-10-07 22:28:45 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2018-04-17 20:50:28 +00:00
|
|
|
volumemounts "github.com/docker/docker/volume/mounts"
|
2022-09-27 20:17:27 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-01-20 23:32:02 +00:00
|
|
|
)
|
2015-11-12 19:55:17 +00:00
|
|
|
|
2022-10-07 22:28:45 +00:00
|
|
|
// containerStatPath stats the filesystem resource at the specified path in this
|
|
|
|
// container. Returns stat info about the resource.
|
|
|
|
func (daemon *Daemon) containerStatPath(container *container.Container, path string) (stat *types.ContainerPathStat, err error) {
|
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
cfs, err := daemon.openContainerFS(container)
|
2022-10-07 22:28:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-12 23:51:20 +00:00
|
|
|
defer cfs.Close()
|
2022-10-07 22:28:45 +00:00
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
return cfs.Stat(context.TODO(), path)
|
2022-10-07 22:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// containerArchivePath creates an archive of the filesystem resource at the specified
|
|
|
|
// path in this container. Returns a tar archive of the resource and stat info
|
|
|
|
// about the resource.
|
|
|
|
func (daemon *Daemon) containerArchivePath(container *container.Container, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error) {
|
|
|
|
container.Lock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// Wait to unlock the container until the archive is fully read
|
|
|
|
// (see the ReadCloseWrapper func below) or if there is an error
|
|
|
|
// before that occurs.
|
|
|
|
container.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
cfs, err := daemon.openContainerFS(container)
|
|
|
|
if err != nil {
|
2022-10-07 22:28:45 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2022-10-12 23:51:20 +00:00
|
|
|
cfs.Close()
|
2022-10-07 22:28:45 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
absPath := archive.PreserveTrailingDotOrSeparator(filepath.Join("/", path), path)
|
2022-10-07 22:28:45 +00:00
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
stat, err = cfs.Stat(context.TODO(), absPath)
|
2022-10-07 22:28:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
sourceDir, sourceBase := absPath, "."
|
2022-10-07 22:28:45 +00:00
|
|
|
if stat.Mode&os.ModeDir == 0 { // not dir
|
2022-10-12 23:51:20 +00:00
|
|
|
sourceDir, sourceBase = filepath.Split(absPath)
|
2022-10-07 22:28:45 +00:00
|
|
|
}
|
|
|
|
opts := archive.TarResourceRebaseOpts(sourceBase, filepath.Base(absPath))
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
tb, err := archive.NewTarballer(sourceDir, opts)
|
2022-10-07 22:28:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
cfs.GoInFS(context.TODO(), tb.Do)
|
|
|
|
data := tb.Reader()
|
2022-10-07 22:28:45 +00:00
|
|
|
content = ioutils.NewReadCloserWrapper(data, func() error {
|
|
|
|
err := data.Close()
|
2022-10-12 23:51:20 +00:00
|
|
|
_ = cfs.Close()
|
2022-10-07 22:28:45 +00:00
|
|
|
container.Unlock()
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2023-08-26 13:24:46 +00:00
|
|
|
daemon.LogContainerEvent(container, events.ActionArchivePath)
|
2022-10-07 22:28:45 +00:00
|
|
|
|
|
|
|
return content, stat, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// containerExtractToDir extracts the given tar archive to the specified location in the
|
|
|
|
// filesystem of this container. The given path must be of a directory in the
|
|
|
|
// container. If it is not, the error will be an errdefs.InvalidParameter. If
|
|
|
|
// noOverwriteDirNonDir is true then it will be an error if unpacking the
|
|
|
|
// given content would cause an existing directory to be replaced with a non-
|
|
|
|
// directory and vice versa.
|
|
|
|
func (daemon *Daemon) containerExtractToDir(container *container.Container, path string, copyUIDGID, noOverwriteDirNonDir bool, content io.Reader) (err error) {
|
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
cfs, err := daemon.openContainerFS(container)
|
2022-10-07 22:28:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-12 23:51:20 +00:00
|
|
|
defer cfs.Close()
|
2022-10-07 22:28:45 +00:00
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
err = cfs.RunInFS(context.TODO(), func() error {
|
|
|
|
// The destination path needs to be resolved with all symbolic links
|
|
|
|
// followed. Note that we need to also evaluate the last path element if
|
|
|
|
// it is a symlink. This is so that you can extract an archive to a
|
|
|
|
// symlink that points to a directory.
|
|
|
|
absPath, err := filepath.EvalSymlinks(filepath.Join("/", path))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
absPath = archive.PreserveTrailingDotOrSeparator(absPath, path)
|
2022-10-07 22:28:45 +00:00
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
stat, err := os.Lstat(absPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !stat.IsDir() {
|
|
|
|
return errdefs.InvalidParameter(errors.New("extraction point is not a directory"))
|
|
|
|
}
|
2022-10-07 22:28:45 +00:00
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
// Need to check if the path is in a volume. If it is, it cannot be in a
|
|
|
|
// read-only volume. If it is not in a volume, the container cannot be
|
|
|
|
// configured with a read-only rootfs.
|
|
|
|
toVolume, err := checkIfPathIsInAVolume(container, absPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-07 22:28:45 +00:00
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
if !toVolume && container.HostConfig.ReadonlyRootfs {
|
|
|
|
return errdefs.InvalidParameter(errors.New("container rootfs is marked read-only"))
|
|
|
|
}
|
2022-10-07 22:28:45 +00:00
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
options := daemon.defaultTarCopyOptions(noOverwriteDirNonDir)
|
2022-10-07 22:28:45 +00:00
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
if copyUIDGID {
|
|
|
|
var err error
|
|
|
|
// tarCopyOptions will appropriately pull in the right uid/gid for the
|
|
|
|
// user/group and will set the options.
|
|
|
|
options, err = daemon.tarCopyOptions(container, noOverwriteDirNonDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-07 22:28:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
return archive.Untar(content, absPath, options)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-10-07 22:28:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-26 13:24:46 +00:00
|
|
|
daemon.LogContainerEvent(container, events.ActionExtractToDir)
|
2022-10-07 22:28:45 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) containerCopy(container *container.Container, resource string) (rc io.ReadCloser, err error) {
|
|
|
|
container.Lock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// Wait to unlock the container until the archive is fully read
|
|
|
|
// (see the ReadCloseWrapper func below) or if there is an error
|
|
|
|
// before that occurs.
|
|
|
|
container.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
cfs, err := daemon.openContainerFS(container)
|
|
|
|
if err != nil {
|
2022-10-07 22:28:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2022-10-12 23:51:20 +00:00
|
|
|
cfs.Close()
|
2022-10-07 22:28:45 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
err = cfs.RunInFS(context.TODO(), func() error {
|
|
|
|
_, err := os.Stat(resource)
|
|
|
|
return err
|
|
|
|
})
|
2022-10-07 22:28:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-12 23:51:20 +00:00
|
|
|
|
|
|
|
tb, err := archive.NewTarballer(resource, &archive.TarOptions{
|
|
|
|
Compression: archive.Uncompressed,
|
|
|
|
})
|
2022-10-07 22:28:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-12 23:51:20 +00:00
|
|
|
cfs.GoInFS(context.TODO(), tb.Do)
|
|
|
|
archv := tb.Reader()
|
2022-10-07 22:28:45 +00:00
|
|
|
reader := ioutils.NewReadCloserWrapper(archv, func() error {
|
|
|
|
err := archv.Close()
|
2022-10-12 23:51:20 +00:00
|
|
|
_ = cfs.Close()
|
2022-10-07 22:28:45 +00:00
|
|
|
container.Unlock()
|
|
|
|
return err
|
|
|
|
})
|
2023-08-26 13:24:46 +00:00
|
|
|
daemon.LogContainerEvent(container, events.ActionCopy)
|
2022-10-07 22:28:45 +00:00
|
|
|
return reader, nil
|
|
|
|
}
|
|
|
|
|
2015-07-16 21:14:58 +00:00
|
|
|
// checkIfPathIsInAVolume checks if the path is in a volume. If it is, it
|
|
|
|
// cannot be in a read-only volume. If it is not in a volume, the container
|
|
|
|
// cannot be configured with a read-only rootfs.
|
2015-11-12 19:55:17 +00:00
|
|
|
func checkIfPathIsInAVolume(container *container.Container, absPath string) (bool, error) {
|
2015-07-16 21:14:58 +00:00
|
|
|
var toVolume bool
|
2021-06-11 19:01:18 +00:00
|
|
|
parser := volumemounts.NewParser()
|
2015-07-16 21:14:58 +00:00
|
|
|
for _, mnt := range container.MountPoints {
|
2017-08-01 17:32:44 +00:00
|
|
|
if toVolume = parser.HasResource(mnt, absPath); toVolume {
|
2015-07-16 21:14:58 +00:00
|
|
|
if mnt.RW {
|
|
|
|
break
|
|
|
|
}
|
2022-09-27 20:17:27 +00:00
|
|
|
return false, errdefs.InvalidParameter(errors.New("mounted volume is marked read-only"))
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return toVolume, nil
|
|
|
|
}
|