diff --git a/utils/devices.go b/utils/devices.go index 935b7a581..d3dba02c8 100644 --- a/utils/devices.go +++ b/utils/devices.go @@ -8,9 +8,11 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" "path/filepath" "strconv" "strings" + "syscall" "time" "github.com/cenkalti/backoff/v4" @@ -1210,11 +1212,20 @@ func (d *LUKSDevice) EnsureFormattedAndOpen(ctx context.Context, luksPassphrase func ensureLUKSDevice(ctx context.Context, luksDevice LUKSDeviceInterface, luksPassphrase string) (formatted bool, err error) { formatted = false // Check if LUKS device is already opened + // if device is closed; err status will be 4 isOpen, err := luksDevice.IsOpen(ctx) if err != nil { - return formatted, err + status := 1 + if exitError, ok := err.(*exec.ExitError); ok { + status = exitError.Sys().(syscall.WaitStatus).ExitStatus() + } + if status != 0 && status != 4 { + return formatted, err + } } + if isOpen { + formatted = true return formatted, nil } @@ -1238,9 +1249,10 @@ func ensureLUKSDevice(ctx context.Context, luksDevice LUKSDeviceInterface, luksP if err != nil { return formatted, err } - formatted = true } + // The device should have either already had been LUKS formated or just have had LUKS installed. Hence, formatted will always be true + formatted = true // Open the device, creating a new LUKS encrypted device with the name chosen by us err = luksDevice.Open(ctx, luksPassphrase) if nil != err { diff --git a/utils/devices_linux.go b/utils/devices_linux.go index a80689f98..9a78b2527 100644 --- a/utils/devices_linux.go +++ b/utils/devices_linux.go @@ -183,10 +183,7 @@ func IsLUKSDeviceOpen(ctx context.Context, luksDevicePath string) (bool, error) _, err := execCommandWithTimeoutAndInput(ctx, "cryptsetup", luksCommandTimeout, true, "", "status", luksDevicePath) if err != nil { - if _, ok := err.(*exec.ExitError); !ok { - return false, err - } - return false, nil + return false, err } return true, nil }