blob: 766c839c3253e1eaaf1431db0a0164aeabf5eecf [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 (
Mateusz Zalega367f7592021-12-20 18:13:31 +010010 "bytes"
Serge Bazanskie2e03712021-12-17 12:47:03 +010011 "context"
Mateusz Zalega43e21072021-10-08 18:05:29 +020012 "fmt"
Mateusz Zalega43e21072021-10-08 18:05:29 +020013 "log"
14 "os"
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010015 "path/filepath"
Mateusz Zalega43e21072021-10-08 18:05:29 +020016 "syscall"
17 "testing"
Serge Bazanski05cf33d2023-03-16 20:50:59 +010018 "time"
Mateusz Zalega43e21072021-10-08 18:05:29 +020019
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +010020 "github.com/bazelbuild/rules_go/go/runfiles"
21 "github.com/diskfs/go-diskfs"
Mateusz Zalega43e21072021-10-08 18:05:29 +020022 "github.com/diskfs/go-diskfs/disk"
23 "github.com/diskfs/go-diskfs/partition/gpt"
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010024
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +010025 "source.monogon.dev/metropolis/proto/api"
26
Mateusz Zalega43e21072021-10-08 18:05:29 +020027 mctl "source.monogon.dev/metropolis/cli/metroctl/core"
Tim Windelschmidtc2290c22024-08-15 19:56:00 +020028 "source.monogon.dev/osbase/build/mkimage/osimage"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020029 "source.monogon.dev/osbase/cmd"
Mateusz Zalega43e21072021-10-08 18:05:29 +020030)
31
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000032var (
33 // These are filled by bazel at linking time with the canonical path of
34 // their corresponding file. Inside the init function we resolve it
35 // with the rules_go runfiles package to the real path.
36 xOvmfCodePath string
37 xOvmfVarsPath string
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000038 xInstallerPath string
39 xBundlePath string
40)
41
42func init() {
43 var err error
44 for _, path := range []*string{
Tim Windelschmidt492434a2024-10-22 14:29:55 +020045 &xOvmfCodePath, &xOvmfVarsPath,
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000046 &xInstallerPath, &xBundlePath,
47 } {
48 *path, err = runfiles.Rlocation(*path)
49 if err != nil {
50 panic(err)
51 }
52 }
53}
54
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010055// Each variable in this block points to either a test dependency or a side
56// effect. These variables are initialized in TestMain using Bazel.
57var (
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010058 // installerImage is a filesystem path pointing at the installer image that
59 // is generated during the test, and is removed afterwards.
60 installerImage string
Mateusz Zalega43e21072021-10-08 18:05:29 +020061)
62
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020063// runQemu starts a new QEMU process, expecting the given output to appear
64// in any line printed. It returns true, if the expected string was found,
65// and false otherwise.
Serge Bazanskie2e03712021-12-17 12:47:03 +010066//
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020067// QEMU is killed shortly after the string is found, or when the context is
68// cancelled.
Serge Bazanskie2e03712021-12-17 12:47:03 +010069func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega43e21072021-10-08 18:05:29 +020070 defaultArgs := []string{
71 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
72 "-m", "512",
73 "-smp", "2",
74 "-cpu", "host",
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000075 "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath,
76 "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath,
Mateusz Zalega43e21072021-10-08 18:05:29 +020077 "-serial", "stdio",
78 "-no-reboot",
79 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020080 qemuArgs := append(defaultArgs, args...)
Mateusz Zalegab838e052022-08-12 18:08:10 +020081 pf := cmd.TerminateIfFound(expectedOutput, nil)
Tim Windelschmidt492434a2024-10-22 14:29:55 +020082 return cmd.RunCommand(ctx, "qemu-system-x86_64", qemuArgs, pf)
Mateusz Zalega43e21072021-10-08 18:05:29 +020083}
84
Serge Bazanskie2e03712021-12-17 12:47:03 +010085// runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
86// same search-through-std{out,err} as runQemu.
87func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010088 args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage)
Serge Bazanskie2e03712021-12-17 12:47:03 +010089 return runQemu(ctx, args, expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010090}
91
Mateusz Zalega43e21072021-10-08 18:05:29 +020092// getStorage creates a sparse file, given a size expressed in mebibytes, and
93// returns a path to that file. It may return an error.
94func getStorage(size int64) (string, error) {
Serge Bazanski05cf33d2023-03-16 20:50:59 +010095 nodeStorageDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "storage")
96 if err != nil {
97 return "", err
98 }
99 nodeStorage := filepath.Join(nodeStorageDir, "stor.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100100 image, err := os.Create(nodeStorage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200101 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100102 return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200103 }
104 if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100105 return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200106 }
107 image.Close()
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100108 return nodeStorage, nil
Mateusz Zalega43e21072021-10-08 18:05:29 +0200109}
110
111// qemuDriveParam returns QEMU parameters required to run it with a
112// raw-format image at path.
113func qemuDriveParam(path string) []string {
114 return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path}
115}
116
117// checkEspContents verifies the presence of the EFI payload inside of image's
118// first partition. It returns nil on success.
119func checkEspContents(image *disk.Disk) error {
120 // Get the ESP.
121 fs, err := image.GetFilesystem(1)
122 if err != nil {
123 return fmt.Errorf("couldn't read the installer ESP: %w", err)
124 }
125 // Make sure the EFI payload exists by attempting to open it.
126 efiPayload, err := fs.OpenFile(osimage.EFIPayloadPath, os.O_RDONLY)
127 if err != nil {
128 return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", osimage.EFIPayloadPath, err)
129 }
130 efiPayload.Close()
131 return nil
132}
133
134func TestMain(m *testing.M) {
Serge Bazanski97783222021-12-14 16:04:26 +0100135 installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100136
Tim Windelschmidt82e6af72024-07-23 00:05:42 +0000137 installer, err := os.ReadFile(xInstallerPath)
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100138 if err != nil {
139 log.Fatal(err)
140 }
141
Tim Windelschmidt82e6af72024-07-23 00:05:42 +0000142 bundle, err := os.ReadFile(xBundlePath)
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100143 if err != nil {
144 log.Fatal(err)
145 }
146
Mateusz Zalega43e21072021-10-08 18:05:29 +0200147 iargs := mctl.MakeInstallerImageArgs{
Lorenz Brunad131882023-06-28 16:42:20 +0200148 Installer: bytes.NewReader(installer),
149 TargetPath: installerImage,
150 NodeParams: &api.NodeParameters{},
151 Bundle: bytes.NewReader(bundle),
Mateusz Zalega43e21072021-10-08 18:05:29 +0200152 }
153 if err := mctl.MakeInstallerImage(iargs); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100154 log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200155 }
156 // With common dependencies set up, run the tests.
157 code := m.Run()
158 // Clean up.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100159 os.Remove(installerImage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200160 os.Exit(code)
161}
162
163func TestInstallerImage(t *testing.T) {
164 // This test examines the installer image, making sure that the GPT and the
165 // ESP contents are in order.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100166 image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200167 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100168 t.Errorf("Couldn't open the installer image at %q: %s", installerImage, err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200169 }
170 // Verify that GPT exists.
171 ti, err := image.GetPartitionTable()
Tim Windelschmidt33306eb2024-04-11 01:39:48 +0200172 if err != nil {
173 t.Fatalf("Couldn't read the installer image partition table: %s", err)
174 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200175 if ti.Type() != "gpt" {
176 t.Error("Couldn't verify that the installer image contains a GPT.")
177 }
178 // Check that the first partition is likely to be a valid ESP.
179 pi := ti.GetPartitions()
180 esp := (pi[0]).(*gpt.Partition)
181 if esp.Start == 0 || esp.End == 0 {
182 t.Error("The installer's ESP GPT entry looks off.")
183 }
184 // Verify that the image contains only one partition.
185 second := (pi[1]).(*gpt.Partition)
186 if second.Name != "" || second.Start != 0 || second.End != 0 {
187 t.Error("It appears the installer image contains more than one partition.")
188 }
189 // Verify the ESP contents.
190 if err := checkEspContents(image); err != nil {
191 t.Error(err.Error())
192 }
193}
194
195func TestNoBlockDevices(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100196 ctx, ctxC := context.WithCancel(context.Background())
197 defer ctxC()
198
Mateusz Zalega43e21072021-10-08 18:05:29 +0200199 // No block devices are passed to QEMU aside from the install medium. Expect
200 // the installer to fail at the device probe stage rather than attempting to
201 // use the medium as the target device.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200202 expectedOutput := "couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100203 result, err := runQemuWithInstaller(ctx, nil, expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200204 if err != nil {
205 t.Error(err.Error())
206 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200207 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200208 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
209 }
210}
211
212func TestBlockDeviceTooSmall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100213 ctx, ctxC := context.WithCancel(context.Background())
214 defer ctxC()
215
Mateusz Zalega43e21072021-10-08 18:05:29 +0200216 // Prepare the block device the installer will install to. This time the
217 // target device is too small to host a Metropolis installation.
218 imagePath, err := getStorage(64)
219 defer os.Remove(imagePath)
220 if err != nil {
Tim Windelschmidt677de972024-09-25 05:30:04 +0200221 t.Error(err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200222 }
223
224 // Run QEMU. Expect the installer to fail with a predefined error string.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200225 expectedOutput := "couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100226 result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200227 if err != nil {
228 t.Error(err.Error())
229 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200230 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200231 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
232 }
233}
234
235func TestInstall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100236 ctx, ctxC := context.WithCancel(context.Background())
237 defer ctxC()
238
Mateusz Zalega43e21072021-10-08 18:05:29 +0200239 // Prepare the block device image the installer will install to.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200240 // Needs enough storage for two 4096 MiB system partitions, a 384 MiB ESP
241 // and a 128 MiB data partition. In addition at the start and end we need
242 // 1MiB for GPT headers and alignment.
243 storagePath, err := getStorage(4096*2 + 384 + 128 + 2)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200244 defer os.Remove(storagePath)
245 if err != nil {
Tim Windelschmidt677de972024-09-25 05:30:04 +0200246 t.Error(err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200247 }
248
249 // Run QEMU. Expect the installer to succeed.
250 expectedOutput := "Installation completed"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100251 result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200252 if err != nil {
253 t.Error(err.Error())
254 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200255 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200256 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
257 }
258
259 // Verify the resulting node image. Check whether the node GPT was created.
260 storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly)
261 if err != nil {
262 t.Errorf("Couldn't open the resulting node image at %q: %s", storagePath, err.Error())
263 }
264 // Verify that GPT exists.
265 ti, err := storage.GetPartitionTable()
Tim Windelschmidt096654a2024-04-18 23:10:19 +0200266 if err != nil {
267 t.Fatalf("Couldn't read the installer image partition table: %s", err)
268 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200269 if ti.Type() != "gpt" {
270 t.Error("Couldn't verify that the resulting node image contains a GPT.")
271 }
272 // Check that the first partition is likely to be a valid ESP.
273 pi := ti.GetPartitions()
274 esp := (pi[0]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200275 if esp.Name != osimage.ESPLabel || esp.Start == 0 || esp.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200276 t.Error("The node's ESP GPT entry looks off.")
277 }
278 // Verify the system partition's GPT entry.
279 system := (pi[1]).(*gpt.Partition)
Lorenz Brun35fcf032023-06-29 04:15:58 +0200280 if system.Name != osimage.SystemALabel || system.Start == 0 || system.End == 0 {
281 t.Error("The node's system partition GPT entry looks off.")
282 }
283 // Verify the system partition's GPT entry.
284 systemB := (pi[2]).(*gpt.Partition)
285 if systemB.Name != osimage.SystemBLabel || systemB.Start == 0 || systemB.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200286 t.Error("The node's system partition GPT entry looks off.")
287 }
288 // Verify the data partition's GPT entry.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200289 data := (pi[3]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200290 if data.Name != osimage.DataLabel || data.Start == 0 || data.End == 0 {
291 t.Errorf("The node's data partition GPT entry looks off: %+v", data)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200292 }
293 // Verify that there are no more partitions.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200294 fourth := (pi[4]).(*gpt.Partition)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200295 if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 {
296 t.Error("The resulting node image contains more partitions than expected.")
297 }
298 // Verify the ESP contents.
299 if err := checkEspContents(storage); err != nil {
300 t.Error(err.Error())
301 }
Serge Bazanskif9bdf312023-03-16 21:54:49 +0100302 storage.File.Close()
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100303 // Run QEMU again. Expect TestOS to launch successfully.
304 expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
Serge Bazanski05cf33d2023-03-16 20:50:59 +0100305 time.Sleep(time.Second)
Serge Bazanskie2e03712021-12-17 12:47:03 +0100306 result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100307 if err != nil {
308 t.Error(err.Error())
309 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200310 if !result {
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100311 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
312 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200313}