| 1 |
/* |
| 2 |
Copyright 2018 The Kubernetes Authors. |
| 3 |
|
| 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 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package mount |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"os" |
| 22 |
"time" |
| 23 |
|
| 24 |
"k8s.io/klog/v2" |
| 25 |
) |
| 26 |
|
| 27 |
// CleanupMountPoint unmounts the given path and deletes the remaining directory |
| 28 |
// if successful. If extensiveMountPointCheck is true IsNotMountPoint will be |
| 29 |
// called instead of IsLikelyNotMountPoint. IsNotMountPoint is more expensive |
| 30 |
// but properly handles bind mounts within the same fs. |
| 31 |
func CleanupMountPoint(mountPath string, mounter Interface, extensiveMountPointCheck bool) error { |
| 32 |
pathExists, pathErr := PathExists(mountPath) |
| 33 |
if !pathExists && pathErr == nil { |
| 34 |
klog.Warningf("Warning: mount cleanup skipped because path does not exist: %v", mountPath) |
| 35 |
return nil |
| 36 |
} |
| 37 |
corruptedMnt := IsCorruptedMnt(pathErr) |
| 38 |
if pathErr != nil && !corruptedMnt { |
| 39 |
return fmt.Errorf("Error checking path: %v", pathErr) |
| 40 |
} |
| 41 |
return doCleanupMountPoint(mountPath, mounter, extensiveMountPointCheck, corruptedMnt) |
| 42 |
} |
| 43 |
|
| 44 |
func CleanupMountWithForce(mountPath string, mounter MounterForceUnmounter, extensiveMountPointCheck bool, umountTimeout time.Duration) error { |
| 45 |
pathExists, pathErr := PathExists(mountPath) |
| 46 |
if !pathExists && pathErr == nil { |
| 47 |
klog.Warningf("Warning: mount cleanup skipped because path does not exist: %v", mountPath) |
| 48 |
return nil |
| 49 |
} |
| 50 |
corruptedMnt := IsCorruptedMnt(pathErr) |
| 51 |
if pathErr != nil && !corruptedMnt { |
| 52 |
return fmt.Errorf("Error checking path: %v", pathErr) |
| 53 |
} |
| 54 |
|
| 55 |
if corruptedMnt || mounter.CanSafelySkipMountPointCheck() { |
| 56 |
klog.V(4).Infof("unmounting %q (corruptedMount: %t, mounterCanSkipMountPointChecks: %t)", |
| 57 |
mountPath, corruptedMnt, mounter.CanSafelySkipMountPointCheck()) |
| 58 |
if err := mounter.UnmountWithForce(mountPath, umountTimeout); err != nil { |
| 59 |
return err |
| 60 |
} |
| 61 |
return removePath(mountPath) |
| 62 |
} |
| 63 |
|
| 64 |
notMnt, err := removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck) |
| 65 |
// if mountPath is not a mount point, it's just been removed or there was an error |
| 66 |
if err != nil || notMnt { |
| 67 |
return err |
| 68 |
} |
| 69 |
|
| 70 |
klog.V(4).Infof("%q is a mountpoint, unmounting", mountPath) |
| 71 |
if err := mounter.UnmountWithForce(mountPath, umountTimeout); err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
|
| 75 |
notMnt, err = removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck) |
| 76 |
// if mountPath is not a mount point, it's either just been removed or there was an error |
| 77 |
if notMnt { |
| 78 |
return err |
| 79 |
} |
| 80 |
// mountPath is still a mount point |
| 81 |
return fmt.Errorf("failed to cleanup mount point %v", mountPath) |
| 82 |
} |
| 83 |
|
| 84 |
// doCleanupMountPoint unmounts the given path and |
| 85 |
// deletes the remaining directory if successful. |
| 86 |
// if extensiveMountPointCheck is true |
| 87 |
// IsNotMountPoint will be called instead of IsLikelyNotMountPoint. |
| 88 |
// IsNotMountPoint is more expensive but properly handles bind mounts within the same fs. |
| 89 |
// if corruptedMnt is true, it means that the mountPath is a corrupted mountpoint, and the mount point check |
| 90 |
// will be skipped. The mount point check will also be skipped if the mounter supports it. |
| 91 |
func doCleanupMountPoint(mountPath string, mounter Interface, extensiveMountPointCheck bool, corruptedMnt bool) error { |
| 92 |
if corruptedMnt || mounter.CanSafelySkipMountPointCheck() { |
| 93 |
klog.V(4).Infof("unmounting %q (corruptedMount: %t, mounterCanSkipMountPointChecks: %t)", |
| 94 |
mountPath, corruptedMnt, mounter.CanSafelySkipMountPointCheck()) |
| 95 |
if err := mounter.Unmount(mountPath); err != nil { |
| 96 |
return err |
| 97 |
} |
| 98 |
return removePath(mountPath) |
| 99 |
} |
| 100 |
|
| 101 |
notMnt, err := removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck) |
| 102 |
// if mountPath is not a mount point, it's just been removed or there was an error |
| 103 |
if err != nil || notMnt { |
| 104 |
return err |
| 105 |
} |
| 106 |
|
| 107 |
klog.V(4).Infof("%q is a mountpoint, unmounting", mountPath) |
| 108 |
if err := mounter.Unmount(mountPath); err != nil { |
| 109 |
return err |
| 110 |
} |
| 111 |
|
| 112 |
notMnt, err = removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck) |
| 113 |
// if mountPath is not a mount point, it's either just been removed or there was an error |
| 114 |
if notMnt { |
| 115 |
return err |
| 116 |
} |
| 117 |
// mountPath is still a mount point |
| 118 |
return fmt.Errorf("failed to cleanup mount point %v", mountPath) |
| 119 |
} |
| 120 |
|
| 121 |
// removePathIfNotMountPoint verifies if given mountPath is a mount point if not it attempts |
| 122 |
// to remove the directory. Returns true and nil if directory was not a mount point and removed. |
| 123 |
func removePathIfNotMountPoint(mountPath string, mounter Interface, extensiveMountPointCheck bool) (bool, error) { |
| 124 |
var notMnt bool |
| 125 |
var err error |
| 126 |
|
| 127 |
if extensiveMountPointCheck { |
| 128 |
notMnt, err = IsNotMountPoint(mounter, mountPath) |
| 129 |
} else { |
| 130 |
notMnt, err = mounter.IsLikelyNotMountPoint(mountPath) |
| 131 |
} |
| 132 |
|
| 133 |
if err != nil { |
| 134 |
if os.IsNotExist(err) { |
| 135 |
klog.V(4).Infof("%q does not exist", mountPath) |
| 136 |
return true, nil |
| 137 |
} |
| 138 |
return notMnt, err |
| 139 |
} |
| 140 |
|
| 141 |
if notMnt { |
| 142 |
klog.V(4).Infof("%q is not a mountpoint, deleting", mountPath) |
| 143 |
return notMnt, os.Remove(mountPath) |
| 144 |
} |
| 145 |
return notMnt, nil |
| 146 |
} |
| 147 |
|
| 148 |
// removePath attempts to remove the directory. Returns nil if the directory was removed or does not exist. |
| 149 |
func removePath(mountPath string) error { |
| 150 |
klog.V(4).Infof("Deleting path %q", mountPath) |
| 151 |
err := os.Remove(mountPath) |
| 152 |
if os.IsNotExist(err) { |
| 153 |
klog.V(4).Infof("%q does not exist", mountPath) |
| 154 |
return nil |
| 155 |
} |
| 156 |
return err |
| 157 |
} |