Skip to content

Commit 791e175

Browse files
committed
Windows: Fixes Windows containers with image volumes
Currently, there are few issues that preventing containers with image volumes to properly start on Windows. - Unlike the Linux implementation, the Container volume mount paths were not created if they didn't exist. Those paths are now created. - while copying the image volume contents to the container volume, the layers were not properly deactivated, which means that the container can't start since those layers are still open. The layers are now properly deactivated, allowing the container to start. - even if the above issue didn't exist, the Windows implementation of mount/Mount.Mount deactivates the layers, which wouldn't allow us to copy files from them. The layers are now deactivated after we've copied the necessary files from them. - the target argument of the Windows implementation of mount/Mount.Mount was unused, which means that folder was always empty. We're now symlinking the Layer Mount Path into the target folder. - hcsshim needs its Container Mount Paths to be properly formated, to be prefixed by C:. This was an issue for Volumes defined with Linux-like paths (e.g.: /test_dir). filepath.Abs solves this issue. Signed-off-by: Claudiu Belu <cbelu@cloudbasesolutions.com>
1 parent 7ddf5e5 commit 791e175

File tree

3 files changed

+67
-26
lines changed

3 files changed

+67
-26
lines changed

mount/mount_windows.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package mount
1818

1919
import (
2020
"encoding/json"
21+
"os"
2122
"path/filepath"
2223
"strings"
2324

@@ -50,15 +51,21 @@ func (m *Mount) Mount(target string) error {
5051
if err = hcsshim.ActivateLayer(di, layerID); err != nil {
5152
return errors.Wrapf(err, "failed to activate layer %s", m.Source)
5253
}
53-
defer func() {
54-
if err != nil {
55-
hcsshim.DeactivateLayer(di, layerID)
56-
}
57-
}()
5854

5955
if err = hcsshim.PrepareLayer(di, layerID, parentLayerPaths); err != nil {
6056
return errors.Wrapf(err, "failed to prepare layer %s", m.Source)
6157
}
58+
59+
// We can link the layer mount path to the given target. It is an UNC path, and it needs
60+
// a trailing backslash.
61+
mountPath, err := hcsshim.GetLayerMountPath(di, layerID)
62+
if err != nil {
63+
return errors.Wrapf(err, "failed to get layer mount path for %s", m.Source)
64+
}
65+
mountPath = mountPath + `\`
66+
if err = os.Symlink(mountPath, target); err != nil {
67+
return errors.Wrapf(err, "failed to link mount to taget %s", target)
68+
}
6269
return nil
6370
}
6471

pkg/cri/opts/container.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"path/filepath"
24+
goruntime "runtime"
25+
"strings"
2426

2527
"github.com/containerd/containerd"
2628
"github.com/containerd/containerd/containers"
@@ -76,29 +78,51 @@ func WithVolumes(volumeMounts map[string]string) containerd.NewContainerOpts {
7678
// refer to https://github.com/containerd/containerd/pull/1868
7779
// https://github.com/containerd/containerd/pull/1785
7880
defer os.Remove(root) // nolint: errcheck
79-
if err := mount.All(mounts, root); err != nil {
80-
return errors.Wrap(err, "failed to mount")
81-
}
82-
defer func() {
83-
if uerr := mount.Unmount(root, 0); uerr != nil {
84-
log.G(ctx).WithError(uerr).Errorf("Failed to unmount snapshot %q", c.SnapshotKey)
81+
82+
unmounter := func(mountPath string) {
83+
if uerr := mount.Unmount(mountPath, 0); uerr != nil {
84+
log.G(ctx).WithError(uerr).Errorf("Failed to unmount snapshot %q", root)
8585
if err == nil {
8686
err = uerr
8787
}
8888
}
89-
}()
89+
}
9090

91-
for host, volume := range volumeMounts {
92-
src := filepath.Join(root, volume)
93-
if _, err := os.Stat(src); err != nil {
94-
if os.IsNotExist(err) {
95-
// Skip copying directory if it does not exist.
96-
continue
91+
var mountPaths []string
92+
if goruntime.GOOS == "windows" {
93+
for _, m := range mounts {
94+
// appending the layerID to the root.
95+
mountPath := filepath.Join(root, filepath.Base(m.Source))
96+
mountPaths = append(mountPaths, mountPath)
97+
if err := m.Mount(mountPath); err != nil {
98+
return err
9799
}
98-
return errors.Wrap(err, "stat volume in rootfs")
100+
101+
defer unmounter(m.Source)
102+
}
103+
} else {
104+
mountPaths = append(mountPaths, root)
105+
if err := mount.All(mounts, root); err != nil {
106+
return errors.Wrap(err, "failed to mount")
99107
}
100-
if err := copyExistingContents(src, host); err != nil {
101-
return errors.Wrap(err, "taking runtime copy of volume")
108+
defer unmounter(root)
109+
}
110+
111+
for host, volume := range volumeMounts {
112+
// The volume may have been defined with a C: prefix, which we can't use here.
113+
volume = strings.TrimPrefix(volume, "C:")
114+
for _, mountPath := range mountPaths {
115+
src := filepath.Join(mountPath, volume)
116+
if _, err := os.Stat(src); err != nil {
117+
if os.IsNotExist(err) {
118+
// Skip copying directory if it does not exist.
119+
continue
120+
}
121+
return errors.Wrap(err, "stat volume in rootfs")
122+
}
123+
if err := copyExistingContents(src, host); err != nil {
124+
return errors.Wrap(err, "taking runtime copy of volume")
125+
}
102126
}
103127
}
104128
return nil

pkg/cri/opts/spec_windows.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package opts
1818

1919
import (
2020
"context"
21+
"os"
2122
"path/filepath"
2223
"sort"
2324
"strings"
@@ -119,19 +120,28 @@ func WithWindowsMounts(osi osinterface.OS, config *runtime.ContainerConfig, extr
119120
// paths, so don't use it.
120121
if !namedPipePath(src) {
121122
if _, err := osi.Stat(src); err != nil {
122-
// If the source doesn't exist, return an error instead
123-
// of creating the source. This aligns with Docker's
124-
// behavior on windows.
125-
return errors.Wrapf(err, "failed to stat %q", src)
123+
// Create the host path if it doesn't exist. This will align
124+
// the behavior with the Linux implementation, but it doesn't
125+
// align with Docker's behavior on Windows.
126+
if !os.IsNotExist(err) {
127+
return errors.Wrapf(err, "failed to stat %q", src)
128+
}
129+
if err := osi.MkdirAll(src, 0755); err != nil {
130+
return errors.Wrapf(err, "failed to mkdir %q", src)
131+
}
126132
}
127133
var err error
128134
src, err = osi.ResolveSymbolicLink(src)
129135
if err != nil {
130136
return errors.Wrapf(err, "failed to resolve symlink %q", src)
131137
}
132-
// hcsshim requires clean path, especially '/' -> '\'.
138+
// hcsshim requires clean path, especially '/' -> '\'. Additionally,
139+
// for the destination, absolute paths should have the C: prefix.
133140
src = filepath.Clean(src)
134141
dst = filepath.Clean(dst)
142+
if dst[0] == '\\' {
143+
dst = "C:" + dst
144+
}
135145
}
136146

137147
var options []string

0 commit comments

Comments
 (0)