Skip to content

Commit f8206c6

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 e9e0a7f commit f8206c6

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

csi/crypto/crypto.go

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

148+
// IsDeviceOpenAtNullPath determines if encrypted device is already open at unknown device path. The command 'cryptsetup status [device]' show "device: (null)"
149+
func IsDeviceOpenAtNullPath(device string) (bool, error) {
150+
devPath, mappedFile, err := DeviceEncryptionStatus(device)
151+
if err != nil {
152+
return false, err
153+
}
154+
if mappedFile != "" && strings.Contains(devPath, "null") {
155+
return true, nil
156+
}
157+
return false, nil
158+
}
159+
148160
// IsDeviceOpen determines if encrypted device is already open.
149161
func IsDeviceOpen(device string) (bool, error) {
150162
_, mappedFile, err := DeviceEncryptionStatus(device)

csi/node_server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,15 @@ 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+
if isOpenAtNullPath, err := crypto.IsDeviceOpenAtNullPath(cryptoDevice); err != nil {
491+
return nil, status.Error(codes.Internal, err.Error())
492+
} else if isOpenAtNullPath {
493+
log.Infof("Volume %s closing active crypto device %s", volumeID, cryptoDevice)
494+
if err := crypto.CloseVolume(volumeID); err != nil {
495+
return nil, status.Error(codes.Internal, err.Error())
496+
}
497+
}
498+
490499
if err := crypto.OpenVolume(volumeID, devicePath, passphrase); err != nil {
491500
return nil, status.Error(codes.Internal, err.Error())
492501
}

0 commit comments

Comments
 (0)