2014-04-17 17:43:01 -04:00
|
|
|
package daemon
|
2014-02-14 20:15:40 -05:00
|
|
|
|
|
|
|
import (
|
2015-05-12 21:21:26 -04:00
|
|
|
"errors"
|
2014-02-14 20:15:40 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2014-05-05 17:45:14 -04:00
|
|
|
|
2015-09-14 13:21:17 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-06-12 09:25:32 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-09-17 14:54:14 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2014-10-29 15:06:51 -04:00
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2015-06-11 14:29:29 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-05-19 16:05:25 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2014-02-14 20:15:40 -05:00
|
|
|
)
|
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
var (
|
|
|
|
// ErrVolumeReadonly is used to signal an error when trying to copy data into
|
|
|
|
// a volume mount that is not writable.
|
|
|
|
ErrVolumeReadonly = errors.New("mounted volume is marked read-only")
|
|
|
|
)
|
2015-05-12 21:21:26 -04:00
|
|
|
|
2015-07-30 19:10:56 -04:00
|
|
|
// mountPoint is the intersection point between a volume and a container. It
|
|
|
|
// specifies which volume is to be used and where inside a container it should
|
|
|
|
// be mounted.
|
2015-05-19 16:05:25 -04:00
|
|
|
type mountPoint struct {
|
|
|
|
Name string
|
|
|
|
Destination string
|
|
|
|
Driver string
|
|
|
|
RW bool
|
|
|
|
Volume volume.Volume `json:"-"`
|
|
|
|
Source string
|
2015-07-22 09:24:35 -04:00
|
|
|
Mode string `json:"Relabel"` // Originally field was `Relabel`"
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2014-12-16 10:06:45 -05:00
|
|
|
|
2015-07-30 19:10:56 -04:00
|
|
|
// Setup sets up a mount point by either mounting the volume if it is
|
|
|
|
// configured, or creating the source directory if supplied.
|
2015-05-19 16:05:25 -04:00
|
|
|
func (m *mountPoint) Setup() (string, error) {
|
|
|
|
if m.Volume != nil {
|
|
|
|
return m.Volume.Mount()
|
2014-08-21 17:57:46 -04:00
|
|
|
}
|
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
if len(m.Source) > 0 {
|
|
|
|
if _, err := os.Stat(m.Source); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-09-16 14:21:34 -04:00
|
|
|
logrus.Warnf("Auto-creating non-existant volume host path %s, this is deprecated and will be removed soon", m.Source)
|
2015-06-11 14:29:29 -04:00
|
|
|
if err := system.MkdirAll(m.Source, 0755); err != nil {
|
2015-05-19 16:05:25 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2015-02-10 13:45:50 -05:00
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
return m.Source, nil
|
2014-08-28 10:18:08 -04:00
|
|
|
}
|
|
|
|
|
2015-09-16 14:56:26 -04:00
|
|
|
return "", derr.ErrorCodeMountSetup
|
2014-12-16 10:06:45 -05:00
|
|
|
}
|
|
|
|
|
2015-05-12 21:21:26 -04:00
|
|
|
// hasResource checks whether the given absolute path for a container is in
|
|
|
|
// this mount point. If the relative path starts with `../` then the resource
|
|
|
|
// is outside of this mount point, but we can't simply check for this prefix
|
|
|
|
// because it misses `..` which is also outside of the mount, so check both.
|
|
|
|
func (m *mountPoint) hasResource(absolutePath string) bool {
|
|
|
|
relPath, err := filepath.Rel(m.Destination, absolutePath)
|
|
|
|
|
|
|
|
return err == nil && relPath != ".." && !strings.HasPrefix(relPath, fmt.Sprintf("..%c", filepath.Separator))
|
|
|
|
}
|
|
|
|
|
2015-07-30 19:10:56 -04:00
|
|
|
// Path returns the path of a volume in a mount point.
|
2015-05-19 16:05:25 -04:00
|
|
|
func (m *mountPoint) Path() string {
|
|
|
|
if m.Volume != nil {
|
|
|
|
return m.Volume.Path()
|
2014-08-21 17:57:46 -04:00
|
|
|
}
|
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
return m.Source
|
2014-08-28 10:18:08 -04:00
|
|
|
}
|
2014-08-21 17:57:46 -04:00
|
|
|
|
2015-07-30 19:10:56 -04:00
|
|
|
// copyExistingContents copies from the source to the destination and
|
|
|
|
// ensures the ownership is appropriately set.
|
2014-06-02 17:16:30 -04:00
|
|
|
func copyExistingContents(source, destination string) error {
|
|
|
|
volList, err := ioutil.ReadDir(source)
|
2014-05-19 18:18:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(volList) > 0 {
|
2014-06-02 17:16:30 -04:00
|
|
|
srcList, err := ioutil.ReadDir(destination)
|
2014-05-19 18:04:51 -04:00
|
|
|
if err != nil {
|
2014-03-18 18:28:40 -04:00
|
|
|
return err
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
2014-05-19 18:18:37 -04:00
|
|
|
if len(srcList) == 0 {
|
|
|
|
// If the source volume is empty copy files from the root into the volume
|
2014-10-29 15:06:51 -04:00
|
|
|
if err := chrootarchive.CopyWithTar(source, destination); err != nil {
|
2014-02-14 20:15:40 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 18:18:37 -04:00
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
return copyOwnership(source, destination)
|
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
|
|
|
|
// volumeToAPIType converts a volume.Volume to the type used by the remote API
|
|
|
|
func volumeToAPIType(v volume.Volume) *types.Volume {
|
|
|
|
return &types.Volume{
|
|
|
|
Name: v.Name(),
|
|
|
|
Driver: v.DriverName(),
|
|
|
|
Mountpoint: v.Path(),
|
|
|
|
}
|
|
|
|
}
|