github.com/dpiddy/docker@v1.12.2-rc1/volume/local/local_unix.go (about)

     1  // +build linux freebsd solaris
     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
     7  
     8  import (
     9  	"fmt"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"github.com/docker/docker/pkg/mount"
    14  )
    15  
    16  var (
    17  	oldVfsDir = filepath.Join("vfs", "dir")
    18  
    19  	validOpts = map[string]bool{
    20  		"type":   true, // specify the filesystem type for mount, e.g. nfs
    21  		"o":      true, // generic mount options
    22  		"device": true, // device to mount from
    23  	}
    24  )
    25  
    26  type optsConfig struct {
    27  	MountType   string
    28  	MountOpts   string
    29  	MountDevice string
    30  }
    31  
    32  // scopedPath verifies that the path where the volume is located
    33  // is under Docker's root and the valid local paths.
    34  func (r *Root) scopedPath(realPath string) bool {
    35  	// Volumes path for Docker version >= 1.7
    36  	if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) && realPath != filepath.Join(r.scope, volumesPathName) {
    37  		return true
    38  	}
    39  
    40  	// Volumes path for Docker version < 1.7
    41  	if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) {
    42  		return true
    43  	}
    44  
    45  	return false
    46  }
    47  
    48  func setOpts(v *localVolume, opts map[string]string) error {
    49  	if len(opts) == 0 {
    50  		return nil
    51  	}
    52  	if err := validateOpts(opts); err != nil {
    53  		return err
    54  	}
    55  
    56  	v.opts = &optsConfig{
    57  		MountType:   opts["type"],
    58  		MountOpts:   opts["o"],
    59  		MountDevice: opts["device"],
    60  	}
    61  	return nil
    62  }
    63  
    64  func (v *localVolume) mount() error {
    65  	if v.opts.MountDevice == "" {
    66  		return fmt.Errorf("missing device in volume options")
    67  	}
    68  	return mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, v.opts.MountOpts)
    69  }