github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/volume/volume.go (about)

     1  package volume
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  	"strings"
     7  
     8  	"github.com/Sirupsen/logrus"
     9  	derr "github.com/docker/docker/errors"
    10  	"github.com/docker/docker/pkg/system"
    11  )
    12  
    13  // DefaultDriverName is the driver name used for the driver
    14  // implemented in the local package.
    15  const DefaultDriverName string = "local"
    16  
    17  // Driver is for creating and removing volumes.
    18  type Driver interface {
    19  	// Name returns the name of the volume driver.
    20  	Name() string
    21  	// Create makes a new volume with the given id.
    22  	Create(name string, opts map[string]string) (Volume, error)
    23  	// Remove deletes the volume.
    24  	Remove(vol Volume) (err error)
    25  	// List lists all the volumes the driver has
    26  	List() ([]Volume, error)
    27  	// Get retreives the volume with the requested name
    28  	Get(name string) (Volume, error)
    29  }
    30  
    31  // Volume is a place to store data. It is backed by a specific driver, and can be mounted.
    32  type Volume interface {
    33  	// Name returns the name of the volume
    34  	Name() string
    35  	// DriverName returns the name of the driver which owns this volume.
    36  	DriverName() string
    37  	// Path returns the absolute path to the volume.
    38  	Path() string
    39  	// Mount mounts the volume and returns the absolute path to
    40  	// where it can be consumed.
    41  	Mount() (string, error)
    42  	// Unmount unmounts the volume when it is no longer in use.
    43  	Unmount() error
    44  }
    45  
    46  // MountPoint is the intersection point between a volume and a container. It
    47  // specifies which volume is to be used and where inside a container it should
    48  // be mounted.
    49  type MountPoint struct {
    50  	Source      string // Container host directory
    51  	Destination string // Inside the container
    52  	RW          bool   // True if writable
    53  	Name        string // Name set by user
    54  	Driver      string // Volume driver to use
    55  	Volume      Volume `json:"-"`
    56  
    57  	// Note Mode is not used on Windows
    58  	Mode string `json:"Relabel"` // Originally field was `Relabel`"
    59  
    60  	// Note Propagation is not used on Windows
    61  	Propagation string // Mount propagation string
    62  	Named       bool   // specifies if the mountpoint was specified by name
    63  }
    64  
    65  // Setup sets up a mount point by either mounting the volume if it is
    66  // configured, or creating the source directory if supplied.
    67  func (m *MountPoint) Setup() (string, error) {
    68  	if m.Volume != nil {
    69  		return m.Volume.Mount()
    70  	}
    71  	if len(m.Source) > 0 {
    72  		if _, err := os.Stat(m.Source); err != nil {
    73  			if !os.IsNotExist(err) {
    74  				return "", err
    75  			}
    76  			if runtime.GOOS != "windows" { // Windows does not have deprecation issues here
    77  				logrus.Warnf("Auto-creating non-existent volume host path %s, this is deprecated and will be removed soon", m.Source)
    78  				if err := system.MkdirAll(m.Source, 0755); err != nil {
    79  					return "", err
    80  				}
    81  			}
    82  		}
    83  		return m.Source, nil
    84  	}
    85  	return "", derr.ErrorCodeMountSetup
    86  }
    87  
    88  // Path returns the path of a volume in a mount point.
    89  func (m *MountPoint) Path() string {
    90  	if m.Volume != nil {
    91  		return m.Volume.Path()
    92  	}
    93  	return m.Source
    94  }
    95  
    96  // ParseVolumesFrom ensure that the supplied volumes-from is valid.
    97  func ParseVolumesFrom(spec string) (string, string, error) {
    98  	if len(spec) == 0 {
    99  		return "", "", derr.ErrorCodeVolumeFromBlank.WithArgs(spec)
   100  	}
   101  
   102  	specParts := strings.SplitN(spec, ":", 2)
   103  	id := specParts[0]
   104  	mode := "rw"
   105  
   106  	if len(specParts) == 2 {
   107  		mode = specParts[1]
   108  		if !ValidMountMode(mode) {
   109  			return "", "", derr.ErrorCodeVolumeInvalidMode.WithArgs(mode)
   110  		}
   111  		// For now don't allow propagation properties while importing
   112  		// volumes from data container. These volumes will inherit
   113  		// the same propagation property as of the original volume
   114  		// in data container. This probably can be relaxed in future.
   115  		if HasPropagation(mode) {
   116  			return "", "", derr.ErrorCodeVolumeInvalidMode.WithArgs(mode)
   117  		}
   118  	}
   119  	return id, mode, nil
   120  }