Debian Codegraph @

mount/mount_helper_common.go

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