github.com/endocode/docker@v1.4.2-0.20160113120958-46eb4700391e/daemon/graphdriver/vfs/driver.go (about)

     1  package vfs
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/docker/docker/daemon/graphdriver"
     9  	"github.com/docker/docker/pkg/chrootarchive"
    10  	"github.com/docker/docker/pkg/idtools"
    11  
    12  	"github.com/opencontainers/runc/libcontainer/label"
    13  )
    14  
    15  var (
    16  	// CopyWithTar defines the copy method to use.
    17  	CopyWithTar = chrootarchive.CopyWithTar
    18  )
    19  
    20  func init() {
    21  	graphdriver.Register("vfs", Init)
    22  }
    23  
    24  // Init returns a new VFS driver.
    25  // This sets the home directory for the driver and returns NaiveDiffDriver.
    26  func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
    27  	d := &Driver{
    28  		home:    home,
    29  		uidMaps: uidMaps,
    30  		gidMaps: gidMaps,
    31  	}
    32  	rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	if err := idtools.MkdirAllAs(home, 0700, rootUID, rootGID); err != nil {
    37  		return nil, err
    38  	}
    39  	return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
    40  }
    41  
    42  // Driver holds information about the driver, home directory of the driver.
    43  // Driver implements graphdriver.ProtoDriver. It uses only basic vfs operations.
    44  // In order to support layering, files are copied from the parent layer into the new layer. There is no copy-on-write support.
    45  // Driver must be wrapped in NaiveDiffDriver to be used as a graphdriver.Driver
    46  type Driver struct {
    47  	home    string
    48  	uidMaps []idtools.IDMap
    49  	gidMaps []idtools.IDMap
    50  }
    51  
    52  func (d *Driver) String() string {
    53  	return "vfs"
    54  }
    55  
    56  // Status is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any status information.
    57  func (d *Driver) Status() [][2]string {
    58  	return nil
    59  }
    60  
    61  // GetMetadata is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any meta data.
    62  func (d *Driver) GetMetadata(id string) (map[string]string, error) {
    63  	return nil, nil
    64  }
    65  
    66  // Cleanup is used to implement graphdriver.ProtoDriver. There is no cleanup required for this driver.
    67  func (d *Driver) Cleanup() error {
    68  	return nil
    69  }
    70  
    71  // Create prepares the filesystem for the VFS driver and copies the directory for the given id under the parent.
    72  func (d *Driver) Create(id, parent, mountLabel string) error {
    73  	dir := d.dir(id)
    74  	rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	if err := idtools.MkdirAllAs(filepath.Dir(dir), 0700, rootUID, rootGID); err != nil {
    79  		return err
    80  	}
    81  	if err := idtools.MkdirAs(dir, 0755, rootUID, rootGID); err != nil {
    82  		return err
    83  	}
    84  	opts := []string{"level:s0"}
    85  	if _, mountLabel, err := label.InitLabels(opts); err == nil {
    86  		label.SetFileLabel(dir, mountLabel)
    87  	}
    88  	if parent == "" {
    89  		return nil
    90  	}
    91  	parentDir, err := d.Get(parent, "")
    92  	if err != nil {
    93  		return fmt.Errorf("%s: %s", parent, err)
    94  	}
    95  	if err := CopyWithTar(parentDir, dir); err != nil {
    96  		return err
    97  	}
    98  	return nil
    99  }
   100  
   101  func (d *Driver) dir(id string) string {
   102  	return filepath.Join(d.home, "dir", filepath.Base(id))
   103  }
   104  
   105  // Remove deletes the content from the directory for a given id.
   106  func (d *Driver) Remove(id string) error {
   107  	if err := os.RemoveAll(d.dir(id)); err != nil && !os.IsNotExist(err) {
   108  		return err
   109  	}
   110  	return nil
   111  }
   112  
   113  // Get returns the directory for the given id.
   114  func (d *Driver) Get(id, mountLabel string) (string, error) {
   115  	dir := d.dir(id)
   116  	if st, err := os.Stat(dir); err != nil {
   117  		return "", err
   118  	} else if !st.IsDir() {
   119  		return "", fmt.Errorf("%s: not a directory", dir)
   120  	}
   121  	return dir, nil
   122  }
   123  
   124  // Put is a noop for vfs that return nil for the error, since this driver has no runtime resources to clean up.
   125  func (d *Driver) Put(id string) error {
   126  	// The vfs driver has no runtime resources (e.g. mounts)
   127  	// to clean up, so we don't need anything here
   128  	return nil
   129  }
   130  
   131  // Exists checks to see if the directory exists for the given id.
   132  func (d *Driver) Exists(id string) bool {
   133  	_, err := os.Stat(d.dir(id))
   134  	return err == nil
   135  }