forked from Foxboron/sbctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbctl.go
230 lines (194 loc) · 4.75 KB
/
sbctl.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package sbctl
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"github.com/foxboron/sbctl/fs"
"github.com/spf13/afero"
)
// Functions that doesn't fit anywhere else
type LsblkEntry struct {
Parttype string `json:"parttype"`
Mountpoint string `json:"mountpoint"`
Pttype string `json:"pttype"`
Fstype string `json:"fstype"`
}
type LsblkRoot struct {
Blockdevices []LsblkEntry `json:"blockdevices"`
}
var espLocations = []string{
"/boot",
"/boot/efi",
"/efi",
}
var ErrNoESP = errors.New("failed to find EFI system partition")
// Slightly more advanced check
func GetESP() (string, error) {
for _, env := range []string{"SYSTEMD_ESP_PATH", "ESP_PATH"} {
envEspPath, found := os.LookupEnv(env)
if found {
return envEspPath, nil
}
}
for _, location := range espLocations {
// "Read" a file inside all candiadate locations to trigger an
// automount if there's an automount partition.
_, _ = fs.Fs.Stat(fmt.Sprintf("%s/does-not-exist", location))
}
out, err := exec.Command(
"lsblk",
"--json",
"--output", "PARTTYPE,MOUNTPOINT,PTTYPE,FSTYPE").Output()
if err != nil {
return "", err
}
var lsblkRoot LsblkRoot
if err = json.Unmarshal(out, &lsblkRoot); err != nil {
return "", fmt.Errorf("failed to parse json: %v", err)
}
var pathBootEntry *LsblkEntry
var pathBootEfiEntry *LsblkEntry
var pathEfiEntry *LsblkEntry
for _, lsblkEntry := range lsblkRoot.Blockdevices {
switch lsblkEntry.Mountpoint {
case "/boot":
pathBootEntry = new(LsblkEntry)
*pathBootEntry = lsblkEntry
case "/boot/efi":
pathBootEfiEntry = new(LsblkEntry)
*pathBootEfiEntry = lsblkEntry
case "/efi":
pathEfiEntry = new(LsblkEntry)
*pathEfiEntry = lsblkEntry
}
}
for _, entryToCheck := range []*LsblkEntry{pathEfiEntry, pathBootEntry, pathBootEfiEntry} {
if entryToCheck == nil {
continue
}
if entryToCheck.Pttype != "gpt" {
continue
}
if entryToCheck.Fstype != "vfat" {
continue
}
if entryToCheck.Parttype != "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" {
continue
}
return entryToCheck.Mountpoint, nil
}
return "", ErrNoESP
}
func Sign(file, output string, enroll bool) error {
file, err := filepath.Abs(file)
if err != nil {
return err
}
if output == "" {
output = file
} else {
output, err = filepath.Abs(output)
if err != nil {
return err
}
}
files, err := ReadFileDatabase(DBPath)
if err != nil {
return fmt.Errorf("couldn't open database: %s", DBPath)
}
if entry, ok := files[file]; ok {
err = SignFile(DBKey, DBCert, entry.File, entry.OutputFile, entry.Checksum)
// return early if signing fails
if err != nil {
return err
}
checksum, err := ChecksumFile(file)
if err != nil {
return err
}
entry.Checksum = checksum
files[file] = entry
if err := WriteFileDatabase(DBPath, files); err != nil {
return err
}
} else {
err = SignFile(DBKey, DBCert, file, output, "")
// return early if signing fails
if err != nil {
return err
}
}
if enroll {
checksum, err := ChecksumFile(file)
if err != nil {
return err
}
files[file] = &SigningEntry{File: file, OutputFile: output, Checksum: checksum}
if err := WriteFileDatabase(DBPath, files); err != nil {
return err
}
}
return err
}
func CombineFiles(microcode, initramfs string) (afero.File, error) {
for _, file := range []string{microcode, initramfs} {
if _, err := fs.Fs.Stat(file); err != nil {
return nil, fmt.Errorf("%s: %w", file, errors.Unwrap(err))
}
}
tmpFile, err := afero.TempFile(fs.Fs, "/var/tmp", "initramfs-")
if err != nil {
return nil, err
}
one, _ := fs.Fs.Open(microcode)
defer one.Close()
two, _ := fs.Fs.Open(initramfs)
defer two.Close()
_, err = io.Copy(tmpFile, one)
if err != nil {
return nil, fmt.Errorf("failed to append microcode file to output: %w", err)
}
_, err = io.Copy(tmpFile, two)
if err != nil {
return nil, fmt.Errorf("failed to append initramfs file to output: %w", err)
}
return tmpFile, nil
}
func CreateBundle(bundle Bundle) error {
var microcode string
make_bundle := false
if bundle.IntelMicrocode != "" {
microcode = bundle.IntelMicrocode
make_bundle = true
} else if bundle.AMDMicrocode != "" {
microcode = bundle.AMDMicrocode
make_bundle = true
}
if make_bundle {
tmpFile, err := CombineFiles(microcode, bundle.Initramfs)
if err != nil {
return err
}
defer fs.Fs.Remove(tmpFile.Name())
bundle.Initramfs = tmpFile.Name()
}
out, err := GenerateBundle(&bundle)
if err != nil {
return err
}
if !out {
return fmt.Errorf("failed to generate bundle %s", bundle.Output)
}
return nil
}
// Checks if sbctl is setup on this computer
func CheckSbctlInstallation(path string) bool {
if _, err := fs.Fs.Stat(path); errors.Is(err, os.ErrNotExist) {
return false
}
return true
}