github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/volume/local/local_unix.go (about)

     1  // +build linux freebsd
     2  
     3  // Package local provides the default implementation for volumes. It
     4  // is used to mount data volume containers and directories local to
     5  // the host server.
     6  package local // import "github.com/docker/docker/volume/local"
     7  
     8  import (
     9  	"fmt"
    10  	"net"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"syscall"
    15  	"time"
    16  
    17  	"github.com/docker/docker/errdefs"
    18  	"github.com/moby/sys/mount"
    19  	"github.com/pkg/errors"
    20  )
    21  
    22  var (
    23  	oldVfsDir = filepath.Join("vfs", "dir")
    24  
    25  	validOpts = map[string]struct{}{
    26  		"type":   {}, // specify the filesystem type for mount, e.g. nfs
    27  		"o":      {}, // generic mount options
    28  		"device": {}, // device to mount from
    29  	}
    30  	mandatoryOpts = map[string]struct{}{
    31  		"device": {},
    32  		"type":   {},
    33  	}
    34  )
    35  
    36  type optsConfig struct {
    37  	MountType   string
    38  	MountOpts   string
    39  	MountDevice string
    40  }
    41  
    42  func (o *optsConfig) String() string {
    43  	return fmt.Sprintf("type='%s' device='%s' o='%s'", o.MountType, o.MountDevice, o.MountOpts)
    44  }
    45  
    46  // scopedPath verifies that the path where the volume is located
    47  // is under Docker's root and the valid local paths.
    48  func (r *Root) scopedPath(realPath string) bool {
    49  	// Volumes path for Docker version >= 1.7
    50  	if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) && realPath != filepath.Join(r.scope, volumesPathName) {
    51  		return true
    52  	}
    53  
    54  	// Volumes path for Docker version < 1.7
    55  	if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) {
    56  		return true
    57  	}
    58  
    59  	return false
    60  }
    61  
    62  func setOpts(v *localVolume, opts map[string]string) error {
    63  	if len(opts) == 0 {
    64  		return nil
    65  	}
    66  	if err := validateOpts(opts); err != nil {
    67  		return err
    68  	}
    69  
    70  	v.opts = &optsConfig{
    71  		MountType:   opts["type"],
    72  		MountOpts:   opts["o"],
    73  		MountDevice: opts["device"],
    74  	}
    75  	return nil
    76  }
    77  
    78  func validateOpts(opts map[string]string) error {
    79  	if len(opts) == 0 {
    80  		return nil
    81  	}
    82  	for opt := range opts {
    83  		if _, ok := validOpts[opt]; !ok {
    84  			return errdefs.InvalidParameter(errors.Errorf("invalid option: %q", opt))
    85  		}
    86  	}
    87  	for opt := range mandatoryOpts {
    88  		if _, ok := opts[opt]; !ok {
    89  			return errdefs.InvalidParameter(errors.Errorf("missing required option: %q", opt))
    90  		}
    91  	}
    92  	return nil
    93  }
    94  
    95  func (v *localVolume) mount() error {
    96  	if v.opts.MountDevice == "" {
    97  		return fmt.Errorf("missing device in volume options")
    98  	}
    99  	mountOpts := v.opts.MountOpts
   100  	switch v.opts.MountType {
   101  	case "nfs", "cifs":
   102  		if addrValue := getAddress(v.opts.MountOpts); addrValue != "" && net.ParseIP(addrValue).To4() == nil {
   103  			ipAddr, err := net.ResolveIPAddr("ip", addrValue)
   104  			if err != nil {
   105  				return errors.Wrapf(err, "error resolving passed in network volume address")
   106  			}
   107  			mountOpts = strings.Replace(mountOpts, "addr="+addrValue, "addr="+ipAddr.String(), 1)
   108  		}
   109  	}
   110  	err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, mountOpts)
   111  	return errors.Wrap(err, "failed to mount local volume")
   112  }
   113  
   114  func (v *localVolume) CreatedAt() (time.Time, error) {
   115  	fileInfo, err := os.Stat(v.path)
   116  	if err != nil {
   117  		return time.Time{}, err
   118  	}
   119  	sec, nsec := fileInfo.Sys().(*syscall.Stat_t).Ctim.Unix()
   120  	return time.Unix(sec, nsec), nil
   121  }