2013-11-22 22:58:19 +00:00
|
|
|
package 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"
|
2014-10-29 19:06:51 +00:00
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2015-10-08 15:51:41 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
|
|
|
|
2015-07-16 23:00:55 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2013-11-08 01:52:56 +00:00
|
|
|
)
|
|
|
|
|
2015-11-18 22:20:54 +00:00
|
|
|
var (
|
|
|
|
// CopyWithTar defines the copy method to use.
|
|
|
|
CopyWithTar = chrootarchive.CopyWithTar
|
|
|
|
)
|
|
|
|
|
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{
|
2015-10-08 15:51:41 +00:00
|
|
|
home: home,
|
|
|
|
uidMaps: uidMaps,
|
|
|
|
gidMaps: gidMaps,
|
|
|
|
}
|
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := idtools.MkdirAllAs(home, 0700, rootUID, rootGID); err != nil {
|
|
|
|
return nil, err
|
2013-11-08 01:52:56 +00:00
|
|
|
}
|
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 {
|
2015-10-08 15:51:41 +00:00
|
|
|
home string
|
|
|
|
uidMaps []idtools.IDMap
|
|
|
|
gidMaps []idtools.IDMap
|
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
|
|
|
|
}
|
|
|
|
|
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.
|
2015-10-28 13:19:51 +00:00
|
|
|
func (d *Driver) Create(id, parent, mountLabel string) error {
|
2013-11-08 01:52:56 +00:00
|
|
|
dir := d.dir(id)
|
2015-10-08 15:51:41 +00:00
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := idtools.MkdirAllAs(filepath.Dir(dir), 0700, rootUID, rootGID); err != nil {
|
2013-11-08 01:52:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-10-08 15:51:41 +00:00
|
|
|
if err := idtools.MkdirAs(dir, 0755, rootUID, rootGID); err != nil {
|
2013-11-08 01:52:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-08-20 20:36:42 +00:00
|
|
|
opts := []string{"level:s0"}
|
|
|
|
if _, mountLabel, err := label.InitLabels(opts); 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)
|
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
if err := CopyWithTar(parentDir, dir); err != nil {
|
2013-11-08 01:52:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if _, err := os.Stat(d.dir(id)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.RemoveAll(d.dir(id))
|
|
|
|
}
|
|
|
|
|
2015-07-28 16:48:20 +00:00
|
|
|
// Get returns the directory for the given id.
|
2014-04-17 23:47:27 +00:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (string, error) {
|
2013-11-08 01:52:56 +00:00
|
|
|
dir := d.dir(id)
|
|
|
|
if st, err := os.Stat(dir); err != nil {
|
|
|
|
return "", err
|
|
|
|
} else if !st.IsDir() {
|
|
|
|
return "", fmt.Errorf("%s: not a directory", dir)
|
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|
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
|
|
|
|
}
|