|
| 1 | +// +build windows |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright The containerd Authors. |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package lcow |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "io" |
| 24 | + "os/exec" |
| 25 | + "path" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/containerd/containerd/archive/compression" |
| 29 | + "github.com/containerd/containerd/content" |
| 30 | + "github.com/containerd/containerd/diff" |
| 31 | + "github.com/containerd/containerd/errdefs" |
| 32 | + "github.com/containerd/containerd/images" |
| 33 | + "github.com/containerd/containerd/log" |
| 34 | + "github.com/containerd/containerd/metadata" |
| 35 | + "github.com/containerd/containerd/mount" |
| 36 | + "github.com/containerd/containerd/plugin" |
| 37 | + digest "github.com/opencontainers/go-digest" |
| 38 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 39 | + "github.com/pkg/errors" |
| 40 | + "github.com/sirupsen/logrus" |
| 41 | +) |
| 42 | + |
| 43 | +func init() { |
| 44 | + plugin.Register(&plugin.Registration{ |
| 45 | + Type: plugin.DiffPlugin, |
| 46 | + ID: "windows-lcow", |
| 47 | + Requires: []plugin.Type{ |
| 48 | + plugin.MetadataPlugin, |
| 49 | + }, |
| 50 | + InitFn: func(ic *plugin.InitContext) (interface{}, error) { |
| 51 | + md, err := ic.Get(plugin.MetadataPlugin) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + ic.Meta.Platforms = append(ic.Meta.Platforms, ocispec.Platform{ |
| 57 | + OS: "linux", |
| 58 | + Architecture: "amd64", |
| 59 | + }) |
| 60 | + return NewWindowsLcowDiff(md.(*metadata.DB).ContentStore()) |
| 61 | + }, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +// CompareApplier handles both comparison and |
| 66 | +// application of layer diffs. |
| 67 | +type CompareApplier interface { |
| 68 | + diff.Applier |
| 69 | + diff.Comparer |
| 70 | +} |
| 71 | + |
| 72 | +// windowsLcowDiff does filesystem comparison and application |
| 73 | +// for Windows specific Linux layer diffs. |
| 74 | +type windowsLcowDiff struct { |
| 75 | + store content.Store |
| 76 | +} |
| 77 | + |
| 78 | +var emptyDesc = ocispec.Descriptor{} |
| 79 | + |
| 80 | +// NewWindowsLcowDiff is the Windows LCOW container layer implementation |
| 81 | +// for comparing and applying Linux filesystem layers on Windows |
| 82 | +func NewWindowsLcowDiff(store content.Store) (CompareApplier, error) { |
| 83 | + return windowsLcowDiff{ |
| 84 | + store: store, |
| 85 | + }, nil |
| 86 | +} |
| 87 | + |
| 88 | +// Apply applies the content associated with the provided digests onto the |
| 89 | +// provided mounts. Archive content will be extracted and decompressed if |
| 90 | +// necessary. |
| 91 | +func (s windowsLcowDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts []mount.Mount) (d ocispec.Descriptor, err error) { |
| 92 | + t1 := time.Now() |
| 93 | + defer func() { |
| 94 | + if err == nil { |
| 95 | + log.G(ctx).WithFields(logrus.Fields{ |
| 96 | + "d": time.Now().Sub(t1), |
| 97 | + "dgst": desc.Digest, |
| 98 | + "size": desc.Size, |
| 99 | + "media": desc.MediaType, |
| 100 | + }).Debugf("diff applied") |
| 101 | + } |
| 102 | + }() |
| 103 | + |
| 104 | + layer, _, err := mountsToLayerAndParents(mounts) |
| 105 | + if err != nil { |
| 106 | + return emptyDesc, err |
| 107 | + } |
| 108 | + |
| 109 | + isCompressed, err := images.IsCompressedDiff(ctx, desc.MediaType) |
| 110 | + if err != nil { |
| 111 | + return emptyDesc, errors.Wrapf(errdefs.ErrNotImplemented, "unsupported diff media type: %v", desc.MediaType) |
| 112 | + } |
| 113 | + |
| 114 | + ra, err := s.store.ReaderAt(ctx, desc) |
| 115 | + if err != nil { |
| 116 | + return emptyDesc, errors.Wrap(err, "failed to get reader from content store") |
| 117 | + } |
| 118 | + defer ra.Close() |
| 119 | + rdr := content.NewReader(ra) |
| 120 | + if isCompressed { |
| 121 | + ds, err := compression.DecompressStream(rdr) |
| 122 | + if err != nil { |
| 123 | + return emptyDesc, err |
| 124 | + } |
| 125 | + defer ds.Close() |
| 126 | + rdr = ds |
| 127 | + } |
| 128 | + // Calculate the Digest as we go |
| 129 | + digester := digest.Canonical.Digester() |
| 130 | + rc := &readCounter{ |
| 131 | + r: io.TeeReader(rdr, digester.Hash()), |
| 132 | + } |
| 133 | + |
| 134 | + cmd := exec.Command( |
| 135 | + "runhcs.exe", |
| 136 | + "tar2vhd", |
| 137 | + "--scratchpath", path.Join(layer, "sandbox.vhdx"), // TODO: JTERRY75 when the snapshotter changes this to be scratch.vhdx update it here too. |
| 138 | + "--destpath", path.Join(layer, "layer.vhd")) |
| 139 | + |
| 140 | + cmd.Stdin = rc |
| 141 | + if bytes, err := cmd.CombinedOutput(); err != nil { |
| 142 | + return emptyDesc, errors.Wrapf(err, "failed to exec runhcs.exe tar2vhd: %s", string(bytes)) |
| 143 | + } |
| 144 | + |
| 145 | + return ocispec.Descriptor{ |
| 146 | + MediaType: ocispec.MediaTypeImageLayer, |
| 147 | + Size: rc.c, |
| 148 | + Digest: digester.Digest(), |
| 149 | + }, nil |
| 150 | +} |
| 151 | + |
| 152 | +// Compare creates a diff between the given mounts and uploads the result |
| 153 | +// to the content store. |
| 154 | +func (s windowsLcowDiff) Compare(ctx context.Context, lower, upper []mount.Mount, opts ...diff.Opt) (d ocispec.Descriptor, err error) { |
| 155 | + return emptyDesc, errdefs.ErrNotImplemented |
| 156 | +} |
| 157 | + |
| 158 | +type readCounter struct { |
| 159 | + r io.Reader |
| 160 | + c int64 |
| 161 | +} |
| 162 | + |
| 163 | +func (rc *readCounter) Read(p []byte) (n int, err error) { |
| 164 | + n, err = rc.r.Read(p) |
| 165 | + rc.c += int64(n) |
| 166 | + return |
| 167 | +} |
| 168 | + |
| 169 | +func mountsToLayerAndParents(mounts []mount.Mount) (string, []string, error) { |
| 170 | + if len(mounts) != 1 { |
| 171 | + return "", nil, errors.Wrap(errdefs.ErrInvalidArgument, "number of mounts should always be 1 for Windows lcow-layers") |
| 172 | + } |
| 173 | + mnt := mounts[0] |
| 174 | + if mnt.Type != "lcow-layer" { |
| 175 | + return "", nil, errors.Wrap(errdefs.ErrInvalidArgument, "mount layer type must be lcow-layer") |
| 176 | + } |
| 177 | + |
| 178 | + parentLayerPaths, err := mnt.GetParentPaths() |
| 179 | + if err != nil { |
| 180 | + return "", nil, err |
| 181 | + } |
| 182 | + |
| 183 | + return mnt.Source, parentLayerPaths, nil |
| 184 | +} |
0 commit comments