Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 7f44c53

Browse files
kolyshkinAkihiroSuda
authored andcommitted
Fix/improve overlay support check
Before this commit, overlay check was performed by looking for `overlay` in /proc/filesystem. This obviously might not work for rootless Docker (fs is there, but one can't use it as non-root). This commit changes the check to perform the actual mount, by reusing the code previously written to check for multiple lower dirs support. The old check is removed from both drivers, as well as the additional check for the multiple lower dirs support in overlay2 since it's now a part of the main check. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com> (cherry picked from commit 649e4c88899878c9cdf9036f6bc7d62e2b39c04b) Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp> Upstream-commit: 1b0edb155f261396a45762bf5fa60ebe19dae2a3 Component: engine
1 parent a5c9db1 commit 7f44c53

3 files changed

Lines changed: 22 additions & 82 deletions

File tree

components/engine/daemon/graphdriver/overlay/overlay.go

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package overlay // import "github.com/docker/docker/daemon/graphdriver/overlay"
44

55
import (
6-
"bufio"
76
"fmt"
87
"io"
98
"io/ioutil"
@@ -123,10 +122,6 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
123122
return nil, err
124123
}
125124

126-
if err := supportsOverlay(); err != nil {
127-
return nil, graphdriver.ErrNotSupported
128-
}
129-
130125
// Perform feature detection on /var/lib/docker/overlay if it's an existing directory.
131126
// This covers situations where /var/lib/docker/overlay is a mount, and on a different
132127
// filesystem than /var/lib/docker.
@@ -136,6 +131,11 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
136131
testdir = filepath.Dir(testdir)
137132
}
138133

134+
if err := overlayutils.SupportsOverlay(testdir, false); err != nil {
135+
logrus.WithField("storage-driver", "overlay").Error(err)
136+
return nil, graphdriver.ErrNotSupported
137+
}
138+
139139
fsMagic, err := graphdriver.GetFSMagic(testdir)
140140
if err != nil {
141141
return nil, err
@@ -199,33 +199,6 @@ func parseOptions(options []string) (*overlayOptions, error) {
199199
return o, nil
200200
}
201201

202-
func supportsOverlay() error {
203-
// Access overlay filesystem so that Linux loads it (if possible).
204-
mountTarget, err := ioutil.TempDir("", "supportsOverlay")
205-
if err != nil {
206-
logrus.WithError(err).WithField("storage-driver", "overlay2").Error("could not create temporary directory, so assuming that 'overlay' is not supported")
207-
return graphdriver.ErrNotSupported
208-
}
209-
/* The mounting will fail--after the module has been loaded.*/
210-
defer os.RemoveAll(mountTarget)
211-
unix.Mount("overlay", mountTarget, "overlay", 0, "")
212-
213-
f, err := os.Open("/proc/filesystems")
214-
if err != nil {
215-
return err
216-
}
217-
defer f.Close()
218-
219-
s := bufio.NewScanner(f)
220-
for s.Scan() {
221-
if s.Text() == "nodev\toverlay" {
222-
return nil
223-
}
224-
}
225-
logrus.WithField("storage-driver", "overlay").Error("'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded.")
226-
return graphdriver.ErrNotSupported
227-
}
228-
229202
func (d *Driver) String() string {
230203
return "overlay"
231204
}

components/engine/daemon/graphdriver/overlay2/overlay.go

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package overlay2 // import "github.com/docker/docker/daemon/graphdriver/overlay2"
44

55
import (
6-
"bufio"
76
"context"
87
"errors"
98
"fmt"
@@ -133,10 +132,6 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
133132
return nil, err
134133
}
135134

136-
if err := supportsOverlay(); err != nil {
137-
return nil, graphdriver.ErrNotSupported
138-
}
139-
140135
// require kernel 4.0.0 to ensure multiple lower dirs are supported
141136
v, err := kernel.GetKernelVersion()
142137
if err != nil {
@@ -152,6 +147,11 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
152147
testdir = filepath.Dir(testdir)
153148
}
154149

150+
if err := overlayutils.SupportsOverlay(testdir, true); err != nil {
151+
logger.Error(err)
152+
return nil, graphdriver.ErrNotSupported
153+
}
154+
155155
fsMagic, err := graphdriver.GetFSMagic(testdir)
156156
if err != nil {
157157
return nil, err
@@ -176,16 +176,6 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
176176
}
177177
}
178178

179-
if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: 4, Major: 0, Minor: 0}) < 0 {
180-
if opts.overrideKernelCheck {
181-
logger.Warn("Using pre-4.0.0 kernel for overlay2, mount failures may require kernel update")
182-
} else {
183-
if err := overlayutils.SupportsMultipleLowerDir(testdir); err != nil {
184-
logger.Debugf("Multiple lower dirs not supported: %v", err)
185-
return nil, graphdriver.ErrNotSupported
186-
}
187-
}
188-
}
189179
supportsDType, err := fsutils.SupportsDType(testdir)
190180
if err != nil {
191181
return nil, err
@@ -274,33 +264,6 @@ func parseOptions(options []string) (*overlayOptions, error) {
274264
return o, nil
275265
}
276266

277-
func supportsOverlay() error {
278-
// Access overlay filesystem so that Linux loads it (if possible).
279-
mountTarget, err := ioutil.TempDir("", "supportsOverlay2")
280-
if err != nil {
281-
logrus.WithError(err).WithField("storage-driver", "overlay2").Error("could not create temporary directory, so assuming that 'overlay' is not supported")
282-
return graphdriver.ErrNotSupported
283-
}
284-
/* The mounting will fail--after the module has been loaded.*/
285-
defer os.RemoveAll(mountTarget)
286-
unix.Mount("overlay", mountTarget, "overlay", 0, "")
287-
288-
f, err := os.Open("/proc/filesystems")
289-
if err != nil {
290-
return err
291-
}
292-
defer f.Close()
293-
294-
s := bufio.NewScanner(f)
295-
for s.Scan() {
296-
if s.Text() == "nodev\toverlay" {
297-
return nil
298-
}
299-
}
300-
logger.Error("'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded.")
301-
return graphdriver.ErrNotSupported
302-
}
303-
304267
func useNaiveDiff(home string) bool {
305268
useNaiveDiffLock.Do(func() {
306269
if err := doesSupportNativeDiff(home); err != nil {

components/engine/daemon/graphdriver/overlayutils/overlayutils.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ func ErrDTypeNotSupported(driver, backingFs string) error {
3131
return graphdriver.NotSupportedError(msg)
3232
}
3333

34-
// SupportsMultipleLowerDir checks if the system supports multiple lowerdirs,
35-
// which is required for the overlay2 driver. On 4.x kernels, multiple lowerdirs
36-
// are always available (so this check isn't needed), and backported to RHEL and
37-
// CentOS 3.x kernels (3.10.0-693.el7.x86_64 and up). This function is to detect
38-
// support on those kernels, without doing a kernel version compare.
39-
func SupportsMultipleLowerDir(d string) error {
40-
td, err := ioutil.TempDir(d, "multiple-lowerdir-check")
34+
// SupportsOverlay checks if the system supports overlay filesystem
35+
// by performing an actual overlay mount.
36+
//
37+
// checkMultipleLowers parameter enables check for multiple lowerdirs,
38+
// which is required for the overlay2 driver.
39+
func SupportsOverlay(d string, checkMultipleLowers bool) error {
40+
td, err := ioutil.TempDir(d, "check-overlayfs-support")
4141
if err != nil {
4242
return err
4343
}
@@ -54,7 +54,11 @@ func SupportsMultipleLowerDir(d string) error {
5454
}
5555

5656
mnt := filepath.Join(td, "merged")
57-
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"))
57+
lowerDir := path.Join(td, "lower2")
58+
if checkMultipleLowers {
59+
lowerDir += ":" + path.Join(td, "lower1")
60+
}
61+
opts := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lowerDir, path.Join(td, "upper"), path.Join(td, "work"))
5862
if err := unix.Mount("overlay", mnt, "overlay", 0, opts); err != nil {
5963
return errors.Wrap(err, "failed to mount overlay")
6064
}

0 commit comments

Comments
 (0)