driver_linux.go 3.4 KB

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