1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #43591 from thaJeztah/volumes_fixup_part1

volumes/local: remove legacy migration code, and some cleanups
This commit is contained in:
Brian Goff 2022-05-13 14:37:12 -07:00 committed by GitHub
commit 8a0cb10840
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 52 deletions

View file

@ -21,11 +21,11 @@ import (
"github.com/sirupsen/logrus"
)
// VolumeDataPathName is the name of the directory where the volume data is stored.
// It uses a very distinctive name to avoid collisions migrating data between
// Docker versions.
const (
VolumeDataPathName = "_data"
// volumeDataPathName is the name of the directory where the volume data is stored.
// It uses a very distinctive name to avoid collisions migrating data between
// Docker versions.
volumeDataPathName = "_data"
volumesPathName = "volumes"
)
@ -47,26 +47,23 @@ type activeMount struct {
// is the base path that the Root instance uses to store its
// volumes. The base path is created here if it does not exist.
func New(scope string, rootIdentity idtools.Identity) (*Root, error) {
rootDirectory := filepath.Join(scope, volumesPathName)
if err := idtools.MkdirAllAndChown(rootDirectory, 0701, idtools.CurrentIdentity()); err != nil {
return nil, err
}
r := &Root{
scope: scope,
path: rootDirectory,
path: filepath.Join(scope, volumesPathName),
volumes: make(map[string]*localVolume),
rootIdentity: rootIdentity,
}
dirs, err := os.ReadDir(rootDirectory)
if err := idtools.MkdirAllAndChown(r.path, 0701, idtools.CurrentIdentity()); err != nil {
return nil, err
}
dirs, err := os.ReadDir(r.path)
if err != nil {
return nil, err
}
if r.quotaCtl, err = quota.NewControl(rootDirectory); err != nil {
logrus.Debugf("No quota support for local volumes in %s: %v", rootDirectory, err)
if r.quotaCtl, err = quota.NewControl(r.path); err != nil {
logrus.Debugf("No quota support for local volumes in %s: %v", r.path, err)
}
for _, d := range dirs {
@ -74,7 +71,7 @@ func New(scope string, rootIdentity idtools.Identity) (*Root, error) {
continue
}
name := filepath.Base(d.Name())
name := d.Name()
v := &localVolume{
driverName: r.Name(),
name: name,
@ -82,8 +79,7 @@ func New(scope string, rootIdentity idtools.Identity) (*Root, error) {
quotaCtl: r.quotaCtl,
}
r.volumes[name] = v
optsFilePath := filepath.Join(rootDirectory, name, "opts.json")
if b, err := os.ReadFile(optsFilePath); err == nil {
if b, err := os.ReadFile(filepath.Join(r.path, name, "opts.json")); err == nil {
opts := optsConfig{}
if err := json.Unmarshal(b, &opts); err != nil {
return nil, errors.Wrapf(err, "error while unmarshaling volume options for volume: %s", name)
@ -107,7 +103,6 @@ func New(scope string, rootIdentity idtools.Identity) (*Root, error) {
// commands to create/remove dirs within its provided scope.
type Root struct {
m sync.Mutex
scope string
path string
quotaCtl *quota.Control
volumes map[string]*localVolume
@ -127,7 +122,7 @@ func (r *Root) List() ([]volume.Volume, error) {
// DataPath returns the constructed path of this volume.
func (r *Root) DataPath(volumeName string) string {
return filepath.Join(r.path, volumeName, VolumeDataPathName)
return filepath.Join(r.path, volumeName, volumeDataPathName)
}
// Name returns the name of Root, defined in the volume package in the DefaultDriverName constant.
@ -224,8 +219,8 @@ func (r *Root) Remove(v volume.Volume) error {
realPath = filepath.Dir(lv.path)
}
if !r.scopedPath(realPath) {
return errdefs.System(errors.Errorf("Unable to remove a directory outside of the local volume root %s: %s", r.scope, realPath))
if realPath == r.path || !strings.HasPrefix(realPath, r.path) {
return errdefs.System(errors.Errorf("unable to remove a directory outside of the local volume root %s: %s", r.path, realPath))
}
if err := removePath(realPath); err != nil {

View file

@ -10,7 +10,6 @@ import (
"fmt"
"net"
"os"
"path/filepath"
"strings"
"syscall"
"time"
@ -24,8 +23,6 @@ import (
)
var (
oldVfsDir = filepath.Join("vfs", "dir")
validOpts = map[string]struct{}{
"type": {}, // specify the filesystem type for mount, e.g. nfs
"o": {}, // generic mount options
@ -50,22 +47,6 @@ func (o *optsConfig) String() string {
return fmt.Sprintf("type='%s' device='%s' o='%s' size='%d'", o.MountType, o.MountDevice, o.MountOpts, o.Quota.Size)
}
// scopedPath verifies that the path where the volume is located
// is under Docker's root and the valid local paths.
func (r *Root) scopedPath(realPath string) bool {
// Volumes path for Docker version >= 1.7
if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) && realPath != filepath.Join(r.scope, volumesPathName) {
return true
}
// Volumes path for Docker version < 1.7
if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) {
return true
}
return false
}
func setOpts(v *localVolume, opts map[string]string) error {
if len(opts) == 0 {
return nil

View file

@ -5,8 +5,6 @@ package local // import "github.com/docker/docker/volume/local"
import (
"os"
"path/filepath"
"strings"
"syscall"
"time"
@ -16,15 +14,6 @@ import (
type optsConfig struct{}
// scopedPath verifies that the path where the volume is located
// is under Docker's root and the valid local paths.
func (r *Root) scopedPath(realPath string) bool {
if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) && realPath != filepath.Join(r.scope, volumesPathName) {
return true
}
return false
}
func setOpts(v *localVolume, opts map[string]string) error {
if len(opts) > 0 {
return errdefs.InvalidParameter(errors.New("options are not supported on this platform"))