github.com/rawahars/moby@v24.0.4+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  	"context"
     8  	"encoding/json"
     9  	"os"
    10  	"path/filepath"
    11  	"reflect"
    12  	"strings"
    13  	"sync"
    14  
    15  	"github.com/containerd/containerd/log"
    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/quota"
    20  	"github.com/docker/docker/volume"
    21  	"github.com/pkg/errors"
    22  	"github.com/sirupsen/logrus"
    23  )
    24  
    25  const (
    26  	// volumeDataPathName is the name of the directory where the volume data is stored.
    27  	// It uses a very distinctive name to avoid collisions migrating data between
    28  	// Docker versions.
    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 = errors.New("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  	_ volume.LiveRestorer = (*localVolume)(nil)
    42  )
    43  
    44  type activeMount struct {
    45  	count   uint64
    46  	mounted bool
    47  }
    48  
    49  // New instantiates a new Root instance with the provided scope. Scope
    50  // is the base path that the Root instance uses to store its
    51  // volumes. The base path is created here if it does not exist.
    52  func New(scope string, rootIdentity idtools.Identity) (*Root, error) {
    53  	r := &Root{
    54  		path:         filepath.Join(scope, volumesPathName),
    55  		volumes:      make(map[string]*localVolume),
    56  		rootIdentity: rootIdentity,
    57  	}
    58  
    59  	if err := idtools.MkdirAllAndChown(r.path, 0701, idtools.CurrentIdentity()); err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	dirs, err := os.ReadDir(r.path)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	if r.quotaCtl, err = quota.NewControl(r.path); err != nil {
    69  		logrus.Debugf("No quota support for local volumes in %s: %v", r.path, err)
    70  	}
    71  
    72  	for _, d := range dirs {
    73  		if !d.IsDir() {
    74  			continue
    75  		}
    76  
    77  		name := d.Name()
    78  		v := &localVolume{
    79  			driverName: r.Name(),
    80  			name:       name,
    81  			rootPath:   filepath.Join(r.path, name),
    82  			path:       filepath.Join(r.path, name, volumeDataPathName),
    83  			quotaCtl:   r.quotaCtl,
    84  		}
    85  
    86  		// unmount anything that may still be mounted (for example, from an
    87  		// unclean shutdown). This is a no-op on windows
    88  		unmount(v.path)
    89  
    90  		if err := v.loadOpts(); err != nil {
    91  			return nil, err
    92  		}
    93  		r.volumes[name] = v
    94  	}
    95  
    96  	return r, nil
    97  }
    98  
    99  // Root implements the Driver interface for the volume package and
   100  // manages the creation/removal of volumes. It uses only standard vfs
   101  // commands to create/remove dirs within its provided scope.
   102  type Root struct {
   103  	m            sync.Mutex
   104  	path         string
   105  	quotaCtl     *quota.Control
   106  	volumes      map[string]*localVolume
   107  	rootIdentity idtools.Identity
   108  }
   109  
   110  // List lists all the volumes
   111  func (r *Root) List() ([]volume.Volume, error) {
   112  	var ls []volume.Volume
   113  	r.m.Lock()
   114  	for _, v := range r.volumes {
   115  		ls = append(ls, v)
   116  	}
   117  	r.m.Unlock()
   118  	return ls, nil
   119  }
   120  
   121  // Name returns the name of Root, defined in the volume package in the DefaultDriverName constant.
   122  func (r *Root) Name() string {
   123  	return volume.DefaultDriverName
   124  }
   125  
   126  // Create creates a new volume.Volume with the provided name, creating
   127  // the underlying directory tree required for this volume in the
   128  // process.
   129  func (r *Root) Create(name string, opts map[string]string) (volume.Volume, error) {
   130  	if err := r.validateName(name); err != nil {
   131  		return nil, err
   132  	}
   133  	if err := r.validateOpts(opts); err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	r.m.Lock()
   138  	defer r.m.Unlock()
   139  
   140  	v, exists := r.volumes[name]
   141  	if exists {
   142  		return v, nil
   143  	}
   144  
   145  	v = &localVolume{
   146  		driverName: r.Name(),
   147  		name:       name,
   148  		rootPath:   filepath.Join(r.path, name),
   149  		path:       filepath.Join(r.path, name, volumeDataPathName),
   150  		quotaCtl:   r.quotaCtl,
   151  	}
   152  
   153  	// Root dir does not need to be accessed by the remapped root
   154  	if err := idtools.MkdirAllAndChown(v.rootPath, 0701, idtools.CurrentIdentity()); err != nil {
   155  		return nil, errors.Wrapf(errdefs.System(err), "error while creating volume root path '%s'", v.rootPath)
   156  	}
   157  
   158  	// Remapped root does need access to the data path
   159  	if err := idtools.MkdirAllAndChown(v.path, 0755, r.rootIdentity); err != nil {
   160  		return nil, errors.Wrapf(errdefs.System(err), "error while creating volume data path '%s'", v.path)
   161  	}
   162  
   163  	var err error
   164  	defer func() {
   165  		if err != nil {
   166  			os.RemoveAll(v.rootPath)
   167  		}
   168  	}()
   169  
   170  	if err = v.setOpts(opts); err != nil {
   171  		return nil, err
   172  	}
   173  
   174  	r.volumes[name] = v
   175  	return v, nil
   176  }
   177  
   178  // Remove removes the specified volume and all underlying data. If the
   179  // given volume does not belong to this driver and an error is
   180  // returned. The volume is reference counted, if all references are
   181  // not released then the volume is not removed.
   182  func (r *Root) Remove(v volume.Volume) error {
   183  	r.m.Lock()
   184  	defer r.m.Unlock()
   185  
   186  	lv, ok := v.(*localVolume)
   187  	if !ok {
   188  		return errdefs.System(errors.Errorf("unknown volume type %T", v))
   189  	}
   190  
   191  	if lv.active.count > 0 {
   192  		return errdefs.System(errors.New("volume has active mounts"))
   193  	}
   194  
   195  	if err := lv.unmount(); err != nil {
   196  		return err
   197  	}
   198  
   199  	// TODO(thaJeztah) is there a reason we're evaluating the data-path here, and not the volume's rootPath?
   200  	realPath, err := filepath.EvalSymlinks(lv.path)
   201  	if err != nil {
   202  		if !os.IsNotExist(err) {
   203  			return err
   204  		}
   205  		// if the volume's data-directory wasn't found, fall back to using the
   206  		// volume's root path (see 8d27417bfeff316346d00c07a456b0e1b056e788)
   207  		realPath = lv.rootPath
   208  	}
   209  
   210  	if realPath == r.path || !strings.HasPrefix(realPath, r.path) {
   211  		return errdefs.System(errors.Errorf("unable to remove a directory outside of the local volume root %s: %s", r.path, realPath))
   212  	}
   213  
   214  	if err := removePath(realPath); err != nil {
   215  		return err
   216  	}
   217  
   218  	delete(r.volumes, lv.name)
   219  	return removePath(lv.rootPath)
   220  }
   221  
   222  func removePath(path string) error {
   223  	if err := os.RemoveAll(path); err != nil {
   224  		if os.IsNotExist(err) {
   225  			return nil
   226  		}
   227  		return errdefs.System(errors.Wrapf(err, "error removing volume path '%s'", path))
   228  	}
   229  	return nil
   230  }
   231  
   232  // Get looks up the volume for the given name and returns it if found
   233  func (r *Root) Get(name string) (volume.Volume, error) {
   234  	r.m.Lock()
   235  	v, exists := r.volumes[name]
   236  	r.m.Unlock()
   237  	if !exists {
   238  		return nil, ErrNotFound
   239  	}
   240  	return v, nil
   241  }
   242  
   243  // Scope returns the local volume scope
   244  func (r *Root) Scope() string {
   245  	return volume.LocalScope
   246  }
   247  
   248  func (r *Root) validateName(name string) error {
   249  	if len(name) == 1 {
   250  		return errdefs.InvalidParameter(errors.New("volume name is too short, names should be at least two alphanumeric characters"))
   251  	}
   252  	if !volumeNameRegex.MatchString(name) {
   253  		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))
   254  	}
   255  	return nil
   256  }
   257  
   258  // localVolume implements the Volume interface from the volume package and
   259  // represents the volumes created by Root.
   260  type localVolume struct {
   261  	m sync.Mutex
   262  	// unique name of the volume
   263  	name string
   264  	// rootPath is the volume's root path, where the volume's metadata is stored.
   265  	rootPath string
   266  	// path is the path on the host where the data lives
   267  	path string
   268  	// driverName is the name of the driver that created the volume.
   269  	driverName string
   270  	// opts is the parsed list of options used to create the volume
   271  	opts *optsConfig
   272  	// active refcounts the active mounts
   273  	active activeMount
   274  	// reference to Root instances quotaCtl
   275  	quotaCtl *quota.Control
   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  	logger := log.G(context.TODO()).WithField("volume", v.name)
   304  	if v.needsMount() {
   305  		if !v.active.mounted {
   306  			logger.Debug("Mounting volume")
   307  			if err := v.mount(); err != nil {
   308  				return "", errdefs.System(err)
   309  			}
   310  			v.active.mounted = true
   311  		}
   312  		v.active.count++
   313  		logger.WithField("active mounts", v.active).Debug("Decremented active mount count")
   314  	}
   315  	if err := v.postMount(); err != nil {
   316  		return "", err
   317  	}
   318  	return v.path, nil
   319  }
   320  
   321  // Unmount dereferences the id, and if it is the last reference will unmount any resources
   322  // that were previously mounted.
   323  func (v *localVolume) Unmount(id string) error {
   324  	v.m.Lock()
   325  	defer v.m.Unlock()
   326  	logger := log.G(context.TODO()).WithField("volume", v.name)
   327  
   328  	// Always decrement the count, even if the unmount fails
   329  	// Essentially docker doesn't care if this fails, it will send an error, but
   330  	// ultimately there's nothing that can be done. If we don't decrement the count
   331  	// this volume can never be removed until a daemon restart occurs.
   332  	if v.needsMount() {
   333  		v.active.count--
   334  		logger.WithField("active mounts", v.active).Debug("Decremented active mount count")
   335  	}
   336  
   337  	if v.active.count > 0 {
   338  		return nil
   339  	}
   340  
   341  	logger.Debug("Unmounting volume")
   342  	return v.unmount()
   343  }
   344  
   345  func (v *localVolume) Status() map[string]interface{} {
   346  	return nil
   347  }
   348  
   349  func (v *localVolume) loadOpts() error {
   350  	b, err := os.ReadFile(filepath.Join(v.rootPath, "opts.json"))
   351  	if err != nil {
   352  		if !errors.Is(err, os.ErrNotExist) {
   353  			logrus.WithError(err).Warnf("error while loading volume options for volume: %s", v.name)
   354  		}
   355  		return nil
   356  	}
   357  	opts := optsConfig{}
   358  	if err := json.Unmarshal(b, &opts); err != nil {
   359  		return errors.Wrapf(err, "error while unmarshaling volume options for volume: %s", v.name)
   360  	}
   361  	// Make sure this isn't an empty optsConfig.
   362  	// This could be empty due to buggy behavior in older versions of Docker.
   363  	if !reflect.DeepEqual(opts, optsConfig{}) {
   364  		v.opts = &opts
   365  	}
   366  	return nil
   367  }
   368  
   369  func (v *localVolume) saveOpts() error {
   370  	var b []byte
   371  	b, err := json.Marshal(v.opts)
   372  	if err != nil {
   373  		return err
   374  	}
   375  	err = os.WriteFile(filepath.Join(v.rootPath, "opts.json"), b, 0600)
   376  	if err != nil {
   377  		return errdefs.System(errors.Wrap(err, "error while persisting volume options"))
   378  	}
   379  	return nil
   380  }
   381  
   382  // LiveRestoreVolume restores reference counts for mounts
   383  // It is assumed that the volume is already mounted since this is only called for active, live-restored containers.
   384  func (v *localVolume) LiveRestoreVolume(ctx context.Context, _ string) error {
   385  	v.m.Lock()
   386  	defer v.m.Unlock()
   387  
   388  	if !v.needsMount() {
   389  		return nil
   390  	}
   391  	v.active.count++
   392  	v.active.mounted = true
   393  	log.G(ctx).WithFields(logrus.Fields{
   394  		"volume":        v.name,
   395  		"active mounts": v.active,
   396  	}).Debugf("Live restored volume")
   397  	return nil
   398  }
   399  
   400  // getAddress finds out address/hostname from options
   401  func getAddress(opts string) string {
   402  	for _, opt := range strings.Split(opts, ",") {
   403  		if strings.HasPrefix(opt, "addr=") {
   404  			return strings.TrimPrefix(opt, "addr=")
   405  		}
   406  	}
   407  	return ""
   408  }
   409  
   410  // getPassword finds out a password from options
   411  func getPassword(opts string) string {
   412  	for _, opt := range strings.Split(opts, ",") {
   413  		if strings.HasPrefix(opt, "password=") {
   414  			return strings.TrimPrefix(opt, "password=")
   415  		}
   416  	}
   417  	return ""
   418  }