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

Commit 6da9593

Browse files
authored
Merge pull request #40462 from AkihiroSuda/cherrypick-40210-1903
[19.03 backport] overlay[2]: rm extra checks in init Upstream-commit: 4ac62b478d539d2539e49d7c710eaea482e47293 Component: engine
2 parents 789f1ad + c3a1bdb commit 6da9593

5 files changed

Lines changed: 58 additions & 139 deletions

File tree

components/engine/daemon/graphdriver/devmapper/deviceset.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,14 @@ func xfsSupported() error {
540540
return err // error text is descriptive enough
541541
}
542542

543-
// Check if kernel supports xfs filesystem or not.
544-
exec.Command("modprobe", "xfs").Run()
543+
mountTarget, err := ioutil.TempDir("", "supportsXFS")
544+
if err != nil {
545+
return errors.Wrapf(err, "error checking for xfs support")
546+
}
547+
548+
/* The mounting will fail--after the module has been loaded.*/
549+
defer os.RemoveAll(mountTarget)
550+
unix.Mount("none", mountTarget, "xfs", 0, "")
545551

546552
f, err := os.Open("/proc/filesystems")
547553
if err != nil {

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

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

55
import (
6-
"bufio"
76
"fmt"
87
"io"
98
"io/ioutil"
109
"os"
11-
"os/exec"
1210
"path"
1311
"path/filepath"
1412
"strconv"
@@ -124,10 +122,6 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
124122
return nil, err
125123
}
126124

127-
if err := supportsOverlay(); err != nil {
128-
return nil, graphdriver.ErrNotSupported
129-
}
130-
131125
// Perform feature detection on /var/lib/docker/overlay if it's an existing directory.
132126
// This covers situations where /var/lib/docker/overlay is a mount, and on a different
133127
// filesystem than /var/lib/docker.
@@ -137,18 +131,9 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
137131
testdir = filepath.Dir(testdir)
138132
}
139133

140-
fsMagic, err := graphdriver.GetFSMagic(testdir)
141-
if err != nil {
142-
return nil, err
143-
}
144-
if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
145-
backingFs = fsName
146-
}
147-
148-
switch fsMagic {
149-
case graphdriver.FsMagicAufs, graphdriver.FsMagicBtrfs, graphdriver.FsMagicEcryptfs, graphdriver.FsMagicNfsFs, graphdriver.FsMagicOverlay, graphdriver.FsMagicZfs:
150-
logrus.WithField("storage-driver", "overlay").Errorf("'overlay' is not supported over %s", backingFs)
151-
return nil, graphdriver.ErrIncompatibleFS
134+
if err := overlayutils.SupportsOverlay(testdir, false); err != nil {
135+
logrus.WithField("storage-driver", "overlay").Error(err)
136+
return nil, graphdriver.ErrNotSupported
152137
}
153138

154139
supportsDType, err := fsutils.SupportsDType(testdir)
@@ -200,27 +185,6 @@ func parseOptions(options []string) (*overlayOptions, error) {
200185
return o, nil
201186
}
202187

203-
func supportsOverlay() error {
204-
// We can try to modprobe overlay first before looking at
205-
// proc/filesystems for when overlay is supported
206-
exec.Command("modprobe", "overlay").Run()
207-
208-
f, err := os.Open("/proc/filesystems")
209-
if err != nil {
210-
return err
211-
}
212-
defer f.Close()
213-
214-
s := bufio.NewScanner(f)
215-
for s.Scan() {
216-
if s.Text() == "nodev\toverlay" {
217-
return nil
218-
}
219-
}
220-
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.")
221-
return graphdriver.ErrNotSupported
222-
}
223-
224188
func (d *Driver) String() string {
225189
return "overlay"
226190
}

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

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -99,35 +99,3 @@ func doesSupportNativeDiff(d string) error {
9999

100100
return nil
101101
}
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-
}

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

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

55
import (
6-
"bufio"
76
"context"
87
"errors"
98
"fmt"
109
"io"
1110
"io/ioutil"
1211
"os"
13-
"os/exec"
1412
"path"
1513
"path/filepath"
1614
"strconv"
@@ -29,7 +27,6 @@ import (
2927
"github.com/docker/docker/pkg/locker"
3028
"github.com/docker/docker/pkg/mount"
3129
"github.com/docker/docker/pkg/parsers"
32-
"github.com/docker/docker/pkg/parsers/kernel"
3330
"github.com/docker/docker/pkg/system"
3431
"github.com/docker/go-units"
3532
rsystem "github.com/opencontainers/runc/libcontainer/system"
@@ -134,16 +131,6 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
134131
return nil, err
135132
}
136133

137-
if err := supportsOverlay(); err != nil {
138-
return nil, graphdriver.ErrNotSupported
139-
}
140-
141-
// require kernel 4.0.0 to ensure multiple lower dirs are supported
142-
v, err := kernel.GetKernelVersion()
143-
if err != nil {
144-
return nil, err
145-
}
146-
147134
// Perform feature detection on /var/lib/docker/overlay2 if it's an existing directory.
148135
// This covers situations where /var/lib/docker/overlay2 is a mount, and on a different
149136
// filesystem than /var/lib/docker.
@@ -153,40 +140,11 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
153140
testdir = filepath.Dir(testdir)
154141
}
155142

156-
fsMagic, err := graphdriver.GetFSMagic(testdir)
157-
if err != nil {
158-
return nil, err
159-
}
160-
if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
161-
backingFs = fsName
162-
}
163-
164-
switch fsMagic {
165-
case graphdriver.FsMagicAufs, graphdriver.FsMagicEcryptfs, graphdriver.FsMagicNfsFs, graphdriver.FsMagicOverlay, graphdriver.FsMagicZfs:
166-
logger.Errorf("'overlay2' is not supported over %s", backingFs)
167-
return nil, graphdriver.ErrIncompatibleFS
168-
case graphdriver.FsMagicBtrfs:
169-
// Support for OverlayFS on BTRFS was added in kernel 4.7
170-
// See https://btrfs.wiki.kernel.org/index.php/Changelog
171-
if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: 4, Major: 7, Minor: 0}) < 0 {
172-
if !opts.overrideKernelCheck {
173-
logger.Errorf("'overlay2' requires kernel 4.7 to use on %s", backingFs)
174-
return nil, graphdriver.ErrIncompatibleFS
175-
}
176-
logger.Warn("Using pre-4.7.0 kernel for overlay2 on btrfs, may require kernel update")
177-
}
143+
if err := overlayutils.SupportsOverlay(testdir, true); err != nil {
144+
logger.Error(err)
145+
return nil, graphdriver.ErrNotSupported
178146
}
179147

180-
if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: 4, Major: 0, Minor: 0}) < 0 {
181-
if opts.overrideKernelCheck {
182-
logger.Warn("Using pre-4.0.0 kernel for overlay2, mount failures may require kernel update")
183-
} else {
184-
if err := supportsMultipleLowerDir(testdir); err != nil {
185-
logger.Debugf("Multiple lower dirs not supported: %v", err)
186-
return nil, graphdriver.ErrNotSupported
187-
}
188-
}
189-
}
190148
supportsDType, err := fsutils.SupportsDType(testdir)
191149
if err != nil {
192150
return nil, err
@@ -275,27 +233,6 @@ func parseOptions(options []string) (*overlayOptions, error) {
275233
return o, nil
276234
}
277235

278-
func supportsOverlay() error {
279-
// We can try to modprobe overlay first before looking at
280-
// proc/filesystems for when overlay is supported
281-
exec.Command("modprobe", "overlay").Run()
282-
283-
f, err := os.Open("/proc/filesystems")
284-
if err != nil {
285-
return err
286-
}
287-
defer f.Close()
288-
289-
s := bufio.NewScanner(f)
290-
for s.Scan() {
291-
if s.Text() == "nodev\toverlay" {
292-
return nil
293-
}
294-
}
295-
logger.Error("'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded.")
296-
return graphdriver.ErrNotSupported
297-
}
298-
299236
func useNaiveDiff(home string) bool {
300237
useNaiveDiffLock.Do(func() {
301238
if err := doesSupportNativeDiff(home); err != nil {

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ package overlayutils // import "github.com/docker/docker/daemon/graphdriver/over
44

55
import (
66
"fmt"
7+
"io/ioutil"
8+
"os"
9+
"path"
10+
"path/filepath"
711

812
"github.com/docker/docker/daemon/graphdriver"
13+
"github.com/pkg/errors"
14+
"github.com/sirupsen/logrus"
15+
"golang.org/x/sys/unix"
916
)
1017

1118
// ErrDTypeNotSupported denotes that the backing filesystem doesn't support d_type.
@@ -23,3 +30,40 @@ func ErrDTypeNotSupported(driver, backingFs string) error {
2330

2431
return graphdriver.NotSupportedError(msg)
2532
}
33+
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")
41+
if err != nil {
42+
return err
43+
}
44+
defer func() {
45+
if err := os.RemoveAll(td); err != nil {
46+
logrus.Warnf("Failed to remove check directory %v: %v", td, err)
47+
}
48+
}()
49+
50+
for _, dir := range []string{"lower1", "lower2", "upper", "work", "merged"} {
51+
if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil {
52+
return err
53+
}
54+
}
55+
56+
mnt := filepath.Join(td, "merged")
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"))
62+
if err := unix.Mount("overlay", mnt, "overlay", 0, opts); err != nil {
63+
return errors.Wrap(err, "failed to mount overlay")
64+
}
65+
if err := unix.Unmount(mnt, 0); err != nil {
66+
logrus.Warnf("Failed to unmount check directory %v: %v", mnt, err)
67+
}
68+
return nil
69+
}

0 commit comments

Comments
 (0)