daemon/graphdriver/vfs/driver.go
cee0a292
 package vfs
81674fbb
 
 import (
51c93c0f
 	"fmt"
81674fbb
 	"os"
e89f837b
 	"path/filepath"
73617e5e
 
 	"github.com/docker/docker/daemon/graphdriver"
1cb17f03
 	"github.com/docker/docker/pkg/chrootarchive"
7a7357da
 	"github.com/docker/docker/pkg/containerfs"
442b4562
 	"github.com/docker/docker/pkg/idtools"
54dcbab2
 	"github.com/docker/docker/pkg/system"
abbbf914
 	"github.com/opencontainers/selinux/go-selinux/label"
81674fbb
 )
 
4352da78
 var (
 	// CopyWithTar defines the copy method to use.
967ef7e6
 	CopyWithTar = chrootarchive.NewArchiver(nil).CopyWithTar
4352da78
 )
 
81674fbb
 func init() {
cee0a292
 	graphdriver.Register("vfs", Init)
81674fbb
 }
 
3e7f9c63
 // Init returns a new VFS driver.
 // This sets the home directory for the driver and returns NaiveDiffDriver.
442b4562
 func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
81674fbb
 	d := &Driver{
acdbc285
 		home:       home,
 		idMappings: idtools.NewIDMappingsFromMaps(uidMaps, gidMaps),
442b4562
 	}
93fbdb69
 	rootIDs := d.idMappings.RootPair()
acdbc285
 	if err := idtools.MkdirAllAndChown(home, 0700, rootIDs); err != nil {
442b4562
 		return nil, err
81674fbb
 	}
442b4562
 	return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
81674fbb
 }
 
3e7f9c63
 // Driver holds information about the driver, home directory of the driver.
 // Driver implements graphdriver.ProtoDriver. It uses only basic vfs operations.
 // In order to support layering, files are copied from the parent layer into the new layer. There is no copy-on-write support.
 // Driver must be wrapped in NaiveDiffDriver to be used as a graphdriver.Driver
81674fbb
 type Driver struct {
acdbc285
 	home       string
 	idMappings *idtools.IDMappings
81674fbb
 }
 
51c93c0f
 func (d *Driver) String() string {
cee0a292
 	return "vfs"
51c93c0f
 }
 
3e7f9c63
 // Status is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any status information.
243843c0
 func (d *Driver) Status() [][2]string {
 	return nil
 }
 
3e7f9c63
 // GetMetadata is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any meta data.
407a626b
 func (d *Driver) GetMetadata(id string) (map[string]string, error) {
 	return nil, nil
 }
 
3e7f9c63
 // Cleanup is used to implement graphdriver.ProtoDriver. There is no cleanup required for this driver.
81674fbb
 func (d *Driver) Cleanup() error {
 	return nil
 }
 
ef5bfad3
 // CreateReadWrite creates a layer that is writable for use as a container
 // file system.
b937aa8e
 func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
 	return d.Create(id, parent, opts)
ef5bfad3
 }
 
3e7f9c63
 // Create prepares the filesystem for the VFS driver and copies the directory for the given id under the parent.
b937aa8e
 func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
 	if opts != nil && len(opts.StorageOpt) != 0 {
b16decfc
 		return fmt.Errorf("--storage-opt is not supported for vfs")
 	}
 
81674fbb
 	dir := d.dir(id)
93fbdb69
 	rootIDs := d.idMappings.RootPair()
acdbc285
 	if err := idtools.MkdirAllAndChown(filepath.Dir(dir), 0700, rootIDs); err != nil {
81674fbb
 		return err
 	}
acdbc285
 	if err := idtools.MkdirAndChown(dir, 0755, rootIDs); err != nil {
81674fbb
 		return err
 	}
b937aa8e
 	labelOpts := []string{"level:s0"}
 	if _, mountLabel, err := label.InitLabels(labelOpts); err == nil {
4eb2fd16
 		label.SetFileLabel(dir, mountLabel)
73617e5e
 	}
81674fbb
 	if parent == "" {
 		return nil
 	}
f0e6e135
 	parentDir, err := d.Get(parent, "")
81674fbb
 	if err != nil {
 		return fmt.Errorf("%s: %s", parent, err)
 	}
7a7357da
 	return CopyWithTar(parentDir.Path(), dir)
81674fbb
 }
 
 func (d *Driver) dir(id string) string {
e89f837b
 	return filepath.Join(d.home, "dir", filepath.Base(id))
81674fbb
 }
 
3e7f9c63
 // Remove deletes the content from the directory for a given id.
81674fbb
 func (d *Driver) Remove(id string) error {
f7f101d5
 	return system.EnsureRemoveAll(d.dir(id))
81674fbb
 }
 
3e7f9c63
 // Get returns the directory for the given id.
7a7357da
 func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
81674fbb
 	dir := d.dir(id)
 	if st, err := os.Stat(dir); err != nil {
7a7357da
 		return nil, err
81674fbb
 	} else if !st.IsDir() {
7a7357da
 		return nil, fmt.Errorf("%s: not a directory", dir)
81674fbb
 	}
7a7357da
 	return containerfs.NewLocalContainerFS(dir), nil
81674fbb
 }
1b28cdc7
 
3e7f9c63
 // Put is a noop for vfs that return nil for the error, since this driver has no runtime resources to clean up.
00fd63e5
 func (d *Driver) Put(id string) error {
bcaf6c23
 	// The vfs driver has no runtime resources (e.g. mounts)
 	// to clean up, so we don't need anything here
00fd63e5
 	return nil
bcaf6c23
 }
 
3e7f9c63
 // Exists checks to see if the directory exists for the given id.
1b28cdc7
 func (d *Driver) Exists(id string) bool {
 	_, err := os.Stat(d.dir(id))
 	return err == nil
 }