mount_helper_common.go 3.2 KB

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