2018-09-05 14:54:38 -04:00
|
|
|
package wclayer
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/Microsoft/go-winio"
|
2018-09-05 14:54:38 -04:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcserror"
|
|
|
|
"github.com/Microsoft/hcsshim/internal/safefile"
|
2018-03-19 18:57:30 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2019-02-28 13:16:30 -05:00
|
|
|
func ImportLayer(path string, importFolderPath string, parentLayerPaths []string) (err error) {
|
|
|
|
title := "hcsshim::ImportLayer"
|
|
|
|
fields := logrus.Fields{
|
|
|
|
"path": path,
|
|
|
|
"importFolderPath": importFolderPath,
|
|
|
|
}
|
|
|
|
logrus.WithFields(fields).Debug(title)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
fields[logrus.ErrorKey] = err
|
|
|
|
logrus.WithFields(fields).Error(err)
|
|
|
|
} else {
|
|
|
|
logrus.WithFields(fields).Debug(title + " - succeeded")
|
|
|
|
}
|
|
|
|
}()
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// Generate layer descriptors
|
|
|
|
layers, err := layerPathsToDescriptors(parentLayerPaths)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
err = importLayer(&stdDriverInfo, path, importFolderPath, layers)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2019-02-28 13:16:30 -05:00
|
|
|
return hcserror.New(err, title+" - failed", "")
|
2018-03-19 18:57:30 -04: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 {
|
|
|
|
*legacyLayerWriter
|
|
|
|
path string
|
|
|
|
parentLayerPaths []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *legacyLayerWriterWrapper) Close() error {
|
|
|
|
defer os.RemoveAll(r.root.Name())
|
|
|
|
defer r.legacyLayerWriter.CloseRoots()
|
|
|
|
err := r.legacyLayerWriter.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
if err = ImportLayer(r.destRoot.Name(), r.path, r.parentLayerPaths); err != nil {
|
2018-03-19 18:57:30 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, name := range r.Tombstones {
|
2018-09-05 14:54:38 -04:00
|
|
|
if err = safefile.RemoveRelative(name, r.destRoot); err != nil && !os.IsNotExist(err) {
|
2018-03-19 18:57:30 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add any hard links that were collected.
|
|
|
|
for _, lnk := range r.PendingLinks {
|
2018-09-05 14:54:38 -04:00
|
|
|
if err = safefile.RemoveRelative(lnk.Path, r.destRoot); err != nil && !os.IsNotExist(err) {
|
2018-03-19 18:57:30 -04:00
|
|
|
return err
|
|
|
|
}
|
2018-09-05 14:54:38 -04:00
|
|
|
if err = safefile.LinkRelative(lnk.Target, lnk.TargetRoot, lnk.Path, r.destRoot); err != nil {
|
2018-03-19 18:57:30 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Prepare the utility VM for use if one is present in the layer.
|
|
|
|
if r.HasUtilityVM {
|
2018-09-05 14:54:38 -04:00
|
|
|
err := safefile.EnsureNotReparsePointRelative("UtilityVM", r.destRoot)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ProcessUtilityVMImage(filepath.Join(r.destRoot.Name(), "UtilityVM"))
|
|
|
|
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.
|
2018-09-05 14:54:38 -04:00
|
|
|
func NewLayerWriter(path string, parentLayerPaths []string) (LayerWriter, error) {
|
2018-03-19 18:57:30 -04:00
|
|
|
if len(parentLayerPaths) == 0 {
|
|
|
|
// This is a base layer. It gets imported differently.
|
2018-09-05 14:54:38 -04:00
|
|
|
f, err := safefile.OpenRoot(path)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &baseLayerWriter{
|
|
|
|
root: f,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:16:30 -05:00
|
|
|
importPath, err := ioutil.TempDir("", "hcs")
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-28 13:16:30 -05:00
|
|
|
w, err := newLegacyLayerWriter(importPath, parentLayerPaths, path)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2019-02-28 13:16:30 -05:00
|
|
|
return nil, err
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
2019-02-28 13:16:30 -05:00
|
|
|
return &legacyLayerWriterWrapper{
|
|
|
|
legacyLayerWriter: w,
|
|
|
|
path: importPath,
|
|
|
|
parentLayerPaths: parentLayerPaths,
|
|
|
|
}, nil
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|