github.com/cdoern/storage@v1.12.13/drivers/driver_linux.go (about) 1 // +build linux 2 3 package graphdriver 4 5 import ( 6 "path/filepath" 7 8 "github.com/containers/storage/pkg/mount" 9 "golang.org/x/sys/unix" 10 ) 11 12 const ( 13 // FsMagicAufs filesystem id for Aufs 14 FsMagicAufs = FsMagic(0x61756673) 15 // FsMagicBtrfs filesystem id for Btrfs 16 FsMagicBtrfs = FsMagic(0x9123683E) 17 // FsMagicCramfs filesystem id for Cramfs 18 FsMagicCramfs = FsMagic(0x28cd3d45) 19 // FsMagicEcryptfs filesystem id for eCryptfs 20 FsMagicEcryptfs = FsMagic(0xf15f) 21 // FsMagicExtfs filesystem id for Extfs 22 FsMagicExtfs = FsMagic(0x0000EF53) 23 // FsMagicF2fs filesystem id for F2fs 24 FsMagicF2fs = FsMagic(0xF2F52010) 25 // FsMagicGPFS filesystem id for GPFS 26 FsMagicGPFS = FsMagic(0x47504653) 27 // FsMagicJffs2Fs filesystem if for Jffs2Fs 28 FsMagicJffs2Fs = FsMagic(0x000072b6) 29 // FsMagicJfs filesystem id for Jfs 30 FsMagicJfs = FsMagic(0x3153464a) 31 // FsMagicNfsFs filesystem id for NfsFs 32 FsMagicNfsFs = FsMagic(0x00006969) 33 // FsMagicRAMFs filesystem id for RamFs 34 FsMagicRAMFs = FsMagic(0x858458f6) 35 // FsMagicReiserFs filesystem id for ReiserFs 36 FsMagicReiserFs = FsMagic(0x52654973) 37 // FsMagicSmbFs filesystem id for SmbFs 38 FsMagicSmbFs = FsMagic(0x0000517B) 39 // FsMagicSquashFs filesystem id for SquashFs 40 FsMagicSquashFs = FsMagic(0x73717368) 41 // FsMagicTmpFs filesystem id for TmpFs 42 FsMagicTmpFs = FsMagic(0x01021994) 43 // FsMagicVxFS filesystem id for VxFs 44 FsMagicVxFS = FsMagic(0xa501fcf5) 45 // FsMagicXfs filesystem id for Xfs 46 FsMagicXfs = FsMagic(0x58465342) 47 // FsMagicZfs filesystem id for Zfs 48 FsMagicZfs = FsMagic(0x2fc12fc1) 49 // FsMagicOverlay filesystem id for overlay 50 FsMagicOverlay = FsMagic(0x794C7630) 51 ) 52 53 var ( 54 // Slice of drivers that should be used in an order 55 priority = []string{ 56 "overlay", 57 // We don't support devicemapper without configuration 58 // "devicemapper", 59 "aufs", 60 "btrfs", 61 "zfs", 62 "vfs", 63 } 64 65 // FsNames maps filesystem id to name of the filesystem. 66 FsNames = map[FsMagic]string{ 67 FsMagicAufs: "aufs", 68 FsMagicBtrfs: "btrfs", 69 FsMagicCramfs: "cramfs", 70 FsMagicEcryptfs: "ecryptfs", 71 FsMagicExtfs: "extfs", 72 FsMagicF2fs: "f2fs", 73 FsMagicGPFS: "gpfs", 74 FsMagicJffs2Fs: "jffs2", 75 FsMagicJfs: "jfs", 76 FsMagicNfsFs: "nfs", 77 FsMagicOverlay: "overlayfs", 78 FsMagicRAMFs: "ramfs", 79 FsMagicReiserFs: "reiserfs", 80 FsMagicSmbFs: "smb", 81 FsMagicSquashFs: "squashfs", 82 FsMagicTmpFs: "tmpfs", 83 FsMagicUnsupported: "unsupported", 84 FsMagicVxFS: "vxfs", 85 FsMagicXfs: "xfs", 86 FsMagicZfs: "zfs", 87 } 88 ) 89 90 // GetFSMagic returns the filesystem id given the path. 91 func GetFSMagic(rootpath string) (FsMagic, error) { 92 var buf unix.Statfs_t 93 if err := unix.Statfs(filepath.Dir(rootpath), &buf); err != nil { 94 return 0, err 95 } 96 return FsMagic(buf.Type), nil 97 } 98 99 // NewFsChecker returns a checker configured for the provided FsMagic 100 func NewFsChecker(t FsMagic) Checker { 101 return &fsChecker{ 102 t: t, 103 } 104 } 105 106 type fsChecker struct { 107 t FsMagic 108 } 109 110 func (c *fsChecker) IsMounted(path string) bool { 111 m, _ := Mounted(c.t, path) 112 return m 113 } 114 115 // NewDefaultChecker returns a check that parses /proc/mountinfo to check 116 // if the specified path is mounted. 117 func NewDefaultChecker() Checker { 118 return &defaultChecker{} 119 } 120 121 type defaultChecker struct { 122 } 123 124 func (c *defaultChecker) IsMounted(path string) bool { 125 m, _ := mount.Mounted(path) 126 return m 127 } 128 129 // Mounted checks if the given path is mounted as the fs type 130 func Mounted(fsType FsMagic, mountPath string) (bool, error) { 131 var buf unix.Statfs_t 132 if err := unix.Statfs(mountPath, &buf); err != nil { 133 return false, err 134 } 135 return FsMagic(buf.Type) == fsType, nil 136 }