@@ -390,22 +390,47 @@ func (n *NodeService) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu
390390 }
391391
392392 targetPath := req .GetTargetPath ()
393- klog . V ( 5 ). Infof ( "Unmounting %s" , targetPath )
394- err := n . mounter . Unmount (targetPath )
393+
394+ mountExists , err := checkIfMountExists (targetPath )
395395 if err != nil {
396- klog .Errorf ("failed to unmount %v" , err )
396+ klog .Errorf ("failed to check if mount exists %v" , err )
397397 return nil , err
398398 }
399399
400+ if mountExists {
401+ klog .V (5 ).Infof ("Unmounting %s" , targetPath )
402+ err = n .mounter .Unmount (targetPath )
403+ if err != nil {
404+ klog .Errorf ("failed to unmount %v" , err )
405+ return nil , err
406+ }
407+ }
408+
400409 if err = os .RemoveAll (targetPath ); err != nil {
401410 klog .Errorf ("failed to remove %s, %v" , targetPath , err )
402411 return nil , fmt .Errorf ("remove target path: %w" , err )
403412 }
404- klog .V (3 ).Info ("Validate Node unpublish completed" )
413+ klog .V (3 ).Info ("Node unpublish completed" )
405414
406415 return & csi.NodeUnpublishVolumeResponse {}, nil
407416}
408417
418+ func checkIfMountExists (target string ) (bool , error ) {
419+ klog .V (4 ).Infof ("Checking if mount exists for %s" , target )
420+ command := exec .Command ("mountpoint" , target )
421+ cmdOutput , err := command .CombinedOutput ()
422+ if err != nil {
423+ output := string (cmdOutput )
424+
425+ if strings .Contains (output , "is not a mountpoint" ) ||
426+ strings .Contains (output , "No such file or directory" ) {
427+ return false , nil
428+ }
429+ return false , fmt .Errorf ("mountpoint check failed: %v\n mountpoint arguments: %s\n Output: %s" , err , target , output )
430+ }
431+ return true , nil
432+ }
433+
409434func (n * NodeService ) NodeGetVolumeStats (_ context.Context , req * csi.NodeGetVolumeStatsRequest ) (* csi.NodeGetVolumeStatsResponse , error ) {
410435 klog .V (4 ).InfoS ("NodeGetVolumeStats: called" , "args" , req )
411436 if len (req .GetVolumeId ()) == 0 {
0 commit comments