...
1
16
17 package mount
18
19 import (
20 "fmt"
21 "os"
22
23 "k8s.io/klog/v2"
24 )
25
26
27
28
29
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
44
45
46
47
48
49
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
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
92
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 }
104
View as plain text