Skip to content

Commit

Permalink
checks if var is encrypted before trying to decrypt it
Browse files Browse the repository at this point in the history
  • Loading branch information
taukakao committed Jul 22, 2024
1 parent 159e9c8 commit 4efe354
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cmd/unlock-var.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ package cmd
*/

import (
"errors"
"os"
"os/exec"
"path/filepath"

"github.com/spf13/cobra"

"github.com/vanilla-os/abroot/core"
"github.com/vanilla-os/abroot/settings"
"github.com/vanilla-os/orchid/cmdr"
)

Expand All @@ -37,6 +40,12 @@ func (e *VarInvalidError) Error() string {
return "the /var disk " + e.passedDisk + " does not exist"
}

type NotEncryptedError struct{}

func (e *NotEncryptedError) Error() string {
return "the var partition is not encrypted"
}

func NewUnlockVarCommand() *cmdr.Command {
cmd := cmdr.NewCommand(
"unlock-var",
Expand All @@ -63,6 +72,15 @@ func NewUnlockVarCommand() *cmdr.Command {
),
)

cmd.WithBoolFlag(
cmdr.NewBoolFlag(
"check-encrypted",
"c",
"check if drive is encrypted and return",
false,
),
)

cmd.Example = "abroot unlock-var"

cmd.Hidden = true
Expand Down Expand Up @@ -93,6 +111,20 @@ func unlockVar(cmd *cobra.Command, _ []string) error {
return err
}

check_only, err := cmd.Flags().GetBool("check-encrypted")
if err != nil {
return err
}

_, err = os.Stat(filepath.Join("/dev/disk/by-label/", settings.Cnf.PartLabelVar))
if err == nil || !errors.Is(err, os.ErrNotExist) {
return &NotEncryptedError{}
}
if check_only {
cmdr.Info.Println("The var partition is encrypted.")
return nil
}

if varDisk == "" {
return &VarConfigError{}
}
Expand Down

0 comments on commit 4efe354

Please sign in to comment.