Add unlock and lock commands for decrypt-only operations

unlock: decrypts a LUKS volume without mounting. Idempotent — reports
existing cleartext device if already unlocked.

lock: locks a LUKS volume. If mounted, unmounts first (udisks2 with
privileged fallback) then locks. Idempotent — reports if already locked.

Both commands support alias completion and config resolution.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-24 09:37:19 -07:00
parent aadc01383b
commit ce10c41466
2 changed files with 127 additions and 0 deletions

59
cmd/unlock.go Normal file
View File

@@ -0,0 +1,59 @@
package cmd
import (
"fmt"
"git.wntrmute.dev/kyle/arca/internal/config"
"git.wntrmute.dev/kyle/arca/internal/udisks"
"git.wntrmute.dev/kyle/arca/internal/unlock"
"github.com/spf13/cobra"
)
var unlockCmd = &cobra.Command{
Use: "unlock <device|alias>",
Short: "Unlock a LUKS volume without mounting",
Args: cobra.ExactArgs(1),
RunE: runUnlock,
ValidArgsFunction: completeDeviceOrAlias,
}
func init() {
rootCmd.AddCommand(unlockCmd)
}
func runUnlock(cmd *cobra.Command, args []string) error {
cfg := config.Load()
target := args[0]
client, err := udisks.NewClient()
if err != nil {
return fmt.Errorf("connecting to udisks2: %w", err)
}
defer client.Close()
devCfg := cfg.ResolveDevice(target)
dev, err := client.FindDevice(devCfg.UUID, target)
if err != nil {
return err
}
// Check if already unlocked.
if cleartext, err := client.CleartextDevice(dev); err == nil {
fmt.Printf("%s is already unlocked (%s)\n", target, cleartext.DevicePath)
return nil
}
u := unlock.New(client, unlock.Options{
ReadPassphrase: readPassphrase,
KeyfilePath: devCfg.Keyfile,
})
result, err := u.Unlock(dev, devCfg.Methods)
if err != nil {
return err
}
fmt.Printf("Unlocked %s -> %s\n", target, result.Device.DevicePath)
return nil
}