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