blob: 3c8f1a61ccb48060ee26df603ab4c91f66a616f9 [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"
Tim Windelschmidtc2290c22024-08-15 19:56:00 +020027 "source.monogon.dev/osbase/build/mkimage/osimage"
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ärc1b6df42025-03-20 08:52:18 +000030 "source.monogon.dev/osbase/structfs"
Mateusz Zalega43e21072021-10-08 18:05:29 +020031)
32
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000033var (
34 // These are filled by bazel at linking time with the canonical path of
35 // their corresponding file. Inside the init function we resolve it
36 // with the rules_go runfiles package to the real path.
37 xOvmfCodePath string
38 xOvmfVarsPath string
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000039 xInstallerPath string
Jan Schär5fdca562025-04-14 11:33:29 +000040 xImagePath string
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000041)
42
43func init() {
44 var err error
45 for _, path := range []*string{
Tim Windelschmidt492434a2024-10-22 14:29:55 +020046 &xOvmfCodePath, &xOvmfVarsPath,
Jan Schär5fdca562025-04-14 11:33:29 +000047 &xInstallerPath, &xImagePath,
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000048 } {
49 *path, err = runfiles.Rlocation(*path)
50 if err != nil {
51 panic(err)
52 }
53 }
54}
55
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010056// Each variable in this block points to either a test dependency or a side
57// effect. These variables are initialized in TestMain using Bazel.
58var (
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010059 // installerImage is a filesystem path pointing at the installer image that
60 // is generated during the test, and is removed afterwards.
61 installerImage string
Mateusz Zalega43e21072021-10-08 18:05:29 +020062)
63
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020064// runQemu starts a new QEMU process, expecting the given output to appear
65// in any line printed. It returns true, if the expected string was found,
66// and false otherwise.
Serge Bazanskie2e03712021-12-17 12:47:03 +010067//
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020068// QEMU is killed shortly after the string is found, or when the context is
69// cancelled.
Serge Bazanskie2e03712021-12-17 12:47:03 +010070func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega43e21072021-10-08 18:05:29 +020071 defaultArgs := []string{
72 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
73 "-m", "512",
74 "-smp", "2",
75 "-cpu", "host",
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000076 "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath,
77 "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath,
Mateusz Zalega43e21072021-10-08 18:05:29 +020078 "-serial", "stdio",
79 "-no-reboot",
80 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020081 qemuArgs := append(defaultArgs, args...)
Mateusz Zalegab838e052022-08-12 18:08:10 +020082 pf := cmd.TerminateIfFound(expectedOutput, nil)
Tim Windelschmidt492434a2024-10-22 14:29:55 +020083 return cmd.RunCommand(ctx, "qemu-system-x86_64", qemuArgs, pf)
Mateusz Zalega43e21072021-10-08 18:05:29 +020084}
85
Serge Bazanskie2e03712021-12-17 12:47:03 +010086// runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
87// same search-through-std{out,err} as runQemu.
88func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010089 args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage)
Serge Bazanskie2e03712021-12-17 12:47:03 +010090 return runQemu(ctx, args, expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010091}
92
Mateusz Zalega43e21072021-10-08 18:05:29 +020093// getStorage creates a sparse file, given a size expressed in mebibytes, and
94// returns a path to that file. It may return an error.
95func getStorage(size int64) (string, error) {
Serge Bazanski05cf33d2023-03-16 20:50:59 +010096 nodeStorageDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "storage")
97 if err != nil {
98 return "", err
99 }
100 nodeStorage := filepath.Join(nodeStorageDir, "stor.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100101 image, err := os.Create(nodeStorage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200102 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100103 return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200104 }
105 if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100106 return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200107 }
108 image.Close()
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100109 return nodeStorage, nil
Mateusz Zalega43e21072021-10-08 18:05:29 +0200110}
111
112// qemuDriveParam returns QEMU parameters required to run it with a
113// raw-format image at path.
114func qemuDriveParam(path string) []string {
115 return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path}
116}
117
118// checkEspContents verifies the presence of the EFI payload inside of image's
119// first partition. It returns nil on success.
120func checkEspContents(image *disk.Disk) error {
121 // Get the ESP.
122 fs, err := image.GetFilesystem(1)
123 if err != nil {
124 return fmt.Errorf("couldn't read the installer ESP: %w", err)
125 }
126 // Make sure the EFI payload exists by attempting to open it.
Jan Schär091b8a62025-05-13 09:07:50 +0000127 efiPayload, err := fs.OpenFile("/"+osimage.EFIPayloadPath, os.O_RDONLY)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200128 if err != nil {
129 return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", osimage.EFIPayloadPath, err)
130 }
131 efiPayload.Close()
132 return nil
133}
134
135func TestMain(m *testing.M) {
Serge Bazanski97783222021-12-14 16:04:26 +0100136 installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100137
Jan Schärc1b6df42025-03-20 08:52:18 +0000138 installer, err := structfs.OSPathBlob(xInstallerPath)
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100139 if err != nil {
140 log.Fatal(err)
141 }
142
Jan Schär5fdca562025-04-14 11:33:29 +0000143 image, err := oci.ReadLayout(xImagePath)
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100144 if err != nil {
145 log.Fatal(err)
146 }
147
Mateusz Zalega43e21072021-10-08 18:05:29 +0200148 iargs := mctl.MakeInstallerImageArgs{
Jan Schärc1b6df42025-03-20 08:52:18 +0000149 Installer: installer,
Lorenz Brunad131882023-06-28 16:42:20 +0200150 TargetPath: installerImage,
151 NodeParams: &api.NodeParameters{},
Jan Schär5fdca562025-04-14 11:33:29 +0000152 Image: image,
Mateusz Zalega43e21072021-10-08 18:05:29 +0200153 }
154 if err := mctl.MakeInstallerImage(iargs); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100155 log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200156 }
157 // With common dependencies set up, run the tests.
158 code := m.Run()
159 // Clean up.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100160 os.Remove(installerImage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200161 os.Exit(code)
162}
163
164func TestInstallerImage(t *testing.T) {
165 // This test examines the installer image, making sure that the GPT and the
166 // ESP contents are in order.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100167 image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200168 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100169 t.Errorf("Couldn't open the installer image at %q: %s", installerImage, err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200170 }
171 // Verify that GPT exists.
172 ti, err := image.GetPartitionTable()
Tim Windelschmidt33306eb2024-04-11 01:39:48 +0200173 if err != nil {
174 t.Fatalf("Couldn't read the installer image partition table: %s", err)
175 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200176 if ti.Type() != "gpt" {
177 t.Error("Couldn't verify that the installer image contains a GPT.")
178 }
179 // Check that the first partition is likely to be a valid ESP.
180 pi := ti.GetPartitions()
181 esp := (pi[0]).(*gpt.Partition)
182 if esp.Start == 0 || esp.End == 0 {
183 t.Error("The installer's ESP GPT entry looks off.")
184 }
185 // Verify that the image contains only one partition.
186 second := (pi[1]).(*gpt.Partition)
187 if second.Name != "" || second.Start != 0 || second.End != 0 {
188 t.Error("It appears the installer image contains more than one partition.")
189 }
190 // Verify the ESP contents.
191 if err := checkEspContents(image); err != nil {
192 t.Error(err.Error())
193 }
194}
195
196func TestNoBlockDevices(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100197 ctx, ctxC := context.WithCancel(context.Background())
198 defer ctxC()
199
Mateusz Zalega43e21072021-10-08 18:05:29 +0200200 // No block devices are passed to QEMU aside from the install medium. Expect
201 // the installer to fail at the device probe stage rather than attempting to
202 // use the medium as the target device.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200203 expectedOutput := "couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100204 result, err := runQemuWithInstaller(ctx, nil, expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200205 if err != nil {
206 t.Error(err.Error())
207 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200208 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200209 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
210 }
211}
212
213func TestBlockDeviceTooSmall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100214 ctx, ctxC := context.WithCancel(context.Background())
215 defer ctxC()
216
Mateusz Zalega43e21072021-10-08 18:05:29 +0200217 // Prepare the block device the installer will install to. This time the
218 // target device is too small to host a Metropolis installation.
219 imagePath, err := getStorage(64)
220 defer os.Remove(imagePath)
221 if err != nil {
Tim Windelschmidt677de972024-09-25 05:30:04 +0200222 t.Error(err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200223 }
224
225 // Run QEMU. Expect the installer to fail with a predefined error string.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200226 expectedOutput := "couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100227 result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200228 if err != nil {
229 t.Error(err.Error())
230 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200231 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200232 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
233 }
234}
235
236func TestInstall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100237 ctx, ctxC := context.WithCancel(context.Background())
238 defer ctxC()
239
Mateusz Zalega43e21072021-10-08 18:05:29 +0200240 // Prepare the block device image the installer will install to.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200241 // Needs enough storage for two 4096 MiB system partitions, a 384 MiB ESP
242 // and a 128 MiB data partition. In addition at the start and end we need
243 // 1MiB for GPT headers and alignment.
244 storagePath, err := getStorage(4096*2 + 384 + 128 + 2)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200245 defer os.Remove(storagePath)
246 if err != nil {
Tim Windelschmidt677de972024-09-25 05:30:04 +0200247 t.Error(err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200248 }
249
250 // Run QEMU. Expect the installer to succeed.
251 expectedOutput := "Installation completed"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100252 result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200253 if err != nil {
254 t.Error(err.Error())
255 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200256 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200257 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
258 }
259
260 // Verify the resulting node image. Check whether the node GPT was created.
261 storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly)
262 if err != nil {
263 t.Errorf("Couldn't open the resulting node image at %q: %s", storagePath, err.Error())
264 }
265 // Verify that GPT exists.
266 ti, err := storage.GetPartitionTable()
Tim Windelschmidt096654a2024-04-18 23:10:19 +0200267 if err != nil {
268 t.Fatalf("Couldn't read the installer image partition table: %s", err)
269 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200270 if ti.Type() != "gpt" {
271 t.Error("Couldn't verify that the resulting node image contains a GPT.")
272 }
273 // Check that the first partition is likely to be a valid ESP.
274 pi := ti.GetPartitions()
275 esp := (pi[0]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200276 if esp.Name != osimage.ESPLabel || esp.Start == 0 || esp.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200277 t.Error("The node's ESP GPT entry looks off.")
278 }
279 // Verify the system partition's GPT entry.
280 system := (pi[1]).(*gpt.Partition)
Lorenz Brun35fcf032023-06-29 04:15:58 +0200281 if system.Name != osimage.SystemALabel || system.Start == 0 || system.End == 0 {
282 t.Error("The node's system partition GPT entry looks off.")
283 }
284 // Verify the system partition's GPT entry.
285 systemB := (pi[2]).(*gpt.Partition)
286 if systemB.Name != osimage.SystemBLabel || systemB.Start == 0 || systemB.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200287 t.Error("The node's system partition GPT entry looks off.")
288 }
289 // Verify the data partition's GPT entry.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200290 data := (pi[3]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200291 if data.Name != osimage.DataLabel || data.Start == 0 || data.End == 0 {
292 t.Errorf("The node's data partition GPT entry looks off: %+v", data)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200293 }
294 // Verify that there are no more partitions.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200295 fourth := (pi[4]).(*gpt.Partition)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200296 if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 {
297 t.Error("The resulting node image contains more partitions than expected.")
298 }
299 // Verify the ESP contents.
300 if err := checkEspContents(storage); err != nil {
301 t.Error(err.Error())
302 }
Serge Bazanskif9bdf312023-03-16 21:54:49 +0100303 storage.File.Close()
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100304 // Run QEMU again. Expect TestOS to launch successfully.
305 expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
Serge Bazanski05cf33d2023-03-16 20:50:59 +0100306 time.Sleep(time.Second)
Serge Bazanskie2e03712021-12-17 12:47:03 +0100307 result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100308 if err != nil {
309 t.Error(err.Error())
310 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200311 if !result {
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100312 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
313 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200314}