-
Notifications
You must be signed in to change notification settings - Fork 466
/
grub.go
296 lines (231 loc) · 6.06 KB
/
grub.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package grub
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
goruntime "runtime"
"strings"
"text/template"
"github.com/talos-systems/go-blockdevice/blockdevice"
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime"
"github.com/talos-systems/talos/pkg/machinery/constants"
)
// Cfg reprsents the cfg file.
type Cfg struct {
Default string
Fallback string
Labels []*Label
}
// Label reprsents a label in the cfg file.
type Label struct {
Root string
Kernel string
Initrd string
Append string
}
const grubCfgTpl = `set default="{{ .Default }}"
{{ with .Fallback -}}
set fallback="{{ . }}"
{{- end }}
set timeout=3
insmod all_video
terminal_input console
terminal_output console
{{ range $label := .Labels -}}
menuentry "{{ $label.Root }}" {
set gfxmode=auto
set gfxpayload=text
linux {{ $label.Kernel }} {{ $label.Append }}
initrd {{ $label.Initrd }}
}
{{ end }}
`
const (
amd64 = "amd64"
arm64 = "arm64"
)
// Grub represents the grub bootloader.
type Grub struct {
BootDisk string
Arch string
}
// Labels implements the Bootloader interface.
func (g *Grub) Labels() (current, next string, err error) {
var b []byte
if b, err = ioutil.ReadFile(GrubConfig); err != nil {
if errors.Is(err, os.ErrNotExist) {
next = BootA
return current, next, nil
}
return "", "", err
}
re := regexp.MustCompile(`^set default="(.*)"`)
matches := re.FindAllSubmatch(b, -1)
if len(matches) != 1 {
return "", "", fmt.Errorf("failed to find default")
}
if len(matches[0]) != 2 {
return "", "", fmt.Errorf("expected 2 matches, got %d", len(matches[0]))
}
current = string(matches[0][1])
switch current {
case BootA:
next = BootB
case BootB:
next = BootA
default:
return "", "", fmt.Errorf("unknown grub menuentry: %q", current)
}
return current, next, err
}
// BootEntry describes GRUB boot entry.
type BootEntry struct {
// Paths to kernel and initramfs image.
Linux, Initrd string
// Cmdline for the kernel.
Cmdline string
}
// GetCurrentEntry fetches current boot entry, vmlinuz/initrd path, boot args.
//
//nolint:gocyclo
func (g *Grub) GetCurrentEntry() (*BootEntry, error) {
f, err := os.Open(GrubConfig)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}
return nil, err
}
defer f.Close() //nolint:errcheck
scanner := bufio.NewScanner(f)
entry := &BootEntry{}
var (
defaultEntry string
currentEntry string
)
for scanner.Scan() {
line := scanner.Text()
switch {
case strings.HasPrefix(line, "set default"):
matches := regexp.MustCompile(`set default="(.*)"`).FindStringSubmatch(line)
if len(matches) != 2 {
return nil, fmt.Errorf("malformed default entry: %q", line)
}
defaultEntry = matches[1]
case strings.HasPrefix(line, "menuentry"):
matches := regexp.MustCompile(`menuentry "(.*)"`).FindStringSubmatch(line)
if len(matches) != 2 {
return nil, fmt.Errorf("malformed menuentry: %q", line)
}
currentEntry = matches[1]
case strings.HasPrefix(line, " linux "):
if currentEntry != defaultEntry {
continue
}
parts := strings.SplitN(line[8:], " ", 2)
entry.Linux = parts[0]
if len(parts) == 2 {
entry.Cmdline = parts[1]
}
case strings.HasPrefix(line, " initrd "):
if currentEntry != defaultEntry {
continue
}
entry.Initrd = line[9:]
}
}
if entry.Linux == "" || entry.Initrd == "" {
return nil, scanner.Err()
}
return entry, scanner.Err()
}
// Install implements the Bootloader interface. It sets up grub with the
// specified kernel parameters.
//
//nolint:gocyclo
func (g *Grub) Install(fallback string, config interface{}, sequence runtime.Sequence) (err error) {
grubcfg, ok := config.(*Cfg)
if !ok {
return errors.New("expected a grub config")
}
if err = writeCfg(GrubConfig, grubcfg); err != nil {
return err
}
dev, err := blockdevice.Open(g.BootDisk)
if err != nil {
return err
}
//nolint:errcheck
defer dev.Close()
// verify that BootDisk has boot partition
_, err = dev.GetPartition(constants.BootPartitionLabel)
if err != nil {
return err
}
blk := dev.Device().Name()
loopDevice := strings.HasPrefix(blk, "/dev/loop")
var platforms []string
switch g.Arch {
case amd64:
platforms = []string{"x86_64-efi", "i386-pc"}
case arm64:
platforms = []string{"arm64-efi"}
}
if goruntime.GOARCH == amd64 && g.Arch == amd64 && !loopDevice {
// let grub choose the platform automatically if not building an image
platforms = []string{""}
}
for _, platform := range platforms {
args := []string{"--boot-directory=" + constants.BootMountPoint, "--efi-directory=" + constants.EFIMountPoint, "--removable"}
if loopDevice {
args = append(args, "--no-nvram")
}
if platform != "" {
args = append(args, "--target="+platform)
}
args = append(args, blk)
log.Printf("executing: grub-install %s", strings.Join(args, " "))
cmd := exec.Command("grub-install", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
return fmt.Errorf("failed to install grub: %w", err)
}
}
return nil
}
// Default implements the bootloader interface.
func (g *Grub) Default(label string) (err error) {
var b []byte
if b, err = ioutil.ReadFile(GrubConfig); err != nil {
return err
}
re := regexp.MustCompile(`^set default="(.*)"`)
b = re.ReplaceAll(b, []byte(fmt.Sprintf(`set default="%s"`, label)))
log.Printf("writing %s to disk", GrubConfig)
return ioutil.WriteFile(GrubConfig, b, 0o600)
}
func writeCfg(path string, grubcfg *Cfg) (err error) {
b := []byte{}
wr := bytes.NewBuffer(b)
t := template.Must(template.New("grub").Parse(grubCfgTpl))
if err = t.Execute(wr, grubcfg); err != nil {
return err
}
dir := filepath.Dir(path)
if err = os.MkdirAll(dir, os.ModeDir); err != nil {
return err
}
log.Printf("writing %s to disk", path)
return ioutil.WriteFile(path, wr.Bytes(), 0o600)
}