2018-02-05 16:05:59 -05:00
|
|
|
package vfs // import "github.com/docker/docker/daemon/graphdriver/vfs"
|
2013-11-07 20:52:56 -05:00
|
|
|
|
|
|
|
import (
|
2013-11-07 22:02:15 -05:00
|
|
|
"fmt"
|
2013-11-07 20:52:56 -05:00
|
|
|
"os"
|
2015-05-26 15:45:08 -04:00
|
|
|
"path/filepath"
|
2014-08-20 16:36:42 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
2017-10-30 16:18:14 -04:00
|
|
|
"github.com/docker/docker/daemon/graphdriver/quota"
|
2019-01-15 06:49:23 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2015-10-08 11:51:41 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2019-01-15 06:49:23 -05:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2017-02-14 13:35:20 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2018-05-19 07:38:54 -04:00
|
|
|
"github.com/docker/go-units"
|
2017-04-18 09:26:36 -04:00
|
|
|
"github.com/opencontainers/selinux/go-selinux/label"
|
2019-01-15 06:49:23 -05:00
|
|
|
"github.com/pkg/errors"
|
2013-11-07 20:52:56 -05:00
|
|
|
)
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
var (
|
2017-11-21 13:29:27 -05:00
|
|
|
// CopyDir defines the copy method to use.
|
|
|
|
CopyDir = dirCopy
|
2015-11-18 17:20:54 -05:00
|
|
|
)
|
|
|
|
|
2013-11-07 20:52:56 -05:00
|
|
|
func init() {
|
2013-11-22 17:58:19 -05:00
|
|
|
graphdriver.Register("vfs", Init)
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// Init returns a new VFS driver.
|
|
|
|
// This sets the home directory for the driver and returns NaiveDiffDriver.
|
2015-10-08 11:51:41 -04:00
|
|
|
func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
|
2013-11-07 20:52:56 -05:00
|
|
|
d := &Driver{
|
2017-11-16 01:20:33 -05:00
|
|
|
home: home,
|
|
|
|
idMapping: idtools.NewIDMappingsFromMaps(uidMaps, gidMaps),
|
2015-10-08 11:51:41 -04:00
|
|
|
}
|
2019-01-15 06:49:23 -05:00
|
|
|
|
|
|
|
if err := d.parseOptions(options); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-16 01:20:33 -05:00
|
|
|
rootIDs := d.idMapping.RootPair()
|
2017-05-24 16:25:13 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(home, 0700, rootIDs); err != nil {
|
2015-10-08 11:51:41 -04:00
|
|
|
return nil, err
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
2017-10-30 16:18:14 -04:00
|
|
|
|
2017-12-19 16:47:12 -05:00
|
|
|
setupDriverQuota(d)
|
2017-10-30 16:18:14 -04:00
|
|
|
|
2019-01-15 06:49:23 -05:00
|
|
|
if size := d.getQuotaOpt(); !d.quotaSupported() && size > 0 {
|
|
|
|
return nil, quota.ErrQuotaNotSupported
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// 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
|
2013-11-07 20:52:56 -05:00
|
|
|
type Driver struct {
|
2017-10-30 16:18:14 -04:00
|
|
|
driverQuota
|
2017-11-16 01:20:33 -05:00
|
|
|
home string
|
|
|
|
idMapping *idtools.IdentityMapping
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
|
|
|
|
2013-11-07 22:02:15 -05:00
|
|
|
func (d *Driver) String() string {
|
2013-11-22 17:58:19 -05:00
|
|
|
return "vfs"
|
2013-11-07 22:02:15 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// Status is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any status information.
|
2013-11-15 05:04:02 -05:00
|
|
|
func (d *Driver) Status() [][2]string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// GetMetadata is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any meta data.
|
2015-06-15 14:05:10 -04:00
|
|
|
func (d *Driver) GetMetadata(id string) (map[string]string, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// Cleanup is used to implement graphdriver.ProtoDriver. There is no cleanup required for this driver.
|
2013-11-07 20:52:56 -05:00
|
|
|
func (d *Driver) Cleanup() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-15 06:49:23 -05:00
|
|
|
func (d *Driver) parseOptions(options []string) error {
|
|
|
|
for _, option := range options {
|
|
|
|
key, val, err := parsers.ParseKeyValueOpt(option)
|
|
|
|
if err != nil {
|
|
|
|
return errdefs.InvalidParameter(err)
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "size":
|
|
|
|
size, err := units.RAMInBytes(val)
|
|
|
|
if err != nil {
|
|
|
|
return errdefs.InvalidParameter(err)
|
|
|
|
}
|
|
|
|
if err = d.setQuotaOpt(uint64(size)); err != nil {
|
|
|
|
return errdefs.InvalidParameter(errors.Wrap(err, "failed to set option size for vfs"))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errdefs.InvalidParameter(errors.Errorf("unknown option %s for vfs", key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-01-15 06:49:23 -05:00
|
|
|
quotaSize := d.getQuotaOpt()
|
2017-10-30 16:18:14 -04:00
|
|
|
|
|
|
|
if opts != nil {
|
|
|
|
for key, val := range opts.StorageOpt {
|
|
|
|
switch key {
|
|
|
|
case "size":
|
|
|
|
if !d.quotaSupported() {
|
|
|
|
return quota.ErrQuotaNotSupported
|
|
|
|
}
|
2019-01-15 06:49:23 -05:00
|
|
|
size, err := units.RAMInBytes(val)
|
|
|
|
if err != nil {
|
|
|
|
return errdefs.InvalidParameter(err)
|
2017-10-30 16:18:14 -04:00
|
|
|
}
|
2019-01-15 06:49:23 -05:00
|
|
|
quotaSize = uint64(size)
|
2017-10-30 16:18:14 -04:00
|
|
|
default:
|
2019-01-15 06:49:23 -05:00
|
|
|
return errdefs.InvalidParameter(errors.Errorf("Storage opt %s not supported", key))
|
2017-10-30 16:18:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 06:49:23 -05:00
|
|
|
return d.create(id, parent, quotaSize)
|
2016-02-18 20:24:59 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// Create prepares the filesystem for the VFS driver and copies the directory for the given id under the parent.
|
2016-11-09 15:59:58 -05:00
|
|
|
func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
|
|
|
|
if opts != nil && len(opts.StorageOpt) != 0 {
|
2017-10-30 16:18:14 -04:00
|
|
|
return fmt.Errorf("--storage-opt is not supported for vfs on read-only layers")
|
2016-03-20 00:42:58 -04:00
|
|
|
}
|
|
|
|
|
2017-10-30 16:18:14 -04:00
|
|
|
return d.create(id, parent, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) create(id, parent string, size uint64) error {
|
2013-11-07 20:52:56 -05:00
|
|
|
dir := d.dir(id)
|
2017-11-16 01:20:33 -05:00
|
|
|
rootIDs := d.idMapping.RootPair()
|
2017-05-24 16:25:13 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(filepath.Dir(dir), 0700, rootIDs); err != nil {
|
2013-11-07 20:52:56 -05:00
|
|
|
return err
|
|
|
|
}
|
2017-05-24 16:25:13 -04:00
|
|
|
if err := idtools.MkdirAndChown(dir, 0755, rootIDs); err != nil {
|
2013-11-07 20:52:56 -05:00
|
|
|
return err
|
|
|
|
}
|
2017-10-30 16:18:14 -04:00
|
|
|
|
|
|
|
if size != 0 {
|
|
|
|
if err := d.setupQuota(dir, size); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
labelOpts := []string{"level:s0"}
|
|
|
|
if _, mountLabel, err := label.InitLabels(labelOpts); err == nil {
|
2015-03-05 13:47:40 -05:00
|
|
|
label.SetFileLabel(dir, mountLabel)
|
2014-08-20 16:36:42 -04:00
|
|
|
}
|
2013-11-07 20:52:56 -05:00
|
|
|
if parent == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-17 19:47:27 -04:00
|
|
|
parentDir, err := d.Get(parent, "")
|
2013-11-07 20:52:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", parent, err)
|
|
|
|
}
|
2017-11-21 13:29:27 -05:00
|
|
|
return CopyDir(parentDir.Path(), dir)
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) dir(id string) string {
|
2015-05-26 15:45:08 -04:00
|
|
|
return filepath.Join(d.home, "dir", filepath.Base(id))
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// Remove deletes the content from the directory for a given id.
|
2013-11-07 20:52:56 -05:00
|
|
|
func (d *Driver) Remove(id string) error {
|
2017-09-11 14:55:05 -04:00
|
|
|
return system.EnsureRemoveAll(d.dir(id))
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// Get returns the directory for the given id.
|
2017-08-03 20:22:00 -04:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
|
2013-11-07 20:52:56 -05:00
|
|
|
dir := d.dir(id)
|
|
|
|
if st, err := os.Stat(dir); err != nil {
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2013-11-07 20:52:56 -05:00
|
|
|
} else if !st.IsDir() {
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, fmt.Errorf("%s: not a directory", dir)
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
return containerfs.NewLocalContainerFS(dir), nil
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
2013-11-19 05:32:08 -05:00
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// Put is a noop for vfs that return nil for the error, since this driver has no runtime resources to clean up.
|
2015-01-09 17:14:52 -05:00
|
|
|
func (d *Driver) Put(id string) error {
|
2013-12-05 16:18:02 -05:00
|
|
|
// The vfs driver has no runtime resources (e.g. mounts)
|
|
|
|
// to clean up, so we don't need anything here
|
2015-01-09 17:14:52 -05:00
|
|
|
return nil
|
2013-12-05 16:18:02 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// Exists checks to see if the directory exists for the given id.
|
2013-11-19 05:32:08 -05:00
|
|
|
func (d *Driver) Exists(id string) bool {
|
|
|
|
_, err := os.Stat(d.dir(id))
|
|
|
|
return err == nil
|
|
|
|
}
|