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"
|
|
|
|
"sync"
|
|
|
|
|
2015-10-20 13:09:48 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-10-08 11:51:41 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2015-10-20 13:09:48 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2015-05-19 16:05:25 -04:00
|
|
|
"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-12-13 11:00:39 -05:00
|
|
|
// It uses a very distinctive name to avoid collisions migrating data between
|
2015-06-03 12:26:41 -04:00
|
|
|
// Docker versions.
|
|
|
|
const (
|
|
|
|
VolumeDataPathName = "_data"
|
|
|
|
volumesPathName = "volumes"
|
|
|
|
)
|
|
|
|
|
2015-10-20 13:09:48 -04:00
|
|
|
var (
|
|
|
|
// ErrNotFound is the typed error returned when the requested volume name can't be found
|
|
|
|
ErrNotFound = errors.New("volume not found")
|
2015-12-13 11:00:39 -05:00
|
|
|
// volumeNameRegex ensures the name assigned for the volume is valid.
|
2015-10-20 13:09:48 -04:00
|
|
|
// This name is used to create the bind directory, so we need to avoid characters that
|
|
|
|
// would make the path to escape the root directory.
|
|
|
|
volumeNameRegex = utils.RestrictedNamePattern
|
|
|
|
)
|
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-10-08 11:51:41 -04:00
|
|
|
func New(scope string, rootUID, rootGID int) (*Root, error) {
|
2015-06-03 12:26:41 -04:00
|
|
|
rootDirectory := filepath.Join(scope, volumesPathName)
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := idtools.MkdirAllAs(rootDirectory, 0700, rootUID, rootGID); err != nil {
|
2015-05-19 16:05:25 -04:00
|
|
|
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-10-08 11:51:41 -04:00
|
|
|
rootUID: rootUID,
|
|
|
|
rootGID: rootGID,
|
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-10-08 11:51:41 -04:00
|
|
|
rootUID int
|
|
|
|
rootGID int
|
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-10-20 13:09:48 -04:00
|
|
|
if err := r.validateName(name); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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)
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := idtools.MkdirAllAs(path, 0755, r.rootUID, r.rootGID); err != nil {
|
2015-06-12 09:25:32 -04:00
|
|
|
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-09-08 22:01:02 -04:00
|
|
|
|
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 {
|
2015-09-08 22:01:02 -04:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
realPath = filepath.Dir(lv.path)
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
2015-09-08 22:01:02 -04:00
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
if !r.scopedPath(realPath) {
|
2015-09-08 22:01:02 -04:00
|
|
|
return fmt.Errorf("Unable to remove a directory of out the Docker root %s: %s", r.scope, realPath)
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
2015-06-03 12:26:41 -04:00
|
|
|
|
2015-09-08 22:01:02 -04:00
|
|
|
if err := removePath(realPath); err != nil {
|
2015-06-12 09:25:32 -04:00
|
|
|
return err
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
|
|
|
|
delete(r.volumes, lv.name)
|
2015-09-08 22:01:02 -04:00
|
|
|
return removePath(filepath.Dir(lv.path))
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePath(path string) error {
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-10-20 13:09:48 -04:00
|
|
|
func (r *Root) validateName(name string) error {
|
|
|
|
if !volumeNameRegex.MatchString(name) {
|
|
|
|
return derr.ErrorCodeVolumeName.WithArgs(name, utils.RestrictedNameChars)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|