blob: f209ce3a9dc2fd70b92943f80c9ab4f177765974 [file] [log] [blame]
Lorenz Brunaadeb792023-03-27 15:53:56 +02001package main
2
3import (
4 "archive/zip"
5 "bytes"
Lorenz Brun54a5a052023-10-02 16:40:11 +02006 _ "embed"
Lorenz Brunaadeb792023-03-27 15:53:56 +02007 "errors"
8 "fmt"
Lorenz Brunad131882023-06-28 16:42:20 +02009 "io/fs"
Lorenz Brunaadeb792023-03-27 15:53:56 +020010 "net/http"
11 "path/filepath"
12
13 "github.com/cenkalti/backoff/v4"
14 "google.golang.org/protobuf/proto"
15
16 bpb "source.monogon.dev/cloud/bmaas/server/api"
17 "source.monogon.dev/metropolis/node/build/mkimage/osimage"
Lorenz Brunad131882023-06-28 16:42:20 +020018 "source.monogon.dev/metropolis/pkg/blockdev"
Lorenz Brunaadeb792023-03-27 15:53:56 +020019 "source.monogon.dev/metropolis/pkg/efivarfs"
20 "source.monogon.dev/metropolis/pkg/logtree"
Tim Windelschmidtfac48742023-04-24 19:04:55 +020021 npb "source.monogon.dev/net/proto"
Lorenz Brunaadeb792023-03-27 15:53:56 +020022)
23
Lorenz Brun54a5a052023-10-02 16:40:11 +020024//go:embed metropolis/node/core/abloader/abloader_bin.efi
25var abloader []byte
26
Lorenz Brunad131882023-06-28 16:42:20 +020027// FileSizedReader is a small adapter from fs.File to fs.SizedReader
28// Panics on Stat() failure, so should only be used with sources where Stat()
29// cannot fail.
30type FileSizedReader struct {
31 fs.File
32}
33
34func (f FileSizedReader) Size() int64 {
35 stat, err := f.Stat()
36 if err != nil {
37 panic(err)
38 }
39 return stat.Size()
40}
41
Lorenz Brunaadeb792023-03-27 15:53:56 +020042// install dispatches OSInstallationRequests to the appropriate installer
43// method
Tim Windelschmidtfac48742023-04-24 19:04:55 +020044func install(req *bpb.OSInstallationRequest, netConfig *npb.Net, l logtree.LeveledLogger, isEFIBoot bool) error {
Lorenz Brunaadeb792023-03-27 15:53:56 +020045 switch reqT := req.Type.(type) {
46 case *bpb.OSInstallationRequest_Metropolis:
Tim Windelschmidtfac48742023-04-24 19:04:55 +020047 return installMetropolis(reqT.Metropolis, netConfig, l, isEFIBoot)
Lorenz Brunaadeb792023-03-27 15:53:56 +020048 default:
49 return errors.New("unknown installation request type")
50 }
51}
52
Tim Windelschmidtfac48742023-04-24 19:04:55 +020053func installMetropolis(req *bpb.MetropolisInstallationRequest, netConfig *npb.Net, l logtree.LeveledLogger, isEFIBoot bool) error {
Lorenz Brunaadeb792023-03-27 15:53:56 +020054 if !isEFIBoot {
55 return errors.New("Monogon OS can only be installed on EFI-booted machines, this one is not")
56 }
Tim Windelschmidtfac48742023-04-24 19:04:55 +020057
58 // Override the NodeParameters.NetworkConfig with the current NetworkConfig
59 // if it's missing.
60 if req.NodeParameters.NetworkConfig == nil {
61 req.NodeParameters.NetworkConfig = netConfig
62 }
63
Lorenz Brunaadeb792023-03-27 15:53:56 +020064 // Download into a buffer as ZIP files cannot efficiently be read from
65 // HTTP in Go as the ReaderAt has no way of indicating continuous sections,
66 // thus a ton of small range requests would need to be used, causing
67 // a huge latency penalty as well as costing a lot of money on typical
68 // object storages. This should go away when we switch to a better bundle
69 // format which can be streamed.
70 var bundleRaw bytes.Buffer
71 b := backoff.NewExponentialBackOff()
72 err := backoff.Retry(func() error {
73 bundleRes, err := http.Get(req.BundleUrl)
74 if err != nil {
75 l.Warningf("Metropolis bundle request failed: %v", err)
76 return fmt.Errorf("HTTP request failed: %v", err)
77 }
78 defer bundleRes.Body.Close()
79 switch bundleRes.StatusCode {
80 case http.StatusTooEarly, http.StatusTooManyRequests,
81 http.StatusInternalServerError, http.StatusBadGateway,
82 http.StatusServiceUnavailable, http.StatusGatewayTimeout:
83 l.Warningf("Metropolis bundle request HTTP %d error, retrying", bundleRes.StatusCode)
84 return fmt.Errorf("HTTP error %d", bundleRes.StatusCode)
85 default:
86 // Non-standard code range used for proxy-related issue by various
87 // vendors. Treat as non-permanent error.
88 if bundleRes.StatusCode >= 520 && bundleRes.StatusCode < 599 {
89 l.Warningf("Metropolis bundle request HTTP %d error, retrying", bundleRes.StatusCode)
90 return fmt.Errorf("HTTP error %d", bundleRes.StatusCode)
91 }
92 if bundleRes.StatusCode != 200 {
93 l.Errorf("Metropolis bundle request permanent HTTP %d error, aborting", bundleRes.StatusCode)
94 return backoff.Permanent(fmt.Errorf("HTTP error %d", bundleRes.StatusCode))
95 }
96 }
97 if _, err := bundleRaw.ReadFrom(bundleRes.Body); err != nil {
98 l.Warningf("Metropolis bundle download failed, retrying: %v", err)
99 bundleRaw.Reset()
100 return err
101 }
102 return nil
103 }, b)
104 if err != nil {
105 return fmt.Errorf("error downloading Metropolis bundle: %v", err)
106 }
107 l.Info("Metropolis Bundle downloaded")
108 bundle, err := zip.NewReader(bytes.NewReader(bundleRaw.Bytes()), int64(bundleRaw.Len()))
109 if err != nil {
110 return fmt.Errorf("failed to open node bundle: %w", err)
111 }
112 efiPayload, err := bundle.Open("kernel_efi.efi")
113 if err != nil {
114 return fmt.Errorf("invalid bundle: %w", err)
115 }
116 defer efiPayload.Close()
117 systemImage, err := bundle.Open("verity_rootfs.img")
118 if err != nil {
119 return fmt.Errorf("invalid bundle: %w", err)
120 }
121 defer systemImage.Close()
122
123 nodeParamsRaw, err := proto.Marshal(req.NodeParameters)
124 if err != nil {
125 return fmt.Errorf("failed marshaling: %w", err)
126 }
127
Lorenz Brunad131882023-06-28 16:42:20 +0200128 rootDev, err := blockdev.Open(filepath.Join("/dev", req.RootDevice))
129 if err != nil {
130 return fmt.Errorf("failed to open root device: %w", err)
131 }
132
Lorenz Brunaadeb792023-03-27 15:53:56 +0200133 installParams := osimage.Params{
134 PartitionSize: osimage.PartitionSizeInfo{
Lorenz Brun35fcf032023-06-29 04:15:58 +0200135 ESP: 384,
Lorenz Brunaadeb792023-03-27 15:53:56 +0200136 System: 4096,
137 Data: 128,
138 },
139 SystemImage: systemImage,
Lorenz Brunad131882023-06-28 16:42:20 +0200140 EFIPayload: FileSizedReader{efiPayload},
Lorenz Brun54a5a052023-10-02 16:40:11 +0200141 ABLoader: bytes.NewReader(abloader),
Lorenz Brunaadeb792023-03-27 15:53:56 +0200142 NodeParameters: bytes.NewReader(nodeParamsRaw),
Lorenz Brunad131882023-06-28 16:42:20 +0200143 Output: rootDev,
Lorenz Brunaadeb792023-03-27 15:53:56 +0200144 }
145
146 be, err := osimage.Create(&installParams)
147 if err != nil {
148 return err
149 }
Lorenz Brunca1cff02023-06-26 17:52:44 +0200150 bootEntryIdx, err := efivarfs.AddBootEntry(be)
Lorenz Brunaadeb792023-03-27 15:53:56 +0200151 if err != nil {
152 return fmt.Errorf("error creating EFI boot entry: %w", err)
153 }
Lorenz Brun9933ef02023-07-06 18:28:29 +0200154 if err := efivarfs.SetBootOrder(efivarfs.BootOrder{uint16(bootEntryIdx)}); err != nil {
Lorenz Brunaadeb792023-03-27 15:53:56 +0200155 return fmt.Errorf("error setting EFI boot order: %w", err)
156 }
157 l.Info("Metropolis installation completed")
158 return nil
159}