Skip to content

Commit c170231

Browse files
committed
fix: user forceUmounter and lazy umount
1 parent c103561 commit c170231

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

pkg/mounter/safe_mounter_unix.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@ limitations under the License.
2020
package mounter
2121

2222
import (
23+
"runtime"
24+
2325
mount "k8s.io/mount-utils"
2426
utilexec "k8s.io/utils/exec"
2527
)
2628

2729
func NewSafeMounter(removeSMBMappingDuringUnmount bool) (*mount.SafeFormatAndMount, error) {
30+
mounter := mount.New("")
31+
if runtime.GOOS == "linux" {
32+
// MounterForceUnmounter is only implemented on Linux now
33+
mounter = mounter.(mount.MounterForceUnmounter)
34+
}
2835
return &mount.SafeFormatAndMount{
29-
Interface: mount.New(""),
36+
Interface: mounter,
3037
Exec: utilexec.New(),
3138
}, nil
3239
}

pkg/smb/smb_common_linux.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ limitations under the License.
2020
package smb
2121

2222
import (
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

3641
func 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

4059
func preparePublishPath(path string, m *mount.SafeFormatAndMount) error {
@@ -48,3 +67,12 @@ func prepareStagePath(path string, m *mount.SafeFormatAndMount) error {
4867
func 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

Comments
 (0)