github.com/dpiddy/docker@v1.12.2-rc1/volume/local/local.go (about) 1 // Package local provides the default implementation for volumes. It 2 // is used to mount data volume containers and directories local to 3 // the host server. 4 package local 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "reflect" 13 "sync" 14 15 "github.com/Sirupsen/logrus" 16 "github.com/docker/docker/pkg/idtools" 17 "github.com/docker/docker/pkg/mount" 18 "github.com/docker/docker/utils" 19 "github.com/docker/docker/volume" 20 ) 21 22 // VolumeDataPathName is the name of the directory where the volume data is stored. 23 // It uses a very distinctive name to avoid collisions migrating data between 24 // Docker versions. 25 const ( 26 VolumeDataPathName = "_data" 27 volumesPathName = "volumes" 28 ) 29 30 var ( 31 // ErrNotFound is the typed error returned when the requested volume name can't be found 32 ErrNotFound = fmt.Errorf("volume not found") 33 // volumeNameRegex ensures the name assigned for the volume is valid. 34 // This name is used to create the bind directory, so we need to avoid characters that 35 // would make the path to escape the root directory. 36 volumeNameRegex = utils.RestrictedVolumeNamePattern 37 ) 38 39 type validationError struct { 40 error 41 } 42 43 func (validationError) IsValidationError() bool { 44 return true 45 } 46 47 type activeMount struct { 48 count uint64 49 mounted bool 50 } 51 52 // New instantiates a new Root instance with the provided scope. Scope 53 // is the base path that the Root instance uses to store its 54 // volumes. The base path is created here if it does not exist. 55 func New(scope string, rootUID, rootGID int) (*Root, error) { 56 rootDirectory := filepath.Join(scope, volumesPathName) 57 58 if err := idtools.MkdirAllAs(rootDirectory, 0700, rootUID, rootGID); err != nil { 59 return nil, err 60 } 61 62 r := &Root{ 63 scope: scope, 64 path: rootDirectory, 65 volumes: make(map[string]*localVolume), 66 rootUID: rootUID, 67 rootGID: rootGID, 68 } 69 70 dirs, err := ioutil.ReadDir(rootDirectory) 71 if err != nil { 72 return nil, err 73 } 74 75 mountInfos, err := mount.GetMounts() 76 if err != nil { 77 logrus.Debugf("error looking up mounts for local volume cleanup: %v", err) 78 } 79 80 for _, d := range dirs { 81 if !d.IsDir() { 82 continue 83 } 84 85 name := filepath.Base(d.Name()) 86 v := &localVolume{ 87 driverName: r.Name(), 88 name: name, 89 path: r.DataPath(name), 90 } 91 r.volumes[name] = v 92 optsFilePath := filepath.Join(rootDirectory, name, "opts.json") 93 if b, err := ioutil.ReadFile(optsFilePath); err == nil { 94 opts := optsConfig{} 95 if err := json.Unmarshal(b, &opts); err != nil { 96 return nil, err 97 } 98 if !reflect.DeepEqual(opts, optsConfig{}) { 99 v.opts = &opts 100 } 101 102 // unmount anything that may still be mounted (for example, from an unclean shutdown) 103 for _, info := range mountInfos { 104 if info.Mountpoint == v.path { 105 mount.Unmount(v.path) 106 break 107 } 108 } 109 } 110 } 111 112 return r, nil 113 } 114 115 // Root implements the Driver interface for the volume package and 116 // manages the creation/removal of volumes. It uses only standard vfs 117 // commands to create/remove dirs within its provided scope. 118 type Root struct { 119 m sync.Mutex 120 scope string 121 path string 122 volumes map[string]*localVolume 123 rootUID int 124 rootGID int 125 } 126 127 // List lists all the volumes 128 func (r *Root) List() ([]volume.Volume, error) { 129 var ls []volume.Volume 130 r.m.Lock() 131 for _, v := range r.volumes { 132 ls = append(ls, v) 133 } 134 r.m.Unlock() 135 return ls, nil 136 } 137 138 // DataPath returns the constructed path of this volume. 139 func (r *Root) DataPath(volumeName string) string { 140 return filepath.Join(r.path, volumeName, VolumeDataPathName) 141 } 142 143 // Name returns the name of Root, defined in the volume package in the DefaultDriverName constant. 144 func (r *Root) Name() string { 145 return volume.DefaultDriverName 146 } 147 148 // Create creates a new volume.Volume with the provided name, creating 149 // the underlying directory tree required for this volume in the 150 // process. 151 func (r *Root) Create(name string, opts map[string]string) (volume.Volume, error) { 152 if err := r.validateName(name); err != nil { 153 return nil, err 154 } 155 156 r.m.Lock() 157 defer r.m.Unlock() 158 159 v, exists := r.volumes[name] 160 if exists { 161 return v, nil 162 } 163 164 path := r.DataPath(name) 165 if err := idtools.MkdirAllAs(path, 0755, r.rootUID, r.rootGID); err != nil { 166 if os.IsExist(err) { 167 return nil, fmt.Errorf("volume already exists under %s", filepath.Dir(path)) 168 } 169 return nil, err 170 } 171 172 var err error 173 defer func() { 174 if err != nil { 175 os.RemoveAll(filepath.Dir(path)) 176 } 177 }() 178 179 v = &localVolume{ 180 driverName: r.Name(), 181 name: name, 182 path: path, 183 } 184 185 if len(opts) != 0 { 186 if err = setOpts(v, opts); err != nil { 187 return nil, err 188 } 189 var b []byte 190 b, err = json.Marshal(v.opts) 191 if err != nil { 192 return nil, err 193 } 194 if err = ioutil.WriteFile(filepath.Join(filepath.Dir(path), "opts.json"), b, 600); err != nil { 195 return nil, err 196 } 197 } 198 199 r.volumes[name] = v 200 return v, nil 201 } 202 203 // Remove removes the specified volume and all underlying data. If the 204 // given volume does not belong to this driver and an error is 205 // returned. The volume is reference counted, if all references are 206 // not released then the volume is not removed. 207 func (r *Root) Remove(v volume.Volume) error { 208 r.m.Lock() 209 defer r.m.Unlock() 210 211 lv, ok := v.(*localVolume) 212 if !ok { 213 return fmt.Errorf("unknown volume type %T", v) 214 } 215 216 realPath, err := filepath.EvalSymlinks(lv.path) 217 if err != nil { 218 if !os.IsNotExist(err) { 219 return err 220 } 221 realPath = filepath.Dir(lv.path) 222 } 223 224 if !r.scopedPath(realPath) { 225 return fmt.Errorf("Unable to remove a directory of out the Docker root %s: %s", r.scope, realPath) 226 } 227 228 if err := removePath(realPath); err != nil { 229 return err 230 } 231 232 delete(r.volumes, lv.name) 233 return removePath(filepath.Dir(lv.path)) 234 } 235 236 func removePath(path string) error { 237 if err := os.RemoveAll(path); err != nil { 238 if os.IsNotExist(err) { 239 return nil 240 } 241 return err 242 } 243 return nil 244 } 245 246 // Get looks up the volume for the given name and returns it if found 247 func (r *Root) Get(name string) (volume.Volume, error) { 248 r.m.Lock() 249 v, exists := r.volumes[name] 250 r.m.Unlock() 251 if !exists { 252 return nil, ErrNotFound 253 } 254 return v, nil 255 } 256 257 // Scope returns the local volume scope 258 func (r *Root) Scope() string { 259 return volume.LocalScope 260 } 261 262 func (r *Root) validateName(name string) error { 263 if !volumeNameRegex.MatchString(name) { 264 return validationError{fmt.Errorf("%q includes invalid characters for a local volume name, only %q are allowed", name, utils.RestrictedNameChars)} 265 } 266 return nil 267 } 268 269 // localVolume implements the Volume interface from the volume package and 270 // represents the volumes created by Root. 271 type localVolume struct { 272 m sync.Mutex 273 // unique name of the volume 274 name string 275 // path is the path on the host where the data lives 276 path string 277 // driverName is the name of the driver that created the volume. 278 driverName string 279 // opts is the parsed list of options used to create the volume 280 opts *optsConfig 281 // active refcounts the active mounts 282 active activeMount 283 } 284 285 // Name returns the name of the given Volume. 286 func (v *localVolume) Name() string { 287 return v.name 288 } 289 290 // DriverName returns the driver that created the given Volume. 291 func (v *localVolume) DriverName() string { 292 return v.driverName 293 } 294 295 // Path returns the data location. 296 func (v *localVolume) Path() string { 297 return v.path 298 } 299 300 // Mount implements the localVolume interface, returning the data location. 301 func (v *localVolume) Mount(id string) (string, error) { 302 v.m.Lock() 303 defer v.m.Unlock() 304 if v.opts != nil { 305 if !v.active.mounted { 306 if err := v.mount(); err != nil { 307 return "", err 308 } 309 v.active.mounted = true 310 } 311 v.active.count++ 312 } 313 return v.path, nil 314 } 315 316 // Umount is for satisfying the localVolume interface and does not do anything in this driver. 317 func (v *localVolume) Unmount(id string) error { 318 v.m.Lock() 319 defer v.m.Unlock() 320 if v.opts != nil { 321 v.active.count-- 322 if v.active.count == 0 { 323 if err := mount.Unmount(v.path); err != nil { 324 v.active.count++ 325 return err 326 } 327 v.active.mounted = false 328 } 329 } 330 return nil 331 } 332 333 func validateOpts(opts map[string]string) error { 334 for opt := range opts { 335 if !validOpts[opt] { 336 return validationError{fmt.Errorf("invalid option key: %q", opt)} 337 } 338 } 339 return nil 340 } 341 342 func (v *localVolume) Status() map[string]interface{} { 343 return nil 344 }