浏览代码

Merge pull request #26858 from cpuguy83/better_error_on_mount_fail

Fix some places where low-level errors bubbled up
Aaron Lehmann 8 年之前
父节点
当前提交
37fdba0428
共有 4 个文件被更改,包括 25 次插入12 次删除
  1. 7 5
      volume/local/local.go
  2. 8 1
      volume/local/local_unix.go
  3. 5 3
      volume/store/store.go
  4. 5 3
      volume/volume.go

+ 7 - 5
volume/local/local.go

@@ -12,6 +12,8 @@ import (
 	"reflect"
 	"sync"
 
+	"github.com/pkg/errors"
+
 	"github.com/Sirupsen/logrus"
 	"github.com/docker/docker/pkg/idtools"
 	"github.com/docker/docker/pkg/mount"
@@ -93,7 +95,7 @@ func New(scope string, rootUID, rootGID int) (*Root, error) {
 		if b, err := ioutil.ReadFile(optsFilePath); err == nil {
 			opts := optsConfig{}
 			if err := json.Unmarshal(b, &opts); err != nil {
-				return nil, err
+				return nil, errors.Wrapf(err, "error while unmarshaling volume options for volume: %s", name)
 			}
 			// Make sure this isn't an empty optsConfig.
 			// This could be empty due to buggy behavior in older versions of Docker.
@@ -168,7 +170,7 @@ func (r *Root) Create(name string, opts map[string]string) (volume.Volume, error
 		if os.IsExist(err) {
 			return nil, fmt.Errorf("volume already exists under %s", filepath.Dir(path))
 		}
-		return nil, err
+		return nil, errors.Wrapf(err, "error while creating volume path '%s'", path)
 	}
 
 	var err error
@@ -194,7 +196,7 @@ func (r *Root) Create(name string, opts map[string]string) (volume.Volume, error
 			return nil, err
 		}
 		if err = ioutil.WriteFile(filepath.Join(filepath.Dir(path), "opts.json"), b, 600); err != nil {
-			return nil, err
+			return nil, errors.Wrap(err, "error while persisting volume options")
 		}
 	}
 
@@ -240,7 +242,7 @@ func removePath(path string) error {
 		if os.IsNotExist(err) {
 			return nil
 		}
-		return err
+		return errors.Wrapf(err, "error removing volume path '%s'", path)
 	}
 	return nil
 }
@@ -327,7 +329,7 @@ func (v *localVolume) Unmount(id string) error {
 		if v.active.count == 0 {
 			if err := mount.Unmount(v.path); err != nil {
 				v.active.count++
-				return err
+				return errors.Wrapf(err, "error while unmounting volume path '%s'", v.path)
 			}
 			v.active.mounted = false
 		}

+ 8 - 1
volume/local/local_unix.go

@@ -10,6 +10,8 @@ import (
 	"path/filepath"
 	"strings"
 
+	"src/github.com/pkg/errors"
+
 	"github.com/docker/docker/pkg/mount"
 )
 
@@ -29,6 +31,10 @@ type optsConfig struct {
 	MountDevice string
 }
 
+func (o *optsConfig) String() string {
+	return fmt.Sprintf("type='%s' device='%s' o='%s'", o.MountType, o.MountDevice, o.MountOpts)
+}
+
 // 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 {
@@ -65,5 +71,6 @@ func (v *localVolume) mount() error {
 	if v.opts.MountDevice == "" {
 		return fmt.Errorf("missing device in volume options")
 	}
-	return mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, v.opts.MountOpts)
+	err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, v.opts.MountOpts)
+	return errors.Wrapf(err, "error while mounting volume with options: %s", v.opts)
 }

+ 5 - 3
volume/store/store.go

@@ -8,6 +8,8 @@ import (
 	"sync"
 	"time"
 
+	"github.com/pkg/errors"
+
 	"github.com/Sirupsen/logrus"
 	"github.com/boltdb/bolt"
 	"github.com/docker/docker/pkg/locker"
@@ -70,13 +72,13 @@ func New(rootPath string) (*VolumeStore, error) {
 		var err error
 		vs.db, err = bolt.Open(dbPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
 		if err != nil {
-			return nil, err
+			return nil, errors.Wrap(err, "error while opening volume store metadata database")
 		}
 
 		// initialize volumes bucket
 		if err := vs.db.Update(func(tx *bolt.Tx) error {
 			if _, err := tx.CreateBucketIfNotExists([]byte(volumeBucketName)); err != nil {
-				return err
+				return errors.Wrap(err, "error while setting up volume store metadata database")
 			}
 
 			return nil
@@ -299,7 +301,7 @@ func (s *VolumeStore) create(name, driverName string, opts, labels map[string]st
 			err := b.Put([]byte(name), volData)
 			return err
 		}); err != nil {
-			return nil, err
+			return nil, errors.Wrap(err, "error while persisting volume metadata")
 		}
 	}
 

+ 5 - 3
volume/volume.go

@@ -11,6 +11,7 @@ import (
 	"github.com/docker/docker/pkg/idtools"
 	"github.com/docker/docker/pkg/stringid"
 	"github.com/opencontainers/runc/libcontainer/label"
+	"github.com/pkg/errors"
 )
 
 // DefaultDriverName is the driver name used for the driver
@@ -114,7 +115,8 @@ func (m *MountPoint) Setup(mountLabel string, rootUID, rootGID int) (string, err
 		if m.ID == "" {
 			m.ID = stringid.GenerateNonCryptoID()
 		}
-		return m.Volume.Mount(m.ID)
+		path, err := m.Volume.Mount(m.ID)
+		return path, errors.Wrapf(err, "error while mounting volume '%s'", m.Source)
 	}
 	if len(m.Source) == 0 {
 		return "", fmt.Errorf("Unable to setup mount point, neither source nor volume defined")
@@ -126,14 +128,14 @@ func (m *MountPoint) Setup(mountLabel string, rootUID, rootGID int) (string, err
 		if err := idtools.MkdirAllNewAs(m.Source, 0755, rootUID, rootGID); err != nil {
 			if perr, ok := err.(*os.PathError); ok {
 				if perr.Err != syscall.ENOTDIR {
-					return "", err
+					return "", errors.Wrapf(err, "error while creating mount source path '%s'", m.Source)
 				}
 			}
 		}
 	}
 	if label.RelabelNeeded(m.Mode) {
 		if err := label.Relabel(m.Source, mountLabel, label.IsShared(m.Mode)); err != nil {
-			return "", err
+			return "", errors.Wrapf(err, "error setting label on mount source '%s'", m.Source)
 		}
 	}
 	return m.Source, nil