blob: 82efb44095eefb29bed1872ddc80d563dcb866eb [file] [log] [blame]
Mateusz Zalega43e21072021-10-08 18:05:29 +02001// 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// This package runs the installer image in a VM provided with an empty block
18// device. It then examines the installer console output and the blok device to
19// determine whether the installation process completed without issue.
Mateusz Zalegaedffbb52022-01-11 15:27:22 +010020package installer
Mateusz Zalega43e21072021-10-08 18:05:29 +020021
22import (
Mateusz Zalega367f7592021-12-20 18:13:31 +010023 "bytes"
Serge Bazanskie2e03712021-12-17 12:47:03 +010024 "context"
Mateusz Zalega43e21072021-10-08 18:05:29 +020025 "fmt"
Mateusz Zalega43e21072021-10-08 18:05:29 +020026 "log"
27 "os"
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010028 "path/filepath"
Mateusz Zalega43e21072021-10-08 18:05:29 +020029 "syscall"
30 "testing"
Serge Bazanski05cf33d2023-03-16 20:50:59 +010031 "time"
Mateusz Zalega43e21072021-10-08 18:05:29 +020032
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +010033 "github.com/bazelbuild/rules_go/go/runfiles"
34 "github.com/diskfs/go-diskfs"
Mateusz Zalega43e21072021-10-08 18:05:29 +020035 "github.com/diskfs/go-diskfs/disk"
36 "github.com/diskfs/go-diskfs/partition/gpt"
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010037
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +010038 "source.monogon.dev/metropolis/proto/api"
39
Mateusz Zalega43e21072021-10-08 18:05:29 +020040 mctl "source.monogon.dev/metropolis/cli/metroctl/core"
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010041 "source.monogon.dev/metropolis/node/build/mkimage/osimage"
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020042 "source.monogon.dev/metropolis/pkg/cmd"
Mateusz Zalega43e21072021-10-08 18:05:29 +020043)
44
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010045// Each variable in this block points to either a test dependency or a side
46// effect. These variables are initialized in TestMain using Bazel.
47var (
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010048 // installerImage is a filesystem path pointing at the installer image that
49 // is generated during the test, and is removed afterwards.
50 installerImage string
Mateusz Zalega43e21072021-10-08 18:05:29 +020051)
52
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020053// runQemu starts a new QEMU process, expecting the given output to appear
54// in any line printed. It returns true, if the expected string was found,
55// and false otherwise.
Serge Bazanskie2e03712021-12-17 12:47:03 +010056//
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020057// QEMU is killed shortly after the string is found, or when the context is
58// cancelled.
Serge Bazanskie2e03712021-12-17 12:47:03 +010059func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega43e21072021-10-08 18:05:29 +020060 defaultArgs := []string{
61 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
62 "-m", "512",
63 "-smp", "2",
64 "-cpu", "host",
65 "-drive", "if=pflash,format=raw,readonly,file=external/edk2/OVMF_CODE.fd",
66 "-drive", "if=pflash,format=raw,snapshot=on,file=external/edk2/OVMF_VARS.fd",
Mateusz Zalega43e21072021-10-08 18:05:29 +020067 "-serial", "stdio",
68 "-no-reboot",
69 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020070 qemuArgs := append(defaultArgs, args...)
Mateusz Zalegab838e052022-08-12 18:08:10 +020071 pf := cmd.TerminateIfFound(expectedOutput, nil)
Mateusz Zalega6cdc9762022-08-03 17:15:01 +020072 return cmd.RunCommand(ctx, "external/qemu/qemu-x86_64-softmmu", qemuArgs, pf)
Mateusz Zalega43e21072021-10-08 18:05:29 +020073}
74
Serge Bazanskie2e03712021-12-17 12:47:03 +010075// runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
76// same search-through-std{out,err} as runQemu.
77func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010078 args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage)
Serge Bazanskie2e03712021-12-17 12:47:03 +010079 return runQemu(ctx, args, expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010080}
81
Mateusz Zalega43e21072021-10-08 18:05:29 +020082// getStorage creates a sparse file, given a size expressed in mebibytes, and
83// returns a path to that file. It may return an error.
84func getStorage(size int64) (string, error) {
Serge Bazanski05cf33d2023-03-16 20:50:59 +010085 nodeStorageDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "storage")
86 if err != nil {
87 return "", err
88 }
89 nodeStorage := filepath.Join(nodeStorageDir, "stor.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010090 image, err := os.Create(nodeStorage)
Mateusz Zalega43e21072021-10-08 18:05:29 +020091 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010092 return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +020093 }
94 if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010095 return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +020096 }
97 image.Close()
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010098 return nodeStorage, nil
Mateusz Zalega43e21072021-10-08 18:05:29 +020099}
100
101// qemuDriveParam returns QEMU parameters required to run it with a
102// raw-format image at path.
103func qemuDriveParam(path string) []string {
104 return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path}
105}
106
107// checkEspContents verifies the presence of the EFI payload inside of image's
108// first partition. It returns nil on success.
109func checkEspContents(image *disk.Disk) error {
110 // Get the ESP.
111 fs, err := image.GetFilesystem(1)
112 if err != nil {
113 return fmt.Errorf("couldn't read the installer ESP: %w", err)
114 }
115 // Make sure the EFI payload exists by attempting to open it.
116 efiPayload, err := fs.OpenFile(osimage.EFIPayloadPath, os.O_RDONLY)
117 if err != nil {
118 return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", osimage.EFIPayloadPath, err)
119 }
120 efiPayload.Close()
121 return nil
122}
123
124func TestMain(m *testing.M) {
Serge Bazanski97783222021-12-14 16:04:26 +0100125 installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100126
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100127 installerPath, err := runfiles.Rlocation("_main/metropolis/installer/test/kernel.efi")
128 if err != nil {
129 log.Fatal(err)
130 }
131 installer, err := os.ReadFile(installerPath)
132 if err != nil {
133 log.Fatal(err)
134 }
135
136 bundlePath, err := runfiles.Rlocation("_main/metropolis/installer/test/testos/testos_bundle.zip")
137 if err != nil {
138 log.Fatal(err)
139 }
140 bundle, err := os.ReadFile(bundlePath)
141 if err != nil {
142 log.Fatal(err)
143 }
144
Mateusz Zalega43e21072021-10-08 18:05:29 +0200145 iargs := mctl.MakeInstallerImageArgs{
Lorenz Brunad131882023-06-28 16:42:20 +0200146 Installer: bytes.NewReader(installer),
147 TargetPath: installerImage,
148 NodeParams: &api.NodeParameters{},
149 Bundle: bytes.NewReader(bundle),
Mateusz Zalega43e21072021-10-08 18:05:29 +0200150 }
151 if err := mctl.MakeInstallerImage(iargs); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100152 log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200153 }
154 // With common dependencies set up, run the tests.
155 code := m.Run()
156 // Clean up.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100157 os.Remove(installerImage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200158 os.Exit(code)
159}
160
161func TestInstallerImage(t *testing.T) {
162 // This test examines the installer image, making sure that the GPT and the
163 // ESP contents are in order.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100164 image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200165 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100166 t.Errorf("Couldn't open the installer image at %q: %s", installerImage, err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200167 }
168 // Verify that GPT exists.
169 ti, err := image.GetPartitionTable()
170 if ti.Type() != "gpt" {
171 t.Error("Couldn't verify that the installer image contains a GPT.")
172 }
173 // Check that the first partition is likely to be a valid ESP.
174 pi := ti.GetPartitions()
175 esp := (pi[0]).(*gpt.Partition)
176 if esp.Start == 0 || esp.End == 0 {
177 t.Error("The installer's ESP GPT entry looks off.")
178 }
179 // Verify that the image contains only one partition.
180 second := (pi[1]).(*gpt.Partition)
181 if second.Name != "" || second.Start != 0 || second.End != 0 {
182 t.Error("It appears the installer image contains more than one partition.")
183 }
184 // Verify the ESP contents.
185 if err := checkEspContents(image); err != nil {
186 t.Error(err.Error())
187 }
188}
189
190func TestNoBlockDevices(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100191 ctx, ctxC := context.WithCancel(context.Background())
192 defer ctxC()
193
Mateusz Zalega43e21072021-10-08 18:05:29 +0200194 // No block devices are passed to QEMU aside from the install medium. Expect
195 // the installer to fail at the device probe stage rather than attempting to
196 // use the medium as the target device.
Mateusz Zalegacdcc7392021-12-08 15:34:53 +0100197 expectedOutput := "Couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100198 result, err := runQemuWithInstaller(ctx, nil, expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200199 if err != nil {
200 t.Error(err.Error())
201 }
202 if result != true {
203 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
204 }
205}
206
207func TestBlockDeviceTooSmall(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 // Prepare the block device the installer will install to. This time the
212 // target device is too small to host a Metropolis installation.
213 imagePath, err := getStorage(64)
214 defer os.Remove(imagePath)
215 if err != nil {
216 t.Errorf(err.Error())
217 }
218
219 // Run QEMU. Expect the installer to fail with a predefined error string.
Mateusz Zalegacdcc7392021-12-08 15:34:53 +0100220 expectedOutput := "Couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100221 result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200222 if err != nil {
223 t.Error(err.Error())
224 }
225 if result != true {
226 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
227 }
228}
229
230func TestInstall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100231 ctx, ctxC := context.WithCancel(context.Background())
232 defer ctxC()
233
Mateusz Zalega43e21072021-10-08 18:05:29 +0200234 // Prepare the block device image the installer will install to.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200235 // Needs enough storage for two 4096 MiB system partitions, a 384 MiB ESP
236 // and a 128 MiB data partition. In addition at the start and end we need
237 // 1MiB for GPT headers and alignment.
238 storagePath, err := getStorage(4096*2 + 384 + 128 + 2)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200239 defer os.Remove(storagePath)
240 if err != nil {
241 t.Errorf(err.Error())
242 }
243
244 // Run QEMU. Expect the installer to succeed.
245 expectedOutput := "Installation completed"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100246 result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200247 if err != nil {
248 t.Error(err.Error())
249 }
250 if result != true {
251 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
252 }
253
254 // Verify the resulting node image. Check whether the node GPT was created.
255 storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly)
256 if err != nil {
257 t.Errorf("Couldn't open the resulting node image at %q: %s", storagePath, err.Error())
258 }
259 // Verify that GPT exists.
260 ti, err := storage.GetPartitionTable()
261 if ti.Type() != "gpt" {
262 t.Error("Couldn't verify that the resulting node image contains a GPT.")
263 }
264 // Check that the first partition is likely to be a valid ESP.
265 pi := ti.GetPartitions()
266 esp := (pi[0]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200267 if esp.Name != osimage.ESPLabel || esp.Start == 0 || esp.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200268 t.Error("The node's ESP GPT entry looks off.")
269 }
270 // Verify the system partition's GPT entry.
271 system := (pi[1]).(*gpt.Partition)
Lorenz Brun35fcf032023-06-29 04:15:58 +0200272 if system.Name != osimage.SystemALabel || system.Start == 0 || system.End == 0 {
273 t.Error("The node's system partition GPT entry looks off.")
274 }
275 // Verify the system partition's GPT entry.
276 systemB := (pi[2]).(*gpt.Partition)
277 if systemB.Name != osimage.SystemBLabel || systemB.Start == 0 || systemB.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200278 t.Error("The node's system partition GPT entry looks off.")
279 }
280 // Verify the data partition's GPT entry.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200281 data := (pi[3]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200282 if data.Name != osimage.DataLabel || data.Start == 0 || data.End == 0 {
283 t.Errorf("The node's data partition GPT entry looks off: %+v", data)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200284 }
285 // Verify that there are no more partitions.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200286 fourth := (pi[4]).(*gpt.Partition)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200287 if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 {
288 t.Error("The resulting node image contains more partitions than expected.")
289 }
290 // Verify the ESP contents.
291 if err := checkEspContents(storage); err != nil {
292 t.Error(err.Error())
293 }
Serge Bazanskif9bdf312023-03-16 21:54:49 +0100294 storage.File.Close()
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100295 // Run QEMU again. Expect TestOS to launch successfully.
296 expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
Serge Bazanski05cf33d2023-03-16 20:50:59 +0100297 time.Sleep(time.Second)
Serge Bazanskie2e03712021-12-17 12:47:03 +0100298 result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100299 if err != nil {
300 t.Error(err.Error())
301 }
302 if result != true {
303 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
304 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200305}