mount.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. "os"
  19. "path/filepath"
  20. "strings"
  21. utilexec "k8s.io/utils/exec"
  22. )
  23. const (
  24. // Default mount command if mounter path is not specified.
  25. defaultMountCommand = "mount"
  26. )
  27. // Interface defines the set of methods to allow for mount operations on a system.
  28. type Interface interface {
  29. // Mount mounts source to target as fstype with given options.
  30. Mount(source string, target string, fstype string, options []string) error
  31. // Unmount unmounts given target.
  32. Unmount(target string) error
  33. // List returns a list of all mounted filesystems. This can be large.
  34. // On some platforms, reading mounts directly from the OS is not guaranteed
  35. // consistent (i.e. it could change between chunked reads). This is guaranteed
  36. // to be consistent.
  37. List() ([]MountPoint, error)
  38. // IsLikelyNotMountPoint uses heuristics to determine if a directory
  39. // is not a mountpoint.
  40. // It should return ErrNotExist when the directory does not exist.
  41. // IsLikelyNotMountPoint does NOT properly detect all mountpoint types
  42. // most notably linux bind mounts and symbolic link. For callers that do not
  43. // care about such situations, this is a faster alternative to calling List()
  44. // and scanning that output.
  45. IsLikelyNotMountPoint(file string) (bool, error)
  46. // GetMountRefs finds all mount references to pathname, returning a slice of
  47. // paths. Pathname can be a mountpoint path or a normal directory
  48. // (for bind mount). On Linux, pathname is excluded from the slice.
  49. // For example, if /dev/sdc was mounted at /path/a and /path/b,
  50. // GetMountRefs("/path/a") would return ["/path/b"]
  51. // GetMountRefs("/path/b") would return ["/path/a"]
  52. // On Windows there is no way to query all mount points; as long as pathname is
  53. // a valid mount, it will be returned.
  54. GetMountRefs(pathname string) ([]string, error)
  55. }
  56. // Compile-time check to ensure all Mounter implementations satisfy
  57. // the mount interface.
  58. var _ Interface = &Mounter{}
  59. // MountPoint represents a single line in /proc/mounts or /etc/fstab.
  60. type MountPoint struct {
  61. Device string
  62. Path string
  63. Type string
  64. Opts []string
  65. Freq int
  66. Pass int
  67. }
  68. type MountErrorType string
  69. const (
  70. FilesystemMismatch MountErrorType = "FilesystemMismatch"
  71. HasFilesystemErrors MountErrorType = "HasFilesystemErrors"
  72. UnformattedReadOnly MountErrorType = "UnformattedReadOnly"
  73. FormatFailed MountErrorType = "FormatFailed"
  74. )
  75. type MountError struct {
  76. Type MountErrorType
  77. Message string
  78. }
  79. func (mountError *MountError) String() string {
  80. return mountError.Message
  81. }
  82. func (mountError *MountError) Error() string {
  83. return mountError.Message
  84. }
  85. func NewMountError(mountErrorValue MountErrorType, format string, args ...interface{}) error {
  86. mountError := &MountError{
  87. Type: mountErrorValue,
  88. Message: fmt.Sprintf(format, args...),
  89. }
  90. return mountError
  91. }
  92. // SafeFormatAndMount probes a device to see if it is formatted.
  93. // Namely it checks to see if a file system is present. If so it
  94. // mounts it otherwise the device is formatted first then mounted.
  95. type SafeFormatAndMount struct {
  96. Interface
  97. Exec utilexec.Interface
  98. }
  99. // FormatAndMount formats the given disk, if needed, and mounts it.
  100. // That is if the disk is not formatted and it is not being mounted as
  101. // read-only it will format it first then mount it. Otherwise, if the
  102. // disk is already formatted or it is being mounted as read-only, it
  103. // will be mounted without formatting.
  104. func (mounter *SafeFormatAndMount) FormatAndMount(source string, target string, fstype string, options []string) error {
  105. return mounter.formatAndMount(source, target, fstype, options)
  106. }
  107. // getMountRefsByDev finds all references to the device provided
  108. // by mountPath; returns a list of paths.
  109. // Note that mountPath should be path after the evaluation of any symblolic links.
  110. func getMountRefsByDev(mounter Interface, mountPath string) ([]string, error) {
  111. mps, err := mounter.List()
  112. if err != nil {
  113. return nil, err
  114. }
  115. // Finding the device mounted to mountPath.
  116. diskDev := ""
  117. for i := range mps {
  118. if mountPath == mps[i].Path {
  119. diskDev = mps[i].Device
  120. break
  121. }
  122. }
  123. // Find all references to the device.
  124. var refs []string
  125. for i := range mps {
  126. if mps[i].Device == diskDev || mps[i].Device == mountPath {
  127. if mps[i].Path != mountPath {
  128. refs = append(refs, mps[i].Path)
  129. }
  130. }
  131. }
  132. return refs, nil
  133. }
  134. // GetDeviceNameFromMount given a mnt point, find the device from /proc/mounts
  135. // returns the device name, reference count, and error code.
  136. func GetDeviceNameFromMount(mounter Interface, mountPath string) (string, int, error) {
  137. mps, err := mounter.List()
  138. if err != nil {
  139. return "", 0, err
  140. }
  141. // Find the device name.
  142. // FIXME if multiple devices mounted on the same mount path, only the first one is returned.
  143. device := ""
  144. // If mountPath is symlink, need get its target path.
  145. slTarget, err := filepath.EvalSymlinks(mountPath)
  146. if err != nil {
  147. slTarget = mountPath
  148. }
  149. for i := range mps {
  150. if mps[i].Path == slTarget {
  151. device = mps[i].Device
  152. break
  153. }
  154. }
  155. // Find all references to the device.
  156. refCount := 0
  157. for i := range mps {
  158. if mps[i].Device == device {
  159. refCount++
  160. }
  161. }
  162. return device, refCount, nil
  163. }
  164. // IsNotMountPoint determines if a directory is a mountpoint.
  165. // It should return ErrNotExist when the directory does not exist.
  166. // IsNotMountPoint is more expensive than IsLikelyNotMountPoint.
  167. // IsNotMountPoint detects bind mounts in linux.
  168. // IsNotMountPoint enumerates all the mountpoints using List() and
  169. // the list of mountpoints may be large, then it uses
  170. // isMountPointMatch to evaluate whether the directory is a mountpoint.
  171. func IsNotMountPoint(mounter Interface, file string) (bool, error) {
  172. // IsLikelyNotMountPoint provides a quick check
  173. // to determine whether file IS A mountpoint.
  174. notMnt, notMntErr := mounter.IsLikelyNotMountPoint(file)
  175. if notMntErr != nil && os.IsPermission(notMntErr) {
  176. // We were not allowed to do the simple stat() check, e.g. on NFS with
  177. // root_squash. Fall back to /proc/mounts check below.
  178. notMnt = true
  179. notMntErr = nil
  180. }
  181. if notMntErr != nil {
  182. return notMnt, notMntErr
  183. }
  184. // identified as mountpoint, so return this fact.
  185. if notMnt == false {
  186. return notMnt, nil
  187. }
  188. // Resolve any symlinks in file, kernel would do the same and use the resolved path in /proc/mounts.
  189. resolvedFile, err := filepath.EvalSymlinks(file)
  190. if err != nil {
  191. return true, err
  192. }
  193. // check all mountpoints since IsLikelyNotMountPoint
  194. // is not reliable for some mountpoint types.
  195. mountPoints, mountPointsErr := mounter.List()
  196. if mountPointsErr != nil {
  197. return notMnt, mountPointsErr
  198. }
  199. for _, mp := range mountPoints {
  200. if isMountPointMatch(mp, resolvedFile) {
  201. notMnt = false
  202. break
  203. }
  204. }
  205. return notMnt, nil
  206. }
  207. // MakeBindOpts detects whether a bind mount is being requested and makes the remount options to
  208. // use in case of bind mount, due to the fact that bind mount doesn't respect mount options.
  209. // The list equals:
  210. // options - 'bind' + 'remount' (no duplicate)
  211. func MakeBindOpts(options []string) (bool, []string, []string) {
  212. // Because we have an FD opened on the subpath bind mount, the "bind" option
  213. // needs to be included, otherwise the mount target will error as busy if you
  214. // remount as readonly.
  215. //
  216. // As a consequence, all read only bind mounts will no longer change the underlying
  217. // volume mount to be read only.
  218. bindRemountOpts := []string{"bind", "remount"}
  219. bind := false
  220. bindOpts := []string{"bind"}
  221. // _netdev is a userspace mount option and does not automatically get added when
  222. // bind mount is created and hence we must carry it over.
  223. if checkForNetDev(options) {
  224. bindOpts = append(bindOpts, "_netdev")
  225. }
  226. for _, option := range options {
  227. switch option {
  228. case "bind":
  229. bind = true
  230. break
  231. case "remount":
  232. break
  233. default:
  234. bindRemountOpts = append(bindRemountOpts, option)
  235. }
  236. }
  237. return bind, bindOpts, bindRemountOpts
  238. }
  239. func checkForNetDev(options []string) bool {
  240. for _, option := range options {
  241. if option == "_netdev" {
  242. return true
  243. }
  244. }
  245. return false
  246. }
  247. // PathWithinBase checks if give path is within given base directory.
  248. func PathWithinBase(fullPath, basePath string) bool {
  249. rel, err := filepath.Rel(basePath, fullPath)
  250. if err != nil {
  251. return false
  252. }
  253. if StartsWithBackstep(rel) {
  254. // Needed to escape the base path.
  255. return false
  256. }
  257. return true
  258. }
  259. // StartsWithBackstep checks if the given path starts with a backstep segment.
  260. func StartsWithBackstep(rel string) bool {
  261. // normalize to / and check for ../
  262. return rel == ".." || strings.HasPrefix(filepath.ToSlash(rel), "../")
  263. }