From 2c50b01537ff504ad61acba5c2205505db448acf Mon Sep 17 00:00:00 2001 From: James Lu Date: Wed, 4 Sep 2024 22:08:09 +0800 Subject: [PATCH] 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 --- csi/crypto/crypto.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/csi/crypto/crypto.go b/csi/crypto/crypto.go index d22c4ad922..26cbfe33b3 100644 --- a/csi/crypto/crypto.go +++ b/csi/crypto/crypto.go @@ -97,8 +97,10 @@ func EncryptVolume(devicePath, passphrase string, cryptoParams *EncryptParams) e // OpenVolume opens volume so that it can be used by the client. func OpenVolume(volume, devicePath, passphrase string) error { if isOpen, _ := IsDeviceOpen(VolumeMapper(volume)); isOpen { - logrus.Infof("Device %s is already opened at %s", devicePath, VolumeMapper(volume)) - return nil + logrus.Infof("Device %s is already opened at %s, and close it first", devicePath, VolumeMapper(volume)) + if err := CloseVolume(volume); err != nil { + return err + } } namespaces := []lhtypes.Namespace{lhtypes.NamespaceMnt, lhtypes.NamespaceIpc}