blob: 3da188ae383cd9309a2511b3898501a89414120f [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// This package runs the installer image in a VM provided with an empty block
5// device. It then examines the installer console output and the blok device to
6// determine whether the installation process completed without issue.
Mateusz Zalegaedffbb52022-01-11 15:27:22 +01007package installer
Mateusz Zalega43e21072021-10-08 18:05:29 +02008
9import (
Serge Bazanskie2e03712021-12-17 12:47:03 +010010 "context"
Mateusz Zalega43e21072021-10-08 18:05:29 +020011 "fmt"
Mateusz Zalega43e21072021-10-08 18:05:29 +020012 "log"
13 "os"
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010014 "path/filepath"
Mateusz Zalega43e21072021-10-08 18:05:29 +020015 "syscall"
16 "testing"
Serge Bazanski05cf33d2023-03-16 20:50:59 +010017 "time"
Mateusz Zalega43e21072021-10-08 18:05:29 +020018
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +010019 "github.com/bazelbuild/rules_go/go/runfiles"
20 "github.com/diskfs/go-diskfs"
Mateusz Zalega43e21072021-10-08 18:05:29 +020021 "github.com/diskfs/go-diskfs/disk"
22 "github.com/diskfs/go-diskfs/partition/gpt"
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010023
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +010024 "source.monogon.dev/metropolis/proto/api"
25
Mateusz Zalega43e21072021-10-08 18:05:29 +020026 mctl "source.monogon.dev/metropolis/cli/metroctl/core"
Jan Schäre19d2792025-06-23 12:37:58 +000027 "source.monogon.dev/metropolis/installer/install"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020028 "source.monogon.dev/osbase/cmd"
Jan Schär5fdca562025-04-14 11:33:29 +000029 "source.monogon.dev/osbase/oci"
Jan Schäre19d2792025-06-23 12:37:58 +000030 "source.monogon.dev/osbase/oci/osimage"
Jan Schärc1b6df42025-03-20 08:52:18 +000031 "source.monogon.dev/osbase/structfs"
Mateusz Zalega43e21072021-10-08 18:05:29 +020032)
33
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000034var (
35 // These are filled by bazel at linking time with the canonical path of
36 // their corresponding file. Inside the init function we resolve it
37 // with the rules_go runfiles package to the real path.
38 xOvmfCodePath string
39 xOvmfVarsPath string
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000040 xInstallerPath string
Jan Schär5fdca562025-04-14 11:33:29 +000041 xImagePath string
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +020042 xQEMUPath string
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000043)
44
45func init() {
46 var err error
47 for _, path := range []*string{
Tim Windelschmidt492434a2024-10-22 14:29:55 +020048 &xOvmfCodePath, &xOvmfVarsPath,
Jan Schär5fdca562025-04-14 11:33:29 +000049 &xInstallerPath, &xImagePath,
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +020050 &xQEMUPath,
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000051 } {
52 *path, err = runfiles.Rlocation(*path)
53 if err != nil {
54 panic(err)
55 }
56 }
57}
58
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010059// Each variable in this block points to either a test dependency or a side
60// effect. These variables are initialized in TestMain using Bazel.
61var (
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010062 // installerImage is a filesystem path pointing at the installer image that
63 // is generated during the test, and is removed afterwards.
64 installerImage string
Jan Schär4b888262025-05-13 09:12:03 +000065 bootPath string
Mateusz Zalega43e21072021-10-08 18:05:29 +020066)
67
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020068// runQemu starts a new QEMU process, expecting the given output to appear
69// in any line printed. It returns true, if the expected string was found,
70// and false otherwise.
Serge Bazanskie2e03712021-12-17 12:47:03 +010071//
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020072// QEMU is killed shortly after the string is found, or when the context is
73// cancelled.
Serge Bazanskie2e03712021-12-17 12:47:03 +010074func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega43e21072021-10-08 18:05:29 +020075 defaultArgs := []string{
76 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
77 "-m", "512",
78 "-smp", "2",
79 "-cpu", "host",
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000080 "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath,
81 "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath,
Mateusz Zalega43e21072021-10-08 18:05:29 +020082 "-serial", "stdio",
83 "-no-reboot",
84 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020085 qemuArgs := append(defaultArgs, args...)
Mateusz Zalegab838e052022-08-12 18:08:10 +020086 pf := cmd.TerminateIfFound(expectedOutput, nil)
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +020087 return cmd.RunCommand(ctx, xQEMUPath, qemuArgs, pf)
Mateusz Zalega43e21072021-10-08 18:05:29 +020088}
89
Serge Bazanskie2e03712021-12-17 12:47:03 +010090// runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
91// same search-through-std{out,err} as runQemu.
92func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010093 args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage)
Serge Bazanskie2e03712021-12-17 12:47:03 +010094 return runQemu(ctx, args, expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010095}
96
Mateusz Zalega43e21072021-10-08 18:05:29 +020097// getStorage creates a sparse file, given a size expressed in mebibytes, and
98// returns a path to that file. It may return an error.
99func getStorage(size int64) (string, error) {
Serge Bazanski05cf33d2023-03-16 20:50:59 +0100100 nodeStorageDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "storage")
101 if err != nil {
102 return "", err
103 }
104 nodeStorage := filepath.Join(nodeStorageDir, "stor.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100105 image, err := os.Create(nodeStorage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200106 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100107 return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200108 }
109 if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100110 return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200111 }
112 image.Close()
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100113 return nodeStorage, nil
Mateusz Zalega43e21072021-10-08 18:05:29 +0200114}
115
116// qemuDriveParam returns QEMU parameters required to run it with a
117// raw-format image at path.
118func qemuDriveParam(path string) []string {
119 return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path}
120}
121
122// checkEspContents verifies the presence of the EFI payload inside of image's
123// first partition. It returns nil on success.
124func checkEspContents(image *disk.Disk) error {
125 // Get the ESP.
126 fs, err := image.GetFilesystem(1)
127 if err != nil {
128 return fmt.Errorf("couldn't read the installer ESP: %w", err)
129 }
130 // Make sure the EFI payload exists by attempting to open it.
Jan Schär4b888262025-05-13 09:12:03 +0000131 efiPayload, err := fs.OpenFile("/"+bootPath, os.O_RDONLY)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200132 if err != nil {
Jan Schär4b888262025-05-13 09:12:03 +0000133 return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", bootPath, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200134 }
135 efiPayload.Close()
136 return nil
137}
138
139func TestMain(m *testing.M) {
Serge Bazanski97783222021-12-14 16:04:26 +0100140 installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100141
Jan Schärc1b6df42025-03-20 08:52:18 +0000142 installer, err := structfs.OSPathBlob(xInstallerPath)
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100143 if err != nil {
144 log.Fatal(err)
145 }
146
Jan Schär5fdca562025-04-14 11:33:29 +0000147 image, err := oci.ReadLayout(xImagePath)
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100148 if err != nil {
149 log.Fatal(err)
150 }
151
Jan Schäre19d2792025-06-23 12:37:58 +0000152 osImage, err := osimage.Read(image)
Jan Schär4b888262025-05-13 09:12:03 +0000153 if err != nil {
154 log.Fatal(err)
155 }
Jan Schäre19d2792025-06-23 12:37:58 +0000156 bootPath, err = install.EFIBootPath(osImage.Config.ProductInfo.Architecture())
Jan Schär4b888262025-05-13 09:12:03 +0000157 if err != nil {
158 log.Fatal(err)
159 }
160
Mateusz Zalega43e21072021-10-08 18:05:29 +0200161 iargs := mctl.MakeInstallerImageArgs{
Jan Schärc1b6df42025-03-20 08:52:18 +0000162 Installer: installer,
Lorenz Brunad131882023-06-28 16:42:20 +0200163 TargetPath: installerImage,
164 NodeParams: &api.NodeParameters{},
Jan Schär5fdca562025-04-14 11:33:29 +0000165 Image: image,
Mateusz Zalega43e21072021-10-08 18:05:29 +0200166 }
167 if err := mctl.MakeInstallerImage(iargs); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100168 log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200169 }
170 // With common dependencies set up, run the tests.
171 code := m.Run()
172 // Clean up.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100173 os.Remove(installerImage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200174 os.Exit(code)
175}
176
177func TestInstallerImage(t *testing.T) {
178 // This test examines the installer image, making sure that the GPT and the
179 // ESP contents are in order.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100180 image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200181 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100182 t.Fatalf("Couldn't open the installer image at %q: %s", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200183 }
184 // Verify that GPT exists.
185 ti, err := image.GetPartitionTable()
Tim Windelschmidt33306eb2024-04-11 01:39:48 +0200186 if err != nil {
187 t.Fatalf("Couldn't read the installer image partition table: %s", err)
188 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200189 if ti.Type() != "gpt" {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100190 t.Fatal("Couldn't verify that the installer image contains a GPT.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200191 }
192 // Check that the first partition is likely to be a valid ESP.
193 pi := ti.GetPartitions()
194 esp := (pi[0]).(*gpt.Partition)
195 if esp.Start == 0 || esp.End == 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100196 t.Fatal("The installer's ESP GPT entry looks off.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200197 }
198 // Verify that the image contains only one partition.
199 second := (pi[1]).(*gpt.Partition)
200 if second.Name != "" || second.Start != 0 || second.End != 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100201 t.Fatal("It appears the installer image contains more than one partition.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200202 }
203 // Verify the ESP contents.
204 if err := checkEspContents(image); err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100205 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200206 }
207}
208
209func TestNoBlockDevices(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100210 ctx, ctxC := context.WithCancel(context.Background())
211 defer ctxC()
212
Mateusz Zalega43e21072021-10-08 18:05:29 +0200213 // No block devices are passed to QEMU aside from the install medium. Expect
214 // the installer to fail at the device probe stage rather than attempting to
215 // use the medium as the target device.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200216 expectedOutput := "couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100217 result, err := runQemuWithInstaller(ctx, nil, expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200218 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100219 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200220 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200221 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200222 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
223 }
224}
225
226func TestBlockDeviceTooSmall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100227 ctx, ctxC := context.WithCancel(context.Background())
228 defer ctxC()
229
Mateusz Zalega43e21072021-10-08 18:05:29 +0200230 // Prepare the block device the installer will install to. This time the
231 // target device is too small to host a Metropolis installation.
232 imagePath, err := getStorage(64)
233 defer os.Remove(imagePath)
234 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100235 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200236 }
237
238 // Run QEMU. Expect the installer to fail with a predefined error string.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200239 expectedOutput := "couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100240 result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200241 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100242 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200243 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200244 if !result {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100245 t.Fatalf("QEMU didn't produce the expected output %q", expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200246 }
247}
248
249func TestInstall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100250 ctx, ctxC := context.WithCancel(context.Background())
251 defer ctxC()
252
Mateusz Zalega43e21072021-10-08 18:05:29 +0200253 // Prepare the block device image the installer will install to.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200254 // Needs enough storage for two 4096 MiB system partitions, a 384 MiB ESP
255 // and a 128 MiB data partition. In addition at the start and end we need
256 // 1MiB for GPT headers and alignment.
257 storagePath, err := getStorage(4096*2 + 384 + 128 + 2)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200258 defer os.Remove(storagePath)
259 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100260 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200261 }
262
263 // Run QEMU. Expect the installer to succeed.
264 expectedOutput := "Installation completed"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100265 result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200266 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100267 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200268 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200269 if !result {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100270 t.Fatalf("QEMU didn't produce the expected output %q", expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200271 }
272
273 // Verify the resulting node image. Check whether the node GPT was created.
274 storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly)
275 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100276 t.Fatalf("Couldn't open the resulting node image at %q: %s", storagePath, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200277 }
278 // Verify that GPT exists.
279 ti, err := storage.GetPartitionTable()
Tim Windelschmidt096654a2024-04-18 23:10:19 +0200280 if err != nil {
281 t.Fatalf("Couldn't read the installer image partition table: %s", err)
282 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200283 if ti.Type() != "gpt" {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100284 t.Fatal("Couldn't verify that the resulting node image contains a GPT.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200285 }
286 // Check that the first partition is likely to be a valid ESP.
287 pi := ti.GetPartitions()
288 esp := (pi[0]).(*gpt.Partition)
Jan Schäre19d2792025-06-23 12:37:58 +0000289 if esp.Name != install.ESPLabel || esp.Start == 0 || esp.End == 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100290 t.Fatal("The node's ESP GPT entry looks off.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200291 }
292 // Verify the system partition's GPT entry.
293 system := (pi[1]).(*gpt.Partition)
Jan Schäre19d2792025-06-23 12:37:58 +0000294 if system.Name != install.SystemALabel || system.Start == 0 || system.End == 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100295 t.Fatal("The node's system partition GPT entry looks off.")
Lorenz Brun35fcf032023-06-29 04:15:58 +0200296 }
297 // Verify the system partition's GPT entry.
298 systemB := (pi[2]).(*gpt.Partition)
Jan Schäre19d2792025-06-23 12:37:58 +0000299 if systemB.Name != install.SystemBLabel || systemB.Start == 0 || systemB.End == 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100300 t.Fatal("The node's system partition GPT entry looks off.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200301 }
302 // Verify the data partition's GPT entry.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200303 data := (pi[3]).(*gpt.Partition)
Jan Schäre19d2792025-06-23 12:37:58 +0000304 if data.Name != install.DataLabel || data.Start == 0 || data.End == 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100305 t.Fatalf("The node's data partition GPT entry looks off: %+v", data)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200306 }
307 // Verify that there are no more partitions.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200308 fourth := (pi[4]).(*gpt.Partition)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200309 if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100310 t.Fatal("The resulting node image contains more partitions than expected.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200311 }
312 // Verify the ESP contents.
313 if err := checkEspContents(storage); err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100314 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200315 }
Serge Bazanskif9bdf312023-03-16 21:54:49 +0100316 storage.File.Close()
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100317 // Run QEMU again. Expect TestOS to launch successfully.
318 expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
Serge Bazanski05cf33d2023-03-16 20:50:59 +0100319 time.Sleep(time.Second)
Serge Bazanskie2e03712021-12-17 12:47:03 +0100320 result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100321 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100322 t.Fatal(err)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100323 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200324 if !result {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100325 t.Fatalf("QEMU didn't produce the expected output %q", expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100326 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200327}