Browse code

daemon/graphdriver: move FsMagic utilities to an internal package

These utilities were used in both graphdrivers and snapshotters. Move them
to a separate package, to help decoupling snapshotters and graphdrivers,
and make it internal, as it's not intended to be used as a generic utility
package (we can still make it public if there would be a need).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2024/06/30 05:37:25
Showing 11 changed files
... ...
@@ -37,6 +37,7 @@ import (
37 37
 	"github.com/containerd/containerd/pkg/userns"
38 38
 	"github.com/containerd/log"
39 39
 	"github.com/docker/docker/daemon/graphdriver"
40
+	"github.com/docker/docker/daemon/internal/fstype"
40 41
 	"github.com/docker/docker/internal/containerfs"
41 42
 	"github.com/docker/docker/pkg/idtools"
42 43
 	"github.com/docker/docker/pkg/parsers"
... ...
@@ -68,12 +69,12 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
68 68
 		testdir = filepath.Dir(testdir)
69 69
 	}
70 70
 
71
-	fsMagic, err := graphdriver.GetFSMagic(testdir)
71
+	fsMagic, err := fstype.GetFSMagic(testdir)
72 72
 	if err != nil {
73 73
 		return nil, err
74 74
 	}
75 75
 
76
-	if fsMagic != graphdriver.FsMagicBtrfs {
76
+	if fsMagic != fstype.FsMagicBtrfs {
77 77
 		return nil, graphdriver.ErrPrerequisites
78 78
 	}
79 79
 
... ...
@@ -15,14 +15,6 @@ import (
15 15
 	"github.com/vbatts/tar-split/tar/storage"
16 16
 )
17 17
 
18
-// FsMagic unsigned id of the filesystem in use.
19
-type FsMagic uint32
20
-
21
-const (
22
-	// FsMagicUnsupported is a predefined constant value other than a valid filesystem id.
23
-	FsMagicUnsupported = FsMagic(0x00000000)
24
-)
25
-
26 18
 // All registered drivers
27 19
 var drivers map[string]InitFunc
28 20
 
... ...
@@ -1,105 +1,26 @@
1 1
 package graphdriver // import "github.com/docker/docker/daemon/graphdriver"
2 2
 
3 3
 import (
4
+	"github.com/docker/docker/daemon/internal/fstype"
4 5
 	"github.com/moby/sys/mountinfo"
5
-	"golang.org/x/sys/unix"
6 6
 )
7 7
 
8
-const (
9
-	// FsMagicAufs filesystem id for Aufs
10
-	FsMagicAufs = FsMagic(0x61756673)
11
-	// FsMagicBtrfs filesystem id for Btrfs
12
-	FsMagicBtrfs = FsMagic(0x9123683E)
13
-	// FsMagicCramfs filesystem id for Cramfs
14
-	FsMagicCramfs = FsMagic(0x28cd3d45)
15
-	// FsMagicEcryptfs filesystem id for eCryptfs
16
-	FsMagicEcryptfs = FsMagic(0xf15f)
17
-	// FsMagicExtfs filesystem id for Extfs
18
-	FsMagicExtfs = FsMagic(0x0000EF53)
19
-	// FsMagicF2fs filesystem id for F2fs
20
-	FsMagicF2fs = FsMagic(0xF2F52010)
21
-	// FsMagicGPFS filesystem id for GPFS
22
-	FsMagicGPFS = FsMagic(0x47504653)
23
-	// FsMagicJffs2Fs filesystem if for Jffs2Fs
24
-	FsMagicJffs2Fs = FsMagic(0x000072b6)
25
-	// FsMagicJfs filesystem id for Jfs
26
-	FsMagicJfs = FsMagic(0x3153464a)
27
-	// FsMagicNfsFs filesystem id for NfsFs
28
-	FsMagicNfsFs = FsMagic(0x00006969)
29
-	// FsMagicRAMFs filesystem id for RamFs
30
-	FsMagicRAMFs = FsMagic(0x858458f6)
31
-	// FsMagicReiserFs filesystem id for ReiserFs
32
-	FsMagicReiserFs = FsMagic(0x52654973)
33
-	// FsMagicSmbFs filesystem id for SmbFs
34
-	FsMagicSmbFs = FsMagic(0x0000517B)
35
-	// FsMagicSquashFs filesystem id for SquashFs
36
-	FsMagicSquashFs = FsMagic(0x73717368)
37
-	// FsMagicTmpFs filesystem id for TmpFs
38
-	FsMagicTmpFs = FsMagic(0x01021994)
39
-	// FsMagicVxFS filesystem id for VxFs
40
-	FsMagicVxFS = FsMagic(0xa501fcf5)
41
-	// FsMagicXfs filesystem id for Xfs
42
-	FsMagicXfs = FsMagic(0x58465342)
43
-	// FsMagicZfs filesystem id for Zfs
44
-	FsMagicZfs = FsMagic(0x2fc12fc1)
45
-	// FsMagicOverlay filesystem id for overlay
46
-	FsMagicOverlay = FsMagic(0x794C7630)
47
-	// FsMagicFUSE filesystem id for FUSE
48
-	FsMagicFUSE = FsMagic(0x65735546)
49
-)
50
-
51
-var (
52
-	// List of drivers that should be used in an order
53
-	priority = "overlay2,fuse-overlayfs,btrfs,zfs,vfs"
54
-
55
-	// FsNames maps filesystem id to name of the filesystem.
56
-	FsNames = map[FsMagic]string{
57
-		FsMagicAufs:        "aufs",
58
-		FsMagicBtrfs:       "btrfs",
59
-		FsMagicCramfs:      "cramfs",
60
-		FsMagicEcryptfs:    "ecryptfs",
61
-		FsMagicExtfs:       "extfs",
62
-		FsMagicF2fs:        "f2fs",
63
-		FsMagicFUSE:        "fuse",
64
-		FsMagicGPFS:        "gpfs",
65
-		FsMagicJffs2Fs:     "jffs2",
66
-		FsMagicJfs:         "jfs",
67
-		FsMagicNfsFs:       "nfs",
68
-		FsMagicOverlay:     "overlayfs",
69
-		FsMagicRAMFs:       "ramfs",
70
-		FsMagicReiserFs:    "reiserfs",
71
-		FsMagicSmbFs:       "smb",
72
-		FsMagicSquashFs:    "squashfs",
73
-		FsMagicTmpFs:       "tmpfs",
74
-		FsMagicUnsupported: "unsupported",
75
-		FsMagicVxFS:        "vxfs",
76
-		FsMagicXfs:         "xfs",
77
-		FsMagicZfs:         "zfs",
78
-	}
79
-)
80
-
81
-// GetFSMagic returns the filesystem id given the path.
82
-func GetFSMagic(rootpath string) (FsMagic, error) {
83
-	var buf unix.Statfs_t
84
-	if err := unix.Statfs(rootpath, &buf); err != nil {
85
-		return 0, err
86
-	}
87
-	return FsMagic(buf.Type), nil
88
-}
8
+// List of drivers that should be used in an order
9
+var priority = "overlay2,fuse-overlayfs,btrfs,zfs,vfs"
89 10
 
90 11
 // NewFsChecker returns a checker configured for the provided FsMagic
91
-func NewFsChecker(t FsMagic) Checker {
12
+func NewFsChecker(t fstype.FsMagic) Checker {
92 13
 	return &fsChecker{
93 14
 		t: t,
94 15
 	}
95 16
 }
96 17
 
97 18
 type fsChecker struct {
98
-	t FsMagic
19
+	t fstype.FsMagic
99 20
 }
100 21
 
101 22
 func (c *fsChecker) IsMounted(path string) bool {
102
-	fsType, _ := GetFSMagic(path)
23
+	fsType, _ := fstype.GetFSMagic(path)
103 24
 	return fsType == c.t
104 25
 }
105 26
 
... ...
@@ -4,8 +4,3 @@ package graphdriver // import "github.com/docker/docker/daemon/graphdriver"
4 4
 
5 5
 // List of drivers that should be used in an order
6 6
 var priority = "unsupported"
7
-
8
-// GetFSMagic returns the filesystem id given the path.
9
-func GetFSMagic(rootpath string) (FsMagic, error) {
10
-	return FsMagicUnsupported, nil
11
-}
... ...
@@ -2,9 +2,3 @@ package graphdriver // import "github.com/docker/docker/daemon/graphdriver"
2 2
 
3 3
 // List of drivers that should be used in order
4 4
 var priority = "windowsfilter"
5
-
6
-// GetFSMagic returns the filesystem id given the path.
7
-func GetFSMagic(rootpath string) (FsMagic, error) {
8
-	// Note it is OK to return FsMagicUnsupported on Windows.
9
-	return FsMagicUnsupported, nil
10
-}
... ...
@@ -17,6 +17,7 @@ import (
17 17
 	"github.com/containerd/log"
18 18
 	"github.com/docker/docker/daemon/graphdriver"
19 19
 	"github.com/docker/docker/daemon/graphdriver/overlayutils"
20
+	"github.com/docker/docker/daemon/internal/fstype"
20 21
 	"github.com/docker/docker/internal/containerfs"
21 22
 	"github.com/docker/docker/pkg/archive"
22 23
 	"github.com/docker/docker/pkg/chrootarchive"
... ...
@@ -97,7 +98,7 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
97 97
 	d := &Driver{
98 98
 		home:   home,
99 99
 		idMap:  idMap,
100
-		ctr:    graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicFUSE)),
100
+		ctr:    graphdriver.NewRefCounter(graphdriver.NewFsChecker(fstype.FsMagicFUSE)),
101 101
 		locker: locker.New(),
102 102
 	}
103 103
 
... ...
@@ -19,6 +19,7 @@ import (
19 19
 	"github.com/containerd/log"
20 20
 	"github.com/docker/docker/daemon/graphdriver"
21 21
 	"github.com/docker/docker/daemon/graphdriver/overlayutils"
22
+	"github.com/docker/docker/daemon/internal/fstype"
22 23
 	"github.com/docker/docker/internal/containerfs"
23 24
 	"github.com/docker/docker/pkg/archive"
24 25
 	"github.com/docker/docker/pkg/chrootarchive"
... ...
@@ -142,11 +143,11 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
142 142
 		return nil, graphdriver.ErrNotSupported
143 143
 	}
144 144
 
145
-	fsMagic, err := graphdriver.GetFSMagic(testdir)
145
+	fsMagic, err := fstype.GetFSMagic(testdir)
146 146
 	if err != nil {
147 147
 		return nil, err
148 148
 	}
149
-	if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
149
+	if fsName, ok := fstype.FsNames[fsMagic]; ok {
150 150
 		backingFs = fsName
151 151
 	}
152 152
 
... ...
@@ -178,7 +179,7 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
178 178
 	d := &Driver{
179 179
 		home:          home,
180 180
 		idMap:         idMap,
181
-		ctr:           graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)),
181
+		ctr:           graphdriver.NewRefCounter(graphdriver.NewFsChecker(fstype.FsMagicOverlay)),
182 182
 		supportsDType: supportsDType,
183 183
 		usingMetacopy: usingMetacopy,
184 184
 		locker:        locker.New(),
... ...
@@ -5,19 +5,20 @@ import (
5 5
 
6 6
 	"github.com/containerd/log"
7 7
 	"github.com/docker/docker/daemon/graphdriver"
8
+	"github.com/docker/docker/daemon/internal/fstype"
8 9
 )
9 10
 
10 11
 func checkRootdirFs(rootDir string) error {
11
-	fsMagic, err := graphdriver.GetFSMagic(rootDir)
12
+	fsMagic, err := fstype.GetFSMagic(rootDir)
12 13
 	if err != nil {
13 14
 		return err
14 15
 	}
15 16
 	backingFS := "unknown"
16
-	if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
17
+	if fsName, ok := fstype.FsNames[fsMagic]; ok {
17 18
 		backingFS = fsName
18 19
 	}
19 20
 
20
-	if fsMagic != graphdriver.FsMagicZfs {
21
+	if fsMagic != fstype.FsMagicZfs {
21 22
 		log.G(context.TODO()).WithField("root", rootDir).WithField("backingFS", backingFS).WithField("storage-driver", "zfs").Error("No zfs dataset found for root")
22 23
 		return graphdriver.ErrPrerequisites
23 24
 	}
24 25
new file mode 100644
... ...
@@ -0,0 +1,15 @@
0
+package fstype
1
+
2
+// FsMagic unsigned id of the filesystem in use.
3
+type FsMagic uint32
4
+
5
+// FsMagicUnsupported is a predefined constant value other than a valid filesystem id.
6
+const FsMagicUnsupported = FsMagic(0x00000000)
7
+
8
+// GetFSMagic returns the filesystem id given the path. It returns an error
9
+// when failing to detect the filesystem. it returns [FsMagicUnsupported]
10
+// if detection is not supported by the platform, but no error is returned
11
+// in this case.
12
+func GetFSMagic(rootpath string) (FsMagic, error) {
13
+	return getFSMagic(rootpath)
14
+}
0 15
new file mode 100644
... ...
@@ -0,0 +1,82 @@
0
+package fstype
1
+
2
+import "golang.org/x/sys/unix"
3
+
4
+const (
5
+	// FsMagicAufs filesystem id for Aufs
6
+	FsMagicAufs = FsMagic(0x61756673)
7
+	// FsMagicBtrfs filesystem id for Btrfs
8
+	FsMagicBtrfs = FsMagic(0x9123683E)
9
+	// FsMagicCramfs filesystem id for Cramfs
10
+	FsMagicCramfs = FsMagic(0x28cd3d45)
11
+	// FsMagicEcryptfs filesystem id for eCryptfs
12
+	FsMagicEcryptfs = FsMagic(0xf15f)
13
+	// FsMagicExtfs filesystem id for Extfs
14
+	FsMagicExtfs = FsMagic(0x0000EF53)
15
+	// FsMagicF2fs filesystem id for F2fs
16
+	FsMagicF2fs = FsMagic(0xF2F52010)
17
+	// FsMagicGPFS filesystem id for GPFS
18
+	FsMagicGPFS = FsMagic(0x47504653)
19
+	// FsMagicJffs2Fs filesystem if for Jffs2Fs
20
+	FsMagicJffs2Fs = FsMagic(0x000072b6)
21
+	// FsMagicJfs filesystem id for Jfs
22
+	FsMagicJfs = FsMagic(0x3153464a)
23
+	// FsMagicNfsFs filesystem id for NfsFs
24
+	FsMagicNfsFs = FsMagic(0x00006969)
25
+	// FsMagicRAMFs filesystem id for RamFs
26
+	FsMagicRAMFs = FsMagic(0x858458f6)
27
+	// FsMagicReiserFs filesystem id for ReiserFs
28
+	FsMagicReiserFs = FsMagic(0x52654973)
29
+	// FsMagicSmbFs filesystem id for SmbFs
30
+	FsMagicSmbFs = FsMagic(0x0000517B)
31
+	// FsMagicSquashFs filesystem id for SquashFs
32
+	FsMagicSquashFs = FsMagic(0x73717368)
33
+	// FsMagicTmpFs filesystem id for TmpFs
34
+	FsMagicTmpFs = FsMagic(0x01021994)
35
+	// FsMagicVxFS filesystem id for VxFs
36
+	FsMagicVxFS = FsMagic(0xa501fcf5)
37
+	// FsMagicXfs filesystem id for Xfs
38
+	FsMagicXfs = FsMagic(0x58465342)
39
+	// FsMagicZfs filesystem id for Zfs
40
+	FsMagicZfs = FsMagic(0x2fc12fc1)
41
+	// FsMagicOverlay filesystem id for overlay
42
+	FsMagicOverlay = FsMagic(0x794C7630)
43
+	// FsMagicFUSE filesystem id for FUSE
44
+	FsMagicFUSE = FsMagic(0x65735546)
45
+)
46
+
47
+var (
48
+	// FsNames maps filesystem id to name of the filesystem.
49
+	FsNames = map[FsMagic]string{
50
+		FsMagicAufs:        "aufs",
51
+		FsMagicBtrfs:       "btrfs",
52
+		FsMagicCramfs:      "cramfs",
53
+		FsMagicEcryptfs:    "ecryptfs",
54
+		FsMagicExtfs:       "extfs",
55
+		FsMagicF2fs:        "f2fs",
56
+		FsMagicFUSE:        "fuse",
57
+		FsMagicGPFS:        "gpfs",
58
+		FsMagicJffs2Fs:     "jffs2",
59
+		FsMagicJfs:         "jfs",
60
+		FsMagicNfsFs:       "nfs",
61
+		FsMagicOverlay:     "overlayfs",
62
+		FsMagicRAMFs:       "ramfs",
63
+		FsMagicReiserFs:    "reiserfs",
64
+		FsMagicSmbFs:       "smb",
65
+		FsMagicSquashFs:    "squashfs",
66
+		FsMagicTmpFs:       "tmpfs",
67
+		FsMagicUnsupported: "unsupported",
68
+		FsMagicVxFS:        "vxfs",
69
+		FsMagicXfs:         "xfs",
70
+		FsMagicZfs:         "zfs",
71
+	}
72
+)
73
+
74
+// getFSMagic returns the filesystem id given the path.
75
+func getFSMagic(rootpath string) (FsMagic, error) {
76
+	var buf unix.Statfs_t
77
+	if err := unix.Statfs(rootpath, &buf); err != nil {
78
+		return 0, err
79
+	}
80
+	return FsMagic(buf.Type), nil
81
+}
0 82
new file mode 100644
... ...
@@ -0,0 +1,8 @@
0
+//go:build !linux
1
+
2
+package fstype
3
+
4
+// getFSMagic returns the filesystem id given the path.
5
+func getFSMagic(rootpath string) (FsMagic, error) {
6
+	return FsMagicUnsupported, nil
7
+}