blob: bba323f58ceff8ff7aeba747385578b4b5a78eda [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är4b888262025-05-13 09:12:03 +000030 ociosimage "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 Windelschmidt82e6af72024-07-23 00:05:42 +000042)
43
44func init() {
45 var err error
46 for _, path := range []*string{
Tim Windelschmidt492434a2024-10-22 14:29:55 +020047 &xOvmfCodePath, &xOvmfVarsPath,
Jan Schär5fdca562025-04-14 11:33:29 +000048 &xInstallerPath, &xImagePath,
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000049 } {
50 *path, err = runfiles.Rlocation(*path)
51 if err != nil {
52 panic(err)
53 }
54 }
55}
56
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010057// Each variable in this block points to either a test dependency or a side
58// effect. These variables are initialized in TestMain using Bazel.
59var (
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010060 // installerImage is a filesystem path pointing at the installer image that
61 // is generated during the test, and is removed afterwards.
62 installerImage string
Jan Schär4b888262025-05-13 09:12:03 +000063 bootPath string
Mateusz Zalega43e21072021-10-08 18:05:29 +020064)
65
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020066// runQemu starts a new QEMU process, expecting the given output to appear
67// in any line printed. It returns true, if the expected string was found,
68// and false otherwise.
Serge Bazanskie2e03712021-12-17 12:47:03 +010069//
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020070// QEMU is killed shortly after the string is found, or when the context is
71// cancelled.
Serge Bazanskie2e03712021-12-17 12:47:03 +010072func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega43e21072021-10-08 18:05:29 +020073 defaultArgs := []string{
74 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
75 "-m", "512",
76 "-smp", "2",
77 "-cpu", "host",
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000078 "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath,
79 "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath,
Mateusz Zalega43e21072021-10-08 18:05:29 +020080 "-serial", "stdio",
81 "-no-reboot",
82 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020083 qemuArgs := append(defaultArgs, args...)
Mateusz Zalegab838e052022-08-12 18:08:10 +020084 pf := cmd.TerminateIfFound(expectedOutput, nil)
Tim Windelschmidt492434a2024-10-22 14:29:55 +020085 return cmd.RunCommand(ctx, "qemu-system-x86_64", qemuArgs, pf)
Mateusz Zalega43e21072021-10-08 18:05:29 +020086}
87
Serge Bazanskie2e03712021-12-17 12:47:03 +010088// runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
89// same search-through-std{out,err} as runQemu.
90func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010091 args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage)
Serge Bazanskie2e03712021-12-17 12:47:03 +010092 return runQemu(ctx, args, expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010093}
94
Mateusz Zalega43e21072021-10-08 18:05:29 +020095// getStorage creates a sparse file, given a size expressed in mebibytes, and
96// returns a path to that file. It may return an error.
97func getStorage(size int64) (string, error) {
Serge Bazanski05cf33d2023-03-16 20:50:59 +010098 nodeStorageDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "storage")
99 if err != nil {
100 return "", err
101 }
102 nodeStorage := filepath.Join(nodeStorageDir, "stor.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100103 image, err := os.Create(nodeStorage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200104 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100105 return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200106 }
107 if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100108 return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200109 }
110 image.Close()
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100111 return nodeStorage, nil
Mateusz Zalega43e21072021-10-08 18:05:29 +0200112}
113
114// qemuDriveParam returns QEMU parameters required to run it with a
115// raw-format image at path.
116func qemuDriveParam(path string) []string {
117 return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path}
118}
119
120// checkEspContents verifies the presence of the EFI payload inside of image's
121// first partition. It returns nil on success.
122func checkEspContents(image *disk.Disk) error {
123 // Get the ESP.
124 fs, err := image.GetFilesystem(1)
125 if err != nil {
126 return fmt.Errorf("couldn't read the installer ESP: %w", err)
127 }
128 // Make sure the EFI payload exists by attempting to open it.
Jan Schär4b888262025-05-13 09:12:03 +0000129 efiPayload, err := fs.OpenFile("/"+bootPath, os.O_RDONLY)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200130 if err != nil {
Jan Schär4b888262025-05-13 09:12:03 +0000131 return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", bootPath, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200132 }
133 efiPayload.Close()
134 return nil
135}
136
137func TestMain(m *testing.M) {
Serge Bazanski97783222021-12-14 16:04:26 +0100138 installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100139
Jan Schärc1b6df42025-03-20 08:52:18 +0000140 installer, err := structfs.OSPathBlob(xInstallerPath)
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100141 if err != nil {
142 log.Fatal(err)
143 }
144
Jan Schär5fdca562025-04-14 11:33:29 +0000145 image, err := oci.ReadLayout(xImagePath)
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100146 if err != nil {
147 log.Fatal(err)
148 }
149
Jan Schär4b888262025-05-13 09:12:03 +0000150 osImage, err := ociosimage.Read(image)
151 if err != nil {
152 log.Fatal(err)
153 }
154 bootPath, err = osimage.EFIBootPath(osImage.Config.ProductInfo.Architecture())
155 if err != nil {
156 log.Fatal(err)
157 }
158
Mateusz Zalega43e21072021-10-08 18:05:29 +0200159 iargs := mctl.MakeInstallerImageArgs{
Jan Schärc1b6df42025-03-20 08:52:18 +0000160 Installer: installer,
Lorenz Brunad131882023-06-28 16:42:20 +0200161 TargetPath: installerImage,
162 NodeParams: &api.NodeParameters{},
Jan Schär5fdca562025-04-14 11:33:29 +0000163 Image: image,
Mateusz Zalega43e21072021-10-08 18:05:29 +0200164 }
165 if err := mctl.MakeInstallerImage(iargs); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100166 log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200167 }
168 // With common dependencies set up, run the tests.
169 code := m.Run()
170 // Clean up.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100171 os.Remove(installerImage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200172 os.Exit(code)
173}
174
175func TestInstallerImage(t *testing.T) {
176 // This test examines the installer image, making sure that the GPT and the
177 // ESP contents are in order.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100178 image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200179 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100180 t.Fatalf("Couldn't open the installer image at %q: %s", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200181 }
182 // Verify that GPT exists.
183 ti, err := image.GetPartitionTable()
Tim Windelschmidt33306eb2024-04-11 01:39:48 +0200184 if err != nil {
185 t.Fatalf("Couldn't read the installer image partition table: %s", err)
186 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200187 if ti.Type() != "gpt" {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100188 t.Fatal("Couldn't verify that the installer image contains a GPT.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200189 }
190 // Check that the first partition is likely to be a valid ESP.
191 pi := ti.GetPartitions()
192 esp := (pi[0]).(*gpt.Partition)
193 if esp.Start == 0 || esp.End == 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100194 t.Fatal("The installer's ESP GPT entry looks off.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200195 }
196 // Verify that the image contains only one partition.
197 second := (pi[1]).(*gpt.Partition)
198 if second.Name != "" || second.Start != 0 || second.End != 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100199 t.Fatal("It appears the installer image contains more than one partition.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200200 }
201 // Verify the ESP contents.
202 if err := checkEspContents(image); err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100203 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200204 }
205}
206
207func TestNoBlockDevices(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100208 ctx, ctxC := context.WithCancel(context.Background())
209 defer ctxC()
210
Mateusz Zalega43e21072021-10-08 18:05:29 +0200211 // No block devices are passed to QEMU aside from the install medium. Expect
212 // the installer to fail at the device probe stage rather than attempting to
213 // use the medium as the target device.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200214 expectedOutput := "couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100215 result, err := runQemuWithInstaller(ctx, nil, expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200216 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100217 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200218 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200219 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200220 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
221 }
222}
223
224func TestBlockDeviceTooSmall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100225 ctx, ctxC := context.WithCancel(context.Background())
226 defer ctxC()
227
Mateusz Zalega43e21072021-10-08 18:05:29 +0200228 // Prepare the block device the installer will install to. This time the
229 // target device is too small to host a Metropolis installation.
230 imagePath, err := getStorage(64)
231 defer os.Remove(imagePath)
232 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100233 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200234 }
235
236 // Run QEMU. Expect the installer to fail with a predefined error string.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200237 expectedOutput := "couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100238 result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200239 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100240 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200241 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200242 if !result {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100243 t.Fatalf("QEMU didn't produce the expected output %q", expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200244 }
245}
246
247func TestInstall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100248 ctx, ctxC := context.WithCancel(context.Background())
249 defer ctxC()
250
Mateusz Zalega43e21072021-10-08 18:05:29 +0200251 // Prepare the block device image the installer will install to.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200252 // Needs enough storage for two 4096 MiB system partitions, a 384 MiB ESP
253 // and a 128 MiB data partition. In addition at the start and end we need
254 // 1MiB for GPT headers and alignment.
255 storagePath, err := getStorage(4096*2 + 384 + 128 + 2)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200256 defer os.Remove(storagePath)
257 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100258 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200259 }
260
261 // Run QEMU. Expect the installer to succeed.
262 expectedOutput := "Installation completed"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100263 result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200264 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100265 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200266 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200267 if !result {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100268 t.Fatalf("QEMU didn't produce the expected output %q", expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200269 }
270
271 // Verify the resulting node image. Check whether the node GPT was created.
272 storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly)
273 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100274 t.Fatalf("Couldn't open the resulting node image at %q: %s", storagePath, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200275 }
276 // Verify that GPT exists.
277 ti, err := storage.GetPartitionTable()
Tim Windelschmidt096654a2024-04-18 23:10:19 +0200278 if err != nil {
279 t.Fatalf("Couldn't read the installer image partition table: %s", err)
280 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200281 if ti.Type() != "gpt" {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100282 t.Fatal("Couldn't verify that the resulting node image contains a GPT.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200283 }
284 // Check that the first partition is likely to be a valid ESP.
285 pi := ti.GetPartitions()
286 esp := (pi[0]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200287 if esp.Name != osimage.ESPLabel || esp.Start == 0 || esp.End == 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100288 t.Fatal("The node's ESP GPT entry looks off.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200289 }
290 // Verify the system partition's GPT entry.
291 system := (pi[1]).(*gpt.Partition)
Lorenz Brun35fcf032023-06-29 04:15:58 +0200292 if system.Name != osimage.SystemALabel || system.Start == 0 || system.End == 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100293 t.Fatal("The node's system partition GPT entry looks off.")
Lorenz Brun35fcf032023-06-29 04:15:58 +0200294 }
295 // Verify the system partition's GPT entry.
296 systemB := (pi[2]).(*gpt.Partition)
297 if systemB.Name != osimage.SystemBLabel || systemB.Start == 0 || systemB.End == 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100298 t.Fatal("The node's system partition GPT entry looks off.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200299 }
300 // Verify the data partition's GPT entry.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200301 data := (pi[3]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200302 if data.Name != osimage.DataLabel || data.Start == 0 || data.End == 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100303 t.Fatalf("The node's data partition GPT entry looks off: %+v", data)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200304 }
305 // Verify that there are no more partitions.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200306 fourth := (pi[4]).(*gpt.Partition)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200307 if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100308 t.Fatal("The resulting node image contains more partitions than expected.")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200309 }
310 // Verify the ESP contents.
311 if err := checkEspContents(storage); err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100312 t.Fatal(err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200313 }
Serge Bazanskif9bdf312023-03-16 21:54:49 +0100314 storage.File.Close()
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100315 // Run QEMU again. Expect TestOS to launch successfully.
316 expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
Serge Bazanski05cf33d2023-03-16 20:50:59 +0100317 time.Sleep(time.Second)
Serge Bazanskie2e03712021-12-17 12:47:03 +0100318 result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100319 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100320 t.Fatal(err)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100321 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200322 if !result {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100323 t.Fatalf("QEMU didn't produce the expected output %q", expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100324 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200325}