|
@@ -340,6 +340,10 @@ func (d *Driver) Get(id string, mountLabel string) (string, error) {
|
|
|
if _, err := os.Stat(dir); err != nil {
|
|
|
return "", err
|
|
|
}
|
|
|
+ mergedDir := path.Join(dir, "merged")
|
|
|
+ if count := d.ctr.Increment(mergedDir); count > 1 {
|
|
|
+ return mergedDir, nil
|
|
|
+ }
|
|
|
|
|
|
// If id has a root, just return it
|
|
|
rootDir := path.Join(dir, "root")
|
|
@@ -357,40 +361,24 @@ func (d *Driver) Get(id string, mountLabel string) (string, error) {
|
|
|
lowerDir := path.Join(d.dir(string(lowerID)), "root")
|
|
|
upperDir := path.Join(dir, "upper")
|
|
|
workDir := path.Join(dir, "work")
|
|
|
- mergedDir := path.Join(dir, "merged")
|
|
|
-
|
|
|
- if count := d.ctr.Increment(id); count > 1 {
|
|
|
- return mergedDir, nil
|
|
|
- }
|
|
|
|
|
|
opts := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lowerDir, upperDir, workDir)
|
|
|
|
|
|
- // if it's mounted already, just return
|
|
|
- mounted, err := d.mounted(mergedDir)
|
|
|
- if err != nil {
|
|
|
- d.ctr.Decrement(id)
|
|
|
- return "", err
|
|
|
- }
|
|
|
- if mounted {
|
|
|
- d.ctr.Decrement(id)
|
|
|
- return mergedDir, nil
|
|
|
- }
|
|
|
-
|
|
|
if err := syscall.Mount("overlay", mergedDir, "overlay", 0, label.FormatMountLabel(opts, mountLabel)); err != nil {
|
|
|
- d.ctr.Decrement(id)
|
|
|
+ d.ctr.Decrement(mergedDir)
|
|
|
return "", fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err)
|
|
|
}
|
|
|
// chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a
|
|
|
// user namespace requires this to move a directory from lower to upper.
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
|
|
|
if err != nil {
|
|
|
- d.ctr.Decrement(id)
|
|
|
+ d.ctr.Decrement(mergedDir)
|
|
|
syscall.Unmount(mergedDir, 0)
|
|
|
return "", err
|
|
|
}
|
|
|
|
|
|
if err := os.Chown(path.Join(workDir, "work"), rootUID, rootGID); err != nil {
|
|
|
- d.ctr.Decrement(id)
|
|
|
+ d.ctr.Decrement(mergedDir)
|
|
|
syscall.Unmount(mergedDir, 0)
|
|
|
return "", err
|
|
|
}
|
|
@@ -408,13 +396,14 @@ func (d *Driver) mounted(dir string) (bool, error) {
|
|
|
|
|
|
// Put unmounts the mount path created for the give id.
|
|
|
func (d *Driver) Put(id string) error {
|
|
|
- if count := d.ctr.Decrement(id); count > 0 {
|
|
|
- return nil
|
|
|
- }
|
|
|
d.pathCacheLock.Lock()
|
|
|
mountpoint, exists := d.pathCache[id]
|
|
|
d.pathCacheLock.Unlock()
|
|
|
|
|
|
+ if count := d.ctr.Decrement(mountpoint); count > 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
if !exists {
|
|
|
logrus.Debugf("Put on a non-mounted device %s", id)
|
|
|
// but it might be still here
|