Skip to content

Commit

Permalink
Merge pull request moby#48092 from thaJeztah/fsmagic_internal
Browse files Browse the repository at this point in the history
daemon/graphdriver: split, internalize packages to separate snapshotters and graphdrivers
  • Loading branch information
thaJeztah authored Jul 8, 2024
2 parents 7496f11 + 0f3273e commit c4dcaa0
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 191 deletions.
5 changes: 3 additions & 2 deletions daemon/graphdriver/btrfs/btrfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/containerd/containerd/pkg/userns"
"github.com/containerd/log"
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/daemon/internal/fstype"
"github.com/docker/docker/internal/containerfs"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/parsers"
Expand Down Expand Up @@ -68,12 +69,12 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
testdir = filepath.Dir(testdir)
}

fsMagic, err := graphdriver.GetFSMagic(testdir)
fsMagic, err := fstype.GetFSMagic(testdir)
if err != nil {
return nil, err
}

if fsMagic != graphdriver.FsMagicBtrfs {
if fsMagic != fstype.FsMagicBtrfs {
return nil, graphdriver.ErrPrerequisites
}

Expand Down
14 changes: 0 additions & 14 deletions daemon/graphdriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ import (
"github.com/vbatts/tar-split/tar/storage"
)

// FsMagic unsigned id of the filesystem in use.
type FsMagic uint32

const (
// FsMagicUnsupported is a predefined constant value other than a valid filesystem id.
FsMagicUnsupported = FsMagic(0x00000000)
)

// All registered drivers
var drivers map[string]InitFunc

Expand Down Expand Up @@ -136,12 +128,6 @@ type FileGetCloser interface {
Close() error
}

// Checker makes checks on specified filesystems.
type Checker interface {
// IsMounted returns true if the provided path is mounted for the specific checker
IsMounted(path string) bool
}

func init() {
drivers = make(map[string]InitFunc)
}
Expand Down
117 changes: 2 additions & 115 deletions daemon/graphdriver/driver_linux.go
Original file line number Diff line number Diff line change
@@ -1,117 +1,4 @@
package graphdriver // import "github.com/docker/docker/daemon/graphdriver"

import (
"github.com/moby/sys/mountinfo"
"golang.org/x/sys/unix"
)

const (
// FsMagicAufs filesystem id for Aufs
FsMagicAufs = FsMagic(0x61756673)
// FsMagicBtrfs filesystem id for Btrfs
FsMagicBtrfs = FsMagic(0x9123683E)
// FsMagicCramfs filesystem id for Cramfs
FsMagicCramfs = FsMagic(0x28cd3d45)
// FsMagicEcryptfs filesystem id for eCryptfs
FsMagicEcryptfs = FsMagic(0xf15f)
// FsMagicExtfs filesystem id for Extfs
FsMagicExtfs = FsMagic(0x0000EF53)
// FsMagicF2fs filesystem id for F2fs
FsMagicF2fs = FsMagic(0xF2F52010)
// FsMagicGPFS filesystem id for GPFS
FsMagicGPFS = FsMagic(0x47504653)
// FsMagicJffs2Fs filesystem if for Jffs2Fs
FsMagicJffs2Fs = FsMagic(0x000072b6)
// FsMagicJfs filesystem id for Jfs
FsMagicJfs = FsMagic(0x3153464a)
// FsMagicNfsFs filesystem id for NfsFs
FsMagicNfsFs = FsMagic(0x00006969)
// FsMagicRAMFs filesystem id for RamFs
FsMagicRAMFs = FsMagic(0x858458f6)
// FsMagicReiserFs filesystem id for ReiserFs
FsMagicReiserFs = FsMagic(0x52654973)
// FsMagicSmbFs filesystem id for SmbFs
FsMagicSmbFs = FsMagic(0x0000517B)
// FsMagicSquashFs filesystem id for SquashFs
FsMagicSquashFs = FsMagic(0x73717368)
// FsMagicTmpFs filesystem id for TmpFs
FsMagicTmpFs = FsMagic(0x01021994)
// FsMagicVxFS filesystem id for VxFs
FsMagicVxFS = FsMagic(0xa501fcf5)
// FsMagicXfs filesystem id for Xfs
FsMagicXfs = FsMagic(0x58465342)
// FsMagicZfs filesystem id for Zfs
FsMagicZfs = FsMagic(0x2fc12fc1)
// FsMagicOverlay filesystem id for overlay
FsMagicOverlay = FsMagic(0x794C7630)
// FsMagicFUSE filesystem id for FUSE
FsMagicFUSE = FsMagic(0x65735546)
)

var (
// List of drivers that should be used in an order
priority = "overlay2,fuse-overlayfs,btrfs,zfs,vfs"

// FsNames maps filesystem id to name of the filesystem.
FsNames = map[FsMagic]string{
FsMagicAufs: "aufs",
FsMagicBtrfs: "btrfs",
FsMagicCramfs: "cramfs",
FsMagicEcryptfs: "ecryptfs",
FsMagicExtfs: "extfs",
FsMagicF2fs: "f2fs",
FsMagicFUSE: "fuse",
FsMagicGPFS: "gpfs",
FsMagicJffs2Fs: "jffs2",
FsMagicJfs: "jfs",
FsMagicNfsFs: "nfs",
FsMagicOverlay: "overlayfs",
FsMagicRAMFs: "ramfs",
FsMagicReiserFs: "reiserfs",
FsMagicSmbFs: "smb",
FsMagicSquashFs: "squashfs",
FsMagicTmpFs: "tmpfs",
FsMagicUnsupported: "unsupported",
FsMagicVxFS: "vxfs",
FsMagicXfs: "xfs",
FsMagicZfs: "zfs",
}
)

// GetFSMagic returns the filesystem id given the path.
func GetFSMagic(rootpath string) (FsMagic, error) {
var buf unix.Statfs_t
if err := unix.Statfs(rootpath, &buf); err != nil {
return 0, err
}
return FsMagic(buf.Type), nil
}

// NewFsChecker returns a checker configured for the provided FsMagic
func NewFsChecker(t FsMagic) Checker {
return &fsChecker{
t: t,
}
}

type fsChecker struct {
t FsMagic
}

func (c *fsChecker) IsMounted(path string) bool {
fsType, _ := GetFSMagic(path)
return fsType == c.t
}

// NewDefaultChecker returns a check that parses /proc/mountinfo to check
// if the specified path is mounted.
func NewDefaultChecker() Checker {
return &defaultChecker{}
}

type defaultChecker struct{}

func (c *defaultChecker) IsMounted(path string) bool {
m, _ := mountinfo.Mounted(path)
return m
}
// List of drivers that should be used in an order
var priority = "overlay2,fuse-overlayfs,btrfs,zfs,vfs"
5 changes: 0 additions & 5 deletions daemon/graphdriver/driver_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@ package graphdriver // import "github.com/docker/docker/daemon/graphdriver"

// List of drivers that should be used in an order
var priority = "unsupported"

// GetFSMagic returns the filesystem id given the path.
func GetFSMagic(rootpath string) (FsMagic, error) {
return FsMagicUnsupported, nil
}
6 changes: 0 additions & 6 deletions daemon/graphdriver/driver_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,3 @@ package graphdriver // import "github.com/docker/docker/daemon/graphdriver"

// List of drivers that should be used in order
var priority = "windowsfilter"

// GetFSMagic returns the filesystem id given the path.
func GetFSMagic(rootpath string) (FsMagic, error) {
// Note it is OK to return FsMagicUnsupported on Windows.
return FsMagicUnsupported, nil
}
12 changes: 10 additions & 2 deletions daemon/graphdriver/fuse-overlayfs/fuseoverlayfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/containerd/log"
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/daemon/graphdriver/overlayutils"
"github.com/docker/docker/daemon/internal/fstype"
"github.com/docker/docker/daemon/internal/mountref"
"github.com/docker/docker/internal/containerfs"
"github.com/docker/docker/internal/directory"
"github.com/docker/docker/pkg/archive"
Expand Down Expand Up @@ -58,7 +60,7 @@ const (
type Driver struct {
home string
idMap idtools.IdentityMapping
ctr *graphdriver.RefCounter
ctr *mountref.Counter
naiveDiff graphdriver.DiffDriver
locker *locker.Locker
}
Expand Down Expand Up @@ -97,7 +99,7 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
d := &Driver{
home: home,
idMap: idMap,
ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicFUSE)),
ctr: mountref.NewCounter(isMounted),
locker: locker.New(),
}

Expand All @@ -106,6 +108,12 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
return d, nil
}

// isMounted checks whether the given path is a [fstype.FsMagicFUSE] mount.
func isMounted(path string) bool {
fsType, _ := fstype.GetFSMagic(path)
return fsType == fstype.FsMagicFUSE
}

func (d *Driver) String() string {
return driverName
}
Expand Down
16 changes: 12 additions & 4 deletions daemon/graphdriver/overlay2/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/containerd/log"
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/daemon/graphdriver/overlayutils"
"github.com/docker/docker/daemon/internal/fstype"
"github.com/docker/docker/daemon/internal/mountref"
"github.com/docker/docker/internal/containerfs"
"github.com/docker/docker/internal/directory"
"github.com/docker/docker/pkg/archive"
Expand Down Expand Up @@ -92,7 +94,7 @@ type overlayOptions struct {
type Driver struct {
home string
idMap idtools.IdentityMapping
ctr *graphdriver.RefCounter
ctr *mountref.Counter
quotaCtl *quota.Control
options overlayOptions
naiveDiff graphdriver.DiffDriver
Expand Down Expand Up @@ -142,11 +144,11 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
return nil, graphdriver.ErrNotSupported
}

fsMagic, err := graphdriver.GetFSMagic(testdir)
fsMagic, err := fstype.GetFSMagic(testdir)
if err != nil {
return nil, err
}
if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
if fsName, ok := fstype.FsNames[fsMagic]; ok {
backingFs = fsName
}

Expand Down Expand Up @@ -178,7 +180,7 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
d := &Driver{
home: home,
idMap: idMap,
ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)),
ctr: mountref.NewCounter(isMounted),
supportsDType: supportsDType,
usingMetacopy: usingMetacopy,
locker: locker.New(),
Expand Down Expand Up @@ -224,6 +226,12 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
return d, nil
}

// isMounted checks whether the given path is a [fstype.FsMagicOverlay] mount.
func isMounted(path string) bool {
fsType, _ := fstype.GetFSMagic(path)
return fsType == fstype.FsMagicOverlay
}

func parseOptions(options []string) (*overlayOptions, error) {
o := &overlayOptions{}
for _, option := range options {
Expand Down
17 changes: 9 additions & 8 deletions daemon/graphdriver/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/Microsoft/hcsshim/osversion"
"github.com/containerd/log"
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/daemon/internal/mountref"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/ioutils"
Expand Down Expand Up @@ -72,12 +73,6 @@ func init() {
}
}

type checker struct{}

func (c *checker) IsMounted(path string) bool {
return false
}

type storageOptions struct {
size uint64
}
Expand All @@ -86,7 +81,7 @@ type storageOptions struct {
type Driver struct {
// info stores the shim driver information
info hcsshim.DriverInfo
ctr *graphdriver.RefCounter
ctr *mountref.Counter
// it is safe for windows to use a cache here because it does not support
// restoring containers when the daemon dies.
cacheMu sync.Mutex
Expand Down Expand Up @@ -132,12 +127,18 @@ func InitFilter(home string, options []string, _ idtools.IdentityMapping) (graph
Flavour: filterDriver,
},
cache: make(map[string]string),
ctr: graphdriver.NewRefCounter(&checker{}),
ctr: mountref.NewCounter(isMounted),
defaultStorageOpts: opts,
}
return d, nil
}

// isMounted checks whether the given path is mounted. It always returns
// false for the WindowsFilter graphdriver.
func isMounted(string) bool {
return false
}

// String returns the string representation of a driver. This should match
// the name the graph driver has been registered with.
func (d *Driver) String() string {
Expand Down
12 changes: 10 additions & 2 deletions daemon/graphdriver/zfs/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/containerd/log"
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/daemon/internal/mountref"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/parsers"
zfs "github.com/mistifyio/go-zfs/v3"
Expand Down Expand Up @@ -118,12 +119,19 @@ func Init(base string, opt []string, idMap idtools.IdentityMapping) (graphdriver
options: options,
filesystemsCache: filesystemsCache,
idMap: idMap,
ctr: graphdriver.NewRefCounter(graphdriver.NewDefaultChecker()),
ctr: mountref.NewCounter(isMounted),
locker: locker.New(),
}
return graphdriver.NewNaiveDiffDriver(d, idMap), nil
}

// isMounted parses /proc/mountinfo to check whether the specified path
// is mounted.
func isMounted(path string) bool {
m, _ := mountinfo.Mounted(path)
return m
}

func parseOptions(opt []string) (zfsOptions, error) {
var options zfsOptions
options.fsName = ""
Expand Down Expand Up @@ -175,7 +183,7 @@ type Driver struct {
sync.Mutex // protects filesystem cache against concurrent access
filesystemsCache map[string]bool
idMap idtools.IdentityMapping
ctr *graphdriver.RefCounter
ctr *mountref.Counter
locker *locker.Locker
}

Expand Down
7 changes: 4 additions & 3 deletions daemon/graphdriver/zfs/zfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (

"github.com/containerd/log"
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/daemon/internal/fstype"
)

func checkRootdirFs(rootDir string) error {
fsMagic, err := graphdriver.GetFSMagic(rootDir)
fsMagic, err := fstype.GetFSMagic(rootDir)
if err != nil {
return err
}
backingFS := "unknown"
if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
if fsName, ok := fstype.FsNames[fsMagic]; ok {
backingFS = fsName
}

if fsMagic != graphdriver.FsMagicZfs {
if fsMagic != fstype.FsMagicZfs {
log.G(context.TODO()).WithField("root", rootDir).WithField("backingFS", backingFS).WithField("storage-driver", "zfs").Error("No zfs dataset found for root")
return graphdriver.ErrPrerequisites
}
Expand Down
Loading

0 comments on commit c4dcaa0

Please sign in to comment.