github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/graphdriver/driver_linux.go (about)

     1  package graphdriver // import "github.com/demonoid81/moby/daemon/graphdriver"
     2  
     3  import (
     4  	"github.com/moby/sys/mountinfo"
     5  	"golang.org/x/sys/unix"
     6  )
     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 = "btrfs,zfs,overlay2,fuse-overlayfs,aufs,overlay,devicemapper,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  }
    89  
    90  // NewFsChecker returns a checker configured for the provided FsMagic
    91  func NewFsChecker(t FsMagic) Checker {
    92  	return &fsChecker{
    93  		t: t,
    94  	}
    95  }
    96  
    97  type fsChecker struct {
    98  	t FsMagic
    99  }
   100  
   101  func (c *fsChecker) IsMounted(path string) bool {
   102  	m, _ := Mounted(c.t, path)
   103  	return m
   104  }
   105  
   106  // NewDefaultChecker returns a check that parses /proc/mountinfo to check
   107  // if the specified path is mounted.
   108  func NewDefaultChecker() Checker {
   109  	return &defaultChecker{}
   110  }
   111  
   112  type defaultChecker struct {
   113  }
   114  
   115  func (c *defaultChecker) IsMounted(path string) bool {
   116  	m, _ := mountinfo.Mounted(path)
   117  	return m
   118  }
   119  
   120  // Mounted checks if the given path is mounted as the fs type
   121  func Mounted(fsType FsMagic, mountPath string) (bool, error) {
   122  	var buf unix.Statfs_t
   123  	if err := unix.Statfs(mountPath, &buf); err != nil {
   124  		if err == unix.ENOENT { // not exist, thus not mounted
   125  			err = nil
   126  		}
   127  		return false, err
   128  	}
   129  	return FsMagic(buf.Type) == fsType, nil
   130  }