/
update.go
527 lines (464 loc) · 14.1 KB
/
update.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package gokrazy
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"hash"
"hash/crc32"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
"time"
"golang.org/x/sys/unix"
"github.com/gokrazy/internal/deviceconfig"
"github.com/gokrazy/internal/fat"
"github.com/gokrazy/internal/rootdev"
"github.com/google/renameio/v2"
)
var rootRe = regexp.MustCompile(`root=[^ ]+`)
func readCmdline() ([]byte, error) {
f, err := os.OpenFile(rootdev.Partition(rootdev.Boot), os.O_RDWR, 0600)
if err != nil {
return nil, err
}
defer f.Close()
rd, err := fat.NewReader(f)
if err != nil {
return nil, err
}
offset, length, err := rd.Extents("/cmdline.txt")
if err != nil {
return nil, err
}
if _, err := f.Seek(offset, io.SeekStart); err != nil {
return nil, err
}
b := make([]byte, length)
if _, err := f.Read(b); err != nil {
return nil, err
}
return b, nil
}
func modifyCmdlineFile(f *os.File, offset, length int64, replace func([]byte) []byte) error {
if _, err := f.Seek(offset, io.SeekStart); err != nil {
return err
}
b := make([]byte, length)
if _, err := f.Read(b); err != nil {
return err
}
if _, err := f.Seek(offset, io.SeekStart); err != nil {
return err
}
rep := replace(b)
if pad := len(b) - len(rep); pad > 0 {
// The file content length can shrink when switching from PARTUUID= (the
// default) to /dev/mmcblk0p[23], on an older gokrazy installation.
// Because we overwrite the file in place and have no means to truncate
// it to a smaller length, we pad the command line with spaces instead.
// Note that we need to insert spaces before the trailing newline,
// otherwise the system won’t boot:
rep = bytes.ReplaceAll(rep,
[]byte{'\n'},
append(bytes.Repeat([]byte{' '}, pad), '\n'))
}
if _, err := f.Write(rep); err != nil {
return err
}
return nil
}
func modifyCmdline(replace func([]byte) []byte) error {
f, err := os.OpenFile(rootdev.Partition(rootdev.Boot), os.O_RDWR, 0600)
if err != nil {
return err
}
defer f.Close()
rd, err := fat.NewReader(f)
if err != nil {
return err
}
offset, length, err := rd.Extents("/cmdline.txt")
if err != nil {
return err
}
if err := modifyCmdlineFile(f, offset, length, replace); err != nil {
return err
}
offset, length, err = rd.Extents("/loader/entries/gokrazy.conf")
if err != nil {
// This file might not be present yet when using a recent gokrazy
// installation with an older gokr-packer.
log.Printf("updating systemd-boot config: %v", err)
return nil
}
if err := modifyCmdlineFile(f, offset, length, replace); err != nil {
return err
}
return f.Close()
}
func switchRootPartition(newRootPartition int) error {
return modifyCmdline(func(b []byte) []byte {
return rootRe.ReplaceAllLiteral(b, []byte("root="+rootdev.PartitionCmdline(newRootPartition)))
})
}
func enableTestboot() error {
return modifyCmdline(func(b []byte) []byte {
return bytes.ReplaceAll(b,
[]byte{'\n'},
[]byte(" gokrazy.try_boot_inactive=1\n"))
})
}
func streamRequestTo(path string, offset int64, r io.Reader) error {
f, err := os.OpenFile(path, os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
if offset > 0 {
if _, err := f.Seek(offset, io.SeekStart); err != nil {
return err
}
}
if _, err := io.Copy(f, r); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
return f.Close()
}
// createFile is different from streamRequestTo in that it creates new files
// (instead of just overwriting existing ones) and not syncing to disk, because
// it is intended for the /uploadtemp handler.
func createFile(path string, r io.Reader) error {
f, err := renameio.TempFile("", path)
if err != nil {
return err
}
defer f.Cleanup()
if _, err := io.Copy(f, r); err != nil {
return err
}
return f.CloseAtomicallyReplace()
}
func nonConcurrentUpdateHandler(dest string) func(http.ResponseWriter, *http.Request) {
return nonConcurrentLimitedUpdateHandler(dest, 0, 0)
}
func nonConcurrentLimitedUpdateHandler(dest string, offset int64, maxLength int64) func(http.ResponseWriter, *http.Request) {
var mu sync.Mutex
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
http.Error(w, "expected a PUT request", http.StatusBadRequest)
return
}
mu.Lock()
defer mu.Unlock()
var hash hash.Hash
switch r.Header.Get("X-Gokrazy-Update-Hash") {
case "crc32":
hash = crc32.NewIEEE()
default:
hash = sha256.New()
}
var reader io.Reader = r.Body
if maxLength > 0 {
reader = io.LimitReader(reader, maxLength)
}
if err := streamRequestTo(dest, offset, io.TeeReader(reader, hash)); err != nil {
log.Printf("updating %q failed: %v", dest, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%x", hash.Sum(nil))
}
}
func nonConcurrentSwitchHandler(newRootPartition int) func(http.ResponseWriter, *http.Request) {
var mu sync.Mutex
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "expected a POST request", http.StatusBadRequest)
return
}
mu.Lock()
defer mu.Unlock()
if err := switchRootPartition(newRootPartition); err != nil {
log.Printf("switching root partition to %q failed: %v", newRootPartition, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func nonConcurrentTestbootHandler(newRootPartition int) func(http.ResponseWriter, *http.Request) {
var mu sync.Mutex
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "expected a POST request", http.StatusBadRequest)
return
}
mu.Lock()
defer mu.Unlock()
if err := enableTestboot(); err != nil {
log.Printf("enabling test-boot of new root partition %q failed: %v", newRootPartition, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
type extraUpdateHandler struct {
name string
device string
offset int64
maxLength int64
}
var extraUpdateHandlers []*extraUpdateHandler
func setupDeviceSpecifics() {
modelName := Model()
cfg, ok := deviceconfig.DeviceConfigs[modelName]
if !ok {
return
}
log.Printf("setting up device-specific update handlers for %q", modelName)
for _, rootDevFile := range cfg.RootDeviceFiles {
extraUpdateHandlers = append(extraUpdateHandlers, &extraUpdateHandler{
name: rootDevFile.Name,
device: rootdev.BlockDevice(),
offset: rootDevFile.Offset,
maxLength: rootDevFile.MaxLength,
})
}
}
// Allow for slow VM shutdown of 90 seconds, and then some
const defaultSignalDelay = 100 * time.Second
func initUpdate() error {
// The /update/features handler is used for negotiation of individual
// feature support (e.g. PARTUUID= support) between the packer and update
// target.
gpt := ""
if strings.Contains(rootdev.PARTUUID(), "-") {
gpt = ",gpt"
}
featureStr := fmt.Sprintf("partuuid,updatehash%s,", gpt)
http.HandleFunc("/update/features", func(w http.ResponseWriter, r *http.Request) {
if !jsonRequested(r) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
io.WriteString(w, featureStr)
return
}
// This response contains all information that the
// github.com/gokrazy/updater package needs to obtain from the
// target device, in one single go.
b, err := json.Marshal(struct {
Features string `json:"features"`
EEPROM *eepromVersion `json:"EEPROM"`
}{
Features: featureStr,
EEPROM: lastInstalledEepromVersion,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
})
http.HandleFunc("/update/mbr", nonConcurrentUpdateHandler(rootdev.BlockDevice()))
http.HandleFunc("/update/root", nonConcurrentUpdateHandler(rootdev.Partition(rootdev.InactiveRootPartition())))
http.HandleFunc("/update/switch", nonConcurrentSwitchHandler(rootdev.InactiveRootPartition()))
http.HandleFunc("/update/testboot", nonConcurrentTestbootHandler(rootdev.InactiveRootPartition()))
for _, extraUpdateHandler := range extraUpdateHandlers {
http.HandleFunc(fmt.Sprintf("/update/device-specific/%s", extraUpdateHandler.name), nonConcurrentLimitedUpdateHandler(extraUpdateHandler.device, extraUpdateHandler.offset, extraUpdateHandler.maxLength))
}
// bakery updates only the boot partition, which would reset the active root
// partition to 2.
updateHandler := nonConcurrentUpdateHandler(rootdev.Partition(rootdev.Boot))
http.HandleFunc("/update/boot", updateHandler)
http.HandleFunc("/update/bootonly", func(w http.ResponseWriter, r *http.Request) {
updateHandler(w, r)
if err := switchRootPartition(rootdev.ActiveRootPartition()); err != nil {
log.Printf("switching root partition to %d failed: %v", rootdev.ActiveRootPartition(), err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
http.HandleFunc("/reboot", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "expected a POST request", http.StatusBadRequest)
return
}
signalDelay := defaultSignalDelay
if s := r.FormValue("wait_per_signal"); s != "" {
var err error
signalDelay, err = time.ParseDuration(s)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
rc := http.NewResponseController(w)
start := time.Now()
killSupervisedServicesAndUmountPerm(signalDelay)
fmt.Fprintf(w, "All services killed in %s\n", time.Since(start))
rc.Flush()
log.Println("Rebooting")
w.Write([]byte("Rebooting...\n"))
rc.Flush()
go func() {
time.Sleep(time.Second) // give the http response some time to be sent
if err := reboot(r.FormValue("kexec") != "off"); err != nil {
log.Println("could not reboot:", err)
}
}()
})
http.HandleFunc("/poweroff", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "expected a POST request", http.StatusBadRequest)
return
}
signalDelay := defaultSignalDelay
if s := r.FormValue("wait_per_signal"); s != "" {
var err error
signalDelay, err = time.ParseDuration(s)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
rc := http.NewResponseController(w)
start := time.Now()
killSupervisedServicesAndUmountPerm(signalDelay)
fmt.Fprintf(w, "All services killed in %s\n", time.Since(start))
rc.Flush()
log.Println("Powering off")
w.Write([]byte("Powering off...\n"))
rc.Flush()
go func() {
time.Sleep(time.Second) // give the http response some time to be sent
if err := unix.Reboot(unix.LINUX_REBOOT_CMD_POWER_OFF); err != nil {
log.Println("could not power off:", err)
}
}()
})
http.HandleFunc("/uploadtemp/", uploadTemp)
http.HandleFunc("/divert", divert)
return nil
}
func killSupervisedServicesAndUmountPerm(signalDelay time.Duration) {
killSupervisedServices(signalDelay)
log.Println("Unmounting /perm")
if err := syscall.Unmount("/perm", unix.MNT_FORCE); err != nil {
log.Printf("unmounting /perm failed: %v", err)
}
}
func uploadTemp(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
http.Error(w, "expected a PUT request", http.StatusBadRequest)
return
}
dest := strings.TrimPrefix(r.URL.Path, "/uploadtemp/")
log.Printf("uploadtemp dest=%q", dest)
if strings.Contains(dest, "/") {
// relative path in the temp directory
dest = filepath.Join(os.TempDir(), dest)
if err := os.MkdirAll(filepath.Dir(dest), 0700); err != nil {
log.Printf("updating %q failed: %v", dest, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
// not relative, create a subdirectory underneath the temp directory
tmpdir, err := os.MkdirTemp("", "uploadtemp")
if err != nil {
log.Printf("updating %q failed: %v", dest, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// tmpdir will intentionally not be deleted, as the caller of
// /uploadtemp will use the uploaded file afterwards.
dest = filepath.Join(tmpdir, dest)
}
if err := createFile(dest, r.Body); err != nil {
log.Printf("updating %q failed: %v", dest, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
b, err := json.Marshal(struct {
Dest string `json:"dest"`
}{
Dest: dest,
})
if err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := w.Write(b); err != nil {
log.Print(err)
}
}
type divertRequest struct {
Path string `json:"Path"`
Diversion string `json:"Diversion"`
Flags []string `json:"Flags"`
}
func divert(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "expected a POST request", http.StatusBadRequest)
return
}
var req divertRequest
if r.Header.Get("Content-Type") == "application/json" {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "couldn't read request body", http.StatusBadRequest)
return
}
err = json.Unmarshal(body, &req)
if err != nil {
http.Error(w, "couldn't unmarshal request body", http.StatusBadRequest)
return
}
} else {
req.Diversion = r.FormValue("diversion")
req.Path = r.FormValue("path")
}
if req.Diversion == "" {
http.Error(w, "diversion parameter is not set", http.StatusBadRequest)
return
}
svc := findSvc(req.Path)
newService := svc == nil
if newService {
log.Printf("adding new service in-memory to make diversion work")
cmd := exec.Command(req.Path, req.Flags...)
svc = NewService(cmd).s
}
// Ensure diversion binary is executable (/uploadtemp creates regular,
// non-executable files).
diversion := filepath.Join(os.TempDir(), req.Diversion)
if err := os.Chmod(diversion, 0755); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
svc.setDiversion(diversion)
cmd := svc.Cmd()
cmd.Args = append([]string{cmd.Args[0]}, req.Flags...)
svc.setCmd(cmd)
if newService {
services.Lock()
services.S = append(services.S, svc)
services.Unlock()
go supervise(svc)
}
if err := restart(svc, syscall.SIGTERM); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}