blob: a8db235be73e04f0971570c6c0bbc18485881658 [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"
Tim Windelschmidtc2290c22024-08-15 19:56:00 +020041 "source.monogon.dev/osbase/build/mkimage/osimage"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020042 "source.monogon.dev/osbase/cmd"
Mateusz Zalega43e21072021-10-08 18:05:29 +020043)
44
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000045var (
46 // These are filled by bazel at linking time with the canonical path of
47 // their corresponding file. Inside the init function we resolve it
48 // with the rules_go runfiles package to the real path.
49 xOvmfCodePath string
50 xOvmfVarsPath string
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000051 xInstallerPath string
52 xBundlePath string
53)
54
55func init() {
56 var err error
57 for _, path := range []*string{
Tim Windelschmidt492434a2024-10-22 14:29:55 +020058 &xOvmfCodePath, &xOvmfVarsPath,
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000059 &xInstallerPath, &xBundlePath,
60 } {
61 *path, err = runfiles.Rlocation(*path)
62 if err != nil {
63 panic(err)
64 }
65 }
66}
67
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010068// Each variable in this block points to either a test dependency or a side
69// effect. These variables are initialized in TestMain using Bazel.
70var (
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010071 // installerImage is a filesystem path pointing at the installer image that
72 // is generated during the test, and is removed afterwards.
73 installerImage string
Mateusz Zalega43e21072021-10-08 18:05:29 +020074)
75
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020076// runQemu starts a new QEMU process, expecting the given output to appear
77// in any line printed. It returns true, if the expected string was found,
78// and false otherwise.
Serge Bazanskie2e03712021-12-17 12:47:03 +010079//
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020080// QEMU is killed shortly after the string is found, or when the context is
81// cancelled.
Serge Bazanskie2e03712021-12-17 12:47:03 +010082func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega43e21072021-10-08 18:05:29 +020083 defaultArgs := []string{
84 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
85 "-m", "512",
86 "-smp", "2",
87 "-cpu", "host",
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000088 "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath,
89 "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath,
Mateusz Zalega43e21072021-10-08 18:05:29 +020090 "-serial", "stdio",
91 "-no-reboot",
92 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020093 qemuArgs := append(defaultArgs, args...)
Mateusz Zalegab838e052022-08-12 18:08:10 +020094 pf := cmd.TerminateIfFound(expectedOutput, nil)
Tim Windelschmidt492434a2024-10-22 14:29:55 +020095 return cmd.RunCommand(ctx, "qemu-system-x86_64", qemuArgs, pf)
Mateusz Zalega43e21072021-10-08 18:05:29 +020096}
97
Serge Bazanskie2e03712021-12-17 12:47:03 +010098// runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
99// same search-through-std{out,err} as runQemu.
100func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100101 args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage)
Serge Bazanskie2e03712021-12-17 12:47:03 +0100102 return runQemu(ctx, args, expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100103}
104
Mateusz Zalega43e21072021-10-08 18:05:29 +0200105// getStorage creates a sparse file, given a size expressed in mebibytes, and
106// returns a path to that file. It may return an error.
107func getStorage(size int64) (string, error) {
Serge Bazanski05cf33d2023-03-16 20:50:59 +0100108 nodeStorageDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "storage")
109 if err != nil {
110 return "", err
111 }
112 nodeStorage := filepath.Join(nodeStorageDir, "stor.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100113 image, err := os.Create(nodeStorage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200114 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100115 return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200116 }
117 if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100118 return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200119 }
120 image.Close()
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100121 return nodeStorage, nil
Mateusz Zalega43e21072021-10-08 18:05:29 +0200122}
123
124// qemuDriveParam returns QEMU parameters required to run it with a
125// raw-format image at path.
126func qemuDriveParam(path string) []string {
127 return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path}
128}
129
130// checkEspContents verifies the presence of the EFI payload inside of image's
131// first partition. It returns nil on success.
132func checkEspContents(image *disk.Disk) error {
133 // Get the ESP.
134 fs, err := image.GetFilesystem(1)
135 if err != nil {
136 return fmt.Errorf("couldn't read the installer ESP: %w", err)
137 }
138 // Make sure the EFI payload exists by attempting to open it.
139 efiPayload, err := fs.OpenFile(osimage.EFIPayloadPath, os.O_RDONLY)
140 if err != nil {
141 return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", osimage.EFIPayloadPath, err)
142 }
143 efiPayload.Close()
144 return nil
145}
146
147func TestMain(m *testing.M) {
Serge Bazanski97783222021-12-14 16:04:26 +0100148 installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100149
Tim Windelschmidt82e6af72024-07-23 00:05:42 +0000150 installer, err := os.ReadFile(xInstallerPath)
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100151 if err != nil {
152 log.Fatal(err)
153 }
154
Tim Windelschmidt82e6af72024-07-23 00:05:42 +0000155 bundle, err := os.ReadFile(xBundlePath)
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +0100156 if err != nil {
157 log.Fatal(err)
158 }
159
Mateusz Zalega43e21072021-10-08 18:05:29 +0200160 iargs := mctl.MakeInstallerImageArgs{
Lorenz Brunad131882023-06-28 16:42:20 +0200161 Installer: bytes.NewReader(installer),
162 TargetPath: installerImage,
163 NodeParams: &api.NodeParameters{},
164 Bundle: bytes.NewReader(bundle),
Mateusz Zalega43e21072021-10-08 18:05:29 +0200165 }
166 if err := mctl.MakeInstallerImage(iargs); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100167 log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200168 }
169 // With common dependencies set up, run the tests.
170 code := m.Run()
171 // Clean up.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100172 os.Remove(installerImage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200173 os.Exit(code)
174}
175
176func TestInstallerImage(t *testing.T) {
177 // This test examines the installer image, making sure that the GPT and the
178 // ESP contents are in order.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100179 image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200180 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100181 t.Errorf("Couldn't open the installer image at %q: %s", installerImage, err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200182 }
183 // Verify that GPT exists.
184 ti, err := image.GetPartitionTable()
Tim Windelschmidt33306eb2024-04-11 01:39:48 +0200185 if err != nil {
186 t.Fatalf("Couldn't read the installer image partition table: %s", err)
187 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200188 if ti.Type() != "gpt" {
189 t.Error("Couldn't verify that the installer image contains a GPT.")
190 }
191 // Check that the first partition is likely to be a valid ESP.
192 pi := ti.GetPartitions()
193 esp := (pi[0]).(*gpt.Partition)
194 if esp.Start == 0 || esp.End == 0 {
195 t.Error("The installer's ESP GPT entry looks off.")
196 }
197 // Verify that the image contains only one partition.
198 second := (pi[1]).(*gpt.Partition)
199 if second.Name != "" || second.Start != 0 || second.End != 0 {
200 t.Error("It appears the installer image contains more than one partition.")
201 }
202 // Verify the ESP contents.
203 if err := checkEspContents(image); err != nil {
204 t.Error(err.Error())
205 }
206}
207
208func TestNoBlockDevices(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100209 ctx, ctxC := context.WithCancel(context.Background())
210 defer ctxC()
211
Mateusz Zalega43e21072021-10-08 18:05:29 +0200212 // No block devices are passed to QEMU aside from the install medium. Expect
213 // the installer to fail at the device probe stage rather than attempting to
214 // use the medium as the target device.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200215 expectedOutput := "couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100216 result, err := runQemuWithInstaller(ctx, nil, expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200217 if err != nil {
218 t.Error(err.Error())
219 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200220 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200221 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
222 }
223}
224
225func TestBlockDeviceTooSmall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100226 ctx, ctxC := context.WithCancel(context.Background())
227 defer ctxC()
228
Mateusz Zalega43e21072021-10-08 18:05:29 +0200229 // Prepare the block device the installer will install to. This time the
230 // target device is too small to host a Metropolis installation.
231 imagePath, err := getStorage(64)
232 defer os.Remove(imagePath)
233 if err != nil {
Tim Windelschmidt677de972024-09-25 05:30:04 +0200234 t.Error(err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200235 }
236
237 // Run QEMU. Expect the installer to fail with a predefined error string.
Tim Windelschmidt96e014e2024-09-10 02:26:13 +0200238 expectedOutput := "couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100239 result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200240 if err != nil {
241 t.Error(err.Error())
242 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200243 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200244 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
245 }
246}
247
248func TestInstall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100249 ctx, ctxC := context.WithCancel(context.Background())
250 defer ctxC()
251
Mateusz Zalega43e21072021-10-08 18:05:29 +0200252 // Prepare the block device image the installer will install to.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200253 // Needs enough storage for two 4096 MiB system partitions, a 384 MiB ESP
254 // and a 128 MiB data partition. In addition at the start and end we need
255 // 1MiB for GPT headers and alignment.
256 storagePath, err := getStorage(4096*2 + 384 + 128 + 2)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200257 defer os.Remove(storagePath)
258 if err != nil {
Tim Windelschmidt677de972024-09-25 05:30:04 +0200259 t.Error(err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200260 }
261
262 // Run QEMU. Expect the installer to succeed.
263 expectedOutput := "Installation completed"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100264 result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200265 if err != nil {
266 t.Error(err.Error())
267 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200268 if !result {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200269 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
270 }
271
272 // Verify the resulting node image. Check whether the node GPT was created.
273 storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly)
274 if err != nil {
275 t.Errorf("Couldn't open the resulting node image at %q: %s", storagePath, err.Error())
276 }
277 // Verify that GPT exists.
278 ti, err := storage.GetPartitionTable()
Tim Windelschmidt096654a2024-04-18 23:10:19 +0200279 if err != nil {
280 t.Fatalf("Couldn't read the installer image partition table: %s", err)
281 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200282 if ti.Type() != "gpt" {
283 t.Error("Couldn't verify that the resulting node image contains a GPT.")
284 }
285 // Check that the first partition is likely to be a valid ESP.
286 pi := ti.GetPartitions()
287 esp := (pi[0]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200288 if esp.Name != osimage.ESPLabel || esp.Start == 0 || esp.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200289 t.Error("The node's ESP GPT entry looks off.")
290 }
291 // Verify the system partition's GPT entry.
292 system := (pi[1]).(*gpt.Partition)
Lorenz Brun35fcf032023-06-29 04:15:58 +0200293 if system.Name != osimage.SystemALabel || system.Start == 0 || system.End == 0 {
294 t.Error("The node's system partition GPT entry looks off.")
295 }
296 // Verify the system partition's GPT entry.
297 systemB := (pi[2]).(*gpt.Partition)
298 if systemB.Name != osimage.SystemBLabel || systemB.Start == 0 || systemB.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200299 t.Error("The node's system partition GPT entry looks off.")
300 }
301 // Verify the data partition's GPT entry.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200302 data := (pi[3]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200303 if data.Name != osimage.DataLabel || data.Start == 0 || data.End == 0 {
304 t.Errorf("The node's data partition GPT entry looks off: %+v", data)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200305 }
306 // Verify that there are no more partitions.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200307 fourth := (pi[4]).(*gpt.Partition)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200308 if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 {
309 t.Error("The resulting node image contains more partitions than expected.")
310 }
311 // Verify the ESP contents.
312 if err := checkEspContents(storage); err != nil {
313 t.Error(err.Error())
314 }
Serge Bazanskif9bdf312023-03-16 21:54:49 +0100315 storage.File.Close()
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100316 // Run QEMU again. Expect TestOS to launch successfully.
317 expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
Serge Bazanski05cf33d2023-03-16 20:50:59 +0100318 time.Sleep(time.Second)
Serge Bazanskie2e03712021-12-17 12:47:03 +0100319 result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100320 if err != nil {
321 t.Error(err.Error())
322 }
Tim Windelschmidt885668a2024-04-19 00:01:06 +0200323 if !result {
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100324 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
325 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200326}