Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 22 additions & 3 deletions internal/luks2/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,37 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"sync"

"github.com/snapcore/snapd/osutil"
)

var (
systemdCryptsetupPath = "/lib/systemd/systemd-cryptsetup"
systemdCryptsetupPath string
once sync.Once
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this once to link it to systemdCryptsetupPath, to show that both are related.

However, I would remove that until we have demonstrated that getting an environment has a high cost compared to fork/exec in a subprocess.


// getSystemdCryptsetupPath is internal and can be overridden by tests.
getSystemdCryptsetupPath = defaultSystemdCryptsetupPath
)

func defaultSystemdCryptsetupPath() string {
once.Do(func() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
once.Do(func() {
root := "/"
if p := os.Getenv("SNAP"); p != "" {
root = p
}
return filepath.Join(root, "lib", "systemd", "systemd-cryptsetup")

systemdCryptsetupPath = "/lib/systemd/systemd-cryptsetup"
if snapPath := os.Getenv("SNAP"); snapPath != "" {
systemdCryptsetupPath = filepath.Join(snapPath, "usr/bin/systemd-cryptsetup")
}
})

return systemdCryptsetupPath
}

// Activate unlocks the LUKS device at sourceDevicePath using systemd-cryptsetup and creates a device
// mapping with the supplied volumeName. The device is unlocked using the supplied key. The slot
// arguments specifies which keyslot ID to use - set this to AnySlot to activate with any keyslot.
func Activate(volumeName, sourceDevicePath string, key []byte, slot int) error {
cmd := exec.Command(systemdCryptsetupPath,
systemdCryptsetup := getSystemdCryptsetupPath()
cmd := exec.Command(systemdCryptsetup,
// attach <sourceDevicePath> to /dev/mapper/<volumeName>
"attach", volumeName, sourceDevicePath,
// read key from stdin
Expand All @@ -56,7 +74,8 @@ func Activate(volumeName, sourceDevicePath string, key []byte, slot int) error {

// Deactivate detaches the LUKS volume with the supplied name.
func Deactivate(volumeName string) error {
cmd := exec.Command(systemdCryptsetupPath, "detach", volumeName)
systemdCryptsetup := getSystemdCryptsetupPath()
cmd := exec.Command(systemdCryptsetup, "detach", volumeName)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "SYSTEMD_LOG_TARGET=console")

Expand Down
6 changes: 3 additions & 3 deletions internal/luks2/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func MockDataDeviceInfo(stMock *unix.Stat_t) (restore func()) {
}

func MockSystemdCryptsetupPath(path string) (restore func()) {
origSystemdCryptsetupPath := systemdCryptsetupPath
systemdCryptsetupPath = path
origFn := getSystemdCryptsetupPath
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I would just use orig (you generally don’t suffix with the variable type name)

getSystemdCryptsetupPath = func() string { return path }
return func() {
systemdCryptsetupPath = origSystemdCryptsetupPath
getSystemdCryptsetupPath = origFn
}
}

Expand Down
Loading