Skip to content

Commit e11e152

Browse files
committed
fix(encrypt): close encrypted volume if it is opened
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 <[email protected]>
1 parent f45adc0 commit e11e152

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

csi/crypto/crypto.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ func ResizeEncryptoDevice(volume, passphrase string) error {
145145
return err
146146
}
147147

148+
// IsDeviceMappedToNullPath determines if encrypted device is already open at a null path. The command 'cryptsetup status [crypted_device]' show "device: (null)"
149+
func IsDeviceMappedToNullPath(device string) (bool, error) {
150+
devPath, mappedFile, err := DeviceEncryptionStatus(device)
151+
if err != nil {
152+
return false, err
153+
}
154+
155+
return mappedFile != "" && strings.Compare(devPath, "(null)") == 0, nil
156+
}
157+
148158
// IsDeviceOpen determines if encrypted device is already open.
149159
func IsDeviceOpen(device string) (bool, error) {
150160
_, mappedFile, err := DeviceEncryptionStatus(device)

csi/node_server.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,18 @@ func (ns *NodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
487487
cryptoDevice := crypto.VolumeMapper(volumeID)
488488
log.Infof("Volume %s requires crypto device %s", volumeID, cryptoDevice)
489489

490+
// check if the crypto device is open at the null path.
491+
// this will happen if the crypto device is not closed properly and a new attaching request is made on the same node.
492+
// reference issue: https://github.com/longhorn/longhorn/issues/9385
493+
if isOpenAtNullPath, err := crypto.IsDeviceMappedToNullPath(cryptoDevice); err != nil {
494+
return nil, status.Error(codes.Internal, err.Error())
495+
} else if isOpenAtNullPath {
496+
log.Infof("Closing active crypto device %s for volume %s", cryptoDevice, volumeID)
497+
if err := crypto.CloseVolume(volumeID); err != nil {
498+
return nil, status.Error(codes.Internal, err.Error())
499+
}
500+
}
501+
490502
if err := crypto.OpenVolume(volumeID, devicePath, passphrase); err != nil {
491503
return nil, status.Error(codes.Internal, err.Error())
492504
}

0 commit comments

Comments
 (0)