github.com/jen20/docker@v1.13.1/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 // CreateReadWrite creates a layer that is writable for use as a container 72 // file system. 73 func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error { 74 return d.Create(id, parent, opts) 75 } 76 77 // Create prepares the filesystem for the VFS driver and copies the directory for the given id under the parent. 78 func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error { 79 if opts != nil && len(opts.StorageOpt) != 0 { 80 return fmt.Errorf("--storage-opt is not supported for vfs") 81 } 82 83 dir := d.dir(id) 84 rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps) 85 if err != nil { 86 return err 87 } 88 if err := idtools.MkdirAllAs(filepath.Dir(dir), 0700, rootUID, rootGID); err != nil { 89 return err 90 } 91 if err := idtools.MkdirAs(dir, 0755, rootUID, rootGID); err != nil { 92 return err 93 } 94 labelOpts := []string{"level:s0"} 95 if _, mountLabel, err := label.InitLabels(labelOpts); err == nil { 96 label.SetFileLabel(dir, mountLabel) 97 } 98 if parent == "" { 99 return nil 100 } 101 parentDir, err := d.Get(parent, "") 102 if err != nil { 103 return fmt.Errorf("%s: %s", parent, err) 104 } 105 if err := CopyWithTar(parentDir, dir); err != nil { 106 return err 107 } 108 return nil 109 } 110 111 func (d *Driver) dir(id string) string { 112 return filepath.Join(d.home, "dir", filepath.Base(id)) 113 } 114 115 // Remove deletes the content from the directory for a given id. 116 func (d *Driver) Remove(id string) error { 117 if err := os.RemoveAll(d.dir(id)); err != nil && !os.IsNotExist(err) { 118 return err 119 } 120 return nil 121 } 122 123 // Get returns the directory for the given id. 124 func (d *Driver) Get(id, mountLabel string) (string, error) { 125 dir := d.dir(id) 126 if st, err := os.Stat(dir); err != nil { 127 return "", err 128 } else if !st.IsDir() { 129 return "", fmt.Errorf("%s: not a directory", dir) 130 } 131 return dir, nil 132 } 133 134 // Put is a noop for vfs that return nil for the error, since this driver has no runtime resources to clean up. 135 func (d *Driver) Put(id string) error { 136 // The vfs driver has no runtime resources (e.g. mounts) 137 // to clean up, so we don't need anything here 138 return nil 139 } 140 141 // Exists checks to see if the directory exists for the given id. 142 func (d *Driver) Exists(id string) bool { 143 _, err := os.Stat(d.dir(id)) 144 return err == nil 145 }