mount.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // Mounted returns true if a mount point exists.
  13. func Mounted(mountpoint string) (bool, error) {
  14. mntpoint, err := os.Stat(mountpoint)
  15. if err != nil {
  16. if os.IsNotExist(err) {
  17. return false, nil
  18. }
  19. return false, err
  20. }
  21. parent, err := os.Stat(filepath.Join(mountpoint, ".."))
  22. if err != nil {
  23. return false, err
  24. }
  25. mntpointSt := mntpoint.Sys().(*syscall.Stat_t)
  26. parentSt := parent.Sys().(*syscall.Stat_t)
  27. return mntpointSt.Dev != parentSt.Dev, nil
  28. }
  29. type probeData struct {
  30. fsName string
  31. magic string
  32. offset uint64
  33. }
  34. // ProbeFsType returns the filesystem name for the given device id.
  35. func ProbeFsType(device string) (string, error) {
  36. probes := []probeData{
  37. {"btrfs", "_BHRfS_M", 0x10040},
  38. {"ext4", "\123\357", 0x438},
  39. {"xfs", "XFSB", 0},
  40. }
  41. maxLen := uint64(0)
  42. for _, p := range probes {
  43. l := p.offset + uint64(len(p.magic))
  44. if l > maxLen {
  45. maxLen = l
  46. }
  47. }
  48. file, err := os.Open(device)
  49. if err != nil {
  50. return "", err
  51. }
  52. defer file.Close()
  53. buffer := make([]byte, maxLen)
  54. l, err := file.Read(buffer)
  55. if err != nil {
  56. return "", err
  57. }
  58. if uint64(l) != maxLen {
  59. return "", fmt.Errorf("devmapper: unable to detect filesystem type of %s, short read", device)
  60. }
  61. for _, p := range probes {
  62. if bytes.Equal([]byte(p.magic), buffer[p.offset:p.offset+uint64(len(p.magic))]) {
  63. return p.fsName, nil
  64. }
  65. }
  66. return "", fmt.Errorf("devmapper: Unknown filesystem type on %s", device)
  67. }
  68. func joinMountOptions(a, b string) string {
  69. if a == "" {
  70. return b
  71. }
  72. if b == "" {
  73. return a
  74. }
  75. return a + "," + b
  76. }