@@ -1536,7 +1536,7 @@ func (p *Plugin) nodeStageISCSIVolume(
15361536 }
15371537
15381538 var luksDevice models.LUKSDeviceInterface
1539- luksDevice , err = utils .NewLUKSDevice (publishInfo .DevicePath , req .VolumeContext ["internalName" ])
1539+ luksDevice , err = p . devices .NewLUKSDevice (publishInfo .DevicePath , req .VolumeContext ["internalName" ])
15401540 if err != nil {
15411541 return err
15421542 }
@@ -1836,11 +1836,13 @@ func (p *Plugin) nodePublishISCSIVolume(
18361836 luksDevice , err = p .devices .NewLUKSDeviceFromMappingPath (ctx , devicePath ,
18371837 req .VolumeContext ["internalName" ])
18381838 } else {
1839- luksDevice , err = utils .NewLUKSDevice (publishInfo .DevicePath , req .VolumeContext ["internalName" ])
1839+ luksDevice , err = p . devices .NewLUKSDevice (publishInfo .DevicePath , req .VolumeContext ["internalName" ])
18401840 }
1841+
18411842 if err != nil {
18421843 return nil , status .Error (codes .Internal , err .Error ())
18431844 }
1845+
18441846 err = ensureLUKSVolumePassphrase (ctx , p .restClient , luksDevice , req .GetVolumeId (), req .GetSecrets (), false )
18451847 if err != nil {
18461848 Logc (ctx ).WithError (err ).Error ("Failed to ensure current LUKS passphrase." )
@@ -2441,16 +2443,18 @@ func (p *Plugin) nodeStageNVMeVolume(
24412443 ctx context.Context , req * csi.NodeStageVolumeRequest ,
24422444 publishInfo * models.VolumePublishInfo ,
24432445) error {
2444- isLUKS := utils .ParseBool (publishInfo . LUKSEncryption )
2446+ isLUKS := utils .ParseBool (req . PublishContext [ " LUKSEncryption" ] )
24452447 publishInfo .LUKSEncryption = strconv .FormatBool (isLUKS )
24462448 publishInfo .MountOptions = req .PublishContext ["mountOptions" ]
24472449 publishInfo .NVMeSubsystemNQN = req .PublishContext ["nvmeSubsystemNqn" ]
24482450 publishInfo .NVMeNamespaceUUID = req .PublishContext ["nvmeNamespaceUUID" ]
24492451 publishInfo .NVMeTargetIPs = strings .Split (req .PublishContext ["nvmeTargetIPs" ], "," )
24502452 publishInfo .SANType = req .PublishContext ["SANType" ]
24512453
2452- if err := utils .AttachNVMeVolumeRetry (ctx , req .VolumeContext ["internalName" ], "" , publishInfo , nil ,
2453- utils .NVMeAttachTimeout ); err != nil {
2454+ err := utils .AttachNVMeVolumeRetry (
2455+ ctx , req .VolumeContext ["internalName" ], "" , publishInfo , req .GetSecrets (), utils .NVMeAttachTimeout ,
2456+ )
2457+ if err != nil {
24542458 return err
24552459 }
24562460
@@ -2460,11 +2464,11 @@ func (p *Plugin) nodeStageNVMeVolume(
24602464 }
24612465
24622466 if isLUKS {
2463- luksDevice , err := p .devices .NewLUKSDeviceFromMappingPath (ctx , publishInfo .DevicePath ,
2464- req .VolumeContext ["internalName" ])
2467+ luksDevice , err := p .devices .NewLUKSDevice (publishInfo .DevicePath , req .VolumeContext ["internalName" ])
24652468 if err != nil {
24662469 return err
24672470 }
2471+
24682472 // Ensure we update the passphrase in case it has never been set before
24692473 err = ensureLUKSVolumePassphrase (ctx , p .restClient , luksDevice , volumeId , req .GetSecrets (), true )
24702474 if err != nil {
@@ -2500,12 +2504,42 @@ func (p *Plugin) nodeUnstageNVMeVolume(
25002504 publishInfo .NVMeNamespaceUUID )
25012505
25022506 // Get the device using 'nvme-cli' commands. Flush the device IOs.
2507+ // Proceed further with unstage flow, if device is not found.
25032508 nvmeDev , err := p .nvmeHandler .NewNVMeDevice (ctx , publishInfo .NVMeNamespaceUUID )
2504- // Proceed further with Unstage flow, if 'device is not found'.
25052509 if err != nil && ! errors .IsNotFoundError (err ) {
25062510 return nil , fmt .Errorf ("error while getting NVMe device, %v" , err )
25072511 }
25082512
2513+ var devicePath string
2514+ if nvmeDev != nil {
2515+ devicePath = nvmeDev .GetPath ()
2516+ }
2517+
2518+ var luksMapperPath string
2519+ if utils .ParseBool (publishInfo .LUKSEncryption ) && devicePath != "" {
2520+ fields := LogFields {
2521+ "lunID" : publishInfo .IscsiLunNumber ,
2522+ "publishedDevice" : publishInfo .DevicePath ,
2523+ "nvmeDevPath" : nvmeDev .GetPath (),
2524+ }
2525+
2526+ luksMapperPath , err = p .devices .GetLUKSDeviceForMultipathDevice (devicePath )
2527+ if err != nil {
2528+ return & csi.NodeUnstageVolumeResponse {}, err
2529+ }
2530+
2531+ // Ensure the LUKS device is closed if the luksMapperPath is set.
2532+ if luksMapperPath != "" {
2533+ if err = p .devices .EnsureLUKSDeviceClosedWithMaxWaitLimit (ctx , luksMapperPath ); err != nil {
2534+ if ! errors .IsMaxWaitExceededError (err ) {
2535+ Logc (ctx ).WithFields (fields ).WithError (err ).Error ("Failed to close LUKS device." )
2536+ return & csi.NodeUnstageVolumeResponse {}, err
2537+ }
2538+ Logc (ctx ).WithFields (fields ).WithError (err ).Debug ("LUKS close wait time exceeded, continuing with device removal." )
2539+ }
2540+ }
2541+ }
2542+
25092543 if ! nvmeDev .IsNil () {
25102544 // If device is found, proceed to flush and clean up.
25112545 err := nvmeDev .FlushDevice (ctx , p .unsafeDetach , force )
@@ -2580,6 +2614,20 @@ func (p *Plugin) nodeUnstageNVMeVolume(
25802614 return nil , status .Error (codes .Internal , errStr )
25812615 }
25822616
2617+ // If the luks device still exists, it means the device was unable to be closed prior to removing the block
2618+ // device. This can happen if the LUN was deleted or offline. It should be removable by this point.
2619+ // It needs to be removed prior to removing the 'unmappedMpathDevice' device below.
2620+ if luksMapperPath != "" {
2621+ // EnsureLUKSDeviceClosed will not return an error if the device is already closed or removed.
2622+ if err = p .devices .EnsureLUKSDeviceClosed (ctx , luksMapperPath ); err != nil {
2623+ Logc (ctx ).WithFields (LogFields {
2624+ "devicePath" : luksMapperPath ,
2625+ }).WithError (err ).Warning ("Unable to remove LUKS mapper device." )
2626+ }
2627+ // Clear the time duration for the LUKS device.
2628+ utils .LuksCloseDurations .RemoveDurationTracking (luksMapperPath )
2629+ }
2630+
25832631 // Delete the device info we saved to the volume tracking info path so unstage can succeed.
25842632 if err := p .nodeHelper .DeleteTrackingInfo (ctx , volumeId ); err != nil {
25852633 return nil , status .Error (codes .Internal , err .Error ())
@@ -2608,36 +2656,39 @@ func (p *Plugin) nodePublishNVMeVolume(
26082656 publishInfo .MountOptions = utils .AppendToStringList (publishInfo .MountOptions , "ro" , "," )
26092657 }
26102658
2659+ devicePath := publishInfo .DevicePath
26112660 if utils .ParseBool (publishInfo .LUKSEncryption ) {
26122661 // Rotate the LUKS passphrase if needed, on failure, log and continue to publish
2613- luksDevice , err := p .devices .NewLUKSDeviceFromMappingPath (ctx , publishInfo .DevicePath ,
2614- req .VolumeContext ["internalName" ])
2662+ luksDevice , err := p .devices .NewLUKSDevice (devicePath , req .VolumeContext ["internalName" ])
26152663 if err != nil {
26162664 return nil , status .Error (codes .Internal , err .Error ())
26172665 }
2666+
26182667 err = ensureLUKSVolumePassphrase (ctx , p .restClient , luksDevice , req .GetVolumeId (), req .GetSecrets (), false )
26192668 if err != nil {
26202669 Logc (ctx ).WithError (err ).Error ("Failed to ensure current LUKS passphrase." )
26212670 }
2671+
2672+ // At this point, we must reassign the device path to the luks mapper path for mounts to work.
2673+ devicePath = luksDevice .MappedDevicePath ()
26222674 }
26232675
26242676 isRawBlock := publishInfo .FilesystemType == filesystem .Raw
26252677 if isRawBlock {
2626-
26272678 if len (publishInfo .MountOptions ) > 0 {
26282679 publishInfo .MountOptions = utils .AppendToStringList (publishInfo .MountOptions , "bind" , "," )
26292680 } else {
26302681 publishInfo .MountOptions = "bind"
26312682 }
26322683
26332684 // Place the block device at the target path for the raw-block.
2634- err = p .mount .MountDevice (ctx , publishInfo . DevicePath , req .TargetPath , publishInfo .MountOptions , true )
2685+ err = p .mount .MountDevice (ctx , devicePath , req .TargetPath , publishInfo .MountOptions , true )
26352686 if err != nil {
26362687 return nil , status .Errorf (codes .Internal , "unable to bind mount raw device; %s" , err )
26372688 }
26382689 } else {
26392690 // Mount the device.
2640- err = p .mount .MountDevice (ctx , publishInfo . DevicePath , req .TargetPath , publishInfo .MountOptions , false )
2691+ err = p .mount .MountDevice (ctx , devicePath , req .TargetPath , publishInfo .MountOptions , false )
26412692 if err != nil {
26422693 return nil , status .Errorf (codes .Internal , "unable to mount device; %s" , err )
26432694 }
0 commit comments