2015-10-05 18:52:09 -04:00
|
|
|
// +build daemon
|
2015-04-27 10:53:12 -04:00
|
|
|
|
2013-11-22 17:58:19 -05:00
|
|
|
package 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"
|
2014-10-29 15:06:51 -04:00
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2015-10-08 11:51:41 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
|
|
|
|
2015-07-16 19:00:55 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
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{
|
2015-10-08 11:51:41 -04: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-07 20:52:56 -05:00
|
|
|
}
|
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 {
|
2015-10-08 11:51:41 -04:00
|
|
|
home string
|
|
|
|
uidMaps []idtools.IDMap
|
|
|
|
gidMaps []idtools.IDMap
|
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
|
|
|
|
}
|
|
|
|
|
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.
|
2014-04-17 19:47:27 -04:00
|
|
|
func (d *Driver) Create(id, parent string) error {
|
2013-11-07 20:52:56 -05:00
|
|
|
dir := d.dir(id)
|
2015-10-08 11:51:41 -04: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-07 20:52:56 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := idtools.MkdirAs(dir, 0755, rootUID, rootGID); err != nil {
|
2013-11-07 20:52:56 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-08-20 16:36:42 -04:00
|
|
|
opts := []string{"level:s0"}
|
|
|
|
if _, mountLabel, err := label.InitLabels(opts); 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)
|
|
|
|
}
|
2014-10-29 15:06:51 -04:00
|
|
|
if err := chrootarchive.CopyWithTar(parentDir, dir); err != nil {
|
2013-11-07 20:52:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if _, err := os.Stat(d.dir(id)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.RemoveAll(d.dir(id))
|
|
|
|
}
|
|
|
|
|
2015-07-28 12:48:20 -04:00
|
|
|
// Get returns the directory for the given id.
|
2014-04-17 19:47:27 -04:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (string, error) {
|
2013-11-07 20:52:56 -05: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 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
|
|
|
|
}
|