driver_linux.go 3.4 KB

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