2015-06-11 14:29:29 -04:00
|
|
|
//+build windows
|
|
|
|
|
|
|
|
package windows
|
|
|
|
|
|
|
|
import (
|
2016-02-18 21:11:36 -05:00
|
|
|
"bufio"
|
2016-05-13 00:11:18 -04:00
|
|
|
"bytes"
|
2015-07-24 20:49:43 -04:00
|
|
|
"encoding/json"
|
2016-08-08 17:25:33 -04:00
|
|
|
"errors"
|
2015-06-11 14:29:29 -04:00
|
|
|
"fmt"
|
2016-02-18 21:11:36 -05:00
|
|
|
"io"
|
2015-07-24 20:49:43 -04:00
|
|
|
"io/ioutil"
|
2015-06-11 14:29:29 -04:00
|
|
|
"os"
|
2016-02-18 21:11:36 -05:00
|
|
|
"path"
|
2015-06-11 14:29:29 -04:00
|
|
|
"path/filepath"
|
2016-05-13 00:11:18 -04:00
|
|
|
"strconv"
|
2015-07-24 20:49:43 -04:00
|
|
|
"strings"
|
2016-05-10 14:42:03 -04:00
|
|
|
"sync"
|
2016-02-18 21:11:36 -05:00
|
|
|
"syscall"
|
2016-11-10 17:07:11 -05:00
|
|
|
"time"
|
2016-04-13 13:15:38 -04:00
|
|
|
"unsafe"
|
2015-06-11 14:29:29 -04:00
|
|
|
|
2016-02-18 21:11:36 -05:00
|
|
|
"github.com/Microsoft/go-winio"
|
|
|
|
"github.com/Microsoft/go-winio/archive/tar"
|
|
|
|
"github.com/Microsoft/go-winio/backuptar"
|
2016-02-03 15:56:01 -05:00
|
|
|
"github.com/Microsoft/hcsshim"
|
2015-06-11 14:29:29 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-10-08 11:51:41 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2015-06-11 14:29:29 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2016-03-29 12:53:00 -04:00
|
|
|
"github.com/docker/docker/pkg/longpath"
|
2016-05-13 00:11:18 -04:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2016-06-08 15:41:48 -04:00
|
|
|
"github.com/docker/go-units"
|
2015-06-11 14:29:29 -04:00
|
|
|
)
|
|
|
|
|
2016-05-16 19:13:39 -04:00
|
|
|
// filterDriver is an HCSShim driver type for the Windows Filter driver.
|
|
|
|
const filterDriver = 1
|
|
|
|
|
2016-06-08 15:41:48 -04:00
|
|
|
var (
|
2016-09-22 20:12:21 -04:00
|
|
|
// mutatedFiles is a list of files that are mutated by the import process
|
|
|
|
// and must be backed up and restored.
|
|
|
|
mutatedFiles = map[string]string{
|
|
|
|
"UtilityVM/Files/EFI/Microsoft/Boot/BCD": "bcd.bak",
|
|
|
|
"UtilityVM/Files/EFI/Microsoft/Boot/BCD.LOG": "bcd.log.bak",
|
|
|
|
"UtilityVM/Files/EFI/Microsoft/Boot/BCD.LOG1": "bcd.log1.bak",
|
|
|
|
"UtilityVM/Files/EFI/Microsoft/Boot/BCD.LOG2": "bcd.log2.bak",
|
|
|
|
}
|
2016-11-02 17:34:12 -04:00
|
|
|
noreexec = false
|
2016-06-08 15:41:48 -04:00
|
|
|
)
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// init registers the windows graph drivers to the register.
|
2015-06-11 14:29:29 -04:00
|
|
|
func init() {
|
|
|
|
graphdriver.Register("windowsfilter", InitFilter)
|
2016-11-02 17:34:12 -04:00
|
|
|
// DOCKER_WINDOWSFILTER_NOREEXEC allows for inline processing which makes
|
|
|
|
// debugging issues in the re-exec codepath significantly easier.
|
|
|
|
if os.Getenv("DOCKER_WINDOWSFILTER_NOREEXEC") != "" {
|
|
|
|
logrus.Warnf("WindowsGraphDriver is set to not re-exec. This is intended for debugging purposes only.")
|
|
|
|
noreexec = true
|
|
|
|
} else {
|
|
|
|
reexec.Register("docker-windows-write-layer", writeLayerReexec)
|
|
|
|
}
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
2016-05-10 14:42:03 -04:00
|
|
|
type checker struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checker) IsMounted(path string) bool {
|
|
|
|
return false
|
|
|
|
}
|
2015-06-11 14:29:29 -04:00
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// Driver represents a windows graph driver.
|
2015-07-24 20:49:43 -04:00
|
|
|
type Driver struct {
|
2015-09-29 17:43:10 -04:00
|
|
|
// info stores the shim driver information
|
|
|
|
info hcsshim.DriverInfo
|
2016-05-10 14:42:03 -04:00
|
|
|
ctr *graphdriver.RefCounter
|
|
|
|
// it is safe for windows to use a cache here because it does not support
|
|
|
|
// restoring containers when the daemon dies.
|
|
|
|
cacheMu sync.Mutex
|
|
|
|
cache map[string]string
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// InitFilter returns a new Windows storage filter driver.
|
2015-10-08 11:51:41 -04:00
|
|
|
func InitFilter(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
|
2015-06-11 14:29:29 -04:00
|
|
|
logrus.Debugf("WindowsGraphDriver InitFilter at %s", home)
|
2016-08-08 17:25:33 -04:00
|
|
|
|
|
|
|
fsType, err := getFileSystemType(string(home[0]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if strings.ToLower(fsType) == "refs" {
|
|
|
|
return nil, fmt.Errorf("%s is on an ReFS volume - ReFS volumes are not supported", home)
|
|
|
|
}
|
|
|
|
|
2015-07-24 20:49:43 -04:00
|
|
|
d := &Driver{
|
2015-06-11 14:29:29 -04:00
|
|
|
info: hcsshim.DriverInfo{
|
|
|
|
HomeDir: home,
|
|
|
|
Flavour: filterDriver,
|
|
|
|
},
|
2016-05-10 14:42:03 -04:00
|
|
|
cache: make(map[string]string),
|
|
|
|
ctr: graphdriver.NewRefCounter(&checker{}),
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2016-08-08 17:25:33 -04:00
|
|
|
// win32FromHresult is a helper function to get the win32 error code from an HRESULT
|
|
|
|
func win32FromHresult(hr uintptr) uintptr {
|
|
|
|
if hr&0x1fff0000 == 0x00070000 {
|
|
|
|
return hr & 0xffff
|
|
|
|
}
|
|
|
|
return hr
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFileSystemType obtains the type of a file system through GetVolumeInformation
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364993(v=vs.85).aspx
|
|
|
|
func getFileSystemType(drive string) (fsType string, hr error) {
|
|
|
|
var (
|
|
|
|
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
|
|
|
|
procGetVolumeInformation = modkernel32.NewProc("GetVolumeInformationW")
|
|
|
|
buf = make([]uint16, 255)
|
|
|
|
size = syscall.MAX_PATH + 1
|
|
|
|
)
|
|
|
|
if len(drive) != 1 {
|
|
|
|
hr = errors.New("getFileSystemType must be called with a drive letter")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
drive += `:\`
|
|
|
|
n := uintptr(unsafe.Pointer(nil))
|
|
|
|
r0, _, _ := syscall.Syscall9(procGetVolumeInformation.Addr(), 8, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(drive))), n, n, n, n, n, uintptr(unsafe.Pointer(&buf[0])), uintptr(size), 0)
|
|
|
|
if int32(r0) < 0 {
|
|
|
|
hr = syscall.Errno(win32FromHresult(r0))
|
|
|
|
}
|
|
|
|
fsType = syscall.UTF16ToString(buf)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-27 18:53:41 -04:00
|
|
|
// String returns the string representation of a driver. This should match
|
|
|
|
// the name the graph driver has been registered with.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) String() string {
|
2016-05-27 18:53:41 -04:00
|
|
|
return "windowsfilter"
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// Status returns the status of the driver.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) Status() [][2]string {
|
2015-06-11 14:29:29 -04:00
|
|
|
return [][2]string{
|
|
|
|
{"Windows", ""},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// Exists returns true if the given id is registered with this driver.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) Exists(id string) bool {
|
2015-09-29 17:43:10 -04:00
|
|
|
rID, err := d.resolveID(id)
|
2015-07-24 20:49:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-09-29 17:43:10 -04:00
|
|
|
result, err := hcsshim.LayerExists(d.info, rID)
|
2015-06-11 14:29:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-02-18 20:24:59 -05:00
|
|
|
// CreateReadWrite creates a layer that is writable for use as a container
|
|
|
|
// file system.
|
2016-11-09 15:59:58 -05:00
|
|
|
func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
|
|
|
|
if opts != nil {
|
|
|
|
return d.create(id, parent, opts.MountLabel, false, opts.StorageOpt)
|
|
|
|
}
|
2016-11-18 18:57:11 -05:00
|
|
|
return d.create(id, parent, "", false, nil)
|
2016-02-18 20:24:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a new read-only layer with the given id.
|
2016-11-09 15:59:58 -05:00
|
|
|
func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
|
|
|
|
if opts != nil {
|
|
|
|
return d.create(id, parent, opts.MountLabel, true, opts.StorageOpt)
|
|
|
|
}
|
2016-11-18 18:57:11 -05:00
|
|
|
return d.create(id, parent, "", true, nil)
|
2016-02-18 20:24:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) create(id, parent, mountLabel string, readOnly bool, storageOpt map[string]string) error {
|
2015-09-29 17:43:10 -04:00
|
|
|
rPId, err := d.resolveID(parent)
|
2015-07-24 20:49:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
parentChain, err := d.getLayerChain(rPId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var layerChain []string
|
|
|
|
|
2016-02-18 20:24:59 -05:00
|
|
|
if rPId != "" {
|
2015-07-24 20:49:43 -04:00
|
|
|
parentPath, err := hcsshim.GetLayerMountPath(d.info, rPId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-18 20:24:59 -05:00
|
|
|
if _, err := os.Stat(filepath.Join(parentPath, "Files")); err == nil {
|
|
|
|
// This is a legitimate parent layer (not the empty "-init" layer),
|
|
|
|
// so include it in the layer chain.
|
|
|
|
layerChain = []string{parentPath}
|
|
|
|
}
|
2015-07-24 20:49:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
layerChain = append(layerChain, parentChain...)
|
|
|
|
|
2016-02-18 20:24:59 -05:00
|
|
|
if readOnly {
|
|
|
|
if err := hcsshim.CreateLayer(d.info, id, rPId); err != nil {
|
2015-07-24 20:49:43 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2016-02-18 20:24:59 -05:00
|
|
|
var parentPath string
|
|
|
|
if len(layerChain) != 0 {
|
|
|
|
parentPath = layerChain[0]
|
|
|
|
}
|
2016-04-13 13:15:38 -04:00
|
|
|
|
2016-02-18 20:24:59 -05:00
|
|
|
if err := hcsshim.CreateSandboxLayer(d.info, id, parentPath, layerChain); err != nil {
|
2015-07-24 20:49:43 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-06-08 15:41:48 -04:00
|
|
|
|
|
|
|
storageOptions, err := parseStorageOpt(storageOpt)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse storage options - %s", err)
|
|
|
|
}
|
|
|
|
|
2016-10-12 19:58:57 -04:00
|
|
|
if storageOptions.size != 0 {
|
2016-06-08 15:41:48 -04:00
|
|
|
if err := hcsshim.ExpandSandboxSize(d.info, id, storageOptions.size); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 20:49:43 -04:00
|
|
|
}
|
|
|
|
|
2015-08-19 21:01:32 -04:00
|
|
|
if _, err := os.Lstat(d.dir(parent)); err != nil {
|
|
|
|
if err2 := hcsshim.DestroyLayer(d.info, id); err2 != nil {
|
|
|
|
logrus.Warnf("Failed to DestroyLayer %s: %s", id, err2)
|
2015-07-24 20:49:43 -04:00
|
|
|
}
|
2015-08-19 21:01:32 -04:00
|
|
|
return fmt.Errorf("Cannot create layer with missing parent %s: %s", parent, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.setLayerChain(id, layerChain); err != nil {
|
2015-07-24 20:49:43 -04:00
|
|
|
if err2 := hcsshim.DestroyLayer(d.info, id); err2 != nil {
|
2015-08-19 21:01:32 -04:00
|
|
|
logrus.Warnf("Failed to DestroyLayer %s: %s", id, err2)
|
2015-07-24 20:49:43 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// dir returns the absolute path to the layer.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) dir(id string) string {
|
2015-06-11 14:29:29 -04:00
|
|
|
return filepath.Join(d.info.HomeDir, filepath.Base(id))
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// Remove unmounts and removes the dir information.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) Remove(id string) error {
|
2015-09-29 17:43:10 -04:00
|
|
|
rID, err := d.resolveID(id)
|
2015-07-24 20:49:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-31 16:17:57 -04:00
|
|
|
|
2016-11-10 17:07:11 -05:00
|
|
|
// This retry loop is due to a bug in Windows (Internal bug #9432268)
|
|
|
|
// if GetContainers fails with ErrVmcomputeOperationInvalidState
|
|
|
|
// it is a transient error. Retry until it succeeds.
|
|
|
|
var computeSystems []hcsshim.ContainerProperties
|
|
|
|
retryCount := 0
|
|
|
|
for {
|
|
|
|
// Get and terminate any template VMs that are currently using the layer
|
|
|
|
computeSystems, err = hcsshim.GetContainers(hcsshim.ComputeSystemQuery{})
|
|
|
|
if err != nil {
|
|
|
|
if err == hcsshim.ErrVmcomputeOperationInvalidState {
|
|
|
|
if retryCount >= 5 {
|
|
|
|
// If we are unable to get the list of containers
|
|
|
|
// go ahead and attempt to delete the layer anyway
|
|
|
|
// as it will most likely work.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
retryCount++
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
2016-11-08 13:54:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, computeSystem := range computeSystems {
|
|
|
|
if strings.Contains(computeSystem.RuntimeImagePath, id) && computeSystem.IsRuntimeTemplate {
|
|
|
|
container, err := hcsshim.OpenContainer(computeSystem.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer container.Close()
|
|
|
|
err = container.Terminate()
|
|
|
|
if hcsshim.IsPending(err) {
|
|
|
|
err = container.Wait()
|
|
|
|
} else if hcsshim.IsAlreadyStopped(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-31 16:17:57 -04:00
|
|
|
layerPath := filepath.Join(d.info.HomeDir, rID)
|
|
|
|
tmpID := fmt.Sprintf("%s-removing", rID)
|
|
|
|
tmpLayerPath := filepath.Join(d.info.HomeDir, tmpID)
|
|
|
|
if err := os.Rename(layerPath, tmpLayerPath); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := hcsshim.DestroyLayer(d.info, tmpID); err != nil {
|
|
|
|
logrus.Errorf("Failed to DestroyLayer %s: %s", id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
2016-07-21 06:03:37 -04:00
|
|
|
// Get returns the rootfs path for the id. This will mount the dir at its given path.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (string, error) {
|
|
|
|
logrus.Debugf("WindowsGraphDriver Get() id %s mountLabel %s", id, mountLabel)
|
2015-06-11 14:29:29 -04:00
|
|
|
var dir string
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
rID, err := d.resolveID(id)
|
2015-07-24 20:49:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-05-10 14:42:03 -04:00
|
|
|
if count := d.ctr.Increment(rID); count > 1 {
|
|
|
|
return d.cache[rID], nil
|
|
|
|
}
|
2015-07-24 20:49:43 -04:00
|
|
|
|
|
|
|
// Getting the layer paths must be done outside of the lock.
|
2015-09-29 17:43:10 -04:00
|
|
|
layerChain, err := d.getLayerChain(rID)
|
2015-07-24 20:49:43 -04:00
|
|
|
if err != nil {
|
2016-05-10 14:42:03 -04:00
|
|
|
d.ctr.Decrement(rID)
|
2015-07-24 20:49:43 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-03-09 16:23:04 -05:00
|
|
|
if err := hcsshim.ActivateLayer(d.info, rID); err != nil {
|
2016-05-10 14:42:03 -04:00
|
|
|
d.ctr.Decrement(rID)
|
2016-03-09 16:23:04 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := hcsshim.PrepareLayer(d.info, rID, layerChain); err != nil {
|
2016-05-10 14:42:03 -04:00
|
|
|
d.ctr.Decrement(rID)
|
2016-03-09 16:23:04 -05:00
|
|
|
if err2 := hcsshim.DeactivateLayer(d.info, rID); err2 != nil {
|
|
|
|
logrus.Warnf("Failed to Deactivate %s: %s", id, err)
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
2016-03-09 16:23:04 -05:00
|
|
|
return "", err
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
mountPath, err := hcsshim.GetLayerMountPath(d.info, rID)
|
2015-06-11 14:29:29 -04:00
|
|
|
if err != nil {
|
2016-05-10 14:42:03 -04:00
|
|
|
d.ctr.Decrement(rID)
|
2015-09-29 17:43:10 -04:00
|
|
|
if err2 := hcsshim.DeactivateLayer(d.info, rID); err2 != nil {
|
2015-07-24 20:49:43 -04:00
|
|
|
logrus.Warnf("Failed to Deactivate %s: %s", id, err)
|
|
|
|
}
|
2015-06-11 14:29:29 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2016-05-10 14:42:03 -04:00
|
|
|
d.cacheMu.Lock()
|
|
|
|
d.cache[rID] = mountPath
|
|
|
|
d.cacheMu.Unlock()
|
2015-06-11 14:29:29 -04:00
|
|
|
|
|
|
|
// If the layer has a mount path, use that. Otherwise, use the
|
|
|
|
// folder path.
|
|
|
|
if mountPath != "" {
|
|
|
|
dir = mountPath
|
|
|
|
} else {
|
|
|
|
dir = d.dir(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// Put adds a new layer to the driver.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) Put(id string) error {
|
2015-06-11 14:29:29 -04:00
|
|
|
logrus.Debugf("WindowsGraphDriver Put() id %s", id)
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
rID, err := d.resolveID(id)
|
2015-07-24 20:49:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-10 14:42:03 -04:00
|
|
|
if count := d.ctr.Decrement(rID); count > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
d.cacheMu.Lock()
|
|
|
|
delete(d.cache, rID)
|
|
|
|
d.cacheMu.Unlock()
|
2015-07-24 20:49:43 -04:00
|
|
|
|
2016-03-09 16:23:04 -05:00
|
|
|
if err := hcsshim.UnprepareLayer(d.info, rID); err != nil {
|
|
|
|
return err
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
2016-03-09 16:23:04 -05:00
|
|
|
return hcsshim.DeactivateLayer(d.info, rID)
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// Cleanup ensures the information the driver stores is properly removed.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) Cleanup() error {
|
2015-06-11 14:29:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Diff produces an archive of the changes between the specified
|
|
|
|
// layer and its parent layer which may be "".
|
2016-03-09 16:23:04 -05:00
|
|
|
// The layer should be mounted when calling this function
|
2016-10-20 19:40:59 -04:00
|
|
|
func (d *Driver) Diff(id, parent string) (_ io.ReadCloser, err error) {
|
2015-09-29 17:43:10 -04:00
|
|
|
rID, err := d.resolveID(id)
|
2015-07-24 20:49:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
layerChain, err := d.getLayerChain(rID)
|
2015-07-24 20:49:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-09 16:23:04 -05:00
|
|
|
// this is assuming that the layer is unmounted
|
|
|
|
if err := hcsshim.UnprepareLayer(d.info, rID); err != nil {
|
|
|
|
return nil, err
|
2016-03-23 03:33:02 -04:00
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
prepare := func() {
|
2016-03-09 16:23:04 -05:00
|
|
|
if err := hcsshim.PrepareLayer(d.info, rID, layerChain); err != nil {
|
|
|
|
logrus.Warnf("Failed to Deactivate %s: %s", rID, err)
|
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
}
|
2015-07-24 20:49:43 -04:00
|
|
|
|
2016-02-18 21:11:36 -05:00
|
|
|
arch, err := d.exportLayer(rID, layerChain)
|
|
|
|
if err != nil {
|
2016-05-13 00:11:18 -04:00
|
|
|
prepare()
|
2016-02-18 21:11:36 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return ioutils.NewReadCloserWrapper(arch, func() error {
|
2016-05-13 00:11:18 -04:00
|
|
|
err := arch.Close()
|
|
|
|
prepare()
|
|
|
|
return err
|
2016-02-18 21:11:36 -05:00
|
|
|
}), nil
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Changes produces a list of changes between the specified layer
|
|
|
|
// and its parent layer. If parent is "", then all changes will be ADD changes.
|
2016-10-27 18:52:32 -04:00
|
|
|
// The layer should not be mounted when calling this function.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) Changes(id, parent string) ([]archive.Change, error) {
|
2016-02-18 21:11:36 -05:00
|
|
|
rID, err := d.resolveID(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parentChain, err := d.getLayerChain(rID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:52:32 -04:00
|
|
|
if err := hcsshim.ActivateLayer(d.info, rID); err != nil {
|
2016-03-09 16:23:04 -05:00
|
|
|
return nil, err
|
2016-03-23 03:33:02 -04:00
|
|
|
}
|
2016-03-09 16:23:04 -05:00
|
|
|
defer func() {
|
2016-10-27 18:52:32 -04:00
|
|
|
if err2 := hcsshim.DeactivateLayer(d.info, rID); err2 != nil {
|
|
|
|
logrus.Errorf("changes() failed to DeactivateLayer %s %s: %s", id, rID, err2)
|
2016-03-09 16:23:04 -05:00
|
|
|
}
|
|
|
|
}()
|
2016-02-18 21:11:36 -05:00
|
|
|
|
|
|
|
var changes []archive.Change
|
2016-05-13 00:11:18 -04:00
|
|
|
err = winio.RunWithPrivilege(winio.SeBackupPrivilege, func() error {
|
|
|
|
r, err := hcsshim.NewLayerReader(d.info, id, parentChain)
|
2016-02-18 21:11:36 -05:00
|
|
|
if err != nil {
|
2016-05-13 00:11:18 -04:00
|
|
|
return err
|
2016-02-18 21:11:36 -05:00
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
name, _, fileInfo, err := r.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name = filepath.ToSlash(name)
|
|
|
|
if fileInfo == nil {
|
|
|
|
changes = append(changes, archive.Change{Path: name, Kind: archive.ChangeDelete})
|
|
|
|
} else {
|
|
|
|
// Currently there is no way to tell between an add and a modify.
|
|
|
|
changes = append(changes, archive.Change{Path: name, Kind: archive.ChangeModify})
|
|
|
|
}
|
2016-02-18 21:11:36 -05:00
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-02-18 21:11:36 -05:00
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
|
2016-02-18 21:11:36 -05:00
|
|
|
return changes, nil
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyDiff extracts the changeset from the given diff into the
|
|
|
|
// layer with the specified id and parent, returning the size of the
|
|
|
|
// new layer in bytes.
|
2016-03-09 16:23:04 -05:00
|
|
|
// The layer should not be mounted when calling this function
|
2016-10-20 19:40:59 -04:00
|
|
|
func (d *Driver) ApplyDiff(id, parent string, diff io.Reader) (int64, error) {
|
2016-03-29 12:53:00 -04:00
|
|
|
var layerChain []string
|
|
|
|
if parent != "" {
|
|
|
|
rPId, err := d.resolveID(parent)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
parentChain, err := d.getLayerChain(rPId)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
parentPath, err := hcsshim.GetLayerMountPath(d.info, rPId)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
layerChain = append(layerChain, parentPath)
|
|
|
|
layerChain = append(layerChain, parentChain...)
|
2015-07-24 20:49:43 -04:00
|
|
|
}
|
|
|
|
|
2016-03-29 12:53:00 -04:00
|
|
|
size, err := d.importLayer(id, diff, layerChain)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2015-07-24 20:49:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.setLayerChain(id, layerChain); err != nil {
|
2016-03-29 12:53:00 -04:00
|
|
|
return 0, err
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
2016-03-29 12:53:00 -04:00
|
|
|
return size, nil
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiffSize calculates the changes between the specified layer
|
|
|
|
// and its parent and returns the size in bytes of the changes
|
|
|
|
// relative to its base filesystem directory.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) DiffSize(id, parent string) (size int64, err error) {
|
2015-09-29 17:43:10 -04:00
|
|
|
rPId, err := d.resolveID(parent)
|
2015-07-24 20:49:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
changes, err := d.Changes(id, rPId)
|
2015-06-11 14:29:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
layerFs, err := d.Get(id, "")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer d.Put(id)
|
|
|
|
|
|
|
|
return archive.ChangesSize(layerFs, changes), nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// GetMetadata returns custom driver information.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) GetMetadata(id string) (map[string]string, error) {
|
|
|
|
m := make(map[string]string)
|
|
|
|
m["dir"] = d.dir(id)
|
|
|
|
return m, nil
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:11:36 -05:00
|
|
|
func writeTarFromLayer(r hcsshim.LayerReader, w io.Writer) error {
|
|
|
|
t := tar.NewWriter(w)
|
|
|
|
for {
|
|
|
|
name, size, fileInfo, err := r.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2015-06-11 14:29:29 -04:00
|
|
|
if err != nil {
|
2016-02-18 21:11:36 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fileInfo == nil {
|
|
|
|
// Write a whiteout file.
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: filepath.ToSlash(filepath.Join(filepath.Dir(name), archive.WhiteoutPrefix+filepath.Base(name))),
|
|
|
|
}
|
|
|
|
err := t.WriteHeader(hdr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = backuptar.WriteTarFileFromBackupStream(t, r, name, size, fileInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 21:11:36 -05:00
|
|
|
}
|
|
|
|
return t.Close()
|
|
|
|
}
|
2015-06-11 14:29:29 -04:00
|
|
|
|
2016-02-18 21:11:36 -05:00
|
|
|
// exportLayer generates an archive from a layer based on the given ID.
|
2016-10-20 19:40:59 -04:00
|
|
|
func (d *Driver) exportLayer(id string, parentLayerPaths []string) (io.ReadCloser, error) {
|
2016-02-18 21:11:36 -05:00
|
|
|
archive, w := io.Pipe()
|
|
|
|
go func() {
|
2016-05-13 00:11:18 -04:00
|
|
|
err := winio.RunWithPrivilege(winio.SeBackupPrivilege, func() error {
|
|
|
|
r, err := hcsshim.NewLayerReader(d.info, id, parentLayerPaths)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writeTarFromLayer(r, w)
|
|
|
|
cerr := r.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
2016-02-18 21:11:36 -05:00
|
|
|
w.CloseWithError(err)
|
|
|
|
}()
|
2015-06-11 14:29:29 -04:00
|
|
|
|
2016-02-18 21:11:36 -05:00
|
|
|
return archive, nil
|
|
|
|
}
|
|
|
|
|
2016-09-22 20:12:21 -04:00
|
|
|
// writeBackupStreamFromTarAndSaveMutatedFiles reads data from a tar stream and
|
|
|
|
// writes it to a backup stream, and also saves any files that will be mutated
|
|
|
|
// by the import layer process to a backup location.
|
|
|
|
func writeBackupStreamFromTarAndSaveMutatedFiles(buf *bufio.Writer, w io.Writer, t *tar.Reader, hdr *tar.Header, root string) (nextHdr *tar.Header, err error) {
|
|
|
|
var bcdBackup *os.File
|
|
|
|
var bcdBackupWriter *winio.BackupFileWriter
|
|
|
|
if backupPath, ok := mutatedFiles[hdr.Name]; ok {
|
|
|
|
bcdBackup, err = os.Create(filepath.Join(root, backupPath))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
cerr := bcdBackup.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
bcdBackupWriter = winio.NewBackupFileWriter(bcdBackup, false)
|
|
|
|
defer func() {
|
|
|
|
cerr := bcdBackupWriter.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
buf.Reset(io.MultiWriter(w, bcdBackupWriter))
|
|
|
|
} else {
|
|
|
|
buf.Reset(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
ferr := buf.Flush()
|
|
|
|
if err == nil {
|
|
|
|
err = ferr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return backuptar.WriteBackupStreamFromTarFile(buf, t, hdr)
|
|
|
|
}
|
|
|
|
|
2016-10-20 19:40:59 -04:00
|
|
|
func writeLayerFromTar(r io.Reader, w hcsshim.LayerWriter, root string) (int64, error) {
|
2016-02-18 21:11:36 -05:00
|
|
|
t := tar.NewReader(r)
|
|
|
|
hdr, err := t.Next()
|
|
|
|
totalSize := int64(0)
|
|
|
|
buf := bufio.NewWriter(nil)
|
|
|
|
for err == nil {
|
|
|
|
base := path.Base(hdr.Name)
|
|
|
|
if strings.HasPrefix(base, archive.WhiteoutPrefix) {
|
|
|
|
name := path.Join(path.Dir(hdr.Name), base[len(archive.WhiteoutPrefix):])
|
|
|
|
err = w.Remove(filepath.FromSlash(name))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
hdr, err = t.Next()
|
2016-03-29 12:53:00 -04:00
|
|
|
} else if hdr.Typeflag == tar.TypeLink {
|
|
|
|
err = w.AddLink(filepath.FromSlash(hdr.Name), filepath.FromSlash(hdr.Linkname))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
hdr, err = t.Next()
|
2016-02-18 21:11:36 -05:00
|
|
|
} else {
|
|
|
|
var (
|
|
|
|
name string
|
|
|
|
size int64
|
|
|
|
fileInfo *winio.FileBasicInfo
|
|
|
|
)
|
|
|
|
name, size, fileInfo, err = backuptar.FileInfoFromHeader(hdr)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
err = w.Add(filepath.FromSlash(name), fileInfo)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2016-09-22 20:12:21 -04:00
|
|
|
hdr, err = writeBackupStreamFromTarAndSaveMutatedFiles(buf, w, t, hdr, root)
|
2016-02-18 21:11:36 -05:00
|
|
|
totalSize += size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != io.EOF {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return totalSize, nil
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// importLayer adds a new layer to the tag and graph store based on the given data.
|
2016-10-20 19:40:59 -04:00
|
|
|
func (d *Driver) importLayer(id string, layerData io.Reader, parentLayerPaths []string) (size int64, err error) {
|
2016-11-02 17:34:12 -04:00
|
|
|
if !noreexec {
|
|
|
|
cmd := reexec.Command(append([]string{"docker-windows-write-layer", d.info.HomeDir, id}, parentLayerPaths...)...)
|
|
|
|
output := bytes.NewBuffer(nil)
|
|
|
|
cmd.Stdin = layerData
|
|
|
|
cmd.Stdout = output
|
|
|
|
cmd.Stderr = output
|
|
|
|
|
|
|
|
if err = cmd.Start(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
|
2016-11-02 17:34:12 -04:00
|
|
|
if err = cmd.Wait(); err != nil {
|
|
|
|
return 0, fmt.Errorf("re-exec error: %v: output: %s", err, output)
|
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
|
2016-11-02 17:34:12 -04:00
|
|
|
return strconv.ParseInt(output.String(), 10, 64)
|
2016-02-18 21:11:36 -05:00
|
|
|
}
|
2016-11-02 17:34:12 -04:00
|
|
|
return writeLayer(layerData, d.info.HomeDir, id, parentLayerPaths...)
|
2016-05-13 00:11:18 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 17:34:12 -04:00
|
|
|
// writeLayerReexec is the re-exec entry point for writing a layer from a tar file
|
|
|
|
func writeLayerReexec() {
|
|
|
|
size, err := writeLayer(os.Stdin, os.Args[1], os.Args[2], os.Args[3:]...)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Fprint(os.Stdout, size)
|
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
|
2016-11-02 17:34:12 -04:00
|
|
|
// writeLayer writes a layer from a tar file.
|
|
|
|
func writeLayer(layerData io.Reader, home string, id string, parentLayerPaths ...string) (int64, error) {
|
|
|
|
err := winio.EnableProcessPrivileges([]string{winio.SeBackupPrivilege, winio.SeRestorePrivilege})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if noreexec {
|
|
|
|
defer func() {
|
|
|
|
if err := winio.DisableProcessPrivileges([]string{winio.SeBackupPrivilege, winio.SeRestorePrivilege}); err != nil {
|
|
|
|
// This should never happen, but just in case when in debugging mode.
|
|
|
|
// See https://github.com/docker/docker/pull/28002#discussion_r86259241 for rationale.
|
|
|
|
panic("Failed to disabled process privileges while in non re-exec mode")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
|
2016-11-02 17:34:12 -04:00
|
|
|
info := hcsshim.DriverInfo{
|
|
|
|
Flavour: filterDriver,
|
|
|
|
HomeDir: home,
|
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
|
2016-11-02 17:34:12 -04:00
|
|
|
w, err := hcsshim.NewLayerWriter(info, id, parentLayerPaths)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
|
2016-11-02 17:34:12 -04:00
|
|
|
size, err := writeLayerFromTar(layerData, w, filepath.Join(home, id))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2016-05-13 00:11:18 -04:00
|
|
|
|
2016-11-02 17:34:12 -04:00
|
|
|
err = w.Close()
|
2016-02-18 21:11:36 -05:00
|
|
|
if err != nil {
|
2016-11-02 17:34:12 -04:00
|
|
|
return 0, err
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
2016-11-02 17:34:12 -04:00
|
|
|
|
|
|
|
return size, nil
|
2015-06-11 14:29:29 -04:00
|
|
|
}
|
2015-07-24 20:49:43 -04:00
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// resolveID computes the layerID information based on the given id.
|
|
|
|
func (d *Driver) resolveID(id string) (string, error) {
|
|
|
|
content, err := ioutil.ReadFile(filepath.Join(d.dir(id), "layerID"))
|
2015-07-24 20:49:43 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return id, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(content), nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// setID stores the layerId in disk.
|
|
|
|
func (d *Driver) setID(id, altID string) error {
|
|
|
|
err := ioutil.WriteFile(filepath.Join(d.dir(id), "layerId"), []byte(altID), 0600)
|
2015-07-24 20:49:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// getLayerChain returns the layer chain information.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) getLayerChain(id string) ([]string, error) {
|
|
|
|
jPath := filepath.Join(d.dir(id), "layerchain.json")
|
|
|
|
content, err := ioutil.ReadFile(jPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to read layerchain file - %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var layerChain []string
|
|
|
|
err = json.Unmarshal(content, &layerChain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to unmarshall layerchain json - %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return layerChain, nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:43:10 -04:00
|
|
|
// setLayerChain stores the layer chain information in disk.
|
2015-07-24 20:49:43 -04:00
|
|
|
func (d *Driver) setLayerChain(id string, chain []string) error {
|
|
|
|
content, err := json.Marshal(&chain)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to marshall layerchain json - %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
jPath := filepath.Join(d.dir(id), "layerchain.json")
|
|
|
|
err = ioutil.WriteFile(jPath, content, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to write layerchain file - %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
|
2016-02-18 21:11:36 -05:00
|
|
|
type fileGetCloserWithBackupPrivileges struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fg *fileGetCloserWithBackupPrivileges) Get(filename string) (io.ReadCloser, error) {
|
2016-09-22 20:12:21 -04:00
|
|
|
if backupPath, ok := mutatedFiles[filename]; ok {
|
|
|
|
return os.Open(filepath.Join(fg.path, backupPath))
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:11:36 -05:00
|
|
|
var f *os.File
|
|
|
|
// Open the file while holding the Windows backup privilege. This ensures that the
|
|
|
|
// file can be opened even if the caller does not actually have access to it according
|
|
|
|
// to the security descriptor.
|
|
|
|
err := winio.RunWithPrivilege(winio.SeBackupPrivilege, func() error {
|
2016-03-29 12:53:00 -04:00
|
|
|
path := longpath.AddPrefix(filepath.Join(fg.path, filename))
|
2016-02-18 21:11:36 -05:00
|
|
|
p, err := syscall.UTF16FromString(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h, err := syscall.CreateFile(&p[0], syscall.GENERIC_READ, syscall.FILE_SHARE_READ, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
|
|
|
|
if err != nil {
|
|
|
|
return &os.PathError{Op: "open", Path: path, Err: err}
|
|
|
|
}
|
|
|
|
f = os.NewFile(uintptr(h), path)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fg *fileGetCloserWithBackupPrivileges) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-18 20:58:23 -05:00
|
|
|
// DiffGetter returns a FileGetCloser that can read files from the directory that
|
|
|
|
// contains files for the layer differences. Used for direct access for tar-split.
|
2016-02-18 21:11:36 -05:00
|
|
|
func (d *Driver) DiffGetter(id string) (graphdriver.FileGetCloser, error) {
|
|
|
|
id, err := d.resolveID(id)
|
2015-11-18 17:20:54 -05:00
|
|
|
if err != nil {
|
2016-02-18 21:11:36 -05:00
|
|
|
return nil, err
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:11:36 -05:00
|
|
|
return &fileGetCloserWithBackupPrivileges{d.dir(id)}, nil
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
2016-06-08 15:41:48 -04:00
|
|
|
|
|
|
|
type storageOptions struct {
|
|
|
|
size uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseStorageOpt(storageOpt map[string]string) (*storageOptions, error) {
|
|
|
|
options := storageOptions{}
|
|
|
|
|
|
|
|
// Read size to change the block device size per container.
|
|
|
|
for key, val := range storageOpt {
|
|
|
|
key := strings.ToLower(key)
|
|
|
|
switch key {
|
|
|
|
case "size":
|
|
|
|
size, err := units.RAMInBytes(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options.size = uint64(size)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown storage option: %s", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &options, nil
|
|
|
|
}
|