blob: cdc8fa54b142aa7ca9301509865eb8159f51edf7 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Lorenz Brunaadeb792023-03-27 15:53:56 +02004package main
5
6import (
7 "archive/zip"
8 "bytes"
Lorenz Brun54a5a052023-10-02 16:40:11 +02009 _ "embed"
Lorenz Brunaadeb792023-03-27 15:53:56 +020010 "errors"
11 "fmt"
Lorenz Brunad131882023-06-28 16:42:20 +020012 "io/fs"
Lorenz Brunaadeb792023-03-27 15:53:56 +020013 "net/http"
Tim Windelschmidt58321122024-09-10 02:26:03 +020014 "os"
Lorenz Brunaadeb792023-03-27 15:53:56 +020015 "path/filepath"
16
17 "github.com/cenkalti/backoff/v4"
18 "google.golang.org/protobuf/proto"
19
20 bpb "source.monogon.dev/cloud/bmaas/server/api"
Serge Bazanski3c5d0632024-09-12 10:49:12 +000021 "source.monogon.dev/go/logging"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020022 "source.monogon.dev/osbase/blockdev"
Tim Windelschmidtc2290c22024-08-15 19:56:00 +020023 "source.monogon.dev/osbase/build/mkimage/osimage"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020024 "source.monogon.dev/osbase/efivarfs"
Tim Windelschmidt10ef8f92024-08-13 15:35:10 +020025 npb "source.monogon.dev/osbase/net/proto"
Lorenz Brunaadeb792023-03-27 15:53:56 +020026)
27
Tim Windelschmidt1f51cf42024-10-01 17:04:28 +020028//go:embed metropolis/node/core/abloader/abloader.efi
Lorenz Brun54a5a052023-10-02 16:40:11 +020029var abloader []byte
30
Lorenz Brunad131882023-06-28 16:42:20 +020031// FileSizedReader is a small adapter from fs.File to fs.SizedReader
32// Panics on Stat() failure, so should only be used with sources where Stat()
33// cannot fail.
34type FileSizedReader struct {
35 fs.File
36}
37
38func (f FileSizedReader) Size() int64 {
39 stat, err := f.Stat()
40 if err != nil {
41 panic(err)
42 }
43 return stat.Size()
44}
45
Lorenz Brunaadeb792023-03-27 15:53:56 +020046// install dispatches OSInstallationRequests to the appropriate installer
47// method
Serge Bazanski3c5d0632024-09-12 10:49:12 +000048func install(req *bpb.OSInstallationRequest, netConfig *npb.Net, l logging.Leveled) error {
Lorenz Brunaadeb792023-03-27 15:53:56 +020049 switch reqT := req.Type.(type) {
50 case *bpb.OSInstallationRequest_Metropolis:
Tim Windelschmidt58321122024-09-10 02:26:03 +020051 return installMetropolis(reqT.Metropolis, netConfig, l)
Lorenz Brunaadeb792023-03-27 15:53:56 +020052 default:
53 return errors.New("unknown installation request type")
54 }
55}
56
Serge Bazanski3c5d0632024-09-12 10:49:12 +000057func installMetropolis(req *bpb.MetropolisInstallationRequest, netConfig *npb.Net, l logging.Leveled) error {
Tim Windelschmidt58321122024-09-10 02:26:03 +020058 // Validate we are running via EFI.
59 if _, err := os.Stat("/sys/firmware/efi"); os.IsNotExist(err) {
Tim Windelschmidt1f51cf42024-10-01 17:04:28 +020060 // nolint:ST1005
Lorenz Brunaadeb792023-03-27 15:53:56 +020061 return errors.New("Monogon OS can only be installed on EFI-booted machines, this one is not")
62 }
Tim Windelschmidtfac48742023-04-24 19:04:55 +020063
64 // Override the NodeParameters.NetworkConfig with the current NetworkConfig
65 // if it's missing.
66 if req.NodeParameters.NetworkConfig == nil {
67 req.NodeParameters.NetworkConfig = netConfig
68 }
69
Lorenz Brunaadeb792023-03-27 15:53:56 +020070 // Download into a buffer as ZIP files cannot efficiently be read from
71 // HTTP in Go as the ReaderAt has no way of indicating continuous sections,
72 // thus a ton of small range requests would need to be used, causing
73 // a huge latency penalty as well as costing a lot of money on typical
74 // object storages. This should go away when we switch to a better bundle
75 // format which can be streamed.
76 var bundleRaw bytes.Buffer
77 b := backoff.NewExponentialBackOff()
78 err := backoff.Retry(func() error {
79 bundleRes, err := http.Get(req.BundleUrl)
80 if err != nil {
81 l.Warningf("Metropolis bundle request failed: %v", err)
Tim Windelschmidt327cdba2024-05-21 13:51:32 +020082 return fmt.Errorf("HTTP request failed: %w", err)
Lorenz Brunaadeb792023-03-27 15:53:56 +020083 }
84 defer bundleRes.Body.Close()
85 switch bundleRes.StatusCode {
86 case http.StatusTooEarly, http.StatusTooManyRequests,
87 http.StatusInternalServerError, http.StatusBadGateway,
88 http.StatusServiceUnavailable, http.StatusGatewayTimeout:
89 l.Warningf("Metropolis bundle request HTTP %d error, retrying", bundleRes.StatusCode)
90 return fmt.Errorf("HTTP error %d", bundleRes.StatusCode)
91 default:
92 // Non-standard code range used for proxy-related issue by various
93 // vendors. Treat as non-permanent error.
94 if bundleRes.StatusCode >= 520 && bundleRes.StatusCode < 599 {
95 l.Warningf("Metropolis bundle request HTTP %d error, retrying", bundleRes.StatusCode)
96 return fmt.Errorf("HTTP error %d", bundleRes.StatusCode)
97 }
98 if bundleRes.StatusCode != 200 {
99 l.Errorf("Metropolis bundle request permanent HTTP %d error, aborting", bundleRes.StatusCode)
100 return backoff.Permanent(fmt.Errorf("HTTP error %d", bundleRes.StatusCode))
101 }
102 }
103 if _, err := bundleRaw.ReadFrom(bundleRes.Body); err != nil {
104 l.Warningf("Metropolis bundle download failed, retrying: %v", err)
105 bundleRaw.Reset()
106 return err
107 }
108 return nil
109 }, b)
110 if err != nil {
Tim Windelschmidt327cdba2024-05-21 13:51:32 +0200111 return fmt.Errorf("error downloading Metropolis bundle: %w", err)
Lorenz Brunaadeb792023-03-27 15:53:56 +0200112 }
113 l.Info("Metropolis Bundle downloaded")
114 bundle, err := zip.NewReader(bytes.NewReader(bundleRaw.Bytes()), int64(bundleRaw.Len()))
115 if err != nil {
116 return fmt.Errorf("failed to open node bundle: %w", err)
117 }
118 efiPayload, err := bundle.Open("kernel_efi.efi")
119 if err != nil {
120 return fmt.Errorf("invalid bundle: %w", err)
121 }
122 defer efiPayload.Close()
123 systemImage, err := bundle.Open("verity_rootfs.img")
124 if err != nil {
125 return fmt.Errorf("invalid bundle: %w", err)
126 }
127 defer systemImage.Close()
128
129 nodeParamsRaw, err := proto.Marshal(req.NodeParameters)
130 if err != nil {
131 return fmt.Errorf("failed marshaling: %w", err)
132 }
133
Lorenz Brunad131882023-06-28 16:42:20 +0200134 rootDev, err := blockdev.Open(filepath.Join("/dev", req.RootDevice))
135 if err != nil {
136 return fmt.Errorf("failed to open root device: %w", err)
137 }
138
Lorenz Brunaadeb792023-03-27 15:53:56 +0200139 installParams := osimage.Params{
140 PartitionSize: osimage.PartitionSizeInfo{
Lorenz Brun35fcf032023-06-29 04:15:58 +0200141 ESP: 384,
Lorenz Brunaadeb792023-03-27 15:53:56 +0200142 System: 4096,
143 Data: 128,
144 },
145 SystemImage: systemImage,
Lorenz Brunad131882023-06-28 16:42:20 +0200146 EFIPayload: FileSizedReader{efiPayload},
Lorenz Brun54a5a052023-10-02 16:40:11 +0200147 ABLoader: bytes.NewReader(abloader),
Lorenz Brunaadeb792023-03-27 15:53:56 +0200148 NodeParameters: bytes.NewReader(nodeParamsRaw),
Lorenz Brunad131882023-06-28 16:42:20 +0200149 Output: rootDev,
Lorenz Brunaadeb792023-03-27 15:53:56 +0200150 }
151
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +0200152 be, err := osimage.Write(&installParams)
Lorenz Brunaadeb792023-03-27 15:53:56 +0200153 if err != nil {
154 return err
155 }
Lorenz Brunca1cff02023-06-26 17:52:44 +0200156 bootEntryIdx, err := efivarfs.AddBootEntry(be)
Lorenz Brunaadeb792023-03-27 15:53:56 +0200157 if err != nil {
158 return fmt.Errorf("error creating EFI boot entry: %w", err)
159 }
Lorenz Brun9933ef02023-07-06 18:28:29 +0200160 if err := efivarfs.SetBootOrder(efivarfs.BootOrder{uint16(bootEntryIdx)}); err != nil {
Lorenz Brunaadeb792023-03-27 15:53:56 +0200161 return fmt.Errorf("error setting EFI boot order: %w", err)
162 }
163 l.Info("Metropolis installation completed")
164 return nil
165}