mount_helper_common.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2018 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. package mount
  14. import (
  15. "fmt"
  16. "os"
  17. "k8s.io/klog"
  18. )
  19. // CleanupMountPoint unmounts the given path and deletes the remaining directory
  20. // if successful. If extensiveMountPointCheck is true IsNotMountPoint will be
  21. // called instead of IsLikelyNotMountPoint. IsNotMountPoint is more expensive
  22. // but properly handles bind mounts within the same fs.
  23. func CleanupMountPoint(mountPath string, mounter Interface, extensiveMountPointCheck bool) error {
  24. pathExists, pathErr := PathExists(mountPath)
  25. if !pathExists {
  26. klog.Warningf("Warning: Unmount skipped because path does not exist: %v", mountPath)
  27. return nil
  28. }
  29. corruptedMnt := IsCorruptedMnt(pathErr)
  30. if pathErr != nil && !corruptedMnt {
  31. return fmt.Errorf("Error checking path: %v", pathErr)
  32. }
  33. return doCleanupMountPoint(mountPath, mounter, extensiveMountPointCheck, corruptedMnt)
  34. }
  35. // doCleanupMountPoint unmounts the given path and
  36. // deletes the remaining directory if successful.
  37. // if extensiveMountPointCheck is true
  38. // IsNotMountPoint will be called instead of IsLikelyNotMountPoint.
  39. // IsNotMountPoint is more expensive but properly handles bind mounts within the same fs.
  40. // if corruptedMnt is true, it means that the mountPath is a corrupted mountpoint, and the mount point check
  41. // will be skipped
  42. func doCleanupMountPoint(mountPath string, mounter Interface, extensiveMountPointCheck bool, corruptedMnt bool) error {
  43. if !corruptedMnt {
  44. var notMnt bool
  45. var err error
  46. if extensiveMountPointCheck {
  47. notMnt, err = IsNotMountPoint(mounter, mountPath)
  48. } else {
  49. notMnt, err = mounter.IsLikelyNotMountPoint(mountPath)
  50. }
  51. if err != nil {
  52. return err
  53. }
  54. if notMnt {
  55. klog.Warningf("Warning: %q is not a mountpoint, deleting", mountPath)
  56. return os.Remove(mountPath)
  57. }
  58. }
  59. // Unmount the mount path
  60. klog.V(4).Infof("%q is a mountpoint, unmounting", mountPath)
  61. if err := mounter.Unmount(mountPath); err != nil {
  62. return err
  63. }
  64. notMnt, mntErr := mounter.IsLikelyNotMountPoint(mountPath)
  65. if mntErr != nil {
  66. return mntErr
  67. }
  68. if notMnt {
  69. klog.V(4).Infof("%q is unmounted, deleting the directory", mountPath)
  70. return os.Remove(mountPath)
  71. }
  72. return fmt.Errorf("Failed to unmount path %v", mountPath)
  73. }
  74. // PathExists returns true if the specified path exists.
  75. // TODO: clean this up to use pkg/util/file/FileExists
  76. func PathExists(path string) (bool, error) {
  77. _, err := os.Stat(path)
  78. if err == nil {
  79. return true, nil
  80. } else if os.IsNotExist(err) {
  81. return false, nil
  82. } else if IsCorruptedMnt(err) {
  83. return true, err
  84. }
  85. return false, err
  86. }