2015-07-21 13:50:10 -04:00
|
|
|
// Package local provides the default implementation for volumes. It
|
|
|
|
// is used to mount data volume containers and directories local to
|
|
|
|
// the host server.
|
2015-05-19 16:05:25 -04:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-06-03 12:26:41 -04:00
|
|
|
"strings"
|
2015-05-19 16:05:25 -04:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/docker/docker/volume"
|
|
|
|
)
|
|
|
|
|
2015-06-03 12:26:41 -04:00
|
|
|
// VolumeDataPathName is the name of the directory where the volume data is stored.
|
2015-07-21 13:50:10 -04:00
|
|
|
// It uses a very distintive name to avoid collisions migrating data between
|
2015-06-03 12:26:41 -04:00
|
|
|
// Docker versions.
|
|
|
|
const (
|
|
|
|
VolumeDataPathName = "_data"
|
|
|
|
volumesPathName = "volumes"
|
|
|
|
)
|
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
var (
|
|
|
|
// ErrNotFound is the typed error returned when the requested volume name can't be found
|
|
|
|
ErrNotFound = errors.New("volume not found")
|
|
|
|
oldVfsDir = filepath.Join("vfs", "dir")
|
|
|
|
)
|
2015-06-03 12:26:41 -04:00
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// New instantiates a new Root instance with the provided scope. Scope
|
|
|
|
// is the base path that the Root instance uses to store its
|
|
|
|
// volumes. The base path is created here if it does not exist.
|
2015-06-03 12:26:41 -04:00
|
|
|
func New(scope string) (*Root, error) {
|
|
|
|
rootDirectory := filepath.Join(scope, volumesPathName)
|
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
if err := os.MkdirAll(rootDirectory, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-03 12:26:41 -04:00
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
r := &Root{
|
2015-06-03 12:26:41 -04:00
|
|
|
scope: scope,
|
2015-05-19 16:05:25 -04:00
|
|
|
path: rootDirectory,
|
2015-07-21 13:50:10 -04:00
|
|
|
volumes: make(map[string]*localVolume),
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2015-06-03 12:26:41 -04:00
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
dirs, err := ioutil.ReadDir(rootDirectory)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-03 12:26:41 -04:00
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
for _, d := range dirs {
|
|
|
|
name := filepath.Base(d.Name())
|
2015-07-21 13:50:10 -04:00
|
|
|
r.volumes[name] = &localVolume{
|
2015-05-19 16:05:25 -04:00
|
|
|
driverName: r.Name(),
|
|
|
|
name: name,
|
2015-06-03 12:26:41 -04:00
|
|
|
path: r.DataPath(name),
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Root implements the Driver interface for the volume package and
|
|
|
|
// manages the creation/removal of volumes. It uses only standard vfs
|
|
|
|
// commands to create/remove dirs within its provided scope.
|
2015-05-19 16:05:25 -04:00
|
|
|
type Root struct {
|
|
|
|
m sync.Mutex
|
2015-06-03 12:26:41 -04:00
|
|
|
scope string
|
2015-05-19 16:05:25 -04:00
|
|
|
path string
|
2015-07-21 13:50:10 -04:00
|
|
|
volumes map[string]*localVolume
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
// List lists all the volumes
|
|
|
|
func (r *Root) List() []volume.Volume {
|
|
|
|
var ls []volume.Volume
|
|
|
|
for _, v := range r.volumes {
|
|
|
|
ls = append(ls, v)
|
|
|
|
}
|
|
|
|
return ls
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// DataPath returns the constructed path of this volume.
|
2015-06-03 12:26:41 -04:00
|
|
|
func (r *Root) DataPath(volumeName string) string {
|
|
|
|
return filepath.Join(r.path, volumeName, VolumeDataPathName)
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Name returns the name of Root, defined in the volume package in the DefaultDriverName constant.
|
2015-05-19 16:05:25 -04:00
|
|
|
func (r *Root) Name() string {
|
2015-07-21 13:50:10 -04:00
|
|
|
return volume.DefaultDriverName
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Create creates a new volume.Volume with the provided name, creating
|
|
|
|
// the underlying directory tree required for this volume in the
|
|
|
|
// process.
|
2015-06-12 09:25:32 -04:00
|
|
|
func (r *Root) Create(name string, _ map[string]string) (volume.Volume, error) {
|
2015-05-19 16:05:25 -04:00
|
|
|
r.m.Lock()
|
|
|
|
defer r.m.Unlock()
|
2015-06-03 12:26:41 -04:00
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
v, exists := r.volumes[name]
|
2015-06-12 09:25:32 -04:00
|
|
|
if exists {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path := r.DataPath(name)
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
if os.IsExist(err) {
|
|
|
|
return nil, fmt.Errorf("volume already exists under %s", filepath.Dir(path))
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
return nil, err
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
v = &localVolume{
|
|
|
|
driverName: r.Name(),
|
|
|
|
name: name,
|
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
r.volumes[name] = v
|
2015-05-19 16:05:25 -04:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Remove removes the specified volume and all underlying data. If the
|
|
|
|
// given volume does not belong to this driver and an error is
|
|
|
|
// returned. The volume is reference counted, if all references are
|
|
|
|
// not released then the volume is not removed.
|
2015-05-19 16:05:25 -04:00
|
|
|
func (r *Root) Remove(v volume.Volume) error {
|
|
|
|
r.m.Lock()
|
|
|
|
defer r.m.Unlock()
|
2015-07-21 13:50:10 -04:00
|
|
|
lv, ok := v.(*localVolume)
|
2015-05-19 16:05:25 -04:00
|
|
|
if !ok {
|
|
|
|
return errors.New("unknown volume type")
|
|
|
|
}
|
2015-06-03 12:26:41 -04:00
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
realPath, err := filepath.EvalSymlinks(lv.path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !r.scopedPath(realPath) {
|
|
|
|
return fmt.Errorf("Unable to remove a directory of out the Docker root: %s", realPath)
|
|
|
|
}
|
2015-06-03 12:26:41 -04:00
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
if err := os.RemoveAll(realPath); err != nil {
|
|
|
|
return err
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
|
|
|
|
delete(r.volumes, lv.name)
|
|
|
|
return os.RemoveAll(filepath.Dir(lv.path))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get looks up the volume for the given name and returns it if found
|
|
|
|
func (r *Root) Get(name string) (volume.Volume, error) {
|
|
|
|
r.m.Lock()
|
|
|
|
v, exists := r.volumes[name]
|
|
|
|
r.m.Unlock()
|
|
|
|
if !exists {
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
|
|
|
return v, nil
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:26:41 -04:00
|
|
|
// scopedPath verifies that the path where the volume is located
|
|
|
|
// is under Docker's root and the valid local paths.
|
|
|
|
func (r *Root) scopedPath(realPath string) bool {
|
|
|
|
// Volumes path for Docker version >= 1.7
|
|
|
|
if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Volumes path for Docker version < 1.7
|
|
|
|
if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// localVolume implements the Volume interface from the volume package and
|
|
|
|
// represents the volumes created by Root.
|
|
|
|
type localVolume struct {
|
2015-05-19 16:05:25 -04:00
|
|
|
m sync.Mutex
|
|
|
|
usedCount int
|
|
|
|
// unique name of the volume
|
|
|
|
name string
|
|
|
|
// path is the path on the host where the data lives
|
|
|
|
path string
|
|
|
|
// driverName is the name of the driver that created the volume.
|
|
|
|
driverName string
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Name returns the name of the given Volume.
|
|
|
|
func (v *localVolume) Name() string {
|
2015-05-19 16:05:25 -04:00
|
|
|
return v.name
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// DriverName returns the driver that created the given Volume.
|
|
|
|
func (v *localVolume) DriverName() string {
|
2015-05-19 16:05:25 -04:00
|
|
|
return v.driverName
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Path returns the data location.
|
|
|
|
func (v *localVolume) Path() string {
|
2015-05-19 16:05:25 -04:00
|
|
|
return v.path
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Mount implements the localVolume interface, returning the data location.
|
|
|
|
func (v *localVolume) Mount() (string, error) {
|
2015-05-19 16:05:25 -04:00
|
|
|
return v.path, nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Umount is for satisfying the localVolume interface and does not do anything in this driver.
|
|
|
|
func (v *localVolume) Unmount() error {
|
2015-05-19 16:05:25 -04:00
|
|
|
return nil
|
|
|
|
}
|