mount_helper_unix.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // +build !windows
  2. /*
  3. Copyright 2019 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package mount
  15. import (
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "syscall"
  21. utilio "k8s.io/utils/io"
  22. )
  23. const (
  24. // At least number of fields per line in /proc/<pid>/mountinfo.
  25. expectedAtLeastNumFieldsPerMountInfo = 10
  26. // How many times to retry for a consistent read of /proc/mounts.
  27. maxListTries = 3
  28. )
  29. // IsCorruptedMnt return true if err is about corrupted mount point
  30. func IsCorruptedMnt(err error) bool {
  31. if err == nil {
  32. return false
  33. }
  34. var underlyingError error
  35. switch pe := err.(type) {
  36. case nil:
  37. return false
  38. case *os.PathError:
  39. underlyingError = pe.Err
  40. case *os.LinkError:
  41. underlyingError = pe.Err
  42. case *os.SyscallError:
  43. underlyingError = pe.Err
  44. }
  45. return underlyingError == syscall.ENOTCONN || underlyingError == syscall.ESTALE || underlyingError == syscall.EIO || underlyingError == syscall.EACCES
  46. }
  47. // MountInfo represents a single line in /proc/<pid>/mountinfo.
  48. type MountInfo struct {
  49. // Unique ID for the mount (maybe reused after umount).
  50. ID int
  51. // The ID of the parent mount (or of self for the root of this mount namespace's mount tree).
  52. ParentID int
  53. // The value of `st_dev` for files on this filesystem.
  54. MajorMinor string
  55. // The pathname of the directory in the filesystem which forms the root of this mount.
  56. Root string
  57. // Mount source, filesystem-specific information. e.g. device, tmpfs name.
  58. Source string
  59. // Mount point, the pathname of the mount point.
  60. MountPoint string
  61. // Optional fieds, zero or more fields of the form "tag[:value]".
  62. OptionalFields []string
  63. // The filesystem type in the form "type[.subtype]".
  64. FsType string
  65. // Per-mount options.
  66. MountOptions []string
  67. // Per-superblock options.
  68. SuperOptions []string
  69. }
  70. // ParseMountInfo parses /proc/xxx/mountinfo.
  71. func ParseMountInfo(filename string) ([]MountInfo, error) {
  72. content, err := utilio.ConsistentRead(filename, maxListTries)
  73. if err != nil {
  74. return []MountInfo{}, err
  75. }
  76. contentStr := string(content)
  77. infos := []MountInfo{}
  78. for _, line := range strings.Split(contentStr, "\n") {
  79. if line == "" {
  80. // the last split() item is empty string following the last \n
  81. continue
  82. }
  83. // See `man proc` for authoritative description of format of the file.
  84. fields := strings.Fields(line)
  85. if len(fields) < expectedAtLeastNumFieldsPerMountInfo {
  86. return nil, fmt.Errorf("wrong number of fields in (expected at least %d, got %d): %s", expectedAtLeastNumFieldsPerMountInfo, len(fields), line)
  87. }
  88. id, err := strconv.Atoi(fields[0])
  89. if err != nil {
  90. return nil, err
  91. }
  92. parentID, err := strconv.Atoi(fields[1])
  93. if err != nil {
  94. return nil, err
  95. }
  96. info := MountInfo{
  97. ID: id,
  98. ParentID: parentID,
  99. MajorMinor: fields[2],
  100. Root: fields[3],
  101. MountPoint: fields[4],
  102. MountOptions: strings.Split(fields[5], ","),
  103. }
  104. // All fields until "-" are "optional fields".
  105. i := 6
  106. for ; i < len(fields) && fields[i] != "-"; i++ {
  107. info.OptionalFields = append(info.OptionalFields, fields[i])
  108. }
  109. // Parse the rest 3 fields.
  110. i++
  111. if len(fields)-i < 3 {
  112. return nil, fmt.Errorf("expect 3 fields in %s, got %d", line, len(fields)-i)
  113. }
  114. info.FsType = fields[i]
  115. info.Source = fields[i+1]
  116. info.SuperOptions = strings.Split(fields[i+2], ",")
  117. infos = append(infos, info)
  118. }
  119. return infos, nil
  120. }
  121. // isMountPointMatch returns true if the path in mp is the same as dir.
  122. // Handles case where mountpoint dir has been renamed due to stale NFS mount.
  123. func isMountPointMatch(mp MountPoint, dir string) bool {
  124. deletedDir := fmt.Sprintf("%s\\040(deleted)", dir)
  125. return ((mp.Path == dir) || (mp.Path == deletedDir))
  126. }