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