Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions utils/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions utils/devices_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down