-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsystem.go
78 lines (66 loc) · 1.79 KB
/
system.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"path/filepath"
"os/exec"
)
func EncryptDisk() {
cmd := exec.Command("cryptsetup", "--allow-discards", "--cipher", "aes-xts-plain64", "--key-file", "-", "--key-size=256", "open", "--type", "plain", *device, EncryptedDM())
cmd.Stdin = bytes.NewReader(DecryptDataKey())
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
func CheckShell() {
cmd := exec.Command("base64")
cmd.Stdin = bytes.NewReader(DecryptDataKey())
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
func StdOutKey() {
os.Stdout.Write(DecryptDataKey())
}
func DmiString(name string) string {
val, err := ioutil.ReadFile(fmt.Sprintf("/sys/devices/virtual/dmi/id/%s", name))
HandleError(err)
return strings.TrimSpace(string(val))
}
func Contextify(str string) string {
return strings.ToLower(strings.Replace(strings.Replace(strings.Replace(str, "-", "", -1), " ", "", -1), ".", "", -1))
}
func ComputerContext() string {
if *computerContext != "" {
return *computerContext
}
context := []string{DmiString("board_vendor"), DmiString("board_serial"), DmiString("product_uuid")}
return Contextify(strings.Join(context, ""))
}
func DiskContext(device string) string {
if *deviceName != "" {
return Contextify(*deviceName)
} else {
sysfs := fmt.Sprintf("/sys/block/%s/device/wwid", RealDiskDevice(device))
blockInfo, err := ioutil.ReadFile(sysfs)
if err != nil {
log.Fatal(err)
}
fields := strings.Fields(string(blockInfo))
return Contextify(strings.Join(fields, ""))
}
}
func EncryptedDM() string {
return fmt.Sprintf("dmcrypt-%s", RealDiskDevice(*device))
}
func RealDiskDevice(device string) string {
deviceInfo, err := filepath.EvalSymlinks(device)
if err != nil {
log.Fatal(err)
}
return filepath.Base(deviceInfo)
}