| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | // Installer creates a Metropolis image at a suitable block device based on the |
| 18 | // installer bundle present in the installation medium's ESP, after which it |
| 19 | // reboots. It's meant to be used as an init process. |
| 20 | package main |
| 21 | |
| 22 | import ( |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 23 | "archive/zip" |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 24 | "bytes" |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 25 | "context" |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 26 | _ "embed" |
| Lorenz Brun | 57d06a7 | 2022-01-13 14:12:27 +0100 | [diff] [blame] | 27 | "errors" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 28 | "fmt" |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 29 | "io/fs" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 30 | "os" |
| 31 | "path/filepath" |
| 32 | "strings" |
| Lorenz Brun | 57d06a7 | 2022-01-13 14:12:27 +0100 | [diff] [blame] | 33 | "time" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 34 | |
| 35 | "golang.org/x/sys/unix" |
| Serge Bazanski | 9778322 | 2021-12-14 16:04:26 +0100 | [diff] [blame] | 36 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 37 | "source.monogon.dev/osbase/blockdev" |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 38 | "source.monogon.dev/osbase/bringup" |
| Tim Windelschmidt | c2290c2 | 2024-08-15 19:56:00 +0200 | [diff] [blame] | 39 | "source.monogon.dev/osbase/build/mkimage/osimage" |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 40 | "source.monogon.dev/osbase/efivarfs" |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 41 | "source.monogon.dev/osbase/supervisor" |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 42 | "source.monogon.dev/osbase/sysfs" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 43 | ) |
| 44 | |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 45 | //go:embed metropolis/node/core/abloader/abloader_bin.efi |
| 46 | var abloader []byte |
| 47 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 48 | const mib = 1024 * 1024 |
| 49 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 50 | // mountInstallerESP mounts the filesystem the installer was loaded from based |
| 51 | // on espPath, which must point to the appropriate partition block device. The |
| 52 | // filesystem is mounted at /installer. |
| 53 | func mountInstallerESP(espPath string) error { |
| 54 | // Create the mountpoint. |
| 55 | if err := unix.Mkdir("/installer", 0700); err != nil { |
| 56 | return fmt.Errorf("couldn't create the installer mountpoint: %w", err) |
| 57 | } |
| 58 | // Mount the filesystem. |
| 59 | if err := unix.Mount(espPath, "/installer", "vfat", unix.MS_NOEXEC|unix.MS_RDONLY, ""); err != nil { |
| 60 | return fmt.Errorf("couldn't mount the installer ESP (%q -> %q): %w", espPath, "/installer", err) |
| 61 | } |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | // findInstallableBlockDevices returns names of all the block devices suitable |
| 66 | // for hosting a Metropolis installation, limited by the size expressed in |
| 67 | // bytes minSize. The install medium espDev will be excluded from the result. |
| 68 | func findInstallableBlockDevices(espDev string, minSize uint64) ([]string, error) { |
| 69 | // Use the partition's name to find and return the name of its parent |
| 70 | // device. It will be excluded from the list of suitable target devices. |
| 71 | srcDev, err := sysfs.ParentBlockDevice(espDev) |
| Tim Windelschmidt | cc27faa | 2024-08-01 02:18:35 +0200 | [diff] [blame] | 72 | if err != nil { |
| Tim Windelschmidt | 096654a | 2024-04-18 23:10:19 +0200 | [diff] [blame] | 73 | return nil, fmt.Errorf("failed to fetch parent device: %w", err) |
| 74 | } |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 75 | // Build the exclusion list containing forbidden handle prefixes. |
| 76 | exclude := []string{"dm-", "zram", "ram", "loop", srcDev} |
| 77 | |
| 78 | // Get the block device handles by looking up directory contents. |
| 79 | const blkDirPath = "/sys/class/block" |
| 80 | blkDevs, err := os.ReadDir(blkDirPath) |
| 81 | if err != nil { |
| 82 | return nil, fmt.Errorf("couldn't read %q: %w", blkDirPath, err) |
| 83 | } |
| 84 | // Iterate over the handles, skipping any block device that either points to |
| 85 | // a partition, matches the exclusion list, or is smaller than minSize. |
| 86 | var suitable []string |
| 87 | probeLoop: |
| 88 | for _, devInfo := range blkDevs { |
| 89 | // Skip devices according to the exclusion list. |
| 90 | for _, prefix := range exclude { |
| 91 | if strings.HasPrefix(devInfo.Name(), prefix) { |
| 92 | continue probeLoop |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Skip partition symlinks. |
| 97 | if _, err := os.Stat(filepath.Join(blkDirPath, devInfo.Name(), "partition")); err == nil { |
| 98 | continue |
| 99 | } else if !os.IsNotExist(err) { |
| 100 | return nil, fmt.Errorf("while probing sysfs: %w", err) |
| 101 | } |
| 102 | |
| 103 | // Skip devices of insufficient size. |
| 104 | devPath := filepath.Join("/dev", devInfo.Name()) |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 105 | dev, err := blockdev.Open(devPath) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 106 | if err != nil { |
| 107 | return nil, fmt.Errorf("couldn't open a block device at %q: %w", devPath, err) |
| 108 | } |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 109 | devSize := uint64(dev.BlockCount() * dev.BlockSize()) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 110 | dev.Close() |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 111 | if devSize < minSize { |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 112 | continue |
| 113 | } |
| 114 | |
| 115 | suitable = append(suitable, devInfo.Name()) |
| 116 | } |
| 117 | return suitable, nil |
| 118 | } |
| 119 | |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 120 | // FileSizedReader is a small adapter from fs.File to fs.SizedReader |
| 121 | // Panics on Stat() failure, so should only be used with sources where Stat() |
| 122 | // cannot fail. |
| 123 | type FileSizedReader struct { |
| 124 | fs.File |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 127 | func (f FileSizedReader) Size() int64 { |
| 128 | stat, err := f.Stat() |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 129 | if err != nil { |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 130 | panic(err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 131 | } |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 132 | return stat.Size() |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | func main() { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 136 | bringup.Runnable(installerRunnable).Run() |
| 137 | } |
| Mateusz Zalega | cdcc739 | 2021-12-08 15:34:53 +0100 | [diff] [blame] | 138 | |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 139 | func installerRunnable(ctx context.Context) error { |
| 140 | l := supervisor.Logger(ctx) |
| 141 | |
| 142 | l.Info("Metropolis Installer") |
| 143 | l.Info("Copyright (c) 2024 The Monogon Project Authors") |
| 144 | l.Info("") |
| 145 | |
| 146 | // Validate we are running via EFI. |
| 147 | if _, err := os.Stat("/sys/firmware/efi"); os.IsNotExist(err) { |
| 148 | //nolint:ST1005 |
| 149 | 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] | 150 | } |
| Serge Bazanski | f71fe92 | 2023-03-22 01:10:37 +0100 | [diff] [blame] | 151 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 152 | // Read the installer ESP UUID from efivarfs. |
| 153 | espUuid, err := efivarfs.ReadLoaderDevicePartUUID() |
| 154 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 155 | return fmt.Errorf("while reading the installer ESP UUID: %w", err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 156 | } |
| Lorenz Brun | 57d06a7 | 2022-01-13 14:12:27 +0100 | [diff] [blame] | 157 | // Wait for up to 30 tries @ 1s (30s) for the ESP to show up |
| 158 | var espDev string |
| 159 | var retries = 30 |
| 160 | for { |
| 161 | // Look up the installer partition based on espUuid. |
| 162 | espDev, err = sysfs.DeviceByPartUUID(espUuid) |
| 163 | if err == nil { |
| 164 | break |
| 165 | } else if errors.Is(err, sysfs.ErrDevNotFound) && retries > 0 { |
| 166 | time.Sleep(1 * time.Second) |
| 167 | retries-- |
| 168 | } else { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 169 | return fmt.Errorf("while resolving the installer device handle: %w", err) |
| Lorenz Brun | 57d06a7 | 2022-01-13 14:12:27 +0100 | [diff] [blame] | 170 | } |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 171 | } |
| Lorenz Brun | 57d06a7 | 2022-01-13 14:12:27 +0100 | [diff] [blame] | 172 | espPath := filepath.Join("/dev", espDev) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 173 | // Mount the installer partition. The installer bundle will be read from it. |
| 174 | if err := mountInstallerESP(espPath); err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 175 | return fmt.Errorf("while mounting the installer ESP: %w", err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| Lorenz Brun | 6c35e97 | 2021-12-14 03:08:23 +0100 | [diff] [blame] | 178 | nodeParameters, err := os.Open("/installer/metropolis-installer/nodeparams.pb") |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 179 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 180 | return fmt.Errorf("failed to open node parameters from ESP: %w", err) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // TODO(lorenz): Replace with proper bundles |
| Lorenz Brun | 6c35e97 | 2021-12-14 03:08:23 +0100 | [diff] [blame] | 184 | bundle, err := zip.OpenReader("/installer/metropolis-installer/bundle.bin") |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 185 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 186 | return fmt.Errorf("failed to open node bundle from ESP: %w", err) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 187 | } |
| 188 | defer bundle.Close() |
| 189 | efiPayload, err := bundle.Open("kernel_efi.efi") |
| 190 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 191 | return fmt.Errorf("cannot open EFI payload in bundle: %w", err) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 192 | } |
| 193 | defer efiPayload.Close() |
| Mateusz Zalega | 8c2c771 | 2022-01-25 19:42:21 +0100 | [diff] [blame] | 194 | systemImage, err := bundle.Open("verity_rootfs.img") |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 195 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 196 | return fmt.Errorf("cannot open system image in bundle: %w", err) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 197 | } |
| 198 | defer systemImage.Close() |
| 199 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 200 | // Build the osimage parameters. |
| 201 | installParams := osimage.Params{ |
| 202 | PartitionSize: osimage.PartitionSizeInfo{ |
| 203 | // ESP is the size of the node ESP partition, expressed in mebibytes. |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 204 | ESP: 384, |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 205 | // System is the size of the node system partition, expressed in |
| 206 | // mebibytes. |
| 207 | System: 4096, |
| 208 | // Data must be nonzero in order for the data partition to be created. |
| 209 | // osimage will extend the data partition to fill all the available space |
| 210 | // whenever it's writing to block devices, such as now. |
| 211 | Data: 128, |
| 212 | }, |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 213 | SystemImage: systemImage, |
| 214 | EFIPayload: FileSizedReader{efiPayload}, |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 215 | ABLoader: bytes.NewReader(abloader), |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 216 | NodeParameters: FileSizedReader{nodeParameters}, |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 217 | } |
| 218 | // Calculate the minimum target size based on the installation parameters. |
| 219 | minSize := uint64((installParams.PartitionSize.ESP + |
| Jan Schär | 42ef7c7 | 2024-03-18 15:09:51 +0100 | [diff] [blame] | 220 | installParams.PartitionSize.System*2 + |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 221 | installParams.PartitionSize.Data + 1) * mib) |
| 222 | |
| 223 | // Look for suitable block devices, given the minimum size. |
| 224 | blkDevs, err := findInstallableBlockDevices(espDev, minSize) |
| 225 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 226 | return err |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 227 | } |
| 228 | if len(blkDevs) == 0 { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 229 | return fmt.Errorf("couldn't find a suitable block device") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 230 | } |
| 231 | // Set the first suitable block device found as the installation target. |
| 232 | tgtBlkdevName := blkDevs[0] |
| 233 | // Update the osimage parameters with a path pointing at the target device. |
| 234 | tgtBlkdevPath := filepath.Join("/dev", tgtBlkdevName) |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 235 | |
| 236 | tgtBlockDev, err := blockdev.Open(tgtBlkdevPath) |
| 237 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 238 | return fmt.Errorf("error opening target device: %w", err) |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 239 | } |
| 240 | installParams.Output = tgtBlockDev |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 241 | |
| 242 | // 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] | 243 | // Write will return an EFI boot entry on success. |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 244 | l.Infof("Installing to %s...", tgtBlkdevPath) |
| Tim Windelschmidt | cc27faa | 2024-08-01 02:18:35 +0200 | [diff] [blame] | 245 | be, err := osimage.Write(&installParams) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 246 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 247 | return fmt.Errorf("while installing: %w", err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 248 | } |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 249 | |
| 250 | // Create an EFI boot entry for Metropolis. |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 251 | en, err := efivarfs.AddBootEntry(be) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 252 | if err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 253 | return fmt.Errorf("while creating a boot entry: %w", err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 254 | } |
| 255 | // Erase the preexisting boot order, leaving Metropolis as the only option. |
| Lorenz Brun | 9933ef0 | 2023-07-06 18:28:29 +0200 | [diff] [blame] | 256 | if err := efivarfs.SetBootOrder(efivarfs.BootOrder{uint16(en)}); err != nil { |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 257 | return fmt.Errorf("while adjusting the boot order: %w", err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Reboot. |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 261 | tgtBlockDev.Close() |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 262 | unix.Sync() |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 263 | l.Info("Installation completed. Rebooting.") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 264 | unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART) |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 265 | return nil |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 266 | } |