Skip to content

Commit

Permalink
fix(encrypt): close encrypted volume if it is opened
Browse files Browse the repository at this point in the history
In normal process of attaching a volume via CSI, the encrypted volume
should be in closed or inactivated state before Longhorn attempts to
open it.

ref: longhorn/longhorn 9385

Signed-off-by: James Lu <james.lu@suse.com>
  • Loading branch information
mantissahz committed Sep 9, 2024
1 parent f45adc0 commit c6c1511
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions csi/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ func ResizeEncryptoDevice(volume, passphrase string) error {
return err
}

// IsDeviceOpenAtNullPath determines if encrypted device is already open at unknown device path. The command 'cryptsetup status [device]' show "device: (null)"
func IsDeviceOpenAtNullPath(device string) (bool, error) {
devPath, mappedFile, err := DeviceEncryptionStatus(device)
if err != nil {
return false, err
}

return mappedFile != "" && strings.Contains(devPath, "null"), nil
}

// IsDeviceOpen determines if encrypted device is already open.
func IsDeviceOpen(device string) (bool, error) {
_, mappedFile, err := DeviceEncryptionStatus(device)
Expand Down
12 changes: 12 additions & 0 deletions csi/node_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,18 @@ func (ns *NodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
cryptoDevice := crypto.VolumeMapper(volumeID)
log.Infof("Volume %s requires crypto device %s", volumeID, cryptoDevice)

// check if the crypto device is open at the null path.
// this will happen if the crypto device is not closed properly and a new attaching request is made on the same node.
// reference issue: https://github.com/longhorn/longhorn/issues/9385
if isOpenAtNullPath, err := crypto.IsDeviceOpenAtNullPath(cryptoDevice); err != nil {
return nil, status.Error(codes.Internal, err.Error())
} else if isOpenAtNullPath {
log.Infof("Closing active crypto device %s for volume %s", cryptoDevice, volumeID)
if err := crypto.CloseVolume(volumeID); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
}

if err := crypto.OpenVolume(volumeID, devicePath, passphrase); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down

0 comments on commit c6c1511

Please sign in to comment.