Merge pull request #8362 from gabriel-samfira/fix-non-c-volume
Fix non C volumes on Windows
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/containerd/continuity/fs"
|
||||
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
"github.com/containerd/containerd/containers"
|
||||
@@ -55,7 +56,7 @@ func WithNewSnapshot(id string, i containerd.Image, opts ...snapshots.Opt) conta
|
||||
// WithVolumes copies ownership of volume in rootfs to its corresponding host path.
|
||||
// It doesn't update runtime spec.
|
||||
// The passed in map is a host path to container path map for all volumes.
|
||||
func WithVolumes(volumeMounts map[string]string) containerd.NewContainerOpts {
|
||||
func WithVolumes(volumeMounts map[string]string, platform imagespec.Platform) containerd.NewContainerOpts {
|
||||
return func(ctx context.Context, client *containerd.Client, c *containers.Container) (err error) {
|
||||
if c.Snapshotter == "" {
|
||||
return errors.New("no snapshotter set for container")
|
||||
@@ -97,8 +98,24 @@ func WithVolumes(volumeMounts map[string]string) containerd.NewContainerOpts {
|
||||
}()
|
||||
|
||||
for host, volume := range volumeMounts {
|
||||
// The volume may have been defined with a C: prefix, which we can't use here.
|
||||
volume = strings.TrimPrefix(volume, "C:")
|
||||
if platform.OS == "windows" {
|
||||
// Windows allows volume mounts in subfolders under C: and as any other drive letter like D:, E:, etc.
|
||||
// An image may contain files inside a folder defined as a VOLUME in a Dockerfile. On Windows, images
|
||||
// can only contain pre-existing files for volumes situated on the root filesystem, which is C:.
|
||||
// For any other volumes, we need to skip attempting to copy existing contents.
|
||||
//
|
||||
// C:\some\volume --> \some\volume
|
||||
// D:\some\volume --> skip
|
||||
if len(volume) >= 2 && string(volume[1]) == ":" {
|
||||
// Perform a case insensitive comparison to "C", and skip non-C mounted volumes.
|
||||
if !strings.EqualFold(string(volume[0]), "c") {
|
||||
continue
|
||||
}
|
||||
// This is a volume mounted somewhere under C:\. We strip the drive letter and allow fs.RootPath()
|
||||
// to append the remaining path to the rootfs path as seen by the host OS.
|
||||
volume = volume[2:]
|
||||
}
|
||||
}
|
||||
src, err := fs.RootPath(root, volume)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rootpath on mountPath %s, volume %s: %w", root, volume, err)
|
||||
|
||||
@@ -223,7 +223,7 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
|
||||
for _, v := range volumeMounts {
|
||||
mountMap[filepath.Clean(v.HostPath)] = v.ContainerPath
|
||||
}
|
||||
opts = append(opts, customopts.WithVolumes(mountMap))
|
||||
opts = append(opts, customopts.WithVolumes(mountMap, platform))
|
||||
}
|
||||
meta.ImageRef = image.ID
|
||||
meta.StopSignal = image.ImageSpec.Config.StopSignal
|
||||
|
||||
@@ -203,7 +203,14 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
|
||||
for _, v := range volumeMounts {
|
||||
mountMap[filepath.Clean(v.HostPath)] = v.ContainerPath
|
||||
}
|
||||
opts = append(opts, customopts.WithVolumes(mountMap))
|
||||
platform := imagespec.Platform{
|
||||
OS: image.ImageSpec.OS,
|
||||
Architecture: image.ImageSpec.Architecture,
|
||||
OSVersion: image.ImageSpec.OSVersion,
|
||||
OSFeatures: image.ImageSpec.OSFeatures,
|
||||
Variant: image.ImageSpec.Variant,
|
||||
}
|
||||
opts = append(opts, customopts.WithVolumes(mountMap, platform))
|
||||
}
|
||||
meta.ImageRef = image.ID
|
||||
meta.StopSignal = image.ImageSpec.Config.StopSignal
|
||||
|
||||
Reference in New Issue
Block a user