Skip to content

Commit 89fac93

Browse files
andyzhangxbrahmaroutu
authored andcommitted
fix: smb remount issue
typo typo add one logging
1 parent 37cdee1 commit 89fac93

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

mount_windows.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,32 @@ func (mounter *Mounter) MountSensitive(source string, target string, fstype stri
9898
getSMBMountMutex.LockKey(source)
9999
defer getSMBMountMutex.UnlockKey(source)
100100

101-
if output, err := newSMBMapping(allOptions[0], allOptions[1], source); err != nil {
101+
var output string
102+
var err error
103+
username := allOptions[0]
104+
password := allOptions[1]
105+
if output, err = newSMBMapping(username, password, source); err != nil {
106+
klog.Warningf("SMB Mapping(%s) returned with error(%v), output(%s)", source, err, string(output))
102107
if isSMBMappingExist(source) {
103-
klog.V(2).Infof("SMB Mapping(%s) already exists, now begin to remove and remount", source)
104-
if output, err := removeSMBMapping(source); err != nil {
105-
return fmt.Errorf("Remove-SmbGlobalMapping failed: %v, output: %q", err, output)
108+
valid, errPath := isValidPath(source)
109+
if errPath != nil {
110+
return errPath
106111
}
107-
if output, err := newSMBMapping(allOptions[0], allOptions[1], source); err != nil {
108-
return fmt.Errorf("New-SmbGlobalMapping remount failed: %v, output: %q", err, output)
112+
if valid {
113+
err = nil
114+
klog.V(2).Infof("SMB Mapping(%s) already exists and is still valid, skip error", source)
115+
} else {
116+
klog.V(2).Infof("SMB Mapping(%s) already exists while it's not valid, now begin to remove and remount", source)
117+
if output, err = removeSMBMapping(source); err != nil {
118+
return fmt.Errorf("Remove-SmbGlobalMapping failed: %v, output: %q", err, output)
119+
}
120+
output, err = newSMBMapping(username, password, source)
109121
}
110-
} else {
111-
return fmt.Errorf("New-SmbGlobalMapping failed: %v, output: %q", err, output)
112122
}
113123
}
124+
if err != nil {
125+
return fmt.Errorf("New-SmbGlobalMapping(%s) failed: %v, output: %q", source, err, output)
126+
}
114127
}
115128

116129
output, err := exec.Command("cmd", "/c", "mklink", "/D", target, bindSource).CombinedOutput()
@@ -153,6 +166,19 @@ func isSMBMappingExist(remotepath string) bool {
153166
return err == nil
154167
}
155168

169+
// check whether remotepath is valid
170+
// return (true, nil) if remotepath is valid
171+
func isValidPath(remotepath string) (bool, error) {
172+
cmd := exec.Command("powershell", "/c", `Test-Path $Env:remoteapth`)
173+
cmd.Env = append(os.Environ(), fmt.Sprintf("remoteapth=%s", remotepath))
174+
output, err := cmd.CombinedOutput()
175+
if err != nil {
176+
return false, fmt.Errorf("returned output: %s, error: %v", string(output), err)
177+
}
178+
179+
return strings.HasPrefix(strings.ToLower(string(output)), "true"), nil
180+
}
181+
156182
// remove SMB mapping
157183
func removeSMBMapping(remotepath string) (string, error) {
158184
cmd := exec.Command("powershell", "/c", `Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force`)

mount_windows_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,33 @@ func TestNewSMBMapping(t *testing.T) {
331331
}
332332
}
333333
}
334+
335+
func TestIsValidPath(t *testing.T) {
336+
tests := []struct {
337+
remotepath string
338+
expectedResult bool
339+
expectError bool
340+
}{
341+
{
342+
"c:",
343+
true,
344+
false,
345+
},
346+
{
347+
"invalid-path",
348+
false,
349+
false,
350+
},
351+
}
352+
353+
for _, test := range tests {
354+
result, err := isValidPath(test.remotepath)
355+
assert.Equal(t, result, test.expectedResult, "Expect result not equal with isValidPath(%s) return: %q, expected: %q, error: %v",
356+
test.remotepath, result, test.expectedResult, err)
357+
if test.expectError {
358+
assert.NotNil(t, err, "Expect error during isValidPath(%s)", test.remotepath)
359+
} else {
360+
assert.Nil(t, err, "Expect error is nil during isValidPath(%s)", test.remotepath)
361+
}
362+
}
363+
}

0 commit comments

Comments
 (0)