2018-08-27 23:44:22 +00:00
|
|
|
package wclayer
|
2018-02-01 09:00:09 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-11 18:32:09 +00:00
|
|
|
"context"
|
2018-02-01 09:00:09 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-05-11 18:32:09 +00:00
|
|
|
"strings"
|
2018-02-01 09:00:09 +00:00
|
|
|
|
|
|
|
"github.com/Microsoft/go-winio"
|
2018-08-27 23:44:22 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcserror"
|
2020-05-11 18:32:09 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/oc"
|
2018-08-27 23:44:22 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/safefile"
|
2020-05-11 18:32:09 +00:00
|
|
|
"go.opencensus.io/trace"
|
2018-02-01 09:00:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImportLayer will take the contents of the folder at importFolderPath and import
|
|
|
|
// that into a layer with the id layerId. Note that in order to correctly populate
|
|
|
|
// the layer and interperet the transport format, all parent layers must already
|
|
|
|
// be present on the system at the paths provided in parentLayerPaths.
|
2020-05-11 18:32:09 +00:00
|
|
|
func ImportLayer(ctx context.Context, path string, importFolderPath string, parentLayerPaths []string) (err error) {
|
2019-03-06 00:48:39 +00:00
|
|
|
title := "hcsshim::ImportLayer"
|
2020-05-11 18:32:09 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, title)
|
|
|
|
defer span.End()
|
|
|
|
defer func() { oc.SetSpanStatus(span, err) }()
|
|
|
|
span.AddAttributes(
|
|
|
|
trace.StringAttribute("path", path),
|
|
|
|
trace.StringAttribute("importFolderPath", importFolderPath),
|
|
|
|
trace.StringAttribute("parentLayerPaths", strings.Join(parentLayerPaths, ", ")))
|
2018-02-01 09:00:09 +00:00
|
|
|
|
|
|
|
// Generate layer descriptors
|
2020-05-11 18:32:09 +00:00
|
|
|
layers, err := layerPathsToDescriptors(ctx, parentLayerPaths)
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-27 23:44:22 +00:00
|
|
|
err = importLayer(&stdDriverInfo, path, importFolderPath, layers)
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
2019-03-06 00:48:39 +00:00
|
|
|
return hcserror.New(err, title+" - failed", "")
|
2018-02-01 09:00:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LayerWriter is an interface that supports writing a new container image layer.
|
|
|
|
type LayerWriter interface {
|
|
|
|
// Add adds a file to the layer with given metadata.
|
|
|
|
Add(name string, fileInfo *winio.FileBasicInfo) error
|
|
|
|
// AddLink adds a hard link to the layer. The target must already have been added.
|
|
|
|
AddLink(name string, target string) error
|
|
|
|
// Remove removes a file that was present in a parent layer from the layer.
|
|
|
|
Remove(name string) error
|
|
|
|
// Write writes data to the current file. The data must be in the format of a Win32
|
|
|
|
// backup stream.
|
|
|
|
Write(b []byte) (int, error)
|
|
|
|
// Close finishes the layer writing process and releases any resources.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type legacyLayerWriterWrapper struct {
|
2020-05-11 18:32:09 +00:00
|
|
|
ctx context.Context
|
|
|
|
s *trace.Span
|
|
|
|
|
2018-02-01 09:00:09 +00:00
|
|
|
*legacyLayerWriter
|
|
|
|
path string
|
|
|
|
parentLayerPaths []string
|
|
|
|
}
|
|
|
|
|
2020-05-11 18:32:09 +00:00
|
|
|
func (r *legacyLayerWriterWrapper) Close() (err error) {
|
|
|
|
defer r.s.End()
|
|
|
|
defer func() { oc.SetSpanStatus(r.s, err) }()
|
2018-05-11 04:44:43 +00:00
|
|
|
defer os.RemoveAll(r.root.Name())
|
|
|
|
defer r.legacyLayerWriter.CloseRoots()
|
2020-05-11 18:32:09 +00:00
|
|
|
|
|
|
|
err = r.legacyLayerWriter.Close()
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-11 18:32:09 +00:00
|
|
|
if err = ImportLayer(r.ctx, r.destRoot.Name(), r.path, r.parentLayerPaths); err != nil {
|
2018-02-01 09:00:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-05-11 04:44:43 +00:00
|
|
|
for _, name := range r.Tombstones {
|
2018-08-27 23:44:22 +00:00
|
|
|
if err = safefile.RemoveRelative(name, r.destRoot); err != nil && !os.IsNotExist(err) {
|
2018-05-11 04:44:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 09:00:09 +00:00
|
|
|
// Add any hard links that were collected.
|
|
|
|
for _, lnk := range r.PendingLinks {
|
2018-08-27 23:44:22 +00:00
|
|
|
if err = safefile.RemoveRelative(lnk.Path, r.destRoot); err != nil && !os.IsNotExist(err) {
|
2018-02-01 09:00:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-08-27 23:44:22 +00:00
|
|
|
if err = safefile.LinkRelative(lnk.Target, lnk.TargetRoot, lnk.Path, r.destRoot); err != nil {
|
2018-02-01 09:00:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 16:32:28 +00:00
|
|
|
|
|
|
|
// The reapplyDirectoryTimes must be called AFTER we are done with Tombstone
|
|
|
|
// deletion and hard link creation. This is because Tombstone deletion and hard link
|
|
|
|
// creation updates the directory last write timestamps so that will change the
|
|
|
|
// timestamps added by the `Add` call. Some container applications depend on the
|
|
|
|
// correctness of these timestamps and so we should change the timestamps back to
|
|
|
|
// the original value (i.e the value provided in the Add call) after this
|
|
|
|
// processing is done.
|
|
|
|
err = reapplyDirectoryTimes(r.destRoot, r.changedDi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-01 09:00:09 +00:00
|
|
|
// Prepare the utility VM for use if one is present in the layer.
|
|
|
|
if r.HasUtilityVM {
|
2018-08-27 23:44:22 +00:00
|
|
|
err := safefile.EnsureNotReparsePointRelative("UtilityVM", r.destRoot)
|
2018-05-11 04:44:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-11 18:32:09 +00:00
|
|
|
err = ProcessUtilityVMImage(r.ctx, filepath.Join(r.destRoot.Name(), "UtilityVM"))
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLayerWriter returns a new layer writer for creating a layer on disk.
|
|
|
|
// The caller must have taken the SeBackupPrivilege and SeRestorePrivilege privileges
|
|
|
|
// to call this and any methods on the resulting LayerWriter.
|
2020-05-11 18:32:09 +00:00
|
|
|
func NewLayerWriter(ctx context.Context, path string, parentLayerPaths []string) (_ LayerWriter, err error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "hcsshim::NewLayerWriter")
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
oc.SetSpanStatus(span, err)
|
|
|
|
span.End()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
span.AddAttributes(
|
|
|
|
trace.StringAttribute("path", path),
|
|
|
|
trace.StringAttribute("parentLayerPaths", strings.Join(parentLayerPaths, ", ")))
|
|
|
|
|
2018-02-01 09:00:09 +00:00
|
|
|
if len(parentLayerPaths) == 0 {
|
|
|
|
// This is a base layer. It gets imported differently.
|
2018-08-27 23:44:22 +00:00
|
|
|
f, err := safefile.OpenRoot(path)
|
2018-05-11 04:44:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-01 09:00:09 +00:00
|
|
|
return &baseLayerWriter{
|
2020-05-11 18:32:09 +00:00
|
|
|
ctx: ctx,
|
|
|
|
s: span,
|
2018-05-11 04:44:43 +00:00
|
|
|
root: f,
|
2018-02-01 09:00:09 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 00:48:39 +00:00
|
|
|
importPath, err := ioutil.TempDir("", "hcs")
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-06 00:48:39 +00:00
|
|
|
w, err := newLegacyLayerWriter(importPath, parentLayerPaths, path)
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
2019-03-06 00:48:39 +00:00
|
|
|
return nil, err
|
2018-02-01 09:00:09 +00:00
|
|
|
}
|
2019-03-06 00:48:39 +00:00
|
|
|
return &legacyLayerWriterWrapper{
|
2020-05-11 18:32:09 +00:00
|
|
|
ctx: ctx,
|
|
|
|
s: span,
|
2019-03-06 00:48:39 +00:00
|
|
|
legacyLayerWriter: w,
|
|
|
|
path: importPath,
|
|
|
|
parentLayerPaths: parentLayerPaths,
|
|
|
|
}, nil
|
2018-02-01 09:00:09 +00:00
|
|
|
}
|