github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/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  	"strings"
    14  	"sync"
    15  
    16  	"github.com/pkg/errors"
    17  
    18  	"github.com/Sirupsen/logrus"
    19  	"github.com/docker/docker/api"
    20  	"github.com/docker/docker/pkg/idtools"
    21  	"github.com/docker/docker/pkg/mount"
    22  	"github.com/docker/docker/volume"
    23  )
    24  
    25  // VolumeDataPathName is the name of the directory where the volume data is stored.
    26  // It uses a very distinctive name to avoid collisions migrating data between
    27  // Docker versions.
    28  const (
    29  	VolumeDataPathName = "_data"
    30  	volumesPathName    = "volumes"
    31  )
    32  
    33  var (
    34  	// ErrNotFound is the typed error returned when the requested volume name can't be found
    35  	ErrNotFound = fmt.Errorf("volume not found")
    36  	// volumeNameRegex ensures the name assigned for the volume is valid.
    37  	// This name is used to create the bind directory, so we need to avoid characters that
    38  	// would make the path to escape the root directory.
    39  	volumeNameRegex = api.RestrictedNamePattern
    40  )
    41  
    42  type validationError struct {
    43  	error
    44  }
    45  
    46  func (validationError) IsValidationError() bool {
    47  	return true
    48  }
    49  
    50  type activeMount struct {
    51  	count   uint64
    52  	mounted bool
    53  }
    54  
    55  // New instantiates a new Root instance with the provided scope. Scope
    56  // is the base path that the Root instance uses to store its
    57  // volumes. The base path is created here if it does not exist.
    58  func New(scope string, rootIDs idtools.IDPair) (*Root, error) {
    59  	rootDirectory := filepath.Join(scope, volumesPathName)
    60  
    61  	if err := idtools.MkdirAllAndChown(rootDirectory, 0700, rootIDs); err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	r := &Root{
    66  		scope:   scope,
    67  		path:    rootDirectory,
    68  		volumes: make(map[string]*localVolume),
    69  		rootIDs: rootIDs,
    70  	}
    71  
    72  	dirs, err := ioutil.ReadDir(rootDirectory)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	mountInfos, err := mount.GetMounts()
    78  	if err != nil {
    79  		logrus.Debugf("error looking up mounts for local volume cleanup: %v", err)
    80  	}
    81  
    82  	for _, d := range dirs {
    83  		if !d.IsDir() {
    84  			continue
    85  		}
    86  
    87  		name := filepath.Base(d.Name())
    88  		v := &localVolume{
    89  			driverName: r.Name(),
    90  			name:       name,
    91  			path:       r.DataPath(name),
    92  		}
    93  		r.volumes[name] = v
    94  		optsFilePath := filepath.Join(rootDirectory, name, "opts.json")
    95  		if b, err := ioutil.ReadFile(optsFilePath); err == nil {
    96  			opts := optsConfig{}
    97  			if err := json.Unmarshal(b, &opts); err != nil {
    98  				return nil, errors.Wrapf(err, "error while unmarshaling volume options for volume: %s", name)
    99  			}
   100  			// Make sure this isn't an empty optsConfig.
   101  			// This could be empty due to buggy behavior in older versions of Docker.
   102  			if !reflect.DeepEqual(opts, optsConfig{}) {
   103  				v.opts = &opts
   104  			}
   105  
   106  			// unmount anything that may still be mounted (for example, from an unclean shutdown)
   107  			for _, info := range mountInfos {
   108  				if info.Mountpoint == v.path {
   109  					mount.Unmount(v.path)
   110  					break
   111  				}
   112  			}
   113  		}
   114  	}
   115  
   116  	return r, nil
   117  }
   118  
   119  // Root implements the Driver interface for the volume package and
   120  // manages the creation/removal of volumes. It uses only standard vfs
   121  // commands to create/remove dirs within its provided scope.
   122  type Root struct {
   123  	m       sync.Mutex
   124  	scope   string
   125  	path    string
   126  	volumes map[string]*localVolume
   127  	rootIDs idtools.IDPair
   128  }
   129  
   130  // List lists all the volumes
   131  func (r *Root) List() ([]volume.Volume, error) {
   132  	var ls []volume.Volume
   133  	r.m.Lock()
   134  	for _, v := range r.volumes {
   135  		ls = append(ls, v)
   136  	}
   137  	r.m.Unlock()
   138  	return ls, nil
   139  }
   140  
   141  // DataPath returns the constructed path of this volume.
   142  func (r *Root) DataPath(volumeName string) string {
   143  	return filepath.Join(r.path, volumeName, VolumeDataPathName)
   144  }
   145  
   146  // Name returns the name of Root, defined in the volume package in the DefaultDriverName constant.
   147  func (r *Root) Name() string {
   148  	return volume.DefaultDriverName
   149  }
   150  
   151  // Create creates a new volume.Volume with the provided name, creating
   152  // the underlying directory tree required for this volume in the
   153  // process.
   154  func (r *Root) Create(name string, opts map[string]string) (volume.Volume, error) {
   155  	if err := r.validateName(name); err != nil {
   156  		return nil, err
   157  	}
   158  
   159  	r.m.Lock()
   160  	defer r.m.Unlock()
   161  
   162  	v, exists := r.volumes[name]
   163  	if exists {
   164  		return v, nil
   165  	}
   166  
   167  	path := r.DataPath(name)
   168  	if err := idtools.MkdirAllAndChown(path, 0755, r.rootIDs); err != nil {
   169  		if os.IsExist(err) {
   170  			return nil, fmt.Errorf("volume already exists under %s", filepath.Dir(path))
   171  		}
   172  		return nil, errors.Wrapf(err, "error while creating volume path '%s'", path)
   173  	}
   174  
   175  	var err error
   176  	defer func() {
   177  		if err != nil {
   178  			os.RemoveAll(filepath.Dir(path))
   179  		}
   180  	}()
   181  
   182  	v = &localVolume{
   183  		driverName: r.Name(),
   184  		name:       name,
   185  		path:       path,
   186  	}
   187  
   188  	if len(opts) != 0 {
   189  		if err = setOpts(v, opts); err != nil {
   190  			return nil, err
   191  		}
   192  		var b []byte
   193  		b, err = json.Marshal(v.opts)
   194  		if err != nil {
   195  			return nil, err
   196  		}
   197  		if err = ioutil.WriteFile(filepath.Join(filepath.Dir(path), "opts.json"), b, 600); err != nil {
   198  			return nil, errors.Wrap(err, "error while persisting volume options")
   199  		}
   200  	}
   201  
   202  	r.volumes[name] = v
   203  	return v, nil
   204  }
   205  
   206  // Remove removes the specified volume and all underlying data. If the
   207  // given volume does not belong to this driver and an error is
   208  // returned. The volume is reference counted, if all references are
   209  // not released then the volume is not removed.
   210  func (r *Root) Remove(v volume.Volume) error {
   211  	r.m.Lock()
   212  	defer r.m.Unlock()
   213  
   214  	lv, ok := v.(*localVolume)
   215  	if !ok {
   216  		return fmt.Errorf("unknown volume type %T", v)
   217  	}
   218  
   219  	if lv.active.count > 0 {
   220  		return fmt.Errorf("volume has active mounts")
   221  	}
   222  
   223  	if err := lv.unmount(); err != nil {
   224  		return err
   225  	}
   226  
   227  	realPath, err := filepath.EvalSymlinks(lv.path)
   228  	if err != nil {
   229  		if !os.IsNotExist(err) {
   230  			return err
   231  		}
   232  		realPath = filepath.Dir(lv.path)
   233  	}
   234  
   235  	if !r.scopedPath(realPath) {
   236  		return fmt.Errorf("Unable to remove a directory of out the Docker root %s: %s", r.scope, realPath)
   237  	}
   238  
   239  	if err := removePath(realPath); err != nil {
   240  		return err
   241  	}
   242  
   243  	delete(r.volumes, lv.name)
   244  	return removePath(filepath.Dir(lv.path))
   245  }
   246  
   247  func removePath(path string) error {
   248  	if err := os.RemoveAll(path); err != nil {
   249  		if os.IsNotExist(err) {
   250  			return nil
   251  		}
   252  		return errors.Wrapf(err, "error removing volume path '%s'", path)
   253  	}
   254  	return nil
   255  }
   256  
   257  // Get looks up the volume for the given name and returns it if found
   258  func (r *Root) Get(name string) (volume.Volume, error) {
   259  	r.m.Lock()
   260  	v, exists := r.volumes[name]
   261  	r.m.Unlock()
   262  	if !exists {
   263  		return nil, ErrNotFound
   264  	}
   265  	return v, nil
   266  }
   267  
   268  // Scope returns the local volume scope
   269  func (r *Root) Scope() string {
   270  	return volume.LocalScope
   271  }
   272  
   273  func (r *Root) validateName(name string) error {
   274  	if len(name) == 1 {
   275  		return validationError{fmt.Errorf("volume name is too short, names should be at least two alphanumeric characters")}
   276  	}
   277  	if !volumeNameRegex.MatchString(name) {
   278  		return validationError{fmt.Errorf("%q includes invalid characters for a local volume name, only %q are allowed. If you intended to pass a host directory, use absolute path", name, api.RestrictedNameChars)}
   279  	}
   280  	return nil
   281  }
   282  
   283  // localVolume implements the Volume interface from the volume package and
   284  // represents the volumes created by Root.
   285  type localVolume struct {
   286  	m sync.Mutex
   287  	// unique name of the volume
   288  	name string
   289  	// path is the path on the host where the data lives
   290  	path string
   291  	// driverName is the name of the driver that created the volume.
   292  	driverName string
   293  	// opts is the parsed list of options used to create the volume
   294  	opts *optsConfig
   295  	// active refcounts the active mounts
   296  	active activeMount
   297  }
   298  
   299  // Name returns the name of the given Volume.
   300  func (v *localVolume) Name() string {
   301  	return v.name
   302  }
   303  
   304  // DriverName returns the driver that created the given Volume.
   305  func (v *localVolume) DriverName() string {
   306  	return v.driverName
   307  }
   308  
   309  // Path returns the data location.
   310  func (v *localVolume) Path() string {
   311  	return v.path
   312  }
   313  
   314  // Mount implements the localVolume interface, returning the data location.
   315  // If there are any provided mount options, the resources will be mounted at this point
   316  func (v *localVolume) Mount(id string) (string, error) {
   317  	v.m.Lock()
   318  	defer v.m.Unlock()
   319  	if v.opts != nil {
   320  		if !v.active.mounted {
   321  			if err := v.mount(); err != nil {
   322  				return "", err
   323  			}
   324  			v.active.mounted = true
   325  		}
   326  		v.active.count++
   327  	}
   328  	return v.path, nil
   329  }
   330  
   331  // Unmount dereferences the id, and if it is the last reference will unmount any resources
   332  // that were previously mounted.
   333  func (v *localVolume) Unmount(id string) error {
   334  	v.m.Lock()
   335  	defer v.m.Unlock()
   336  
   337  	// Always decrement the count, even if the unmount fails
   338  	// Essentially docker doesn't care if this fails, it will send an error, but
   339  	// ultimately there's nothing that can be done. If we don't decrement the count
   340  	// this volume can never be removed until a daemon restart occurs.
   341  	if v.opts != nil {
   342  		v.active.count--
   343  	}
   344  
   345  	if v.active.count > 0 {
   346  		return nil
   347  	}
   348  
   349  	return v.unmount()
   350  }
   351  
   352  func (v *localVolume) unmount() error {
   353  	if v.opts != nil {
   354  		if err := mount.Unmount(v.path); err != nil {
   355  			if mounted, mErr := mount.Mounted(v.path); mounted || mErr != nil {
   356  				return errors.Wrapf(err, "error while unmounting volume path '%s'", v.path)
   357  			}
   358  		}
   359  		v.active.mounted = false
   360  	}
   361  	return nil
   362  }
   363  
   364  func validateOpts(opts map[string]string) error {
   365  	for opt := range opts {
   366  		if !validOpts[opt] {
   367  			return validationError{fmt.Errorf("invalid option key: %q", opt)}
   368  		}
   369  	}
   370  	return nil
   371  }
   372  
   373  func (v *localVolume) Status() map[string]interface{} {
   374  	return nil
   375  }
   376  
   377  // getAddress finds out address/hostname from options
   378  func getAddress(opts string) string {
   379  	optsList := strings.Split(opts, ",")
   380  	for i := 0; i < len(optsList); i++ {
   381  		if strings.HasPrefix(optsList[i], "addr=") {
   382  			addr := (strings.SplitN(optsList[i], "=", 2)[1])
   383  			return addr
   384  		}
   385  	}
   386  	return ""
   387  }