blob: e9956fd7b3cefae4087da8acf4eda69930936d82 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Mateusz Zalega43e21072021-10-08 18:05:29 +02002// SPDX-License-Identifier: Apache-2.0
Mateusz Zalega43e21072021-10-08 18:05:29 +02003
4// Installer creates a Metropolis image at a suitable block device based on the
5// installer bundle present in the installation medium's ESP, after which it
6// reboots. It's meant to be used as an init process.
7package main
8
9import (
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010010 "archive/zip"
Lorenz Brun54a5a052023-10-02 16:40:11 +020011 "bytes"
Tim Windelschmidt96e014e2024-09-10 02:26:13 +020012 "context"
Lorenz Brun54a5a052023-10-02 16:40:11 +020013 _ "embed"
Lorenz Brun57d06a72022-01-13 14:12:27 +010014 "errors"
Mateusz Zalega43e21072021-10-08 18:05:29 +020015 "fmt"
Lorenz Brunad131882023-06-28 16:42:20 +020016 "io/fs"
Mateusz Zalega43e21072021-10-08 18:05:29 +020017 "os"
18 "path/filepath"
19 "strings"
Lorenz Brun57d06a72022-01-13 14:12:27 +010020 "time"
Mateusz Zalega43e21072021-10-08 18:05:29 +020021
22 "golang.org/x/sys/unix"
Serge Bazanski97783222021-12-14 16:04:26 +010023
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020024 "source.monogon.dev/osbase/blockdev"
Tim Windelschmidt96e014e2024-09-10 02:26:13 +020025 "source.monogon.dev/osbase/bringup"
Tim Windelschmidtc2290c22024-08-15 19:56:00 +020026 "source.monogon.dev/osbase/build/mkimage/osimage"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020027 "source.monogon.dev/osbase/efivarfs"
Tim Windelschmidt96e014e2024-09-10 02:26:13 +020028 "source.monogon.dev/osbase/supervisor"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020029 "source.monogon.dev/osbase/sysfs"
Mateusz Zalega43e21072021-10-08 18:05:29 +020030)
31
Tim Windelschmidt1f51cf42024-10-01 17:04:28 +020032//go:embed metropolis/node/core/abloader/abloader.efi
Lorenz Brun54a5a052023-10-02 16:40:11 +020033var abloader []byte
34
Mateusz Zalega43e21072021-10-08 18:05:29 +020035const mib = 1024 * 1024
36
Mateusz Zalega43e21072021-10-08 18:05:29 +020037// mountInstallerESP mounts the filesystem the installer was loaded from based
38// on espPath, which must point to the appropriate partition block device. The
39// filesystem is mounted at /installer.
40func mountInstallerESP(espPath string) error {
41 // Create the mountpoint.
42 if err := unix.Mkdir("/installer", 0700); err != nil {
43 return fmt.Errorf("couldn't create the installer mountpoint: %w", err)
44 }
45 // Mount the filesystem.
46 if err := unix.Mount(espPath, "/installer", "vfat", unix.MS_NOEXEC|unix.MS_RDONLY, ""); err != nil {
47 return fmt.Errorf("couldn't mount the installer ESP (%q -> %q): %w", espPath, "/installer", err)
48 }
49 return nil
50}
51
52// findInstallableBlockDevices returns names of all the block devices suitable
53// for hosting a Metropolis installation, limited by the size expressed in
54// bytes minSize. The install medium espDev will be excluded from the result.
55func findInstallableBlockDevices(espDev string, minSize uint64) ([]string, error) {
56 // Use the partition's name to find and return the name of its parent
57 // device. It will be excluded from the list of suitable target devices.
58 srcDev, err := sysfs.ParentBlockDevice(espDev)
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +020059 if err != nil {
Tim Windelschmidt096654a2024-04-18 23:10:19 +020060 return nil, fmt.Errorf("failed to fetch parent device: %w", err)
61 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020062 // Build the exclusion list containing forbidden handle prefixes.
63 exclude := []string{"dm-", "zram", "ram", "loop", srcDev}
64
65 // Get the block device handles by looking up directory contents.
66 const blkDirPath = "/sys/class/block"
67 blkDevs, err := os.ReadDir(blkDirPath)
68 if err != nil {
69 return nil, fmt.Errorf("couldn't read %q: %w", blkDirPath, err)
70 }
71 // Iterate over the handles, skipping any block device that either points to
72 // a partition, matches the exclusion list, or is smaller than minSize.
73 var suitable []string
74probeLoop:
75 for _, devInfo := range blkDevs {
76 // Skip devices according to the exclusion list.
77 for _, prefix := range exclude {
78 if strings.HasPrefix(devInfo.Name(), prefix) {
79 continue probeLoop
80 }
81 }
82
83 // Skip partition symlinks.
84 if _, err := os.Stat(filepath.Join(blkDirPath, devInfo.Name(), "partition")); err == nil {
85 continue
86 } else if !os.IsNotExist(err) {
87 return nil, fmt.Errorf("while probing sysfs: %w", err)
88 }
89
90 // Skip devices of insufficient size.
91 devPath := filepath.Join("/dev", devInfo.Name())
Lorenz Brunad131882023-06-28 16:42:20 +020092 dev, err := blockdev.Open(devPath)
Mateusz Zalega43e21072021-10-08 18:05:29 +020093 if err != nil {
94 return nil, fmt.Errorf("couldn't open a block device at %q: %w", devPath, err)
95 }
Lorenz Brunad131882023-06-28 16:42:20 +020096 devSize := uint64(dev.BlockCount() * dev.BlockSize())
Mateusz Zalega43e21072021-10-08 18:05:29 +020097 dev.Close()
Lorenz Brunad131882023-06-28 16:42:20 +020098 if devSize < minSize {
Mateusz Zalega43e21072021-10-08 18:05:29 +020099 continue
100 }
101
102 suitable = append(suitable, devInfo.Name())
103 }
104 return suitable, nil
105}
106
Lorenz Brunad131882023-06-28 16:42:20 +0200107// FileSizedReader is a small adapter from fs.File to fs.SizedReader
108// Panics on Stat() failure, so should only be used with sources where Stat()
109// cannot fail.
110type FileSizedReader struct {
111 fs.File
Mateusz Zalega43e21072021-10-08 18:05:29 +0200112}
113
Lorenz Brunad131882023-06-28 16:42:20 +0200114func (f FileSizedReader) Size() int64 {
115 stat, err := f.Stat()
Mateusz Zalega43e21072021-10-08 18:05:29 +0200116 if err != nil {
Lorenz Brunad131882023-06-28 16:42:20 +0200117 panic(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200118 }
Lorenz Brunad131882023-06-28 16:42:20 +0200119 return stat.Size()
Mateusz Zalega43e21072021-10-08 18:05:29 +0200120}
121
122func main() {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200123 bringup.Runnable(installerRunnable).Run()
124}
Mateusz Zalegacdcc7392021-12-08 15:34:53 +0100125
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200126func installerRunnable(ctx context.Context) error {
127 l := supervisor.Logger(ctx)
128
129 l.Info("Metropolis Installer")
130 l.Info("Copyright (c) 2024 The Monogon Project Authors")
131 l.Info("")
132
133 // Validate we are running via EFI.
134 if _, err := os.Stat("/sys/firmware/efi"); os.IsNotExist(err) {
Tim Windelschmidt1f51cf42024-10-01 17:04:28 +0200135 // nolint:ST1005
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200136 return errors.New("Monogon OS can only be installed on EFI-booted machines, this one is not")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200137 }
Serge Bazanskif71fe922023-03-22 01:10:37 +0100138
Mateusz Zalega43e21072021-10-08 18:05:29 +0200139 // Read the installer ESP UUID from efivarfs.
140 espUuid, err := efivarfs.ReadLoaderDevicePartUUID()
141 if err != nil {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200142 return fmt.Errorf("while reading the installer ESP UUID: %w", err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200143 }
Lorenz Brun57d06a72022-01-13 14:12:27 +0100144 // Wait for up to 30 tries @ 1s (30s) for the ESP to show up
145 var espDev string
146 var retries = 30
147 for {
148 // Look up the installer partition based on espUuid.
149 espDev, err = sysfs.DeviceByPartUUID(espUuid)
150 if err == nil {
151 break
152 } else if errors.Is(err, sysfs.ErrDevNotFound) && retries > 0 {
153 time.Sleep(1 * time.Second)
154 retries--
155 } else {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200156 return fmt.Errorf("while resolving the installer device handle: %w", err)
Lorenz Brun57d06a72022-01-13 14:12:27 +0100157 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200158 }
Lorenz Brun57d06a72022-01-13 14:12:27 +0100159 espPath := filepath.Join("/dev", espDev)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200160 // Mount the installer partition. The installer bundle will be read from it.
161 if err := mountInstallerESP(espPath); err != nil {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200162 return fmt.Errorf("while mounting the installer ESP: %w", err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200163 }
164
Lorenz Brun6c35e972021-12-14 03:08:23 +0100165 nodeParameters, err := os.Open("/installer/metropolis-installer/nodeparams.pb")
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100166 if err != nil {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200167 return fmt.Errorf("failed to open node parameters from ESP: %w", err)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100168 }
169
170 // TODO(lorenz): Replace with proper bundles
Lorenz Brun6c35e972021-12-14 03:08:23 +0100171 bundle, err := zip.OpenReader("/installer/metropolis-installer/bundle.bin")
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100172 if err != nil {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200173 return fmt.Errorf("failed to open node bundle from ESP: %w", err)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100174 }
175 defer bundle.Close()
176 efiPayload, err := bundle.Open("kernel_efi.efi")
177 if err != nil {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200178 return fmt.Errorf("cannot open EFI payload in bundle: %w", err)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100179 }
180 defer efiPayload.Close()
Mateusz Zalega8c2c7712022-01-25 19:42:21 +0100181 systemImage, err := bundle.Open("verity_rootfs.img")
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100182 if err != nil {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200183 return fmt.Errorf("cannot open system image in bundle: %w", err)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100184 }
185 defer systemImage.Close()
186
Mateusz Zalega43e21072021-10-08 18:05:29 +0200187 // Build the osimage parameters.
188 installParams := osimage.Params{
189 PartitionSize: osimage.PartitionSizeInfo{
190 // ESP is the size of the node ESP partition, expressed in mebibytes.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200191 ESP: 384,
Mateusz Zalega43e21072021-10-08 18:05:29 +0200192 // System is the size of the node system partition, expressed in
193 // mebibytes.
194 System: 4096,
195 // Data must be nonzero in order for the data partition to be created.
196 // osimage will extend the data partition to fill all the available space
197 // whenever it's writing to block devices, such as now.
198 Data: 128,
199 },
Lorenz Brunad131882023-06-28 16:42:20 +0200200 SystemImage: systemImage,
201 EFIPayload: FileSizedReader{efiPayload},
Lorenz Brun54a5a052023-10-02 16:40:11 +0200202 ABLoader: bytes.NewReader(abloader),
Lorenz Brunad131882023-06-28 16:42:20 +0200203 NodeParameters: FileSizedReader{nodeParameters},
Mateusz Zalega43e21072021-10-08 18:05:29 +0200204 }
205 // Calculate the minimum target size based on the installation parameters.
206 minSize := uint64((installParams.PartitionSize.ESP +
Jan Schär42ef7c72024-03-18 15:09:51 +0100207 installParams.PartitionSize.System*2 +
Mateusz Zalega43e21072021-10-08 18:05:29 +0200208 installParams.PartitionSize.Data + 1) * mib)
209
210 // Look for suitable block devices, given the minimum size.
211 blkDevs, err := findInstallableBlockDevices(espDev, minSize)
212 if err != nil {
Tim Windelschmidt5f1a7de2024-09-19 02:00:14 +0200213 return err
Mateusz Zalega43e21072021-10-08 18:05:29 +0200214 }
215 if len(blkDevs) == 0 {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200216 return fmt.Errorf("couldn't find a suitable block device")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200217 }
218 // Set the first suitable block device found as the installation target.
219 tgtBlkdevName := blkDevs[0]
220 // Update the osimage parameters with a path pointing at the target device.
221 tgtBlkdevPath := filepath.Join("/dev", tgtBlkdevName)
Lorenz Brunad131882023-06-28 16:42:20 +0200222
223 tgtBlockDev, err := blockdev.Open(tgtBlkdevPath)
224 if err != nil {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200225 return fmt.Errorf("error opening target device: %w", err)
Lorenz Brunad131882023-06-28 16:42:20 +0200226 }
227 installParams.Output = tgtBlockDev
Mateusz Zalega43e21072021-10-08 18:05:29 +0200228
229 // Use osimage to partition the target block device and set up its ESP.
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +0200230 // Write will return an EFI boot entry on success.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200231 l.Infof("Installing to %s...", tgtBlkdevPath)
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +0200232 be, err := osimage.Write(&installParams)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200233 if err != nil {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200234 return fmt.Errorf("while installing: %w", err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200235 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200236
237 // Create an EFI boot entry for Metropolis.
Lorenz Brunca1cff02023-06-26 17:52:44 +0200238 en, err := efivarfs.AddBootEntry(be)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200239 if err != nil {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200240 return fmt.Errorf("while creating a boot entry: %w", err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200241 }
242 // Erase the preexisting boot order, leaving Metropolis as the only option.
Lorenz Brun9933ef02023-07-06 18:28:29 +0200243 if err := efivarfs.SetBootOrder(efivarfs.BootOrder{uint16(en)}); err != nil {
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200244 return fmt.Errorf("while adjusting the boot order: %w", err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200245 }
246
247 // Reboot.
Lorenz Brunad131882023-06-28 16:42:20 +0200248 tgtBlockDev.Close()
Mateusz Zalega43e21072021-10-08 18:05:29 +0200249 unix.Sync()
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200250 l.Info("Installation completed. Rebooting.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200251 unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART)
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200252 return nil
Mateusz Zalega43e21072021-10-08 18:05:29 +0200253}