blob: 56b1128ac8b88d9e88e2882ac0e699cbb9d8f61b [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) {
Tim Windelschmidt244b5672024-02-06 10:18:56 +010060 ovmfVarsPath, err := runfiles.Rlocation("edk2/OVMF_VARS.fd")
61 if err != nil {
62 return false, err
63 }
64 ovmfCodePath, err := runfiles.Rlocation("edk2/OVMF_CODE.fd")
65 if err != nil {
66 return false, err
67 }
68 qemuPath, err := runfiles.Rlocation("qemu/qemu-x86_64-softmmu")
69 if err != nil {
70 return false, err
71 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020072 defaultArgs := []string{
73 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
74 "-m", "512",
75 "-smp", "2",
76 "-cpu", "host",
Tim Windelschmidt244b5672024-02-06 10:18:56 +010077 "-drive", "if=pflash,format=raw,snapshot=on,file=" + ovmfCodePath,
78 "-drive", "if=pflash,format=raw,readonly=on,file=" + ovmfVarsPath,
Mateusz Zalega43e21072021-10-08 18:05:29 +020079 "-serial", "stdio",
80 "-no-reboot",
81 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020082 qemuArgs := append(defaultArgs, args...)
Mateusz Zalegab838e052022-08-12 18:08:10 +020083 pf := cmd.TerminateIfFound(expectedOutput, nil)
Tim Windelschmidt244b5672024-02-06 10:18:56 +010084 return cmd.RunCommand(ctx, qemuPath, qemuArgs, pf)
Mateusz Zalega43e21072021-10-08 18:05:29 +020085}
86
Serge Bazanskie2e03712021-12-17 12:47:03 +010087// runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
88// same search-through-std{out,err} as runQemu.
89func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010090 args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage)
Serge Bazanskie2e03712021-12-17 12:47:03 +010091 return runQemu(ctx, args, expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010092}
93
Mateusz Zalega43e21072021-10-08 18:05:29 +020094// getStorage creates a sparse file, given a size expressed in mebibytes, and
95// returns a path to that file. It may return an error.
96func getStorage(size int64) (string, error) {
Serge Bazanski05cf33d2023-03-16 20:50:59 +010097 nodeStorageDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "storage")
98 if err != nil {
99 return "", err
100 }
101 nodeStorage := filepath.Join(nodeStorageDir, "stor.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100102 image, err := os.Create(nodeStorage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200103 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100104 return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200105 }
106 if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100107 return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200108 }
109 image.Close()
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100110 return nodeStorage, nil
Mateusz Zalega43e21072021-10-08 18:05:29 +0200111}
112
113// qemuDriveParam returns QEMU parameters required to run it with a
114// raw-format image at path.
115func qemuDriveParam(path string) []string {
116 return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path}
117}
118
119// checkEspContents verifies the presence of the EFI payload inside of image's
120// first partition. It returns nil on success.
121func checkEspContents(image *disk.Disk) error {
122 // Get the ESP.
123 fs, err := image.GetFilesystem(1)
124 if err != nil {
125 return fmt.Errorf("couldn't read the installer ESP: %w", err)
126 }
127 // Make sure the EFI payload exists by attempting to open it.
128 efiPayload, err := fs.OpenFile(osimage.EFIPayloadPath, os.O_RDONLY)
129 if err != nil {
130 return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", osimage.EFIPayloadPath, err)
131 }
132 efiPayload.Close()
133 return nil
134}
135
136func TestMain(m *testing.M) {
Serge Bazanski97783222021-12-14 16:04:26 +0100137 installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100138
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100139 installerPath, err := runfiles.Rlocation("_main/metropolis/installer/test/kernel.efi")
140 if err != nil {
141 log.Fatal(err)
142 }
143 installer, err := os.ReadFile(installerPath)
144 if err != nil {
145 log.Fatal(err)
146 }
147
148 bundlePath, err := runfiles.Rlocation("_main/metropolis/installer/test/testos/testos_bundle.zip")
149 if err != nil {
150 log.Fatal(err)
151 }
152 bundle, err := os.ReadFile(bundlePath)
153 if err != nil {
154 log.Fatal(err)
155 }
156
Mateusz Zalega43e21072021-10-08 18:05:29 +0200157 iargs := mctl.MakeInstallerImageArgs{
Lorenz Brunad131882023-06-28 16:42:20 +0200158 Installer: bytes.NewReader(installer),
159 TargetPath: installerImage,
160 NodeParams: &api.NodeParameters{},
161 Bundle: bytes.NewReader(bundle),
Mateusz Zalega43e21072021-10-08 18:05:29 +0200162 }
163 if err := mctl.MakeInstallerImage(iargs); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100164 log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200165 }
166 // With common dependencies set up, run the tests.
167 code := m.Run()
168 // Clean up.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100169 os.Remove(installerImage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200170 os.Exit(code)
171}
172
173func TestInstallerImage(t *testing.T) {
174 // This test examines the installer image, making sure that the GPT and the
175 // ESP contents are in order.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100176 image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200177 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100178 t.Errorf("Couldn't open the installer image at %q: %s", installerImage, err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200179 }
180 // Verify that GPT exists.
181 ti, err := image.GetPartitionTable()
182 if ti.Type() != "gpt" {
183 t.Error("Couldn't verify that the installer image contains a GPT.")
184 }
185 // Check that the first partition is likely to be a valid ESP.
186 pi := ti.GetPartitions()
187 esp := (pi[0]).(*gpt.Partition)
188 if esp.Start == 0 || esp.End == 0 {
189 t.Error("The installer's ESP GPT entry looks off.")
190 }
191 // Verify that the image contains only one partition.
192 second := (pi[1]).(*gpt.Partition)
193 if second.Name != "" || second.Start != 0 || second.End != 0 {
194 t.Error("It appears the installer image contains more than one partition.")
195 }
196 // Verify the ESP contents.
197 if err := checkEspContents(image); err != nil {
198 t.Error(err.Error())
199 }
200}
201
202func TestNoBlockDevices(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100203 ctx, ctxC := context.WithCancel(context.Background())
204 defer ctxC()
205
Mateusz Zalega43e21072021-10-08 18:05:29 +0200206 // No block devices are passed to QEMU aside from the install medium. Expect
207 // the installer to fail at the device probe stage rather than attempting to
208 // use the medium as the target device.
Mateusz Zalegacdcc7392021-12-08 15:34:53 +0100209 expectedOutput := "Couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100210 result, err := runQemuWithInstaller(ctx, nil, expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200211 if err != nil {
212 t.Error(err.Error())
213 }
214 if result != true {
215 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
216 }
217}
218
219func TestBlockDeviceTooSmall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100220 ctx, ctxC := context.WithCancel(context.Background())
221 defer ctxC()
222
Mateusz Zalega43e21072021-10-08 18:05:29 +0200223 // Prepare the block device the installer will install to. This time the
224 // target device is too small to host a Metropolis installation.
225 imagePath, err := getStorage(64)
226 defer os.Remove(imagePath)
227 if err != nil {
228 t.Errorf(err.Error())
229 }
230
231 // Run QEMU. Expect the installer to fail with a predefined error string.
Mateusz Zalegacdcc7392021-12-08 15:34:53 +0100232 expectedOutput := "Couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100233 result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200234 if err != nil {
235 t.Error(err.Error())
236 }
237 if result != true {
238 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
239 }
240}
241
242func TestInstall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100243 ctx, ctxC := context.WithCancel(context.Background())
244 defer ctxC()
245
Mateusz Zalega43e21072021-10-08 18:05:29 +0200246 // Prepare the block device image the installer will install to.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200247 // Needs enough storage for two 4096 MiB system partitions, a 384 MiB ESP
248 // and a 128 MiB data partition. In addition at the start and end we need
249 // 1MiB for GPT headers and alignment.
250 storagePath, err := getStorage(4096*2 + 384 + 128 + 2)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200251 defer os.Remove(storagePath)
252 if err != nil {
253 t.Errorf(err.Error())
254 }
255
256 // Run QEMU. Expect the installer to succeed.
257 expectedOutput := "Installation completed"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100258 result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200259 if err != nil {
260 t.Error(err.Error())
261 }
262 if result != true {
263 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
264 }
265
266 // Verify the resulting node image. Check whether the node GPT was created.
267 storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly)
268 if err != nil {
269 t.Errorf("Couldn't open the resulting node image at %q: %s", storagePath, err.Error())
270 }
271 // Verify that GPT exists.
272 ti, err := storage.GetPartitionTable()
273 if ti.Type() != "gpt" {
274 t.Error("Couldn't verify that the resulting node image contains a GPT.")
275 }
276 // Check that the first partition is likely to be a valid ESP.
277 pi := ti.GetPartitions()
278 esp := (pi[0]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200279 if esp.Name != osimage.ESPLabel || esp.Start == 0 || esp.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200280 t.Error("The node's ESP GPT entry looks off.")
281 }
282 // Verify the system partition's GPT entry.
283 system := (pi[1]).(*gpt.Partition)
Lorenz Brun35fcf032023-06-29 04:15:58 +0200284 if system.Name != osimage.SystemALabel || system.Start == 0 || system.End == 0 {
285 t.Error("The node's system partition GPT entry looks off.")
286 }
287 // Verify the system partition's GPT entry.
288 systemB := (pi[2]).(*gpt.Partition)
289 if systemB.Name != osimage.SystemBLabel || systemB.Start == 0 || systemB.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200290 t.Error("The node's system partition GPT entry looks off.")
291 }
292 // Verify the data partition's GPT entry.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200293 data := (pi[3]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200294 if data.Name != osimage.DataLabel || data.Start == 0 || data.End == 0 {
295 t.Errorf("The node's data partition GPT entry looks off: %+v", data)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200296 }
297 // Verify that there are no more partitions.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200298 fourth := (pi[4]).(*gpt.Partition)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200299 if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 {
300 t.Error("The resulting node image contains more partitions than expected.")
301 }
302 // Verify the ESP contents.
303 if err := checkEspContents(storage); err != nil {
304 t.Error(err.Error())
305 }
Serge Bazanskif9bdf312023-03-16 21:54:49 +0100306 storage.File.Close()
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100307 // Run QEMU again. Expect TestOS to launch successfully.
308 expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
Serge Bazanski05cf33d2023-03-16 20:50:59 +0100309 time.Sleep(time.Second)
Serge Bazanskie2e03712021-12-17 12:47:03 +0100310 result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100311 if err != nil {
312 t.Error(err.Error())
313 }
314 if result != true {
315 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
316 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200317}