@@ -364,18 +364,27 @@ func (ns *NodeServer) nodePublishBlockVolume(volumeID, devicePath, targetPath st
364364
365365 // we ensure the parent directory exists and is valid
366366 if _ , err := ensureDirectory (filepath .Dir (targetPath )); err != nil {
367- return status .Errorf (codes .Internal , "failed to prepare mount point for block device %v: %v" , devicePath , err )
367+ return status .Errorf (codes .Internal , "failed to prepare mount point directory for block device %v: %v" , devicePath , err )
368+ }
369+
370+ // ensure the bind mount point is clear
371+ if err := ensureBindMountPoint (targetPath ); err != nil {
372+ return status .Errorf (codes .Internal , "failed to prepare mount point file for block device %v: %v" , devicePath , err )
368373 }
369374
370375 // create file where we can bind mount the device to
371376 if err := makeFile (targetPath ); err != nil {
377+ if removeErr := unmountAndCleanupMountPoint (targetPath , mount .New ("" )); removeErr != nil {
378+ log .WithError (removeErr ).Errorf ("Failed to clean up the mount target %q for failed bind mount point" , targetPath )
379+ }
372380 return status .Errorf (codes .Internal , "failed to create file %v: %v" , targetPath , err )
373381 }
374382
375383 log .Infof ("Bind mounting device %v at %v" , devicePath , targetPath )
376384 if err := mounter .Mount (devicePath , targetPath , "" , []string {"bind" }); err != nil {
377- if removeErr := os .Remove (targetPath ); removeErr != nil {
378- log .WithError (removeErr ).Errorf ("Failed to remove failed mount target %q" , targetPath )
385+ log .WithError (err ).Infof ("Clean up the mount target %s because of mount error" , targetPath )
386+ if removeErr := unmountAndCleanupMountPoint (targetPath , mount .New ("" )); removeErr != nil {
387+ log .WithError (removeErr ).Errorf ("Failed to clean up the mount target %q for failed mounting" , targetPath )
379388 }
380389 return status .Errorf (codes .Internal , "failed to bind mount %q at %q: %v" , devicePath , targetPath , err )
381390 }
0 commit comments