@@ -20,8 +20,13 @@ limitations under the License.
2020package smb
2121
2222import (
23+ "fmt"
2324 "os"
25+ "os/exec"
26+ "strings"
27+ "time"
2428
29+ "k8s.io/klog/v2"
2530 mount "k8s.io/mount-utils"
2631)
2732
@@ -34,7 +39,21 @@ func CleanupSMBMountPoint(m *mount.SafeFormatAndMount, target string, extensiveM
3439}
3540
3641func CleanupMountPoint (m * mount.SafeFormatAndMount , target string , extensiveMountCheck bool ) error {
37- return mount .CleanupMountPoint (target , m , extensiveMountCheck )
42+ var err error
43+ extensiveMountPointCheck := true
44+ forceUnmounter , ok := m .Interface .(mount.MounterForceUnmounter )
45+ if ok {
46+ klog .V (2 ).Infof ("force unmount on %s" , target )
47+ err = mount .CleanupMountWithForce (target , forceUnmounter , extensiveMountPointCheck , 30 * time .Second )
48+ } else {
49+ err = mount .CleanupMountPoint (target , m .Interface , extensiveMountPointCheck )
50+ }
51+
52+ if err != nil && strings .Contains (err .Error (), "target is busy" ) {
53+ klog .Warningf ("unmount on %s failed with %v, try lazy unmount" , target , err )
54+ err = forceUmount (target )
55+ }
56+ return err
3857}
3958
4059func preparePublishPath (path string , m * mount.SafeFormatAndMount ) error {
@@ -48,3 +67,12 @@ func prepareStagePath(path string, m *mount.SafeFormatAndMount) error {
4867func Mkdir (m * mount.SafeFormatAndMount , name string , perm os.FileMode ) error {
4968 return os .Mkdir (name , perm )
5069}
70+
71+ func forceUmount (path string ) error {
72+ cmd := exec .Command ("umount" , "-lf" , path )
73+ out , cmderr := cmd .CombinedOutput ()
74+ if cmderr != nil {
75+ return fmt .Errorf ("lazy unmount on %s failed with %v, output: %s" , path , cmderr , string (out ))
76+ }
77+ return nil
78+ }
0 commit comments