blob: e4fbbbd2f5cc9dc920bc0200e070bfcf8d469796 [file] [log] [blame]
Lorenz Brun35fcf032023-06-29 04:15:58 +02001package update
2
3import (
4 "archive/zip"
5 "bytes"
6 "context"
Lorenz Brund79881d2023-11-30 19:02:06 +01007 "crypto/sha256"
Lorenz Brund14be0e2023-07-31 16:46:14 +02008 "debug/pe"
Lorenz Brund79881d2023-11-30 19:02:06 +01009 _ "embed"
Lorenz Brun35fcf032023-06-29 04:15:58 +020010 "errors"
11 "fmt"
12 "io"
13 "net/http"
14 "os"
15 "path/filepath"
16 "regexp"
17 "strconv"
Lorenz Brund14be0e2023-07-31 16:46:14 +020018 "strings"
Lorenz Brun35fcf032023-06-29 04:15:58 +020019
20 "github.com/cenkalti/backoff/v4"
Lorenz Brund14be0e2023-07-31 16:46:14 +020021 "golang.org/x/sys/unix"
Lorenz Brun35fcf032023-06-29 04:15:58 +020022 "google.golang.org/grpc/codes"
23 "google.golang.org/grpc/status"
Lorenz Brun54a5a052023-10-02 16:40:11 +020024 "google.golang.org/protobuf/proto"
Lorenz Brun35fcf032023-06-29 04:15:58 +020025
Lorenz Brun54a5a052023-10-02 16:40:11 +020026 abloaderpb "source.monogon.dev/metropolis/node/core/abloader/spec"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020027 "source.monogon.dev/osbase/blockdev"
Tim Windelschmidtc2290c22024-08-15 19:56:00 +020028 "source.monogon.dev/osbase/build/mkimage/osimage"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020029 "source.monogon.dev/osbase/efivarfs"
30 "source.monogon.dev/osbase/gpt"
31 "source.monogon.dev/osbase/kexec"
32 "source.monogon.dev/osbase/logtree"
Lorenz Brun35fcf032023-06-29 04:15:58 +020033)
34
35// Service contains data and functionality to perform A/B updates on a
36// Metropolis node.
37type Service struct {
38 // Path to the mount point of the EFI System Partition (ESP).
39 ESPPath string
Tim Windelschmidt8e87a062023-07-31 01:33:10 +000040 // gpt.Partition of the ESP System Partition.
41 ESPPart *gpt.Partition
Lorenz Brun35fcf032023-06-29 04:15:58 +020042 // Partition number (1-based) of the ESP in the GPT partitions array.
43 ESPPartNumber uint32
Tim Windelschmidt8e87a062023-07-31 01:33:10 +000044
Lorenz Brun35fcf032023-06-29 04:15:58 +020045 // Logger service for the update service.
46 Logger logtree.LeveledLogger
47}
48
49type Slot int
50
51const (
52 SlotInvalid Slot = 0
53 SlotA Slot = 1
54 SlotB Slot = 2
55)
56
57// Other returns the "other" slot, i.e. returns slot A for B and B for A.
58// It returns SlotInvalid for any s which is not SlotA or SlotB.
59func (s Slot) Other() Slot {
60 switch s {
61 case SlotA:
62 return SlotB
63 case SlotB:
64 return SlotA
65 default:
66 return SlotInvalid
67 }
68}
69
70func (s Slot) String() string {
71 switch s {
72 case SlotA:
73 return "A"
74 case SlotB:
75 return "B"
76 default:
77 return "<invalid slot>"
78 }
79}
80
81func (s Slot) EFIBootPath() string {
82 switch s {
83 case SlotA:
84 return osimage.EFIBootAPath
85 case SlotB:
86 return osimage.EFIBootBPath
87 default:
88 return ""
89 }
90}
91
92var slotRegexp = regexp.MustCompile(`PARTLABEL=METROPOLIS-SYSTEM-([AB])`)
93
94// ProvideESP is a convenience function for providing information about the
95// ESP after the update service has been instantiated.
Tim Windelschmidt8e87a062023-07-31 01:33:10 +000096func (s *Service) ProvideESP(path string, partNum uint32, part *gpt.Partition) {
Lorenz Brun35fcf032023-06-29 04:15:58 +020097 s.ESPPath = path
98 s.ESPPartNumber = partNum
Tim Windelschmidt8e87a062023-07-31 01:33:10 +000099 s.ESPPart = part
Lorenz Brun35fcf032023-06-29 04:15:58 +0200100}
101
102// CurrentlyRunningSlot returns the slot the current system is booted from.
103func (s *Service) CurrentlyRunningSlot() Slot {
104 cmdline, err := os.ReadFile("/proc/cmdline")
105 if err != nil {
106 return SlotInvalid
107 }
108 slotMatches := slotRegexp.FindStringSubmatch(string(cmdline))
109 if len(slotMatches) != 2 {
110 return SlotInvalid
111 }
112 switch slotMatches[1] {
113 case "A":
114 return SlotA
115 case "B":
116 return SlotB
117 default:
118 panic("unreachable")
119 }
120}
121
122var bootVarRegexp = regexp.MustCompile(`^Boot([0-9A-Fa-f]{4})$`)
123
Lorenz Brun35fcf032023-06-29 04:15:58 +0200124// MarkBootSuccessful must be called after each boot if some implementation-
125// defined criteria for a successful boot are met. If an update has been
126// installed and booted and this function is called, the updated version is
127// marked as default. If an issue occurs during boot and so this function is
128// not called the old version will be started again on next boot.
129func (s *Service) MarkBootSuccessful() error {
130 if s.ESPPath == "" {
131 return errors.New("no ESP information provided to update service, cannot continue")
132 }
Lorenz Brund79881d2023-11-30 19:02:06 +0100133 if err := s.fixupEFI(); err != nil {
134 s.Logger.Errorf("Error when checking boot entry configuration: %v", err)
135 }
136 if err := s.fixupPreloader(); err != nil {
137 s.Logger.Errorf("Error when fixing A/B preloader: %v", err)
138 }
Lorenz Brun35fcf032023-06-29 04:15:58 +0200139 activeSlot := s.CurrentlyRunningSlot()
Lorenz Brun54a5a052023-10-02 16:40:11 +0200140 abState, err := s.getABState()
Lorenz Brun35fcf032023-06-29 04:15:58 +0200141 if err != nil {
Lorenz Brun54a5a052023-10-02 16:40:11 +0200142 s.Logger.Warningf("Error while getting A/B loader state, recreating: %v", err)
143 abState = &abloaderpb.ABLoaderData{
144 ActiveSlot: abloaderpb.Slot(activeSlot),
Lorenz Brun35fcf032023-06-29 04:15:58 +0200145 }
Lorenz Brun54a5a052023-10-02 16:40:11 +0200146 err := s.setABState(abState)
147 if err != nil {
148 return fmt.Errorf("while recreating A/B loader state: %w", err)
Lorenz Brun35fcf032023-06-29 04:15:58 +0200149 }
150 }
Lorenz Brun54a5a052023-10-02 16:40:11 +0200151 if Slot(abState.ActiveSlot) != activeSlot {
152 err := s.setABState(&abloaderpb.ABLoaderData{
153 ActiveSlot: abloaderpb.Slot(activeSlot),
154 })
155 if err != nil {
156 return fmt.Errorf("while setting next A/B slot: %w", err)
Lorenz Brun35fcf032023-06-29 04:15:58 +0200157 }
158 s.Logger.Infof("Permanently activated slot %v", activeSlot)
159 } else {
160 s.Logger.Infof("Normal boot from slot %v", activeSlot)
161 }
162
163 return nil
164}
165
Lorenz Brunca6da6a2024-09-09 17:55:15 +0200166// Rollback sets the currently-inactive slot as the next boot slot. This is
167// intended to recover from scenarios where roll-forward fixing is difficult.
168// Only the next boot slot is set to make sure that the node is not
169// made unbootable accidentally. On successful bootup that code can switch the
170// active slot to itself.
171func (s *Service) Rollback() error {
172 if s.ESPPath == "" {
173 return errors.New("no ESP information provided to update service, cannot continue")
174 }
175 activeSlot := s.CurrentlyRunningSlot()
176 abState, err := s.getABState()
177 if err != nil {
178 return fmt.Errorf("no valid A/B loader state, cannot rollback: %w", err)
179 }
180 nextSlot := activeSlot.Other()
181 err = s.setABState(&abloaderpb.ABLoaderData{
182 ActiveSlot: abState.ActiveSlot,
183 NextSlot: abloaderpb.Slot(nextSlot),
184 })
185 if err != nil {
186 return fmt.Errorf("while setting next A/B slot: %w", err)
187 }
188 s.Logger.Warningf("Rollback requested, NextSlot set to %v", nextSlot)
189 return nil
190}
191
Lorenz Brun35fcf032023-06-29 04:15:58 +0200192func openSystemSlot(slot Slot) (*blockdev.Device, error) {
193 switch slot {
194 case SlotA:
195 return blockdev.Open("/dev/system-a")
196 case SlotB:
197 return blockdev.Open("/dev/system-b")
198 default:
199 return nil, errors.New("invalid slot identifier given")
200 }
201}
202
Lorenz Brun54a5a052023-10-02 16:40:11 +0200203func (s *Service) getABState() (*abloaderpb.ABLoaderData, error) {
204 abDataRaw, err := os.ReadFile(filepath.Join(s.ESPPath, "EFI/metropolis/loader_state.pb"))
205 if err != nil {
206 return nil, err
207 }
208 var abData abloaderpb.ABLoaderData
209 if err := proto.Unmarshal(abDataRaw, &abData); err != nil {
210 return nil, err
211 }
212 return &abData, nil
213}
214
215func (s *Service) setABState(d *abloaderpb.ABLoaderData) error {
216 abDataRaw, err := proto.Marshal(d)
217 if err != nil {
218 return fmt.Errorf("while marshaling: %w", err)
219 }
220 if err := os.WriteFile(filepath.Join(s.ESPPath, "EFI/metropolis/loader_state.pb"), abDataRaw, 0666); err != nil {
221 return err
222 }
223 return nil
224}
225
Lorenz Brun35fcf032023-06-29 04:15:58 +0200226// InstallBundle installs the bundle at the given HTTP(S) URL into the currently
227// inactive slot and sets that slot to boot next. If it doesn't return an error,
228// a reboot boots into the new slot.
Lorenz Brund14be0e2023-07-31 16:46:14 +0200229func (s *Service) InstallBundle(ctx context.Context, bundleURL string, withKexec bool) error {
Lorenz Brun35fcf032023-06-29 04:15:58 +0200230 if s.ESPPath == "" {
231 return errors.New("no ESP information provided to update service, cannot continue")
232 }
233 // Download into a buffer as ZIP files cannot efficiently be read from
234 // HTTP in Go as the ReaderAt has no way of indicating continuous sections,
235 // thus a ton of small range requests would need to be used, causing
236 // a huge latency penalty as well as costing a lot of money on typical
237 // object storages. This should go away when we switch to a better bundle
238 // format which can be streamed.
239 var bundleRaw bytes.Buffer
240 b := backoff.NewExponentialBackOff()
241 err := backoff.Retry(func() error {
242 return s.tryDownloadBundle(ctx, bundleURL, &bundleRaw)
243 }, backoff.WithContext(b, ctx))
244 if err != nil {
245 return fmt.Errorf("error downloading Metropolis bundle: %v", err)
246 }
247 bundle, err := zip.NewReader(bytes.NewReader(bundleRaw.Bytes()), int64(bundleRaw.Len()))
248 if err != nil {
249 return fmt.Errorf("failed to open node bundle: %w", err)
250 }
251 efiPayload, err := bundle.Open("kernel_efi.efi")
252 if err != nil {
253 return fmt.Errorf("invalid bundle: %w", err)
254 }
255 defer efiPayload.Close()
256 systemImage, err := bundle.Open("verity_rootfs.img")
257 if err != nil {
258 return fmt.Errorf("invalid bundle: %w", err)
259 }
260 defer systemImage.Close()
261 activeSlot := s.CurrentlyRunningSlot()
262 if activeSlot == SlotInvalid {
263 return errors.New("unable to determine active slot, cannot continue")
264 }
265 targetSlot := activeSlot.Other()
266
Lorenz Brun35fcf032023-06-29 04:15:58 +0200267 systemPart, err := openSystemSlot(targetSlot)
268 if err != nil {
269 return status.Errorf(codes.Internal, "Inactive system slot unavailable: %v", err)
270 }
271 defer systemPart.Close()
272 if _, err := io.Copy(blockdev.NewRWS(systemPart), systemImage); err != nil {
273 return status.Errorf(codes.Unavailable, "Failed to copy system image: %v", err)
274 }
275
276 bootFile, err := os.Create(filepath.Join(s.ESPPath, targetSlot.EFIBootPath()))
277 if err != nil {
278 return fmt.Errorf("failed to open boot file: %w", err)
279 }
280 defer bootFile.Close()
281 if _, err := io.Copy(bootFile, efiPayload); err != nil {
282 return fmt.Errorf("failed to write boot file: %w", err)
283 }
284
Lorenz Brund14be0e2023-07-31 16:46:14 +0200285 if withKexec {
286 if err := s.stageKexec(bootFile, targetSlot); err != nil {
287 return fmt.Errorf("while kexec staging: %w", err)
288 }
289 } else {
Lorenz Brun54a5a052023-10-02 16:40:11 +0200290 err := s.setABState(&abloaderpb.ABLoaderData{
291 ActiveSlot: abloaderpb.Slot(activeSlot),
292 NextSlot: abloaderpb.Slot(targetSlot),
293 })
294 if err != nil {
295 return fmt.Errorf("while setting next A/B slot: %w", err)
Lorenz Brund14be0e2023-07-31 16:46:14 +0200296 }
Lorenz Brun35fcf032023-06-29 04:15:58 +0200297 }
298
299 return nil
300}
301
302func (*Service) tryDownloadBundle(ctx context.Context, bundleURL string, bundleRaw *bytes.Buffer) error {
303 bundleReq, err := http.NewRequestWithContext(ctx, "GET", bundleURL, nil)
Tim Windelschmidt096654a2024-04-18 23:10:19 +0200304 if err != nil {
305 return fmt.Errorf("failed to create request: %w", err)
306 }
Lorenz Brun35fcf032023-06-29 04:15:58 +0200307 bundleRes, err := http.DefaultClient.Do(bundleReq)
308 if err != nil {
309 return fmt.Errorf("HTTP request failed: %w", err)
310 }
311 defer bundleRes.Body.Close()
312 switch bundleRes.StatusCode {
313 case http.StatusTooEarly, http.StatusTooManyRequests,
314 http.StatusInternalServerError, http.StatusBadGateway,
315 http.StatusServiceUnavailable, http.StatusGatewayTimeout:
316 return fmt.Errorf("HTTP error %d", bundleRes.StatusCode)
317 default:
318 // Non-standard code range used for proxy-related issue by various
319 // vendors. Treat as non-permanent error.
320 if bundleRes.StatusCode >= 520 && bundleRes.StatusCode < 599 {
321 return fmt.Errorf("HTTP error %d", bundleRes.StatusCode)
322 }
323 if bundleRes.StatusCode != 200 {
324 return backoff.Permanent(fmt.Errorf("HTTP error %d", bundleRes.StatusCode))
325 }
326 }
327 if _, err := bundleRaw.ReadFrom(bundleRes.Body); err != nil {
328 bundleRaw.Reset()
329 return err
330 }
331 return nil
332}
Lorenz Brund14be0e2023-07-31 16:46:14 +0200333
334// newMemfile creates a new file which is not located on a specific filesystem,
335// but is instead backed by anonymous memory.
336func newMemfile(name string, flags int) (*os.File, error) {
337 fd, err := unix.MemfdCreate(name, flags)
338 if err != nil {
339 return nil, fmt.Errorf("memfd_create: %w", err)
340 }
341 return os.NewFile(uintptr(fd), name), nil
342}
343
344// stageKexec stages the kernel, command line and initramfs if available for
345// a future kexec. It extracts the relevant data from the EFI boot executable.
346func (s *Service) stageKexec(bootFile io.ReaderAt, targetSlot Slot) error {
347 bootPE, err := pe.NewFile(bootFile)
348 if err != nil {
349 return fmt.Errorf("unable to open bootFile as PE: %w", err)
350 }
351 var cmdlineRaw []byte
352 cmdlineSection := bootPE.Section(".cmdline")
353 if cmdlineSection == nil {
354 return fmt.Errorf("no .cmdline section in boot PE")
355 }
356 cmdlineRaw, err = cmdlineSection.Data()
357 if err != nil {
358 return fmt.Errorf("while reading .cmdline PE section: %w", err)
359 }
360 cmdline := string(bytes.TrimRight(cmdlineRaw, "\x00"))
361 cmdline = strings.ReplaceAll(cmdline, "METROPOLIS-SYSTEM-X", fmt.Sprintf("METROPOLIS-SYSTEM-%s", targetSlot))
362 kernelFile, err := newMemfile("kernel", 0)
363 if err != nil {
364 return fmt.Errorf("failed to create kernel memfile: %w", err)
365 }
366 defer kernelFile.Close()
367 kernelSection := bootPE.Section(".linux")
368 if kernelSection == nil {
369 return fmt.Errorf("no .linux section in boot PE")
370 }
371 if _, err := io.Copy(kernelFile, kernelSection.Open()); err != nil {
372 return fmt.Errorf("while copying .linux PE section: %w", err)
373 }
374
375 initramfsSection := bootPE.Section(".initrd")
376 var initramfsFile *os.File
377 if initramfsSection != nil && initramfsSection.Size > 0 {
378 initramfsFile, err = newMemfile("initramfs", 0)
379 if err != nil {
380 return fmt.Errorf("failed to create initramfs memfile: %w", err)
381 }
382 defer initramfsFile.Close()
383 if _, err := io.Copy(initramfsFile, initramfsSection.Open()); err != nil {
384 return fmt.Errorf("while copying .initrd PE section: %w", err)
385 }
386 }
387 if err := kexec.FileLoad(kernelFile, initramfsFile, cmdline); err != nil {
388 return fmt.Errorf("while staging new kexec kernel: %w", err)
389 }
390 return nil
391}
Lorenz Brund79881d2023-11-30 19:02:06 +0100392
393//go:embed metropolis/node/core/abloader/abloader_bin.efi
394var abloader []byte
395
396func (s *Service) fixupPreloader() error {
397 abLoaderFile, err := os.Open(filepath.Join(s.ESPPath, osimage.EFIPayloadPath))
398 if err != nil {
399 s.Logger.Warningf("A/B preloader not available, attempting to restore: %v", err)
400 } else {
401 expectedSum := sha256.Sum256(abloader)
402 h := sha256.New()
403 _, err := io.Copy(h, abLoaderFile)
404 abLoaderFile.Close()
405 if err == nil {
406 if bytes.Equal(h.Sum(nil), expectedSum[:]) {
407 // A/B Preloader is present and has correct hash
408 return nil
409 } else {
410 s.Logger.Infof("Replacing A/B preloader with current version: %x %x", h.Sum(nil), expectedSum[:])
411 }
412 } else {
413 s.Logger.Warningf("Error while reading A/B preloader, restoring: %v", err)
414 }
415 }
416 preloader, err := os.Create(filepath.Join(s.ESPPath, "preloader.swp"))
417 if err != nil {
418 return fmt.Errorf("while creating preloader swap file: %w", err)
419 }
420 if _, err := preloader.Write(abloader); err != nil {
421 return fmt.Errorf("while writing preloader swap file: %w", err)
422 }
423 if err := preloader.Sync(); err != nil {
424 return fmt.Errorf("while sync'ing preloader swap file: %w", err)
425 }
426 preloader.Close()
427 if err := os.Rename(filepath.Join(s.ESPPath, "preloader.swp"), filepath.Join(s.ESPPath, osimage.EFIPayloadPath)); err != nil {
428 return fmt.Errorf("while swapping preloader: %w", err)
429 }
430 s.Logger.Info("Successfully wrote current preloader")
431 return nil
432}
433
434// fixupEFI checks for the existence and correctness of the EFI boot entry
435// repairs/recreates it if needed.
436func (s *Service) fixupEFI() error {
437 varNames, err := efivarfs.List(efivarfs.ScopeGlobal)
438 if err != nil {
439 return fmt.Errorf("failed to list EFI variables: %w", err)
440 }
Tim Windelschmidt5e460a92024-04-11 01:33:09 +0200441 var validBootEntryIdx = -1
Lorenz Brund79881d2023-11-30 19:02:06 +0100442 for _, varName := range varNames {
443 m := bootVarRegexp.FindStringSubmatch(varName)
444 if m == nil {
445 continue
446 }
447 idx, err := strconv.ParseUint(m[1], 16, 16)
448 if err != nil {
449 // This cannot be hit as all regexp matches are parseable.
450 panic(err)
451 }
452 e, err := efivarfs.GetBootEntry(int(idx))
453 if err != nil {
454 s.Logger.Warningf("Unable to get boot entry %d, skipping: %v", idx, err)
455 continue
456 }
457 if len(e.FilePath) != 2 {
458 // Not our entry, ours always have two parts
459 continue
460 }
461 switch p := e.FilePath[0].(type) {
462 case *efivarfs.HardDrivePath:
463 gptMatch, ok := p.PartitionMatch.(*efivarfs.PartitionGPT)
464 if ok && gptMatch.PartitionUUID != s.ESPPart.ID {
465 // Not related to our ESP
466 continue
467 }
468 default:
469 continue
470 }
471 switch p := e.FilePath[1].(type) {
472 case efivarfs.FilePath:
473 if string(p) == osimage.EFIPayloadPath {
474 if validBootEntryIdx == -1 {
475 validBootEntryIdx = int(idx)
476 } else {
477 // Another valid boot entry already exists, delete this one
478 err := efivarfs.DeleteBootEntry(int(idx))
479 if err == nil {
480 s.Logger.Infof("Deleted duplicate boot entry %q", e.Description)
481 } else {
482 s.Logger.Warningf("Error while deleting duplicate boot entry %q: %v", e.Description, err)
483 }
484 }
485 } else if strings.Contains(e.Description, "Metropolis") {
486 err := efivarfs.DeleteBootEntry(int(idx))
487 if err == nil {
488 s.Logger.Infof("Deleted orphaned boot entry %q", e.Description)
489 } else {
490 s.Logger.Warningf("Error while deleting orphaned boot entry %q: %v", e.Description, err)
491 }
492 }
493 default:
494 continue
495 }
496 }
497 if validBootEntryIdx == -1 {
498 validBootEntryIdx, err = efivarfs.AddBootEntry(&efivarfs.LoadOption{
499 Description: "Metropolis",
500 FilePath: efivarfs.DevicePath{
501 &efivarfs.HardDrivePath{
502 PartitionNumber: 1,
503 PartitionStartBlock: s.ESPPart.FirstBlock,
504 PartitionSizeBlocks: s.ESPPart.SizeBlocks(),
505 PartitionMatch: efivarfs.PartitionGPT{
506 PartitionUUID: s.ESPPart.ID,
507 },
508 },
509 efivarfs.FilePath(osimage.EFIPayloadPath),
510 },
511 })
512 if err == nil {
513 s.Logger.Infof("Restored missing EFI boot entry for Metropolis")
514 } else {
515 return fmt.Errorf("while restoring missing EFI boot entry for Metropolis: %v", err)
516 }
517 }
518 bootOrder, err := efivarfs.GetBootOrder()
519 if err != nil {
520 return fmt.Errorf("failed to get EFI boot order: %v", err)
521 }
522 for _, bentry := range bootOrder {
523 if bentry == uint16(validBootEntryIdx) {
524 // Our boot entry is in the boot order, everything's ok
525 return nil
526 }
527 }
528 newBootOrder := append(efivarfs.BootOrder{uint16(validBootEntryIdx)}, bootOrder...)
529 if err := efivarfs.SetBootOrder(newBootOrder); err != nil {
530 return fmt.Errorf("while setting EFI boot order: %w", err)
531 }
532 return nil
533}