mirror of
https://github.com/moby/moby.git
synced 2026-08-02 22:26:52 +00:00
Merge pull request #42451 from thaJeztah/remove_lcow_step1
Remove LCOW code (step 1)
This commit is contained in:
@@ -69,12 +69,6 @@ func (daemon *Daemon) containerCreate(opts createOpts) (containertypes.Container
|
||||
if err == nil {
|
||||
os = img.OS
|
||||
}
|
||||
} else {
|
||||
// This mean scratch. On Windows, we can safely assume that this is a linux
|
||||
// container. On other platforms, it's the host OS (which it already is)
|
||||
if isWindows && system.LCOWSupported() {
|
||||
os = "linux"
|
||||
}
|
||||
}
|
||||
|
||||
warnings, err := daemon.verifyContainerSettings(os, opts.params.HostConfig, opts.params.Config, false)
|
||||
|
||||
@@ -499,19 +499,12 @@ func (daemon *Daemon) runAsHyperVContainer(hostConfig *containertypes.HostConfig
|
||||
// conditionalMountOnStart is a platform specific helper function during the
|
||||
// container start to call mount.
|
||||
func (daemon *Daemon) conditionalMountOnStart(container *container.Container) error {
|
||||
|
||||
// Bail out now for Linux containers. We cannot mount the containers filesystem on the
|
||||
// host as it is a non-Windows filesystem.
|
||||
if system.LCOWSupported() && container.OS != "windows" {
|
||||
if daemon.runAsHyperVContainer(container.HostConfig) {
|
||||
// We do not mount if a Hyper-V container as it needs to be mounted inside the
|
||||
// utility VM, not the host.
|
||||
return nil
|
||||
}
|
||||
|
||||
// We do not mount if a Hyper-V container as it needs to be mounted inside the
|
||||
// utility VM, not the host.
|
||||
if !daemon.runAsHyperVContainer(container.HostConfig) {
|
||||
return daemon.Mount(container)
|
||||
}
|
||||
return nil
|
||||
return daemon.Mount(container)
|
||||
}
|
||||
|
||||
// conditionalUnmountOnCleanup is a platform specific helper function called
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,421 +0,0 @@
|
||||
// +build windows
|
||||
|
||||
package lcow // import "github.com/docker/docker/daemon/graphdriver/lcow"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Microsoft/hcsshim"
|
||||
"github.com/Microsoft/opengcs/client"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Code for all the service VM management for the LCOW graphdriver
|
||||
|
||||
var errVMisTerminating = errors.New("service VM is shutting down")
|
||||
var errVMUnknown = errors.New("service vm id is unknown")
|
||||
var errVMStillHasReference = errors.New("Attemping to delete a VM that is still being used")
|
||||
|
||||
// serviceVMMap is the struct representing the id -> service VM mapping.
|
||||
type serviceVMMap struct {
|
||||
sync.Mutex
|
||||
svms map[string]*serviceVMMapItem
|
||||
}
|
||||
|
||||
// serviceVMMapItem is our internal structure representing an item in our
|
||||
// map of service VMs we are maintaining.
|
||||
type serviceVMMapItem struct {
|
||||
svm *serviceVM // actual service vm object
|
||||
refCount int // refcount for VM
|
||||
}
|
||||
|
||||
// attachedVHD is for reference counting SCSI disks attached to a service VM,
|
||||
// and for a counter used to generate a short path name for the container path.
|
||||
type attachedVHD struct {
|
||||
refCount int
|
||||
attachCounter uint64
|
||||
}
|
||||
|
||||
type serviceVM struct {
|
||||
sync.Mutex // Serialises operations being performed in this service VM.
|
||||
scratchAttached bool // Has a scratch been attached?
|
||||
config *client.Config // Represents the service VM item.
|
||||
|
||||
// Indicates that the vm is started
|
||||
startStatus chan interface{}
|
||||
startError error
|
||||
|
||||
// Indicates that the vm is stopped
|
||||
stopStatus chan interface{}
|
||||
stopError error
|
||||
|
||||
attachCounter uint64 // Increasing counter for each add
|
||||
attachedVHDs map[string]*attachedVHD // Map ref counting all the VHDS we've hot-added/hot-removed.
|
||||
unionMounts map[string]int // Map ref counting all the union filesystems we mounted.
|
||||
}
|
||||
|
||||
// add will add an id to the service vm map. There are three cases:
|
||||
// - entry doesn't exist:
|
||||
// - add id to map and return a new vm that the caller can manually configure+start
|
||||
// - entry does exist
|
||||
// - return vm in map and increment ref count
|
||||
// - entry does exist but the ref count is 0
|
||||
// - return the svm and errVMisTerminating. Caller can call svm.getStopError() to wait for stop
|
||||
func (svmMap *serviceVMMap) add(id string) (svm *serviceVM, alreadyExists bool, err error) {
|
||||
svmMap.Lock()
|
||||
defer svmMap.Unlock()
|
||||
if svm, ok := svmMap.svms[id]; ok {
|
||||
if svm.refCount == 0 {
|
||||
return svm.svm, true, errVMisTerminating
|
||||
}
|
||||
svm.refCount++
|
||||
return svm.svm, true, nil
|
||||
}
|
||||
|
||||
// Doesn't exist, so create an empty svm to put into map and return
|
||||
newSVM := &serviceVM{
|
||||
startStatus: make(chan interface{}),
|
||||
stopStatus: make(chan interface{}),
|
||||
attachedVHDs: make(map[string]*attachedVHD),
|
||||
unionMounts: make(map[string]int),
|
||||
config: &client.Config{},
|
||||
}
|
||||
svmMap.svms[id] = &serviceVMMapItem{
|
||||
svm: newSVM,
|
||||
refCount: 1,
|
||||
}
|
||||
return newSVM, false, nil
|
||||
}
|
||||
|
||||
// get will get the service vm from the map. There are three cases:
|
||||
// - entry doesn't exist:
|
||||
// - return errVMUnknown
|
||||
// - entry does exist
|
||||
// - return vm with no error
|
||||
// - entry does exist but the ref count is 0
|
||||
// - return the svm and errVMisTerminating. Caller can call svm.getStopError() to wait for stop
|
||||
func (svmMap *serviceVMMap) get(id string) (*serviceVM, error) {
|
||||
svmMap.Lock()
|
||||
defer svmMap.Unlock()
|
||||
svm, ok := svmMap.svms[id]
|
||||
if !ok {
|
||||
return nil, errVMUnknown
|
||||
}
|
||||
if svm.refCount == 0 {
|
||||
return svm.svm, errVMisTerminating
|
||||
}
|
||||
return svm.svm, nil
|
||||
}
|
||||
|
||||
// decrementRefCount decrements the ref count of the given ID from the map. There are four cases:
|
||||
// - entry doesn't exist:
|
||||
// - return errVMUnknown
|
||||
// - entry does exist but the ref count is 0
|
||||
// - return the svm and errVMisTerminating. Caller can call svm.getStopError() to wait for stop
|
||||
// - entry does exist but ref count is 1
|
||||
// - return vm and set lastRef to true. The caller can then stop the vm, delete the id from this map
|
||||
// - and execute svm.signalStopFinished to signal the threads that the svm has been terminated.
|
||||
// - entry does exist and ref count > 1
|
||||
// - just reduce ref count and return svm
|
||||
func (svmMap *serviceVMMap) decrementRefCount(id string) (_ *serviceVM, lastRef bool, _ error) {
|
||||
svmMap.Lock()
|
||||
defer svmMap.Unlock()
|
||||
|
||||
svm, ok := svmMap.svms[id]
|
||||
if !ok {
|
||||
return nil, false, errVMUnknown
|
||||
}
|
||||
if svm.refCount == 0 {
|
||||
return svm.svm, false, errVMisTerminating
|
||||
}
|
||||
svm.refCount--
|
||||
return svm.svm, svm.refCount == 0, nil
|
||||
}
|
||||
|
||||
// setRefCountZero works the same way as decrementRefCount, but sets ref count to 0 instead of decrementing it.
|
||||
func (svmMap *serviceVMMap) setRefCountZero(id string) (*serviceVM, error) {
|
||||
svmMap.Lock()
|
||||
defer svmMap.Unlock()
|
||||
|
||||
svm, ok := svmMap.svms[id]
|
||||
if !ok {
|
||||
return nil, errVMUnknown
|
||||
}
|
||||
if svm.refCount == 0 {
|
||||
return svm.svm, errVMisTerminating
|
||||
}
|
||||
svm.refCount = 0
|
||||
return svm.svm, nil
|
||||
}
|
||||
|
||||
// deleteID deletes the given ID from the map. If the refcount is not 0 or the
|
||||
// VM does not exist, then this function returns an error.
|
||||
func (svmMap *serviceVMMap) deleteID(id string) error {
|
||||
svmMap.Lock()
|
||||
defer svmMap.Unlock()
|
||||
svm, ok := svmMap.svms[id]
|
||||
if !ok {
|
||||
return errVMUnknown
|
||||
}
|
||||
if svm.refCount != 0 {
|
||||
return errVMStillHasReference
|
||||
}
|
||||
delete(svmMap.svms, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (svm *serviceVM) signalStartFinished(err error) {
|
||||
svm.Lock()
|
||||
svm.startError = err
|
||||
svm.Unlock()
|
||||
close(svm.startStatus)
|
||||
}
|
||||
|
||||
func (svm *serviceVM) getStartError() error {
|
||||
<-svm.startStatus
|
||||
svm.Lock()
|
||||
defer svm.Unlock()
|
||||
return svm.startError
|
||||
}
|
||||
|
||||
func (svm *serviceVM) signalStopFinished(err error) {
|
||||
svm.Lock()
|
||||
svm.stopError = err
|
||||
svm.Unlock()
|
||||
close(svm.stopStatus)
|
||||
}
|
||||
|
||||
func (svm *serviceVM) getStopError() error {
|
||||
<-svm.stopStatus
|
||||
svm.Lock()
|
||||
defer svm.Unlock()
|
||||
return svm.stopError
|
||||
}
|
||||
|
||||
// hotAddVHDs waits for the service vm to start and then attaches the vhds.
|
||||
func (svm *serviceVM) hotAddVHDs(mvds ...hcsshim.MappedVirtualDisk) error {
|
||||
if err := svm.getStartError(); err != nil {
|
||||
return err
|
||||
}
|
||||
return svm.hotAddVHDsAtStart(mvds...)
|
||||
}
|
||||
|
||||
// hotAddVHDsAtStart works the same way as hotAddVHDs but does not wait for the VM to start.
|
||||
func (svm *serviceVM) hotAddVHDsAtStart(mvds ...hcsshim.MappedVirtualDisk) error {
|
||||
svm.Lock()
|
||||
defer svm.Unlock()
|
||||
for i, mvd := range mvds {
|
||||
if _, ok := svm.attachedVHDs[mvd.HostPath]; ok {
|
||||
svm.attachedVHDs[mvd.HostPath].refCount++
|
||||
logrus.Debugf("lcowdriver: UVM %s: %s already present, refCount now %d", svm.config.Name, mvd.HostPath, svm.attachedVHDs[mvd.HostPath].refCount)
|
||||
continue
|
||||
}
|
||||
|
||||
svm.attachCounter++
|
||||
shortContainerPath := remapLongToShortContainerPath(mvd.ContainerPath, svm.attachCounter, svm.config.Name)
|
||||
if err := svm.config.HotAddVhd(mvd.HostPath, shortContainerPath, mvd.ReadOnly, !mvd.AttachOnly); err != nil {
|
||||
svm.hotRemoveVHDsNoLock(mvds[:i]...)
|
||||
return err
|
||||
}
|
||||
svm.attachedVHDs[mvd.HostPath] = &attachedVHD{refCount: 1, attachCounter: svm.attachCounter}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// hotRemoveVHDs waits for the service vm to start and then removes the vhds.
|
||||
// The service VM must not be locked when calling this function.
|
||||
func (svm *serviceVM) hotRemoveVHDs(mvds ...hcsshim.MappedVirtualDisk) error {
|
||||
if err := svm.getStartError(); err != nil {
|
||||
return err
|
||||
}
|
||||
svm.Lock()
|
||||
defer svm.Unlock()
|
||||
return svm.hotRemoveVHDsNoLock(mvds...)
|
||||
}
|
||||
|
||||
// hotRemoveVHDsNoLock removes VHDs from a service VM. When calling this function,
|
||||
// the contract is the service VM lock must be held.
|
||||
func (svm *serviceVM) hotRemoveVHDsNoLock(mvds ...hcsshim.MappedVirtualDisk) error {
|
||||
var retErr error
|
||||
for _, mvd := range mvds {
|
||||
if _, ok := svm.attachedVHDs[mvd.HostPath]; !ok {
|
||||
// We continue instead of returning an error if we try to hot remove a non-existent VHD.
|
||||
// This is because one of the callers of the function is graphdriver.Put(). Since graphdriver.Get()
|
||||
// defers the VM start to the first operation, it's possible that nothing have been hot-added
|
||||
// when Put() is called. To avoid Put returning an error in that case, we simply continue if we
|
||||
// don't find the vhd attached.
|
||||
logrus.Debugf("lcowdriver: UVM %s: %s is not attached, not doing anything", svm.config.Name, mvd.HostPath)
|
||||
continue
|
||||
}
|
||||
|
||||
if svm.attachedVHDs[mvd.HostPath].refCount > 1 {
|
||||
svm.attachedVHDs[mvd.HostPath].refCount--
|
||||
logrus.Debugf("lcowdriver: UVM %s: %s refCount dropped to %d. not removing from UVM", svm.config.Name, mvd.HostPath, svm.attachedVHDs[mvd.HostPath].refCount)
|
||||
continue
|
||||
}
|
||||
|
||||
// last reference to VHD, so remove from VM and map
|
||||
if err := svm.config.HotRemoveVhd(mvd.HostPath); err == nil {
|
||||
delete(svm.attachedVHDs, mvd.HostPath)
|
||||
} else {
|
||||
// Take note of the error, but still continue to remove the other VHDs
|
||||
logrus.Warnf("Failed to hot remove %s: %s", mvd.HostPath, err)
|
||||
if retErr == nil {
|
||||
retErr = err
|
||||
}
|
||||
}
|
||||
}
|
||||
return retErr
|
||||
}
|
||||
|
||||
func (svm *serviceVM) createExt4VHDX(destFile string, sizeGB uint32, cacheFile string) error {
|
||||
if err := svm.getStartError(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svm.Lock()
|
||||
defer svm.Unlock()
|
||||
return svm.config.CreateExt4Vhdx(destFile, sizeGB, cacheFile)
|
||||
}
|
||||
|
||||
// getShortContainerPath looks up where a SCSI disk was actually mounted
|
||||
// in a service VM when we remapped a long path name to a short name.
|
||||
func (svm *serviceVM) getShortContainerPath(mvd *hcsshim.MappedVirtualDisk) string {
|
||||
if mvd.ContainerPath == "" {
|
||||
return ""
|
||||
}
|
||||
avhd, ok := svm.attachedVHDs[mvd.HostPath]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("/tmp/d%d", avhd.attachCounter)
|
||||
}
|
||||
|
||||
func (svm *serviceVM) createUnionMount(mountName string, mvds ...hcsshim.MappedVirtualDisk) (err error) {
|
||||
if len(mvds) == 0 {
|
||||
return fmt.Errorf("createUnionMount: error must have at least 1 layer")
|
||||
}
|
||||
|
||||
if err = svm.getStartError(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svm.Lock()
|
||||
defer svm.Unlock()
|
||||
if _, ok := svm.unionMounts[mountName]; ok {
|
||||
svm.unionMounts[mountName]++
|
||||
return nil
|
||||
}
|
||||
|
||||
var lowerLayers []string
|
||||
if mvds[0].ReadOnly {
|
||||
lowerLayers = append(lowerLayers, svm.getShortContainerPath(&mvds[0]))
|
||||
}
|
||||
|
||||
for i := 1; i < len(mvds); i++ {
|
||||
lowerLayers = append(lowerLayers, svm.getShortContainerPath(&mvds[i]))
|
||||
}
|
||||
|
||||
logrus.Debugf("Doing the overlay mount with union directory=%s", mountName)
|
||||
errOut := &bytes.Buffer{}
|
||||
if err = svm.runProcess(fmt.Sprintf("mkdir -p %s", mountName), nil, nil, errOut); err != nil {
|
||||
return errors.Wrapf(err, "mkdir -p %s failed (%s)", mountName, errOut.String())
|
||||
}
|
||||
|
||||
var cmd string
|
||||
if len(mvds) == 1 {
|
||||
// `FROM SCRATCH` case and the only layer. No overlay required.
|
||||
cmd = fmt.Sprintf("mount %s %s", svm.getShortContainerPath(&mvds[0]), mountName)
|
||||
} else if mvds[0].ReadOnly {
|
||||
// Readonly overlay
|
||||
cmd = fmt.Sprintf("mount -t overlay overlay -olowerdir=%s %s",
|
||||
strings.Join(lowerLayers, ","),
|
||||
mountName)
|
||||
} else {
|
||||
upper := fmt.Sprintf("%s/upper", svm.getShortContainerPath(&mvds[0]))
|
||||
work := fmt.Sprintf("%s/work", svm.getShortContainerPath(&mvds[0]))
|
||||
|
||||
errOut := &bytes.Buffer{}
|
||||
if err = svm.runProcess(fmt.Sprintf("mkdir -p %s %s", upper, work), nil, nil, errOut); err != nil {
|
||||
return errors.Wrapf(err, "mkdir -p %s failed (%s)", mountName, errOut.String())
|
||||
}
|
||||
|
||||
cmd = fmt.Sprintf("mount -t overlay overlay -olowerdir=%s,upperdir=%s,workdir=%s %s",
|
||||
strings.Join(lowerLayers, ":"),
|
||||
upper,
|
||||
work,
|
||||
mountName)
|
||||
}
|
||||
|
||||
logrus.Debugf("createUnionMount: Executing mount=%s", cmd)
|
||||
errOut = &bytes.Buffer{}
|
||||
if err = svm.runProcess(cmd, nil, nil, errOut); err != nil {
|
||||
return errors.Wrapf(err, "%s failed (%s)", cmd, errOut.String())
|
||||
}
|
||||
|
||||
svm.unionMounts[mountName] = 1
|
||||
return nil
|
||||
}
|
||||
|
||||
func (svm *serviceVM) deleteUnionMount(mountName string, disks ...hcsshim.MappedVirtualDisk) error {
|
||||
if err := svm.getStartError(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svm.Lock()
|
||||
defer svm.Unlock()
|
||||
if _, ok := svm.unionMounts[mountName]; !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
if svm.unionMounts[mountName] > 1 {
|
||||
svm.unionMounts[mountName]--
|
||||
return nil
|
||||
}
|
||||
|
||||
logrus.Debugf("Removing union mount %s", mountName)
|
||||
if err := svm.runProcess(fmt.Sprintf("umount %s", mountName), nil, nil, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
delete(svm.unionMounts, mountName)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (svm *serviceVM) runProcess(command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
|
||||
var process hcsshim.Process
|
||||
var err error
|
||||
errOut := &bytes.Buffer{}
|
||||
|
||||
if stderr != nil {
|
||||
process, err = svm.config.RunProcess(command, stdin, stdout, stderr)
|
||||
} else {
|
||||
process, err = svm.config.RunProcess(command, stdin, stdout, errOut)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer process.Close()
|
||||
|
||||
process.WaitTimeout(time.Duration(int(time.Second) * svm.config.UvmTimeoutSeconds))
|
||||
exitCode, err := process.ExitCode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exitCode != 0 {
|
||||
// If the caller isn't explicitly capturing stderr output, then capture it here instead.
|
||||
e := fmt.Sprintf("svm.runProcess: command %s failed with exit code %d", command, exitCode)
|
||||
if stderr == nil {
|
||||
e = fmt.Sprintf("%s. (%s)", e, errOut.String())
|
||||
}
|
||||
return fmt.Errorf(e)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
// +build windows
|
||||
|
||||
package lcow // import "github.com/docker/docker/daemon/graphdriver/lcow"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/Microsoft/hcsshim"
|
||||
"github.com/Microsoft/opengcs/service/gcsutils/remotefs"
|
||||
"github.com/docker/docker/pkg/archive"
|
||||
"github.com/docker/docker/pkg/containerfs"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type lcowfs struct {
|
||||
root string
|
||||
d *Driver
|
||||
mappedDisks []hcsshim.MappedVirtualDisk
|
||||
vmID string
|
||||
currentSVM *serviceVM
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
var _ containerfs.ContainerFS = &lcowfs{}
|
||||
|
||||
// ErrNotSupported is an error for unsupported operations in the remotefs
|
||||
var ErrNotSupported = fmt.Errorf("not supported")
|
||||
|
||||
// Functions to implement the ContainerFS interface
|
||||
func (l *lcowfs) Path() string {
|
||||
return l.root
|
||||
}
|
||||
|
||||
func (l *lcowfs) ResolveScopedPath(path string, rawPath bool) (string, error) {
|
||||
logrus.Debugf("remotefs.resolvescopedpath inputs: %s %s ", path, l.root)
|
||||
|
||||
arg1 := l.Join(l.root, path)
|
||||
if !rawPath {
|
||||
// The l.Join("/", path) will make path an absolute path and then clean it
|
||||
// so if path = ../../X, it will become /X.
|
||||
arg1 = l.Join(l.root, l.Join("/", path))
|
||||
}
|
||||
arg2 := l.root
|
||||
|
||||
output := &bytes.Buffer{}
|
||||
if err := l.runRemoteFSProcess(nil, output, remotefs.ResolvePathCmd, arg1, arg2); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
logrus.Debugf("remotefs.resolvescopedpath success. Output: %s\n", output.String())
|
||||
return output.String(), nil
|
||||
}
|
||||
|
||||
func (l *lcowfs) OS() string {
|
||||
return "linux"
|
||||
}
|
||||
|
||||
func (l *lcowfs) Architecture() string {
|
||||
return runtime.GOARCH
|
||||
}
|
||||
|
||||
// Other functions that are used by docker like the daemon Archiver/Extractor
|
||||
func (l *lcowfs) ExtractArchive(src io.Reader, dst string, opts *archive.TarOptions) error {
|
||||
logrus.Debugf("remotefs.ExtractArchve inputs: %s %+v", dst, opts)
|
||||
|
||||
tarBuf := &bytes.Buffer{}
|
||||
if err := remotefs.WriteTarOptions(tarBuf, opts); err != nil {
|
||||
return fmt.Errorf("failed to marshall tar opts: %s", err)
|
||||
}
|
||||
|
||||
input := io.MultiReader(tarBuf, src)
|
||||
if err := l.runRemoteFSProcess(input, nil, remotefs.ExtractArchiveCmd, dst); err != nil {
|
||||
return fmt.Errorf("failed to extract archive to %s: %s", dst, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *lcowfs) ArchivePath(src string, opts *archive.TarOptions) (io.ReadCloser, error) {
|
||||
logrus.Debugf("remotefs.ArchivePath: %s %+v", src, opts)
|
||||
|
||||
tarBuf := &bytes.Buffer{}
|
||||
if err := remotefs.WriteTarOptions(tarBuf, opts); err != nil {
|
||||
return nil, fmt.Errorf("failed to marshall tar opts: %s", err)
|
||||
}
|
||||
|
||||
r, w := io.Pipe()
|
||||
go func() {
|
||||
defer w.Close()
|
||||
if err := l.runRemoteFSProcess(tarBuf, w, remotefs.ArchivePathCmd, src); err != nil {
|
||||
logrus.Debugf("REMOTEFS: Failed to extract archive: %s %+v %s", src, opts, err)
|
||||
}
|
||||
}()
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
func (l *lcowfs) startVM() error {
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
if l.currentSVM != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
svm, err := l.d.startServiceVMIfNotRunning(l.vmID, l.mappedDisks, fmt.Sprintf("lcowfs.startVM"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = svm.createUnionMount(l.root, l.mappedDisks...); err != nil {
|
||||
return err
|
||||
}
|
||||
l.currentSVM = svm
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *lcowfs) runRemoteFSProcess(stdin io.Reader, stdout io.Writer, args ...string) error {
|
||||
if err := l.startVM(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Append remotefs prefix and setup as a command line string
|
||||
cmd := fmt.Sprintf("%s %s", remotefs.RemotefsCmd, strings.Join(args, " "))
|
||||
stderr := &bytes.Buffer{}
|
||||
if err := l.currentSVM.runProcess(cmd, stdin, stdout, stderr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eerr, err := remotefs.ReadError(stderr)
|
||||
if eerr != nil {
|
||||
// Process returned an error so return that.
|
||||
return remotefs.ExportedToError(eerr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -1,211 +0,0 @@
|
||||
// +build windows
|
||||
|
||||
package lcow // import "github.com/docker/docker/daemon/graphdriver/lcow"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/Microsoft/hcsshim"
|
||||
"github.com/Microsoft/opengcs/service/gcsutils/remotefs"
|
||||
"github.com/containerd/continuity/driver"
|
||||
)
|
||||
|
||||
type lcowfile struct {
|
||||
process hcsshim.Process
|
||||
stdin io.WriteCloser
|
||||
stdout io.ReadCloser
|
||||
stderr io.ReadCloser
|
||||
fs *lcowfs
|
||||
guestPath string
|
||||
}
|
||||
|
||||
func (l *lcowfs) Open(path string) (driver.File, error) {
|
||||
return l.OpenFile(path, os.O_RDONLY, 0)
|
||||
}
|
||||
|
||||
func (l *lcowfs) OpenFile(path string, flag int, perm os.FileMode) (_ driver.File, err error) {
|
||||
flagStr := strconv.FormatInt(int64(flag), 10)
|
||||
permStr := strconv.FormatUint(uint64(perm), 8)
|
||||
|
||||
commandLine := fmt.Sprintf("%s %s %s %s %s", remotefs.RemotefsCmd, remotefs.OpenFileCmd, path, flagStr, permStr)
|
||||
env := make(map[string]string)
|
||||
env["PATH"] = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:"
|
||||
processConfig := &hcsshim.ProcessConfig{
|
||||
EmulateConsole: false,
|
||||
CreateStdInPipe: true,
|
||||
CreateStdOutPipe: true,
|
||||
CreateStdErrPipe: true,
|
||||
CreateInUtilityVm: true,
|
||||
WorkingDirectory: "/bin",
|
||||
Environment: env,
|
||||
CommandLine: commandLine,
|
||||
}
|
||||
|
||||
process, err := l.currentSVM.config.Uvm.CreateProcess(processConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open file %s: %s", path, err)
|
||||
}
|
||||
|
||||
stdin, stdout, stderr, err := process.Stdio()
|
||||
if err != nil {
|
||||
process.Kill()
|
||||
process.Close()
|
||||
return nil, fmt.Errorf("failed to open file pipes %s: %s", path, err)
|
||||
}
|
||||
|
||||
lf := &lcowfile{
|
||||
process: process,
|
||||
stdin: stdin,
|
||||
stdout: stdout,
|
||||
stderr: stderr,
|
||||
fs: l,
|
||||
guestPath: path,
|
||||
}
|
||||
|
||||
if _, err := lf.getResponse(); err != nil {
|
||||
return nil, fmt.Errorf("failed to open file %s: %s", path, err)
|
||||
}
|
||||
return lf, nil
|
||||
}
|
||||
|
||||
func (l *lcowfile) Read(b []byte) (int, error) {
|
||||
hdr := &remotefs.FileHeader{
|
||||
Cmd: remotefs.Read,
|
||||
Size: uint64(len(b)),
|
||||
}
|
||||
|
||||
if err := remotefs.WriteFileHeader(l.stdin, hdr, nil); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
buf, err := l.getResponse()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
n := copy(b, buf)
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (l *lcowfile) Write(b []byte) (int, error) {
|
||||
hdr := &remotefs.FileHeader{
|
||||
Cmd: remotefs.Write,
|
||||
Size: uint64(len(b)),
|
||||
}
|
||||
|
||||
if err := remotefs.WriteFileHeader(l.stdin, hdr, b); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
_, err := l.getResponse()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (l *lcowfile) Seek(offset int64, whence int) (int64, error) {
|
||||
seekHdr := &remotefs.SeekHeader{
|
||||
Offset: offset,
|
||||
Whence: int32(whence),
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := binary.Write(buf, binary.BigEndian, seekHdr); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
hdr := &remotefs.FileHeader{
|
||||
Cmd: remotefs.Write,
|
||||
Size: uint64(buf.Len()),
|
||||
}
|
||||
if err := remotefs.WriteFileHeader(l.stdin, hdr, buf.Bytes()); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
resBuf, err := l.getResponse()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var res int64
|
||||
if err := binary.Read(bytes.NewBuffer(resBuf), binary.BigEndian, &res); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (l *lcowfile) Close() error {
|
||||
hdr := &remotefs.FileHeader{
|
||||
Cmd: remotefs.Close,
|
||||
Size: 0,
|
||||
}
|
||||
|
||||
if err := remotefs.WriteFileHeader(l.stdin, hdr, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := l.getResponse()
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *lcowfile) Readdir(n int) ([]os.FileInfo, error) {
|
||||
nStr := strconv.FormatInt(int64(n), 10)
|
||||
|
||||
// Unlike the other File functions, this one can just be run without maintaining state,
|
||||
// so just do the normal runRemoteFSProcess way.
|
||||
buf := &bytes.Buffer{}
|
||||
if err := l.fs.runRemoteFSProcess(nil, buf, remotefs.ReadDirCmd, l.guestPath, nStr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var info []remotefs.FileInfo
|
||||
if err := json.Unmarshal(buf.Bytes(), &info); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
osInfo := make([]os.FileInfo, len(info))
|
||||
for i := range info {
|
||||
osInfo[i] = &info[i]
|
||||
}
|
||||
return osInfo, nil
|
||||
}
|
||||
|
||||
func (l *lcowfile) getResponse() ([]byte, error) {
|
||||
hdr, err := remotefs.ReadFileHeader(l.stdout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if hdr.Cmd != remotefs.CmdOK {
|
||||
// Something went wrong during the openfile in the server.
|
||||
// Parse stderr and return that as an error
|
||||
eerr, err := remotefs.ReadError(l.stderr)
|
||||
if eerr != nil {
|
||||
return nil, remotefs.ExportedToError(eerr)
|
||||
}
|
||||
|
||||
// Maybe the parsing went wrong?
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// At this point, we know something went wrong in the remotefs program, but
|
||||
// we we don't know why.
|
||||
return nil, fmt.Errorf("unknown error")
|
||||
}
|
||||
|
||||
// Successful command, we might have some data to read (for Read + Seek)
|
||||
buf := make([]byte, hdr.Size, hdr.Size)
|
||||
if _, err := io.ReadFull(l.stdout, buf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf, nil
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
// +build windows
|
||||
|
||||
package lcow // import "github.com/docker/docker/daemon/graphdriver/lcow"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/Microsoft/opengcs/service/gcsutils/remotefs"
|
||||
|
||||
"github.com/containerd/continuity/driver"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var _ driver.Driver = &lcowfs{}
|
||||
|
||||
func (l *lcowfs) Readlink(p string) (string, error) {
|
||||
logrus.Debugf("removefs.readlink args: %s", p)
|
||||
|
||||
result := &bytes.Buffer{}
|
||||
if err := l.runRemoteFSProcess(nil, result, remotefs.ReadlinkCmd, p); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return result.String(), nil
|
||||
}
|
||||
|
||||
func (l *lcowfs) Mkdir(path string, mode os.FileMode) error {
|
||||
return l.mkdir(path, mode, remotefs.MkdirCmd)
|
||||
}
|
||||
|
||||
func (l *lcowfs) MkdirAll(path string, mode os.FileMode) error {
|
||||
return l.mkdir(path, mode, remotefs.MkdirAllCmd)
|
||||
}
|
||||
|
||||
func (l *lcowfs) mkdir(path string, mode os.FileMode, cmd string) error {
|
||||
modeStr := strconv.FormatUint(uint64(mode), 8)
|
||||
logrus.Debugf("remotefs.%s args: %s %s", cmd, path, modeStr)
|
||||
return l.runRemoteFSProcess(nil, nil, cmd, path, modeStr)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Remove(path string) error {
|
||||
return l.remove(path, remotefs.RemoveCmd)
|
||||
}
|
||||
|
||||
func (l *lcowfs) RemoveAll(path string) error {
|
||||
return l.remove(path, remotefs.RemoveAllCmd)
|
||||
}
|
||||
|
||||
func (l *lcowfs) remove(path string, cmd string) error {
|
||||
logrus.Debugf("remotefs.%s args: %s", cmd, path)
|
||||
return l.runRemoteFSProcess(nil, nil, cmd, path)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Link(oldname, newname string) error {
|
||||
return l.link(oldname, newname, remotefs.LinkCmd)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Symlink(oldname, newname string) error {
|
||||
return l.link(oldname, newname, remotefs.SymlinkCmd)
|
||||
}
|
||||
|
||||
func (l *lcowfs) link(oldname, newname, cmd string) error {
|
||||
logrus.Debugf("remotefs.%s args: %s %s", cmd, oldname, newname)
|
||||
return l.runRemoteFSProcess(nil, nil, cmd, oldname, newname)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Lchown(name string, uid, gid int64) error {
|
||||
uidStr := strconv.FormatInt(uid, 10)
|
||||
gidStr := strconv.FormatInt(gid, 10)
|
||||
|
||||
logrus.Debugf("remotefs.lchown args: %s %s %s", name, uidStr, gidStr)
|
||||
return l.runRemoteFSProcess(nil, nil, remotefs.LchownCmd, name, uidStr, gidStr)
|
||||
}
|
||||
|
||||
// Lchmod changes the mode of an file not following symlinks.
|
||||
func (l *lcowfs) Lchmod(path string, mode os.FileMode) error {
|
||||
modeStr := strconv.FormatUint(uint64(mode), 8)
|
||||
logrus.Debugf("remotefs.lchmod args: %s %s", path, modeStr)
|
||||
return l.runRemoteFSProcess(nil, nil, remotefs.LchmodCmd, path, modeStr)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Mknod(path string, mode os.FileMode, major, minor int) error {
|
||||
modeStr := strconv.FormatUint(uint64(mode), 8)
|
||||
majorStr := strconv.FormatUint(uint64(major), 10)
|
||||
minorStr := strconv.FormatUint(uint64(minor), 10)
|
||||
|
||||
logrus.Debugf("remotefs.mknod args: %s %s %s %s", path, modeStr, majorStr, minorStr)
|
||||
return l.runRemoteFSProcess(nil, nil, remotefs.MknodCmd, path, modeStr, majorStr, minorStr)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Mkfifo(path string, mode os.FileMode) error {
|
||||
modeStr := strconv.FormatUint(uint64(mode), 8)
|
||||
logrus.Debugf("remotefs.mkfifo args: %s %s", path, modeStr)
|
||||
return l.runRemoteFSProcess(nil, nil, remotefs.MkfifoCmd, path, modeStr)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Stat(p string) (os.FileInfo, error) {
|
||||
return l.stat(p, remotefs.StatCmd)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Lstat(p string) (os.FileInfo, error) {
|
||||
return l.stat(p, remotefs.LstatCmd)
|
||||
}
|
||||
|
||||
func (l *lcowfs) stat(path string, cmd string) (os.FileInfo, error) {
|
||||
logrus.Debugf("remotefs.stat inputs: %s %s", cmd, path)
|
||||
|
||||
output := &bytes.Buffer{}
|
||||
err := l.runRemoteFSProcess(nil, output, cmd, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var fi remotefs.FileInfo
|
||||
if err := json.Unmarshal(output.Bytes(), &fi); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logrus.Debugf("remotefs.stat success. got: %v\n", fi)
|
||||
return &fi, nil
|
||||
}
|
||||
@@ -1,212 +0,0 @@
|
||||
// +build windows
|
||||
|
||||
package lcow // import "github.com/docker/docker/daemon/graphdriver/lcow"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
pathpkg "path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/containerd/continuity/pathdriver"
|
||||
)
|
||||
|
||||
var _ pathdriver.PathDriver = &lcowfs{}
|
||||
|
||||
// Continuity Path functions can be done locally
|
||||
func (l *lcowfs) Join(path ...string) string {
|
||||
return pathpkg.Join(path...)
|
||||
}
|
||||
|
||||
func (l *lcowfs) IsAbs(path string) bool {
|
||||
return pathpkg.IsAbs(path)
|
||||
}
|
||||
|
||||
func sameWord(a, b string) bool {
|
||||
return a == b
|
||||
}
|
||||
|
||||
// Implementation taken from the Go standard library
|
||||
func (l *lcowfs) Rel(basepath, targpath string) (string, error) {
|
||||
baseVol := ""
|
||||
targVol := ""
|
||||
base := l.Clean(basepath)
|
||||
targ := l.Clean(targpath)
|
||||
if sameWord(targ, base) {
|
||||
return ".", nil
|
||||
}
|
||||
base = base[len(baseVol):]
|
||||
targ = targ[len(targVol):]
|
||||
if base == "." {
|
||||
base = ""
|
||||
}
|
||||
// Can't use IsAbs - `\a` and `a` are both relative in Windows.
|
||||
baseSlashed := len(base) > 0 && base[0] == l.Separator()
|
||||
targSlashed := len(targ) > 0 && targ[0] == l.Separator()
|
||||
if baseSlashed != targSlashed || !sameWord(baseVol, targVol) {
|
||||
return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath)
|
||||
}
|
||||
// Position base[b0:bi] and targ[t0:ti] at the first differing elements.
|
||||
bl := len(base)
|
||||
tl := len(targ)
|
||||
var b0, bi, t0, ti int
|
||||
for {
|
||||
for bi < bl && base[bi] != l.Separator() {
|
||||
bi++
|
||||
}
|
||||
for ti < tl && targ[ti] != l.Separator() {
|
||||
ti++
|
||||
}
|
||||
if !sameWord(targ[t0:ti], base[b0:bi]) {
|
||||
break
|
||||
}
|
||||
if bi < bl {
|
||||
bi++
|
||||
}
|
||||
if ti < tl {
|
||||
ti++
|
||||
}
|
||||
b0 = bi
|
||||
t0 = ti
|
||||
}
|
||||
if base[b0:bi] == ".." {
|
||||
return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath)
|
||||
}
|
||||
if b0 != bl {
|
||||
// Base elements left. Must go up before going down.
|
||||
seps := strings.Count(base[b0:bl], string(l.Separator()))
|
||||
size := 2 + seps*3
|
||||
if tl != t0 {
|
||||
size += 1 + tl - t0
|
||||
}
|
||||
buf := make([]byte, size)
|
||||
n := copy(buf, "..")
|
||||
for i := 0; i < seps; i++ {
|
||||
buf[n] = l.Separator()
|
||||
copy(buf[n+1:], "..")
|
||||
n += 3
|
||||
}
|
||||
if t0 != tl {
|
||||
buf[n] = l.Separator()
|
||||
copy(buf[n+1:], targ[t0:])
|
||||
}
|
||||
return string(buf), nil
|
||||
}
|
||||
return targ[t0:], nil
|
||||
}
|
||||
|
||||
func (l *lcowfs) Base(path string) string {
|
||||
return pathpkg.Base(path)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Dir(path string) string {
|
||||
return pathpkg.Dir(path)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Clean(path string) string {
|
||||
return pathpkg.Clean(path)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Split(path string) (dir, file string) {
|
||||
return pathpkg.Split(path)
|
||||
}
|
||||
|
||||
func (l *lcowfs) Separator() byte {
|
||||
return '/'
|
||||
}
|
||||
|
||||
func (l *lcowfs) Abs(path string) (string, error) {
|
||||
// Abs is supposed to add the current working directory, which is meaningless in lcow.
|
||||
// So, return an error.
|
||||
return "", ErrNotSupported
|
||||
}
|
||||
|
||||
// Implementation taken from the Go standard library
|
||||
func (l *lcowfs) Walk(root string, walkFn filepath.WalkFunc) error {
|
||||
info, err := l.Lstat(root)
|
||||
if err != nil {
|
||||
err = walkFn(root, nil, err)
|
||||
} else {
|
||||
err = l.walk(root, info, walkFn)
|
||||
}
|
||||
if err == filepath.SkipDir {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// walk recursively descends path, calling w.
|
||||
func (l *lcowfs) walk(path string, info os.FileInfo, walkFn filepath.WalkFunc) error {
|
||||
err := walkFn(path, info, nil)
|
||||
if err != nil {
|
||||
if info.IsDir() && err == filepath.SkipDir {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
names, err := l.readDirNames(path)
|
||||
if err != nil {
|
||||
return walkFn(path, info, err)
|
||||
}
|
||||
|
||||
for _, name := range names {
|
||||
filename := l.Join(path, name)
|
||||
fileInfo, err := l.Lstat(filename)
|
||||
if err != nil {
|
||||
if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err = l.walk(filename, fileInfo, walkFn)
|
||||
if err != nil {
|
||||
if !fileInfo.IsDir() || err != filepath.SkipDir {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// readDirNames reads the directory named by dirname and returns
|
||||
// a sorted list of directory entries.
|
||||
func (l *lcowfs) readDirNames(dirname string) ([]string, error) {
|
||||
f, err := l.Open(dirname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files, err := f.Readdir(-1)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
names := make([]string, len(files), len(files))
|
||||
for i := range files {
|
||||
names[i] = files[i].Name()
|
||||
}
|
||||
|
||||
sort.Strings(names)
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// Note that Go's filepath.FromSlash/ToSlash convert between OS paths and '/'. Since the path separator
|
||||
// for LCOW (and Unix) is '/', they are no-ops.
|
||||
func (l *lcowfs) FromSlash(path string) string {
|
||||
return path
|
||||
}
|
||||
|
||||
func (l *lcowfs) ToSlash(path string) string {
|
||||
return path
|
||||
}
|
||||
|
||||
func (l *lcowfs) Match(pattern, name string) (matched bool, err error) {
|
||||
return pathpkg.Match(pattern, name)
|
||||
}
|
||||
@@ -2,6 +2,5 @@ package register // import "github.com/docker/docker/daemon/graphdriver/register
|
||||
|
||||
import (
|
||||
// register the windows graph drivers
|
||||
_ "github.com/docker/docker/daemon/graphdriver/lcow"
|
||||
_ "github.com/docker/docker/daemon/graphdriver/windows"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user