2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux || freebsd
|
2017-11-01 19:37:53 -04:00
|
|
|
// +build linux freebsd
|
2015-09-16 17:18:24 -04:00
|
|
|
|
|
|
|
// Package local provides the default implementation for volumes. It
|
|
|
|
// is used to mount data volume containers and directories local to
|
|
|
|
// the host server.
|
2018-02-05 16:05:59 -05:00
|
|
|
package local // import "github.com/docker/docker/volume/local"
|
2015-09-16 17:18:24 -04:00
|
|
|
|
|
|
|
import (
|
2016-02-11 21:48:16 -05:00
|
|
|
"fmt"
|
2016-10-12 16:11:20 -04:00
|
|
|
"net"
|
2017-05-17 17:19:13 -04:00
|
|
|
"os"
|
2015-09-16 17:18:24 -04:00
|
|
|
"strings"
|
2017-05-17 17:19:13 -04:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2016-02-11 21:48:16 -05:00
|
|
|
|
2018-12-21 21:02:28 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-08-09 15:53:09 -04:00
|
|
|
"github.com/docker/docker/quota"
|
|
|
|
units "github.com/docker/go-units"
|
2020-03-13 19:38:24 -04:00
|
|
|
"github.com/moby/sys/mount"
|
2020-09-19 12:45:41 -04:00
|
|
|
"github.com/moby/sys/mountinfo"
|
2018-12-21 20:28:30 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-02-11 21:48:16 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-12-21 20:23:17 -05:00
|
|
|
validOpts = map[string]struct{}{
|
|
|
|
"type": {}, // specify the filesystem type for mount, e.g. nfs
|
|
|
|
"o": {}, // generic mount options
|
|
|
|
"device": {}, // device to mount from
|
2020-08-09 15:53:09 -04:00
|
|
|
"size": {}, // quota size limit
|
2016-02-11 21:48:16 -05:00
|
|
|
}
|
2020-08-09 15:53:09 -04:00
|
|
|
mandatoryOpts = map[string][]string{
|
2021-01-14 08:54:37 -05:00
|
|
|
"device": {"type"},
|
|
|
|
"type": {"device"},
|
|
|
|
"o": {"device", "type"},
|
2018-04-09 04:09:30 -04:00
|
|
|
}
|
2015-09-16 17:18:24 -04:00
|
|
|
)
|
|
|
|
|
2016-02-11 21:48:16 -05:00
|
|
|
type optsConfig struct {
|
|
|
|
MountType string
|
|
|
|
MountOpts string
|
|
|
|
MountDevice string
|
2020-08-09 15:53:09 -04:00
|
|
|
Quota quota.Quota
|
2016-02-11 21:48:16 -05:00
|
|
|
}
|
2015-09-16 17:18:24 -04:00
|
|
|
|
2016-09-23 10:38:19 -04:00
|
|
|
func (o *optsConfig) String() string {
|
2020-08-09 15:53:09 -04:00
|
|
|
return fmt.Sprintf("type='%s' device='%s' o='%s' size='%d'", o.MountType, o.MountDevice, o.MountOpts, o.Quota.Size)
|
2016-09-23 10:38:19 -04:00
|
|
|
}
|
|
|
|
|
2016-02-11 21:48:16 -05:00
|
|
|
func setOpts(v *localVolume, opts map[string]string) error {
|
|
|
|
if len(opts) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-09 15:53:09 -04:00
|
|
|
err := validateOpts(opts)
|
|
|
|
if err != nil {
|
2016-02-11 21:48:16 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.opts = &optsConfig{
|
|
|
|
MountType: opts["type"],
|
|
|
|
MountOpts: opts["o"],
|
|
|
|
MountDevice: opts["device"],
|
|
|
|
}
|
2020-08-09 15:53:09 -04:00
|
|
|
if val, ok := opts["size"]; ok {
|
|
|
|
size, err := units.RAMInBytes(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if size > 0 && v.quotaCtl == nil {
|
|
|
|
return errdefs.InvalidParameter(errors.Errorf("quota size requested but no quota support"))
|
|
|
|
}
|
|
|
|
v.opts.Quota.Size = uint64(size)
|
|
|
|
}
|
2016-02-11 21:48:16 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-21 21:02:28 -05:00
|
|
|
func validateOpts(opts map[string]string) error {
|
|
|
|
if len(opts) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for opt := range opts {
|
|
|
|
if _, ok := validOpts[opt]; !ok {
|
|
|
|
return errdefs.InvalidParameter(errors.Errorf("invalid option: %q", opt))
|
|
|
|
}
|
|
|
|
}
|
2020-08-09 15:53:09 -04:00
|
|
|
for opt, reqopts := range mandatoryOpts {
|
|
|
|
if _, ok := opts[opt]; ok {
|
|
|
|
for _, reqopt := range reqopts {
|
|
|
|
if _, ok := opts[reqopt]; !ok {
|
|
|
|
return errdefs.InvalidParameter(errors.Errorf("missing required option: %q", reqopt))
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 21:02:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-19 12:45:41 -04:00
|
|
|
func unmount(path string) {
|
|
|
|
_ = mount.Unmount(path)
|
|
|
|
}
|
|
|
|
|
2020-08-09 15:53:09 -04:00
|
|
|
func (v *localVolume) needsMount() bool {
|
|
|
|
if v.opts == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v.opts.MountDevice != "" || v.opts.MountType != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-02-11 21:48:16 -05:00
|
|
|
func (v *localVolume) mount() error {
|
|
|
|
if v.opts.MountDevice == "" {
|
|
|
|
return fmt.Errorf("missing device in volume options")
|
|
|
|
}
|
2016-10-12 16:11:20 -04:00
|
|
|
mountOpts := v.opts.MountOpts
|
2019-05-21 17:34:53 -04:00
|
|
|
switch v.opts.MountType {
|
|
|
|
case "nfs", "cifs":
|
2016-10-12 16:11:20 -04:00
|
|
|
if addrValue := getAddress(v.opts.MountOpts); addrValue != "" && net.ParseIP(addrValue).To4() == nil {
|
|
|
|
ipAddr, err := net.ResolveIPAddr("ip", addrValue)
|
|
|
|
if err != nil {
|
2019-05-21 17:34:53 -04:00
|
|
|
return errors.Wrapf(err, "error resolving passed in network volume address")
|
2016-10-12 16:11:20 -04:00
|
|
|
}
|
|
|
|
mountOpts = strings.Replace(mountOpts, "addr="+addrValue, "addr="+ipAddr.String(), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, mountOpts)
|
2022-05-12 10:53:02 -04:00
|
|
|
if err != nil {
|
|
|
|
if password := getPassword(v.opts.MountOpts); password != "" {
|
|
|
|
err = errors.New(strings.Replace(err.Error(), "password="+password, "password=********", 1))
|
|
|
|
}
|
|
|
|
}
|
2018-10-22 21:30:34 -04:00
|
|
|
return errors.Wrap(err, "failed to mount local volume")
|
2016-02-11 21:48:16 -05:00
|
|
|
}
|
2017-05-17 17:19:13 -04:00
|
|
|
|
2020-08-09 15:53:09 -04:00
|
|
|
func (v *localVolume) postMount() error {
|
|
|
|
if v.opts == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if v.opts.Quota.Size > 0 {
|
|
|
|
if v.quotaCtl != nil {
|
|
|
|
err := v.quotaCtl.SetQuota(v.path, v.opts.Quota)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("size quota requested for volume but no quota support")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-19 12:45:41 -04:00
|
|
|
func (v *localVolume) unmount() error {
|
|
|
|
if v.needsMount() {
|
|
|
|
if err := mount.Unmount(v.path); err != nil {
|
|
|
|
if mounted, mErr := mountinfo.Mounted(v.path); mounted || mErr != nil {
|
|
|
|
return errdefs.System(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.active.mounted = false
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:19:13 -04:00
|
|
|
func (v *localVolume) CreatedAt() (time.Time, error) {
|
|
|
|
fileInfo, err := os.Stat(v.path)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
sec, nsec := fileInfo.Sys().(*syscall.Stat_t).Ctim.Unix()
|
|
|
|
return time.Unix(sec, nsec), nil
|
|
|
|
}
|