mount.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // +build linux
  2. package devmapper
  3. import (
  4. "bytes"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "syscall"
  9. )
  10. // FIXME: this is copy-pasted from the aufs driver.
  11. // It should be moved into the core.
  12. func Mounted(mountpoint string) (bool, error) {
  13. mntpoint, err := os.Stat(mountpoint)
  14. if err != nil {
  15. if os.IsNotExist(err) {
  16. return false, nil
  17. }
  18. return false, err
  19. }
  20. parent, err := os.Stat(filepath.Join(mountpoint, ".."))
  21. if err != nil {
  22. return false, err
  23. }
  24. mntpointSt := mntpoint.Sys().(*syscall.Stat_t)
  25. parentSt := parent.Sys().(*syscall.Stat_t)
  26. return mntpointSt.Dev != parentSt.Dev, nil
  27. }
  28. type probeData struct {
  29. fsName string
  30. magic string
  31. offset uint64
  32. }
  33. func ProbeFsType(device string) (string, error) {
  34. probes := []probeData{
  35. {"btrfs", "_BHRfS_M", 0x10040},
  36. {"ext4", "\123\357", 0x438},
  37. {"xfs", "XFSB", 0},
  38. }
  39. maxLen := uint64(0)
  40. for _, p := range probes {
  41. l := p.offset + uint64(len(p.magic))
  42. if l > maxLen {
  43. maxLen = l
  44. }
  45. }
  46. file, err := os.Open(device)
  47. if err != nil {
  48. return "", err
  49. }
  50. buffer := make([]byte, maxLen)
  51. l, err := file.Read(buffer)
  52. if err != nil {
  53. return "", err
  54. }
  55. file.Close()
  56. if uint64(l) != maxLen {
  57. return "", fmt.Errorf("unable to detect filesystem type of %s, short read", device)
  58. }
  59. for _, p := range probes {
  60. if bytes.Equal([]byte(p.magic), buffer[p.offset:p.offset+uint64(len(p.magic))]) {
  61. return p.fsName, nil
  62. }
  63. }
  64. return "", fmt.Errorf("Unknown filesystem type on %s", device)
  65. }
  66. func joinMountOptions(a, b string) string {
  67. if a == "" {
  68. return b
  69. }
  70. if b == "" {
  71. return a
  72. }
  73. return a + "," + b
  74. }