@@ -98,19 +98,32 @@ func (mounter *Mounter) MountSensitive(source string, target string, fstype stri
98
98
getSMBMountMutex .LockKey (source )
99
99
defer getSMBMountMutex .UnlockKey (source )
100
100
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 ))
102
107
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
106
111
}
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 )
109
121
}
110
- } else {
111
- return fmt .Errorf ("New-SmbGlobalMapping failed: %v, output: %q" , err , output )
112
122
}
113
123
}
124
+ if err != nil {
125
+ return fmt .Errorf ("New-SmbGlobalMapping(%s) failed: %v, output: %q" , source , err , output )
126
+ }
114
127
}
115
128
116
129
output , err := exec .Command ("cmd" , "/c" , "mklink" , "/D" , target , bindSource ).CombinedOutput ()
@@ -153,6 +166,19 @@ func isSMBMappingExist(remotepath string) bool {
153
166
return err == nil
154
167
}
155
168
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
+
156
182
// remove SMB mapping
157
183
func removeSMBMapping (remotepath string ) (string , error ) {
158
184
cmd := exec .Command ("powershell" , "/c" , `Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force` )
0 commit comments