@@ -398,7 +398,7 @@ func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
398
398
options = append (options , collectMountOptions (fsType , mountFlags )... )
399
399
}
400
400
// Mount
401
- err = m . Mounter (). FormatAndMount (devicePath , stagingTarget , fsType , options )
401
+ err = ns . formatAndMountRetry (devicePath , stagingTarget , fsType , options )
402
402
if err != nil {
403
403
return nil , status .Error (codes .Internal , err .Error ())
404
404
}
@@ -426,6 +426,25 @@ func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
426
426
return & csi.NodeStageVolumeResponse {}, nil
427
427
}
428
428
429
+ // formatAndMountRetry attempts to format and mount a device at the given path.
430
+ // If the initial mount fails, it rescans the device and retries the mount operation.
431
+ func (ns * nodeServer ) formatAndMountRetry (devicePath , stagingTarget , fsType string , options []string ) error {
432
+ m := ns .Mount
433
+ err := m .Mounter ().FormatAndMount (devicePath , stagingTarget , fsType , options )
434
+ if err != nil {
435
+ klog .Infof ("Initial format and mount failed: %v. Attempting rescan." , err )
436
+ // Attempting rescan if the initial mount fails
437
+ rescanErr := blockdevice .RescanDevice (devicePath )
438
+ if rescanErr != nil {
439
+ klog .Infof ("Rescan failed: %v. Returning original mount error." , rescanErr )
440
+ return err
441
+ }
442
+ klog .Infof ("Rescan succeeded, retrying format and mount" )
443
+ err = m .Mounter ().FormatAndMount (devicePath , stagingTarget , fsType , options )
444
+ }
445
+ return err
446
+ }
447
+
429
448
func (ns * nodeServer ) NodeUnstageVolume (ctx context.Context , req * csi.NodeUnstageVolumeRequest ) (* csi.NodeUnstageVolumeResponse , error ) {
430
449
klog .V (4 ).Infof ("NodeUnstageVolume: called with args %+v" , protosanitizer .StripSecrets (* req ))
431
450
0 commit comments