2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package cryptmapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"path/filepath"
|
2022-05-02 07:48:57 -04:00
|
|
|
"strings"
|
2022-03-22 11:03:15 -04:00
|
|
|
"sync"
|
2022-05-02 09:24:07 -04:00
|
|
|
"time"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/crypto"
|
2022-03-23 06:00:07 -04:00
|
|
|
cryptsetup "github.com/martinjungblut/go-cryptsetup"
|
2022-03-22 11:03:15 -04:00
|
|
|
mount "k8s.io/mount-utils"
|
|
|
|
utilexec "k8s.io/utils/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-05-13 03:24:54 -04:00
|
|
|
// LUKSHeaderSize is the amount of bytes taken up by the header of a LUKS2 partition.
|
|
|
|
// The header is 16MiB (1048576 Bytes * 16).
|
|
|
|
LUKSHeaderSize = 16777216
|
2022-05-02 07:48:57 -04:00
|
|
|
cryptPrefix = "/dev/mapper/"
|
|
|
|
integritySuffix = "_dif"
|
|
|
|
integrityFSSuffix = "-integrity"
|
|
|
|
keySizeIntegrity = 96
|
|
|
|
keySizeCrypt = 64
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// packageLock is needed to block concurrent use of package functions, since libcryptsetup is not thread safe.
|
|
|
|
// See: https://gitlab.com/cryptsetup/cryptsetup/-/issues/710
|
2022-08-05 09:30:23 -04:00
|
|
|
//
|
|
|
|
// https://stackoverflow.com/questions/30553386/cryptsetup-backend-safe-with-multithreading
|
2022-03-22 11:03:15 -04:00
|
|
|
var packageLock = sync.Mutex{}
|
|
|
|
|
2022-05-10 04:43:48 -04:00
|
|
|
func init() {
|
|
|
|
cryptsetup.SetDebugLevel(cryptsetup.CRYPT_LOG_NORMAL)
|
2022-08-25 11:01:54 -04:00
|
|
|
cryptsetup.SetLogCallback(func(_ int, message string) { fmt.Printf("libcryptsetup: %s\n", message) })
|
2022-05-10 04:43:48 -04:00
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// KeyCreator is an interface to create data encryption keys.
|
|
|
|
type KeyCreator interface {
|
2022-03-24 10:21:19 -04:00
|
|
|
GetDEK(ctx context.Context, dekID string, dekSize int) ([]byte, error)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeviceMapper is an interface for device mapper methods.
|
|
|
|
type DeviceMapper interface {
|
|
|
|
// Init initializes a crypt device backed by 'devicePath'.
|
2022-03-23 06:00:07 -04:00
|
|
|
// Sets the deviceMapper to the newly allocated Device or returns any error encountered.
|
2022-03-22 11:03:15 -04:00
|
|
|
Init(devicePath string) error
|
2022-05-10 04:43:48 -04:00
|
|
|
// InitByName initializes a crypt device from provided active device 'name'.
|
|
|
|
// Sets the deviceMapper to the newly allocated Device or returns any error encountered.
|
|
|
|
InitByName(name string) error
|
|
|
|
// ActivateByPassphrase activates a device by using a passphrase from a specific keyslot.
|
|
|
|
// Returns nil on success, or an error otherwise.
|
|
|
|
ActivateByPassphrase(deviceName string, keyslot int, passphrase string, flags int) error
|
2022-03-22 11:03:15 -04:00
|
|
|
// ActivateByVolumeKey activates a device by using a volume key.
|
|
|
|
// Returns nil on success, or an error otherwise.
|
2022-05-10 04:43:48 -04:00
|
|
|
ActivateByVolumeKey(deviceName string, volumeKey string, volumeKeySize int, flags int) error
|
2022-03-22 11:03:15 -04:00
|
|
|
// Deactivate deactivates a device.
|
|
|
|
// Returns nil on success, or an error otherwise.
|
|
|
|
Deactivate(deviceName string) error
|
|
|
|
// Format formats a Device, using a specific device type, and type-independent parameters.
|
|
|
|
// Returns nil on success, or an error otherwise.
|
|
|
|
Format(deviceType cryptsetup.DeviceType, genericParams cryptsetup.GenericParams) error
|
|
|
|
// Free releases crypt device context and used memory.
|
|
|
|
Free() bool
|
2022-05-11 05:08:23 -04:00
|
|
|
// GetDeviceName gets the path to the underlying device.
|
|
|
|
GetDeviceName() string
|
2022-05-19 02:47:17 -04:00
|
|
|
// GetUUID gets the devices UUID
|
|
|
|
GetUUID() string
|
2022-03-22 11:03:15 -04:00
|
|
|
// Load loads crypt device parameters from the on-disk header.
|
|
|
|
// Returns nil on success, or an error otherwise.
|
2022-03-23 06:00:07 -04:00
|
|
|
Load(cryptsetup.DeviceType) error
|
2022-05-10 04:43:48 -04:00
|
|
|
// KeyslotAddByVolumeKey adds a key slot using a volume key to perform the required security check.
|
|
|
|
// Returns nil on success, or an error otherwise.
|
|
|
|
KeyslotAddByVolumeKey(keyslot int, volumeKey string, passphrase string) error
|
2022-03-22 11:03:15 -04:00
|
|
|
// Wipe removes existing data and clears the device for use with dm-integrity.
|
|
|
|
// Returns nil on success, or an error otherwise.
|
|
|
|
Wipe(devicePath string, pattern int, offset, length uint64, wipeBlockSize int, flags int, progress func(size, offset uint64) int) error
|
2022-05-09 08:35:13 -04:00
|
|
|
// Resize the crypt device.
|
|
|
|
// Returns nil on success, or an error otherwise.
|
|
|
|
Resize(name string, newSize uint64) error
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-07-08 04:59:59 -04:00
|
|
|
// CryptDevice is a wrapper for cryptsetup.Device.
|
2022-03-22 11:03:15 -04:00
|
|
|
type CryptDevice struct {
|
|
|
|
*cryptsetup.Device
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes a crypt device backed by 'devicePath'.
|
|
|
|
// Sets the cryptDevice's deviceMapper to the newly allocated Device or returns any error encountered.
|
|
|
|
func (c *CryptDevice) Init(devicePath string) error {
|
|
|
|
device, err := cryptsetup.Init(devicePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Device = device
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-10 04:43:48 -04:00
|
|
|
// InitByName initializes a crypt device from provided active device 'name'.
|
|
|
|
// Sets the deviceMapper to the newly allocated Device or returns any error encountered.
|
|
|
|
func (c *CryptDevice) InitByName(name string) error {
|
|
|
|
device, err := cryptsetup.InitByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Device = device
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// Free releases crypt device context and used memory.
|
|
|
|
func (c *CryptDevice) Free() bool {
|
|
|
|
res := c.Device.Free()
|
|
|
|
c.Device = nil
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-05-09 08:35:13 -04:00
|
|
|
// CryptMapper manages dm-crypt volumes.
|
|
|
|
type CryptMapper struct {
|
|
|
|
mapper DeviceMapper
|
|
|
|
kms KeyCreator
|
|
|
|
}
|
|
|
|
|
|
|
|
// New initializes a new CryptMapper with the given kms client and key-encryption-key ID.
|
|
|
|
// kms is used to fetch data encryption keys for the dm-crypt volumes.
|
|
|
|
func New(kms KeyCreator, mapper DeviceMapper) *CryptMapper {
|
|
|
|
return &CryptMapper{
|
|
|
|
mapper: mapper,
|
|
|
|
kms: kms,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// CloseCryptDevice closes the crypt device mapped for volumeID.
|
|
|
|
// Returns nil if the volume does not exist.
|
|
|
|
func (c *CryptMapper) CloseCryptDevice(volumeID string) error {
|
|
|
|
source, err := filepath.EvalSymlinks(cryptPrefix + volumeID)
|
|
|
|
if err != nil {
|
|
|
|
var pathErr *fs.PathError
|
|
|
|
if errors.As(err, &pathErr) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("getting device path for disk %q: %w", cryptPrefix+volumeID, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
if err := closeCryptDevice(c.mapper, source, volumeID, "crypt"); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("closing crypt device: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
integrity, err := filepath.EvalSymlinks(cryptPrefix + volumeID + integritySuffix)
|
|
|
|
if err == nil {
|
|
|
|
// If device was created with integrity, we need to also close the integrity device
|
|
|
|
integrityErr := closeCryptDevice(c.mapper, integrity, volumeID+integritySuffix, "integrity")
|
|
|
|
if integrityErr != nil {
|
|
|
|
return integrityErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
var pathErr *fs.PathError
|
|
|
|
if errors.As(err, &pathErr) {
|
|
|
|
// integrity device does not exist
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("getting device path for disk %q: %w", cryptPrefix+volumeID, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-09 08:35:13 -04:00
|
|
|
// OpenCryptDevice maps the volume at source to the crypt device identified by volumeID.
|
|
|
|
// The key used to encrypt the volume is fetched using CryptMapper's kms client.
|
|
|
|
func (c *CryptMapper) OpenCryptDevice(ctx context.Context, source, volumeID string, integrity bool) (string, error) {
|
|
|
|
m := &mount.SafeFormatAndMount{Exec: utilexec.New()}
|
2022-05-19 02:47:17 -04:00
|
|
|
return openCryptDevice(ctx, c.mapper, source, volumeID, integrity, c.kms.GetDEK, m.GetDiskFormat)
|
2022-05-10 04:43:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResizeCryptDevice resizes the underlying crypt device and returns the mapped device path.
|
|
|
|
func (c *CryptMapper) ResizeCryptDevice(ctx context.Context, volumeID string) (string, error) {
|
2022-11-30 02:35:38 -05:00
|
|
|
if err := resizeCryptDevice(ctx, c.mapper, volumeID, c.kms.GetDEK); err != nil {
|
2022-05-10 04:43:48 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cryptPrefix + volumeID, nil
|
2022-05-09 08:35:13 -04:00
|
|
|
}
|
|
|
|
|
2022-07-08 04:59:59 -04:00
|
|
|
// GetDevicePath returns the device path of a mapped crypt device.
|
2022-05-11 05:08:23 -04:00
|
|
|
func (c *CryptMapper) GetDevicePath(volumeID string) (string, error) {
|
|
|
|
return getDevicePath(c.mapper, strings.TrimPrefix(volumeID, cryptPrefix))
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// closeCryptDevice closes the crypt device mapped for volumeID.
|
|
|
|
func closeCryptDevice(device DeviceMapper, source, volumeID, deviceType string) error {
|
|
|
|
packageLock.Lock()
|
|
|
|
defer packageLock.Unlock()
|
|
|
|
|
2022-05-10 04:43:48 -04:00
|
|
|
if err := device.InitByName(volumeID); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("initializing dm-%s to unmap device %q: %w", deviceType, source, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
defer device.Free()
|
|
|
|
|
|
|
|
if err := device.Deactivate(volumeID); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("deactivating dm-%s volume %q for device %q: %w", deviceType, cryptPrefix+volumeID, source, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// openCryptDevice maps the volume at source to the crypt device identified by volumeID.
|
2022-05-19 02:47:17 -04:00
|
|
|
func openCryptDevice(ctx context.Context, device DeviceMapper, source, volumeID string, integrity bool,
|
|
|
|
getKey func(ctx context.Context, keyID string, keySize int) ([]byte, error), diskInfo func(disk string) (string, error),
|
|
|
|
) (string, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
packageLock.Lock()
|
|
|
|
defer packageLock.Unlock()
|
|
|
|
|
|
|
|
var integrityType string
|
2022-05-10 04:43:48 -04:00
|
|
|
keySize := keySizeCrypt
|
2022-03-22 11:03:15 -04:00
|
|
|
if integrity {
|
|
|
|
integrityType = "hmac(sha256)"
|
2022-05-10 04:43:48 -04:00
|
|
|
keySize = keySizeIntegrity
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the block device
|
|
|
|
if err := device.Init(source); err != nil {
|
|
|
|
return "", fmt.Errorf("initializing dm-crypt to map device %q: %w", source, err)
|
|
|
|
}
|
|
|
|
defer device.Free()
|
|
|
|
|
2022-05-19 02:47:17 -04:00
|
|
|
var passphrase []byte
|
2022-03-22 11:03:15 -04:00
|
|
|
// Try to load LUKS headers
|
|
|
|
// If this fails, the device is either not formatted at all, or already formatted with a different FS
|
2022-05-09 08:35:13 -04:00
|
|
|
if err := device.Load(cryptsetup.LUKS2{}); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
format, err := diskInfo(source)
|
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return "", fmt.Errorf("determining if disk is formatted: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
if format != "" {
|
|
|
|
return "", fmt.Errorf("disk %q is already formatted as: %s", source, format)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Device is not formatted, so we can safely create a new LUKS2 partition
|
|
|
|
if err := device.Format(
|
|
|
|
cryptsetup.LUKS2{
|
|
|
|
SectorSize: 4096,
|
|
|
|
Integrity: integrityType,
|
|
|
|
PBKDFType: &cryptsetup.PbkdfType{
|
|
|
|
// Use low memory recommendation from https://datatracker.ietf.org/doc/html/rfc9106#section-7
|
|
|
|
Type: "argon2id",
|
|
|
|
TimeMs: 2000,
|
|
|
|
Iterations: 3,
|
|
|
|
ParallelThreads: 4,
|
|
|
|
MaxMemoryKb: 65536, // ~64MiB
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cryptsetup.GenericParams{
|
|
|
|
Cipher: "aes",
|
|
|
|
CipherMode: "xts-plain64",
|
|
|
|
VolumeKeySize: keySize,
|
|
|
|
}); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return "", fmt.Errorf("formatting device %q: %w", source, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-05-10 04:43:48 -04:00
|
|
|
|
2022-05-19 02:47:17 -04:00
|
|
|
uuid := device.GetUUID()
|
2022-07-26 04:58:39 -04:00
|
|
|
passphrase, err = getKey(ctx, uuid, crypto.StateDiskKeyLength)
|
2022-05-19 02:47:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-07-26 04:58:39 -04:00
|
|
|
if len(passphrase) != crypto.StateDiskKeyLength {
|
|
|
|
return "", fmt.Errorf("expected key length to be [%d] but got [%d]", crypto.StateDiskKeyLength, len(passphrase))
|
2022-05-19 02:47:17 -04:00
|
|
|
}
|
|
|
|
|
2022-05-10 04:43:48 -04:00
|
|
|
// Add a new keyslot using the internal volume key
|
2022-05-19 02:47:17 -04:00
|
|
|
if err := device.KeyslotAddByVolumeKey(0, "", string(passphrase)); err != nil {
|
2022-05-10 04:43:48 -04:00
|
|
|
return "", fmt.Errorf("adding keyslot: %w", err)
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-05-19 02:47:17 -04:00
|
|
|
if integrity {
|
|
|
|
if err := performWipe(device, volumeID); err != nil {
|
|
|
|
return "", fmt.Errorf("wiping device: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uuid := device.GetUUID()
|
2022-07-26 04:58:39 -04:00
|
|
|
passphrase, err = getKey(ctx, uuid, crypto.StateDiskKeyLength)
|
2022-05-19 02:47:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-07-26 04:58:39 -04:00
|
|
|
if len(passphrase) != crypto.StateDiskKeyLength {
|
|
|
|
return "", fmt.Errorf("expected key length to be [%d] but got [%d]", crypto.StateDiskKeyLength, len(passphrase))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 02:47:17 -04:00
|
|
|
if err := device.ActivateByPassphrase(volumeID, 0, string(passphrase), 0); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return "", fmt.Errorf("trying to activate dm-crypt volume: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cryptPrefix + volumeID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// performWipe handles setting up parameters and clearing the device for dm-integrity.
|
2022-05-10 04:43:48 -04:00
|
|
|
func performWipe(device DeviceMapper, volumeID string) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
tmpDevice := "temporary-cryptsetup-" + volumeID
|
|
|
|
|
|
|
|
// Active as temporary device
|
2022-05-10 04:43:48 -04:00
|
|
|
if err := device.ActivateByVolumeKey(tmpDevice, "", 0, (cryptsetup.CRYPT_ACTIVATE_PRIVATE | cryptsetup.CRYPT_ACTIVATE_NO_JOURNAL)); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return fmt.Errorf("trying to activate temporary dm-crypt volume: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-08-25 11:01:54 -04:00
|
|
|
// No terminal available, limit callbacks to once every 30 seconds to not fill up logs with large amount of progress updates
|
|
|
|
ticker := time.NewTicker(30 * time.Second)
|
|
|
|
firstReq := make(chan struct{}, 1)
|
|
|
|
firstReq <- struct{}{}
|
|
|
|
defer ticker.Stop()
|
2022-05-02 09:24:07 -04:00
|
|
|
|
2022-08-25 11:01:54 -04:00
|
|
|
logProgress := func(size, offset uint64) {
|
|
|
|
prog := (float64(offset) / float64(size)) * 100
|
|
|
|
fmt.Printf("Wipe in progress: %.2f%%\n", prog)
|
|
|
|
}
|
2022-05-02 09:24:07 -04:00
|
|
|
|
2022-08-25 11:01:54 -04:00
|
|
|
progressCallback := func(size, offset uint64) int {
|
|
|
|
select {
|
|
|
|
case <-firstReq:
|
|
|
|
logProgress(size, offset)
|
|
|
|
case <-ticker.C:
|
|
|
|
logProgress(size, offset)
|
|
|
|
default:
|
2022-05-02 09:24:07 -04:00
|
|
|
}
|
2022-08-25 11:01:54 -04:00
|
|
|
|
|
|
|
return 0
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-05-10 04:43:48 -04:00
|
|
|
// Wipe the device using the same options as used in cryptsetup: https://gitlab.com/cryptsetup/cryptsetup/-/blob/v2.4.3/src/cryptsetup.c#L1345
|
2022-03-22 11:03:15 -04:00
|
|
|
if err := device.Wipe(cryptPrefix+tmpDevice, cryptsetup.CRYPT_WIPE_ZERO, 0, 0, 1024*1024, 0, progressCallback); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deactivate the temporary device
|
|
|
|
if err := device.Deactivate(tmpDevice); err != nil {
|
|
|
|
return fmt.Errorf("deactivating temporary volume: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-02 07:48:57 -04:00
|
|
|
|
2022-11-30 02:35:38 -05:00
|
|
|
func resizeCryptDevice(ctx context.Context, device DeviceMapper, name string,
|
|
|
|
getKey func(ctx context.Context, keyID string, keySize int) ([]byte, error),
|
|
|
|
) error {
|
2022-05-10 04:43:48 -04:00
|
|
|
packageLock.Lock()
|
|
|
|
defer packageLock.Unlock()
|
|
|
|
|
|
|
|
if err := device.InitByName(name); err != nil {
|
|
|
|
return fmt.Errorf("initializing device: %w", err)
|
|
|
|
}
|
|
|
|
defer device.Free()
|
|
|
|
|
|
|
|
if err := device.Load(cryptsetup.LUKS2{}); err != nil {
|
|
|
|
return fmt.Errorf("loading device: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-30 02:35:38 -05:00
|
|
|
passphrase, err := getKey(ctx, device.GetUUID(), crypto.StateDiskKeyLength)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := device.ActivateByPassphrase("", 0, string(passphrase), cryptsetup.CRYPT_ACTIVATE_KEYRING_KEY); err != nil {
|
2022-05-10 04:43:48 -04:00
|
|
|
return fmt.Errorf("activating keyrung for crypt device %q with passphrase: %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := device.Resize(name, 0); err != nil {
|
|
|
|
return fmt.Errorf("resizing device: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-11 05:08:23 -04:00
|
|
|
func getDevicePath(device DeviceMapper, name string) (string, error) {
|
|
|
|
packageLock.Lock()
|
|
|
|
defer packageLock.Unlock()
|
|
|
|
|
|
|
|
if err := device.InitByName(name); err != nil {
|
|
|
|
return "", fmt.Errorf("initializing device: %w", err)
|
|
|
|
}
|
|
|
|
defer device.Free()
|
|
|
|
|
|
|
|
deviceName := device.GetDeviceName()
|
|
|
|
if deviceName == "" {
|
|
|
|
return "", errors.New("unable to determine device name")
|
|
|
|
}
|
|
|
|
return deviceName, nil
|
|
|
|
}
|
|
|
|
|
2022-05-02 07:48:57 -04:00
|
|
|
// IsIntegrityFS checks if the fstype string contains an integrity suffix.
|
|
|
|
// If yes, returns the trimmed fstype and true, fstype and false otherwise.
|
|
|
|
func IsIntegrityFS(fstype string) (string, bool) {
|
|
|
|
if strings.HasSuffix(fstype, integrityFSSuffix) {
|
|
|
|
return strings.TrimSuffix(fstype, integrityFSSuffix), true
|
|
|
|
}
|
|
|
|
return fstype, false
|
|
|
|
}
|