| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 3 | |
| 4 | // Installer creates a Metropolis image at a suitable block device based on the |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 5 | // OS image present in the installation medium's ESP, after which it reboots. |
| 6 | // It's meant to be used as an init process. |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 7 | package main |
| 8 | |
| 9 | import ( |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 10 | "context" |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 11 | _ "embed" |
| Lorenz Brun | 57d06a7 | 2022-01-13 14:12:27 +0100 | [diff] [blame] | 12 | "errors" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 13 | "fmt" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 14 | "os" |
| 15 | "path/filepath" |
| 16 | "strings" |
| Lorenz Brun | 57d06a7 | 2022-01-13 14:12:27 +0100 | [diff] [blame] | 17 | "time" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 18 | |
| 19 | "golang.org/x/sys/unix" |
| Serge Bazanski | 9778322 | 2021-12-14 16:04:26 +0100 | [diff] [blame] | 20 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 21 | "source.monogon.dev/osbase/blockdev" |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 22 | "source.monogon.dev/osbase/bringup" |
| Tim Windelschmidt | c2290c2 | 2024-08-15 19:56:00 +0200 | [diff] [blame] | 23 | "source.monogon.dev/osbase/build/mkimage/osimage" |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 24 | "source.monogon.dev/osbase/efivarfs" |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 25 | "source.monogon.dev/osbase/oci" |
| 26 | ociosimage "source.monogon.dev/osbase/oci/osimage" |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 27 | "source.monogon.dev/osbase/structfs" |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 28 | "source.monogon.dev/osbase/supervisor" |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 29 | "source.monogon.dev/osbase/sysfs" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 30 | ) |
| 31 | |
| Jan Schär | 778cc33 | 2025-04-29 16:31:40 +0000 | [diff] [blame] | 32 | //go:embed metropolis/node/core/abloader/abloader_bin.efi |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 33 | var abloader []byte |
| 34 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 35 | const mib = 1024 * 1024 |
| 36 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 37 | // 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. |
| 40 | func 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. |
| 55 | func 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 Windelschmidt | cc27faa | 2024-08-01 02:18:35 +0200 | [diff] [blame] | 59 | if err != nil { |
| Tim Windelschmidt | 096654a | 2024-04-18 23:10:19 +0200 | [diff] [blame] | 60 | return nil, fmt.Errorf("failed to fetch parent device: %w", err) |
| 61 | } |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 62 | // 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 |
| 74 | probeLoop: |
| 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 Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 92 | dev, err := blockdev.Open(devPath) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 93 | if err != nil { |
| 94 | return nil, fmt.Errorf("couldn't open a block device at %q: %w", devPath, err) |
| 95 | } |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 96 | devSize := uint64(dev.BlockCount() * dev.BlockSize()) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 97 | dev.Close() |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 98 | if devSize < minSize { |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 99 | continue |
| 100 | } |
| 101 | |
| 102 | suitable = append(suitable, devInfo.Name()) |
| 103 | } |
| 104 | return suitable, nil |
| 105 | } |
| 106 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 107 | func main() { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 108 | bringup.Runnable(installerRunnable).Run() |
| 109 | } |
| Mateusz Zalega | cdcc739 | 2021-12-08 15:34:53 +0100 | [diff] [blame] | 110 | |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 111 | func installerRunnable(ctx context.Context) error { |
| 112 | l := supervisor.Logger(ctx) |
| 113 | |
| 114 | l.Info("Metropolis Installer") |
| 115 | l.Info("Copyright (c) 2024 The Monogon Project Authors") |
| 116 | l.Info("") |
| 117 | |
| 118 | // Validate we are running via EFI. |
| 119 | if _, err := os.Stat("/sys/firmware/efi"); os.IsNotExist(err) { |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 120 | // nolint:ST1005 |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 121 | return errors.New("Monogon OS can only be installed on EFI-booted machines, this one is not") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 122 | } |
| Serge Bazanski | f71fe92 | 2023-03-22 01:10:37 +0100 | [diff] [blame] | 123 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 124 | // Read the installer ESP UUID from efivarfs. |
| 125 | espUuid, err := efivarfs.ReadLoaderDevicePartUUID() |
| 126 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 127 | return fmt.Errorf("while reading the installer ESP UUID: %w", err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 128 | } |
| Lorenz Brun | 57d06a7 | 2022-01-13 14:12:27 +0100 | [diff] [blame] | 129 | // Wait for up to 30 tries @ 1s (30s) for the ESP to show up |
| 130 | var espDev string |
| 131 | var retries = 30 |
| 132 | for { |
| 133 | // Look up the installer partition based on espUuid. |
| 134 | espDev, err = sysfs.DeviceByPartUUID(espUuid) |
| 135 | if err == nil { |
| 136 | break |
| 137 | } else if errors.Is(err, sysfs.ErrDevNotFound) && retries > 0 { |
| 138 | time.Sleep(1 * time.Second) |
| 139 | retries-- |
| 140 | } else { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 141 | return fmt.Errorf("while resolving the installer device handle: %w", err) |
| Lorenz Brun | 57d06a7 | 2022-01-13 14:12:27 +0100 | [diff] [blame] | 142 | } |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 143 | } |
| Lorenz Brun | 57d06a7 | 2022-01-13 14:12:27 +0100 | [diff] [blame] | 144 | espPath := filepath.Join("/dev", espDev) |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 145 | // Mount the installer partition. The OS image will be read from it. |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 146 | if err := mountInstallerESP(espPath); err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 147 | return fmt.Errorf("while mounting the installer ESP: %w", err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 150 | nodeParameters, err := structfs.OSPathBlob("/installer/metropolis-installer/nodeparams.pb") |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 151 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 152 | return fmt.Errorf("failed to open node parameters from ESP: %w", err) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 155 | ociImage, err := oci.ReadLayout("/installer/metropolis-installer/osimage") |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 156 | if err != nil { |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 157 | return fmt.Errorf("failed to read OS image from ESP: %w", err) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 158 | } |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 159 | osImage, err := ociosimage.Read(ociImage) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 160 | if err != nil { |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 161 | return fmt.Errorf("failed to read OS image from ESP: %w", err) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 162 | } |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 163 | |
| 164 | efiPayload, err := osImage.Payload("kernel.efi") |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 165 | if err != nil { |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 166 | return fmt.Errorf("cannot open EFI payload in OS image: %w", err) |
| 167 | } |
| 168 | systemImage, err := osImage.Payload("system") |
| 169 | if err != nil { |
| 170 | return fmt.Errorf("cannot open system image in OS image: %w", err) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 171 | } |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 172 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 173 | // Build the osimage parameters. |
| 174 | installParams := osimage.Params{ |
| 175 | PartitionSize: osimage.PartitionSizeInfo{ |
| 176 | // ESP is the size of the node ESP partition, expressed in mebibytes. |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 177 | ESP: 384, |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 178 | // System is the size of the node system partition, expressed in |
| 179 | // mebibytes. |
| 180 | System: 4096, |
| 181 | // Data must be nonzero in order for the data partition to be created. |
| 182 | // osimage will extend the data partition to fill all the available space |
| 183 | // whenever it's writing to block devices, such as now. |
| 184 | Data: 128, |
| 185 | }, |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 186 | SystemImage: systemImage, |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 187 | EFIPayload: efiPayload, |
| 188 | ABLoader: structfs.Bytes(abloader), |
| 189 | NodeParameters: nodeParameters, |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 190 | } |
| 191 | // Calculate the minimum target size based on the installation parameters. |
| 192 | minSize := uint64((installParams.PartitionSize.ESP + |
| Jan Schär | 42ef7c7 | 2024-03-18 15:09:51 +0100 | [diff] [blame] | 193 | installParams.PartitionSize.System*2 + |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 194 | installParams.PartitionSize.Data + 1) * mib) |
| 195 | |
| 196 | // Look for suitable block devices, given the minimum size. |
| 197 | blkDevs, err := findInstallableBlockDevices(espDev, minSize) |
| 198 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 199 | return err |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 200 | } |
| 201 | if len(blkDevs) == 0 { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 202 | return fmt.Errorf("couldn't find a suitable block device") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 203 | } |
| 204 | // Set the first suitable block device found as the installation target. |
| 205 | tgtBlkdevName := blkDevs[0] |
| 206 | // Update the osimage parameters with a path pointing at the target device. |
| 207 | tgtBlkdevPath := filepath.Join("/dev", tgtBlkdevName) |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 208 | |
| 209 | tgtBlockDev, err := blockdev.Open(tgtBlkdevPath) |
| 210 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 211 | return fmt.Errorf("error opening target device: %w", err) |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 212 | } |
| 213 | installParams.Output = tgtBlockDev |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 214 | |
| 215 | // Use osimage to partition the target block device and set up its ESP. |
| Tim Windelschmidt | cc27faa | 2024-08-01 02:18:35 +0200 | [diff] [blame] | 216 | // Write will return an EFI boot entry on success. |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 217 | l.Infof("Installing to %s...", tgtBlkdevPath) |
| Tim Windelschmidt | cc27faa | 2024-08-01 02:18:35 +0200 | [diff] [blame] | 218 | be, err := osimage.Write(&installParams) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 219 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 220 | return fmt.Errorf("while installing: %w", err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 221 | } |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 222 | |
| 223 | // Create an EFI boot entry for Metropolis. |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 224 | en, err := efivarfs.AddBootEntry(be) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 225 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 226 | return fmt.Errorf("while creating a boot entry: %w", err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 227 | } |
| 228 | // Erase the preexisting boot order, leaving Metropolis as the only option. |
| Lorenz Brun | 9933ef0 | 2023-07-06 18:28:29 +0200 | [diff] [blame] | 229 | if err := efivarfs.SetBootOrder(efivarfs.BootOrder{uint16(en)}); err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 230 | return fmt.Errorf("while adjusting the boot order: %w", err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Reboot. |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 234 | tgtBlockDev.Close() |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 235 | unix.Sync() |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 236 | l.Info("Installation completed. Rebooting.") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 237 | unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART) |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 238 | return nil |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 239 | } |