Browse code

overlay: move supportsMultipleLowerDir to utils

This moves supportsMultipleLowerDir() to overlayutils
so it can be used from both overlay and overlay2.

The only changes made were:
* replace logger with logrus
* don't use workDirName mergedDirName constants
* add mnt var to improve readability a bit

This is a preparation for the next commit.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

Kir Kolyshkin authored on 2019/11/08 07:20:40
Showing 3 changed files
... ...
@@ -99,35 +99,3 @@ func doesSupportNativeDiff(d string) error {
99 99
 
100 100
 	return nil
101 101
 }
102
-
103
-// supportsMultipleLowerDir checks if the system supports multiple lowerdirs,
104
-// which is required for the overlay2 driver. On 4.x kernels, multiple lowerdirs
105
-// are always available (so this check isn't needed), and backported to RHEL and
106
-// CentOS 3.x kernels (3.10.0-693.el7.x86_64 and up). This function is to detect
107
-// support on those kernels, without doing a kernel version compare.
108
-func supportsMultipleLowerDir(d string) error {
109
-	td, err := ioutil.TempDir(d, "multiple-lowerdir-check")
110
-	if err != nil {
111
-		return err
112
-	}
113
-	defer func() {
114
-		if err := os.RemoveAll(td); err != nil {
115
-			logger.Warnf("Failed to remove check directory %v: %v", td, err)
116
-		}
117
-	}()
118
-
119
-	for _, dir := range []string{"lower1", "lower2", "upper", workDirName, mergedDirName} {
120
-		if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil {
121
-			return err
122
-		}
123
-	}
124
-
125
-	opts := fmt.Sprintf("lowerdir=%s:%s,upperdir=%s,workdir=%s", path.Join(td, "lower2"), path.Join(td, "lower1"), path.Join(td, "upper"), path.Join(td, workDirName))
126
-	if err := unix.Mount("overlay", filepath.Join(td, mergedDirName), "overlay", 0, opts); err != nil {
127
-		return errors.Wrap(err, "failed to mount overlay")
128
-	}
129
-	if err := unix.Unmount(filepath.Join(td, mergedDirName), 0); err != nil {
130
-		logger.Warnf("Failed to unmount check directory %v: %v", filepath.Join(td, mergedDirName), err)
131
-	}
132
-	return nil
133
-}
... ...
@@ -180,7 +180,7 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
180 180
 		if opts.overrideKernelCheck {
181 181
 			logger.Warn("Using pre-4.0.0 kernel for overlay2, mount failures may require kernel update")
182 182
 		} else {
183
-			if err := supportsMultipleLowerDir(testdir); err != nil {
183
+			if err := overlayutils.SupportsMultipleLowerDir(testdir); err != nil {
184 184
 				logger.Debugf("Multiple lower dirs not supported: %v", err)
185 185
 				return nil, graphdriver.ErrNotSupported
186 186
 			}
... ...
@@ -4,8 +4,15 @@ package overlayutils // import "github.com/docker/docker/daemon/graphdriver/over
4 4
 
5 5
 import (
6 6
 	"fmt"
7
+	"io/ioutil"
8
+	"os"
9
+	"path"
10
+	"path/filepath"
7 11
 
8 12
 	"github.com/docker/docker/daemon/graphdriver"
13
+	"github.com/pkg/errors"
14
+	"github.com/sirupsen/logrus"
15
+	"golang.org/x/sys/unix"
9 16
 )
10 17
 
11 18
 // ErrDTypeNotSupported denotes that the backing filesystem doesn't support d_type.
... ...
@@ -23,3 +30,36 @@ func ErrDTypeNotSupported(driver, backingFs string) error {
23 23
 
24 24
 	return graphdriver.NotSupportedError(msg)
25 25
 }
26
+
27
+// SupportsMultipleLowerDir checks if the system supports multiple lowerdirs,
28
+// which is required for the overlay2 driver. On 4.x kernels, multiple lowerdirs
29
+// are always available (so this check isn't needed), and backported to RHEL and
30
+// CentOS 3.x kernels (3.10.0-693.el7.x86_64 and up). This function is to detect
31
+// support on those kernels, without doing a kernel version compare.
32
+func SupportsMultipleLowerDir(d string) error {
33
+	td, err := ioutil.TempDir(d, "multiple-lowerdir-check")
34
+	if err != nil {
35
+		return err
36
+	}
37
+	defer func() {
38
+		if err := os.RemoveAll(td); err != nil {
39
+			logrus.Warnf("Failed to remove check directory %v: %v", td, err)
40
+		}
41
+	}()
42
+
43
+	for _, dir := range []string{"lower1", "lower2", "upper", "work", "merged"} {
44
+		if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil {
45
+			return err
46
+		}
47
+	}
48
+
49
+	mnt := filepath.Join(td, "merged")
50
+	opts := fmt.Sprintf("lowerdir=%s:%s,upperdir=%s,workdir=%s", path.Join(td, "lower2"), path.Join(td, "lower1"), path.Join(td, "upper"), path.Join(td, "work"))
51
+	if err := unix.Mount("overlay", mnt, "overlay", 0, opts); err != nil {
52
+		return errors.Wrap(err, "failed to mount overlay")
53
+	}
54
+	if err := unix.Unmount(mnt, 0); err != nil {
55
+		logrus.Warnf("Failed to unmount check directory %v: %v", mnt, err)
56
+	}
57
+	return nil
58
+}