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