1 // +build linux freebsd solaris
3 // Package local provides the default implementation for volumes. It
4 // is used to mount data volume containers and directories local to
17 "github.com/pkg/errors"
19 "github.com/docker/docker/pkg/mount"
23 oldVfsDir = filepath.Join("vfs", "dir")
25 validOpts = map[string]bool{
26 "type": true, // specify the filesystem type for mount, e.g. nfs
27 "o": true, // generic mount options
28 "device": true, // device to mount from
32 type optsConfig struct {
38 func (o *optsConfig) String() string {
39 return fmt.Sprintf("type='%s' device='%s' o='%s'", o.MountType, o.MountDevice, o.MountOpts)
42 // scopedPath verifies that the path where the volume is located
43 // is under Docker's root and the valid local paths.
44 func (r *Root) scopedPath(realPath string) bool {
45 // Volumes path for Docker version >= 1.7
46 if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) && realPath != filepath.Join(r.scope, volumesPathName) {
50 // Volumes path for Docker version < 1.7
51 if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) {
58 func setOpts(v *localVolume, opts map[string]string) error {
62 if err := validateOpts(opts); err != nil {
67 MountType: opts["type"],
69 MountDevice: opts["device"],
74 func (v *localVolume) mount() error {
75 if v.opts.MountDevice == "" {
76 return fmt.Errorf("missing device in volume options")
78 mountOpts := v.opts.MountOpts
79 if v.opts.MountType == "nfs" {
80 if addrValue := getAddress(v.opts.MountOpts); addrValue != "" && net.ParseIP(addrValue).To4() == nil {
81 ipAddr, err := net.ResolveIPAddr("ip", addrValue)
83 return errors.Wrapf(err, "error resolving passed in nfs address")
85 mountOpts = strings.Replace(mountOpts, "addr="+addrValue, "addr="+ipAddr.String(), 1)
88 err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, mountOpts)
89 return errors.Wrapf(err, "error while mounting volume with options: %s", v.opts)
92 func (v *localVolume) CreatedAt() (time.Time, error) {
93 fileInfo, err := os.Stat(v.path)
95 return time.Time{}, err
97 sec, nsec := fileInfo.Sys().(*syscall.Stat_t).Ctim.Unix()
98 return time.Unix(sec, nsec), nil