mount.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // TODO(thockin): This whole pkg is pretty linux-centric. As soon as we have
  14. // an alternate platform, we will need to abstract further.
  15. package mount
  16. import (
  17. "fmt"
  18. "path"
  19. "path/filepath"
  20. "strings"
  21. "github.com/golang/glog"
  22. "k8s.io/kubernetes/pkg/util/exec"
  23. )
  24. type Interface interface {
  25. // Mount mounts source to target as fstype with given options.
  26. Mount(source string, target string, fstype string, options []string) error
  27. // Unmount unmounts given target.
  28. Unmount(target string) error
  29. // List returns a list of all mounted filesystems. This can be large.
  30. // On some platforms, reading mounts is not guaranteed consistent (i.e.
  31. // it could change between chunked reads). This is guaranteed to be
  32. // consistent.
  33. List() ([]MountPoint, error)
  34. // IsLikelyNotMountPoint determines if a directory is a mountpoint.
  35. // It should return ErrNotExist when the directory does not exist.
  36. IsLikelyNotMountPoint(file string) (bool, error)
  37. // DeviceOpened determines if the device is in use elsewhere
  38. // on the system, i.e. still mounted.
  39. DeviceOpened(pathname string) (bool, error)
  40. // PathIsDevice determines if a path is a device.
  41. PathIsDevice(pathname string) (bool, error)
  42. // GetDeviceNameFromMount finds the device name by checking the mount path
  43. // to get the global mount path which matches its plugin directory
  44. GetDeviceNameFromMount(mountPath, pluginDir string) (string, error)
  45. }
  46. // Compile-time check to ensure all Mounter implementations satisfy
  47. // the mount interface
  48. var _ Interface = &Mounter{}
  49. // This represents a single line in /proc/mounts or /etc/fstab.
  50. type MountPoint struct {
  51. Device string
  52. Path string
  53. Type string
  54. Opts []string
  55. Freq int
  56. Pass int
  57. }
  58. // SafeFormatAndMount probes a device to see if it is formatted.
  59. // Namely it checks to see if a file system is present. If so it
  60. // mounts it otherwise the device is formatted first then mounted.
  61. type SafeFormatAndMount struct {
  62. Interface
  63. Runner exec.Interface
  64. }
  65. // FormatAndMount formats the given disk, if needed, and mounts it.
  66. // That is if the disk is not formatted and it is not being mounted as
  67. // read-only it will format it first then mount it. Otherwise, if the
  68. // disk is already formatted or it is being mounted as read-only, it
  69. // will be mounted without formatting.
  70. func (mounter *SafeFormatAndMount) FormatAndMount(source string, target string, fstype string, options []string) error {
  71. // Don't attempt to format if mounting as readonly. Go straight to mounting.
  72. for _, option := range options {
  73. if option == "ro" {
  74. return mounter.Interface.Mount(source, target, fstype, options)
  75. }
  76. }
  77. return mounter.formatAndMount(source, target, fstype, options)
  78. }
  79. // New returns a mount.Interface for the current system.
  80. func New() Interface {
  81. return &Mounter{}
  82. }
  83. // GetMountRefs finds all other references to the device referenced
  84. // by mountPath; returns a list of paths.
  85. func GetMountRefs(mounter Interface, mountPath string) ([]string, error) {
  86. mps, err := mounter.List()
  87. if err != nil {
  88. return nil, err
  89. }
  90. // Find the device name.
  91. deviceName := ""
  92. // If mountPath is symlink, need get its target path.
  93. slTarget, err := filepath.EvalSymlinks(mountPath)
  94. if err != nil {
  95. slTarget = mountPath
  96. }
  97. for i := range mps {
  98. if mps[i].Path == slTarget {
  99. deviceName = mps[i].Device
  100. break
  101. }
  102. }
  103. // Find all references to the device.
  104. var refs []string
  105. if deviceName == "" {
  106. glog.Warningf("could not determine device for path: %q", mountPath)
  107. } else {
  108. for i := range mps {
  109. if mps[i].Device == deviceName && mps[i].Path != slTarget {
  110. refs = append(refs, mps[i].Path)
  111. }
  112. }
  113. }
  114. return refs, nil
  115. }
  116. // GetDeviceNameFromMount: given a mnt point, find the device from /proc/mounts
  117. // returns the device name, reference count, and error code
  118. func GetDeviceNameFromMount(mounter Interface, mountPath string) (string, int, error) {
  119. mps, err := mounter.List()
  120. if err != nil {
  121. return "", 0, err
  122. }
  123. // Find the device name.
  124. // FIXME if multiple devices mounted on the same mount path, only the first one is returned
  125. device := ""
  126. // If mountPath is symlink, need get its target path.
  127. slTarget, err := filepath.EvalSymlinks(mountPath)
  128. if err != nil {
  129. slTarget = mountPath
  130. }
  131. for i := range mps {
  132. if mps[i].Path == slTarget {
  133. device = mps[i].Device
  134. break
  135. }
  136. }
  137. // Find all references to the device.
  138. refCount := 0
  139. for i := range mps {
  140. if mps[i].Device == device {
  141. refCount++
  142. }
  143. }
  144. return device, refCount, nil
  145. }
  146. // getDeviceNameFromMount find the device name from /proc/mounts in which
  147. // the mount path reference should match the given plugin directory. In case no mount path reference
  148. // matches, returns the volume name taken from its given mountPath
  149. func getDeviceNameFromMount(mounter Interface, mountPath, pluginDir string) (string, error) {
  150. refs, err := GetMountRefs(mounter, mountPath)
  151. if err != nil {
  152. glog.V(4).Infof("GetMountRefs failed for mount path %q: %v", mountPath, err)
  153. return "", err
  154. }
  155. if len(refs) == 0 {
  156. glog.V(4).Infof("Directory %s is not mounted", mountPath)
  157. return "", fmt.Errorf("directory %s is not mounted", mountPath)
  158. }
  159. for _, ref := range refs {
  160. if strings.HasPrefix(ref, pluginDir) {
  161. return path.Base(ref), nil
  162. }
  163. }
  164. return path.Base(mountPath), nil
  165. }