m/{t,n}/installer: move to m/installer

Change-Id: I8fd15f1fa1e151369df251d1469e84cfeffd26fd
Reviewed-on: https://review.monogon.dev/c/monogon/+/510
Reviewed-by: Sergiusz Bazanski <serge@monogon.tech>
diff --git a/metropolis/installer/BUILD.bazel b/metropolis/installer/BUILD.bazel
new file mode 100644
index 0000000..442213b
--- /dev/null
+++ b/metropolis/installer/BUILD.bazel
@@ -0,0 +1,46 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
+load("//metropolis/node/build:def.bzl", "node_initramfs")
+load("//metropolis/node/build/genosrelease:defs.bzl", "os_release")
+load("//metropolis/node/build:efi.bzl", "efi_unified_kernel_image")
+
+go_library(
+    name = "go_default_library",
+    srcs = ["main.go"],
+    importpath = "source.monogon.dev/metropolis/installer",
+    visibility = ["//visibility:private"],
+    deps = [
+        "//metropolis/node/build/mkimage/osimage:go_default_library",
+        "//metropolis/pkg/efivarfs:go_default_library",
+        "//metropolis/pkg/sysfs:go_default_library",
+        "@org_golang_x_sys//unix:go_default_library",
+    ],
+)
+
+go_binary(
+    name = "installer",
+    embed = [":go_default_library"],
+    visibility = ["//visibility:private"],
+)
+
+node_initramfs(
+    name = "initramfs",
+    files = {
+        "//metropolis/installer": "/init",
+    },
+    visibility = ["//metropolis/installer/test:__pkg__"],
+)
+
+os_release(
+    name = "installer-release-info",
+    os_id = "metropolis-installer",
+    os_name = "Metropolis Installer",
+    stamp_var = "STABLE_METROPOLIS_version",
+)
+
+efi_unified_kernel_image(
+    name = "kernel",
+    initramfs = ":initramfs",
+    kernel = "//third_party/linux",
+    os_release = ":installer-release-info",
+    visibility = ["//visibility:public"],
+)
diff --git a/metropolis/installer/main.go b/metropolis/installer/main.go
new file mode 100644
index 0000000..e3a4896
--- /dev/null
+++ b/metropolis/installer/main.go
@@ -0,0 +1,320 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Installer creates a Metropolis image at a suitable block device based on the
+// installer bundle present in the installation medium's ESP, after which it
+// reboots. It's meant to be used as an init process.
+package main
+
+import (
+	"archive/zip"
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+	"strings"
+	"syscall"
+
+	"golang.org/x/sys/unix"
+
+	"source.monogon.dev/metropolis/node/build/mkimage/osimage"
+	"source.monogon.dev/metropolis/pkg/efivarfs"
+	"source.monogon.dev/metropolis/pkg/sysfs"
+)
+
+const mib = 1024 * 1024
+
+// mountPseudoFS mounts efivarfs, devtmpfs and sysfs, used by the installer in
+// the block device discovery stage.
+func mountPseudoFS() error {
+	for _, m := range []struct {
+		dir   string
+		fs    string
+		flags uintptr
+	}{
+		{"/sys", "sysfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
+		{efivarfs.Path, "efivarfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
+		{"/dev", "devtmpfs", unix.MS_NOEXEC | unix.MS_NOSUID},
+	} {
+		if err := unix.Mkdir(m.dir, 0700); err != nil && !os.IsExist(err) {
+			return fmt.Errorf("couldn't create the mountpoint at %q: %w", m.dir, err)
+		}
+		if err := unix.Mount(m.fs, m.dir, m.fs, m.flags, ""); err != nil {
+			return fmt.Errorf("couldn't mount %q at %q: %w", m.fs, m.dir, err)
+		}
+	}
+	return nil
+}
+
+// mountInstallerESP mounts the filesystem the installer was loaded from based
+// on espPath, which must point to the appropriate partition block device. The
+// filesystem is mounted at /installer.
+func mountInstallerESP(espPath string) error {
+	// Create the mountpoint.
+	if err := unix.Mkdir("/installer", 0700); err != nil {
+		return fmt.Errorf("couldn't create the installer mountpoint: %w", err)
+	}
+	// Mount the filesystem.
+	if err := unix.Mount(espPath, "/installer", "vfat", unix.MS_NOEXEC|unix.MS_RDONLY, ""); err != nil {
+		return fmt.Errorf("couldn't mount the installer ESP (%q -> %q): %w", espPath, "/installer", err)
+	}
+	return nil
+}
+
+// findInstallableBlockDevices returns names of all the block devices suitable
+// for hosting a Metropolis installation, limited by the size expressed in
+// bytes minSize. The install medium espDev will be excluded from the result.
+func findInstallableBlockDevices(espDev string, minSize uint64) ([]string, error) {
+	// Use the partition's name to find and return the name of its parent
+	// device. It will be excluded from the list of suitable target devices.
+	srcDev, err := sysfs.ParentBlockDevice(espDev)
+	// Build the exclusion list containing forbidden handle prefixes.
+	exclude := []string{"dm-", "zram", "ram", "loop", srcDev}
+
+	// Get the block device handles by looking up directory contents.
+	const blkDirPath = "/sys/class/block"
+	blkDevs, err := os.ReadDir(blkDirPath)
+	if err != nil {
+		return nil, fmt.Errorf("couldn't read %q: %w", blkDirPath, err)
+	}
+	// Iterate over the handles, skipping any block device that either points to
+	// a partition, matches the exclusion list, or is smaller than minSize.
+	var suitable []string
+probeLoop:
+	for _, devInfo := range blkDevs {
+		// Skip devices according to the exclusion list.
+		for _, prefix := range exclude {
+			if strings.HasPrefix(devInfo.Name(), prefix) {
+				continue probeLoop
+			}
+		}
+
+		// Skip partition symlinks.
+		if _, err := os.Stat(filepath.Join(blkDirPath, devInfo.Name(), "partition")); err == nil {
+			continue
+		} else if !os.IsNotExist(err) {
+			return nil, fmt.Errorf("while probing sysfs: %w", err)
+		}
+
+		// Skip devices of insufficient size.
+		devPath := filepath.Join("/dev", devInfo.Name())
+		dev, err := os.Open(devPath)
+		if err != nil {
+			return nil, fmt.Errorf("couldn't open a block device at %q: %w", devPath, err)
+		}
+		size, err := unix.IoctlGetInt(int(dev.Fd()), unix.BLKGETSIZE64)
+		dev.Close()
+		if err != nil {
+			return nil, fmt.Errorf("couldn't probe the size of %q: %w", devPath, err)
+		}
+		if uint64(size) < minSize {
+			continue
+		}
+
+		suitable = append(suitable, devInfo.Name())
+	}
+	return suitable, nil
+}
+
+// rereadPartitionTable causes the kernel to read the partition table present
+// in the block device at blkdevPath. It may return an error.
+func rereadPartitionTable(blkdevPath string) error {
+	dev, err := os.Open(blkdevPath)
+	if err != nil {
+		return fmt.Errorf("couldn't open the block device at %q: %w", blkdevPath, err)
+	}
+	defer dev.Close()
+	ret, err := unix.IoctlRetInt(int(dev.Fd()), unix.BLKRRPART)
+	if err != nil {
+		return fmt.Errorf("while doing an ioctl: %w", err)
+	}
+	if syscall.Errno(ret) == unix.EINVAL {
+		return fmt.Errorf("got an EINVAL from BLKRRPART ioctl")
+	}
+	return nil
+}
+
+// initializeSystemPartition writes image contents to the node's system
+// partition using the block device abstraction layer as opposed to slower
+// go-diskfs. tgtBlkdev must contain a path pointing to the block device
+// associated with the system partition. It may return an error.
+func initializeSystemPartition(image io.Reader, tgtBlkdev string) error {
+	// Check that tgtBlkdev points at an actual block device.
+	info, err := os.Stat(tgtBlkdev)
+	if err != nil {
+		return fmt.Errorf("couldn't stat the system partition at %q: %w", tgtBlkdev, err)
+	}
+	if info.Mode()&os.ModeDevice == 0 {
+		return fmt.Errorf("system partition path %q doesn't point to a block device", tgtBlkdev)
+	}
+
+	// Get the system partition's file descriptor.
+	sys, err := os.OpenFile(tgtBlkdev, os.O_WRONLY, 0600)
+	if err != nil {
+		return fmt.Errorf("couldn't open the system partition at %q: %w", tgtBlkdev, err)
+	}
+	defer sys.Close()
+	// Copy the system partition contents. Use a bigger buffer to optimize disk
+	// writes.
+	buf := make([]byte, mib)
+	if _, err := io.CopyBuffer(sys, image, buf); err != nil {
+		return fmt.Errorf("couldn't copy partition contents: %w", err)
+	}
+	return nil
+}
+
+// panicf is a replacement for log.panicf that doesn't print the error message
+// before calling panic.
+func panicf(format string, v ...interface{}) {
+	s := fmt.Sprintf(format, v...)
+	panic(s)
+}
+
+func main() {
+	// Reboot on panic after a delay. The error string will have been printed
+	// before recover is called.
+	defer func() {
+		if r := recover(); r != nil {
+			fmt.Println(r)
+			fmt.Println("The installation could not be finalized. Please reboot to continue.")
+			syscall.Pause()
+		}
+	}()
+
+	// Mount sysfs, devtmpfs and efivarfs.
+	if err := mountPseudoFS(); err != nil {
+		panicf("While mounting pseudo-filesystems: %v", err)
+	}
+	// Read the installer ESP UUID from efivarfs.
+	espUuid, err := efivarfs.ReadLoaderDevicePartUUID()
+	if err != nil {
+		panicf("While reading the installer ESP UUID: %v", err)
+	}
+	// Look up the installer partition based on espUuid.
+	espDev, err := sysfs.DeviceByPartUUID(espUuid)
+	espPath := filepath.Join("/dev", espDev)
+	if err != nil {
+		panicf("While resolving the installer device handle: %v", err)
+	}
+	// Mount the installer partition. The installer bundle will be read from it.
+	if err := mountInstallerESP(espPath); err != nil {
+		panicf("While mounting the installer ESP: %v", err)
+	}
+
+	nodeParameters, err := os.Open("/installer/metropolis-installer/nodeparams.pb")
+	if err != nil {
+		panicf("Failed to open node parameters from ESP: %v", err)
+	}
+
+	// TODO(lorenz): Replace with proper bundles
+	bundle, err := zip.OpenReader("/installer/metropolis-installer/bundle.bin")
+	if err != nil {
+		panicf("Failed to open node bundle from ESP: %v", err)
+	}
+	defer bundle.Close()
+	efiPayload, err := bundle.Open("kernel_efi.efi")
+	if err != nil {
+		panicf("Cannot open EFI payload in bundle: %v", err)
+	}
+	defer efiPayload.Close()
+	systemImage, err := bundle.Open("rootfs.img")
+	if err != nil {
+		panicf("Cannot open system image in bundle: %v", err)
+	}
+	defer systemImage.Close()
+
+	// Build the osimage parameters.
+	installParams := osimage.Params{
+		PartitionSize: osimage.PartitionSizeInfo{
+			// ESP is the size of the node ESP partition, expressed in mebibytes.
+			ESP: 128,
+			// System is the size of the node system partition, expressed in
+			// mebibytes.
+			System: 4096,
+			// Data must be nonzero in order for the data partition to be created.
+			// osimage will extend the data partition to fill all the available space
+			// whenever it's writing to block devices, such as now.
+			Data: 128,
+		},
+		// Due to a bug in go-diskfs causing slow writes, SystemImage is explicitly
+		// marked unused here, as system partition contents will be written using
+		// a workaround below instead.
+		// TODO(mateusz@monogon.tech): Address that bug either by patching go-diskfs
+		// or rewriting osimage.
+		SystemImage: nil,
+
+		EFIPayload:     efiPayload,
+		NodeParameters: nodeParameters,
+	}
+	// Calculate the minimum target size based on the installation parameters.
+	minSize := uint64((installParams.PartitionSize.ESP +
+		installParams.PartitionSize.System +
+		installParams.PartitionSize.Data + 1) * mib)
+
+	// Look for suitable block devices, given the minimum size.
+	blkDevs, err := findInstallableBlockDevices(espDev, minSize)
+	if err != nil {
+		panicf(err.Error())
+	}
+	if len(blkDevs) == 0 {
+		panicf("Couldn't find a suitable block device.")
+	}
+	// Set the first suitable block device found as the installation target.
+	tgtBlkdevName := blkDevs[0]
+	// Update the osimage parameters with a path pointing at the target device.
+	tgtBlkdevPath := filepath.Join("/dev", tgtBlkdevName)
+	installParams.OutputPath = tgtBlkdevPath
+
+	// Use osimage to partition the target block device and set up its ESP.
+	// Create will return an EFI boot entry on success.
+	fmt.Printf("Installing to %s\n", tgtBlkdevPath)
+	be, err := osimage.Create(&installParams)
+	if err != nil {
+		panicf("While installing: %v", err)
+	}
+	// The target device's partition table has just been updated. Re-read it to
+	// make the node system partition reachable through /dev.
+	if err := rereadPartitionTable(tgtBlkdevPath); err != nil {
+		panicf("While re-reading the partition table of %q: %v", tgtBlkdevPath, err)
+	}
+	// Look up the node's system partition path to be later used in the
+	// initialization step. It's always the second partition, right after
+	// the ESP.
+	sysBlkdevName, err := sysfs.PartitionBlockDevice(tgtBlkdevName, 2)
+	if err != nil {
+		panicf("While looking up the system partition: %v", err)
+	}
+	sysBlkdevPath := filepath.Join("/dev", sysBlkdevName)
+	// Copy the system partition contents.
+	if err := initializeSystemPartition(systemImage, sysBlkdevPath); err != nil {
+		panicf("While initializing the system partition at %q: %v", sysBlkdevPath, err)
+	}
+
+	// Create an EFI boot entry for Metropolis.
+	en, err := efivarfs.CreateBootEntry(be)
+	if err != nil {
+		panicf("While creating a boot entry: %v", err)
+	}
+	// Erase the preexisting boot order, leaving Metropolis as the only option.
+	if err := efivarfs.SetBootOrder(&efivarfs.BootOrder{uint16(en)}); err != nil {
+		panicf("While adjusting the boot order: %v", err)
+	}
+
+	// Reboot.
+	unix.Sync()
+	fmt.Println("Installation completed. Rebooting.")
+	unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART)
+}
diff --git a/metropolis/installer/test/BUILD.bazel b/metropolis/installer/test/BUILD.bazel
new file mode 100644
index 0000000..59c5803
--- /dev/null
+++ b/metropolis/installer/test/BUILD.bazel
@@ -0,0 +1,40 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
+load("//metropolis/node/build:efi.bzl", "efi_unified_kernel_image")
+
+go_test(
+    name = "installer",
+    size = "small",
+    data = [
+        ":kernel",
+        "//metropolis/installer/test/testos:testos_bundle",
+        "//third_party/edk2:firmware",
+        "@qemu//:qemu-x86_64-softmmu",
+    ],
+    embed = [":go_default_library"],
+    rundir = ".",
+)
+
+go_library(
+    name = "go_default_library",
+    srcs = ["main.go"],
+    importpath = "source.monogon.dev/metropolis/installer/test",
+    visibility = ["//visibility:private"],
+    deps = [
+        "//metropolis/cli/metroctl/core:go_default_library",
+        "//metropolis/cli/pkg/datafile:go_default_library",
+        "//metropolis/node/build/mkimage/osimage:go_default_library",
+        "//metropolis/pkg/logbuffer:go_default_library",
+        "//metropolis/proto/api:go_default_library",
+        "@com_github_diskfs_go_diskfs//:go_default_library",
+        "@com_github_diskfs_go_diskfs//disk:go_default_library",
+        "@com_github_diskfs_go_diskfs//partition/gpt:go_default_library",
+    ],
+)
+
+efi_unified_kernel_image(
+    name = "kernel",
+    cmdline = "loglevel=0 console=ttyS0",
+    initramfs = "//metropolis/installer:initramfs",
+    kernel = "//third_party/linux",
+    visibility = ["//visibility:private"],
+)
diff --git a/metropolis/installer/test/main.go b/metropolis/installer/test/main.go
new file mode 100644
index 0000000..c4faf81
--- /dev/null
+++ b/metropolis/installer/test/main.go
@@ -0,0 +1,324 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// This package runs the installer image in a VM provided with an empty block
+// device. It then examines the installer console output and the blok device to
+// determine whether the installation process completed without issue.
+package installer
+
+import (
+	"bytes"
+	"context"
+	"fmt"
+	"io"
+	"log"
+	"os"
+	"os/exec"
+	"path/filepath"
+	"strings"
+	"syscall"
+	"testing"
+
+	diskfs "github.com/diskfs/go-diskfs"
+	"github.com/diskfs/go-diskfs/disk"
+	"github.com/diskfs/go-diskfs/partition/gpt"
+
+	mctl "source.monogon.dev/metropolis/cli/metroctl/core"
+	"source.monogon.dev/metropolis/cli/pkg/datafile"
+	"source.monogon.dev/metropolis/node/build/mkimage/osimage"
+	"source.monogon.dev/metropolis/pkg/logbuffer"
+	"source.monogon.dev/metropolis/proto/api"
+)
+
+// Each variable in this block points to either a test dependency or a side
+// effect. These variables are initialized in TestMain using Bazel.
+var (
+	// installerImage is a filesystem path pointing at the installer image that
+	// is generated during the test, and is removed afterwards.
+	installerImage string
+	// nodeStorage is a filesystem path pointing at the VM block device image
+	// Metropolis is installed to during the test. The file is removed afterwards.
+	nodeStorage string
+)
+
+// runQemu starts a QEMU process and waits until it either finishes or the given
+// expectedOutput appears in a line emitted to stdout or stderr. It returns true
+// if it was found, false otherwise.
+//
+// The qemu process will be killed when the context cancels or the function
+// exits.
+func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
+	// Prepare the default parameter list.
+	defaultArgs := []string{
+		"-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
+		"-m", "512",
+		"-smp", "2",
+		"-cpu", "host",
+		"-drive", "if=pflash,format=raw,readonly,file=external/edk2/OVMF_CODE.fd",
+		"-drive", "if=pflash,format=raw,snapshot=on,file=external/edk2/OVMF_VARS.fd",
+		"-serial", "stdio",
+		"-no-reboot",
+	}
+
+	// Make a sub-context to ensure that qemu exits when this function is done.
+	ctxQ, ctxC := context.WithCancel(ctx)
+	defer ctxC()
+
+	// Join the parameter lists and prepare the Qemu command, but don't run it
+	// just yet.
+	qemuArgs := append(defaultArgs, args...)
+	qemuCmd := exec.CommandContext(ctxQ, "external/qemu/qemu-x86_64-softmmu", qemuArgs...)
+
+	// Copy the stdout and stderr output to a single channel of lines so that they
+	// can then be matched against expectedOutput.
+
+	// Since LineBuffer can write its buffered contents on a deferred Close,
+	// after the reader loop is broken, avoid deadlocks by making lineC a
+	// buffered channel.
+	lineC := make(chan string, 2)
+	outBuffer := logbuffer.NewLineBuffer(1024, func(l *logbuffer.Line) {
+		lineC <- l.Data
+	})
+	defer outBuffer.Close()
+	errBuffer := logbuffer.NewLineBuffer(1024, func(l *logbuffer.Line) {
+		lineC <- l.Data
+	})
+	defer errBuffer.Close()
+
+	// Tee std{out,err} into the linebuffers above and the process' std{out,err}, to
+	// allow easier debugging.
+	qemuCmd.Stdout = io.MultiWriter(os.Stdout, outBuffer)
+	qemuCmd.Stderr = io.MultiWriter(os.Stderr, errBuffer)
+	if err := qemuCmd.Start(); err != nil {
+		return false, fmt.Errorf("couldn't start QEMU: %w", err)
+	}
+
+	// Try matching against expectedOutput and return the result.
+	for {
+		select {
+		case <-ctx.Done():
+			return false, ctx.Err()
+		case line := <-lineC:
+			if strings.Contains(line, expectedOutput) {
+				return true, nil
+			}
+		}
+	}
+}
+
+// runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
+// same search-through-std{out,err} as runQemu.
+func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) {
+	args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage)
+	return runQemu(ctx, args, expectedOutput)
+}
+
+// getStorage creates a sparse file, given a size expressed in mebibytes, and
+// returns a path to that file. It may return an error.
+func getStorage(size int64) (string, error) {
+	image, err := os.Create(nodeStorage)
+	if err != nil {
+		return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
+	}
+	if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil {
+		return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err)
+	}
+	image.Close()
+	return nodeStorage, nil
+}
+
+// qemuDriveParam returns QEMU parameters required to run it with a
+// raw-format image at path.
+func qemuDriveParam(path string) []string {
+	return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path}
+}
+
+// checkEspContents verifies the presence of the EFI payload inside of image's
+// first partition. It returns nil on success.
+func checkEspContents(image *disk.Disk) error {
+	// Get the ESP.
+	fs, err := image.GetFilesystem(1)
+	if err != nil {
+		return fmt.Errorf("couldn't read the installer ESP: %w", err)
+	}
+	// Make sure the EFI payload exists by attempting to open it.
+	efiPayload, err := fs.OpenFile(osimage.EFIPayloadPath, os.O_RDONLY)
+	if err != nil {
+		return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", osimage.EFIPayloadPath, err)
+	}
+	efiPayload.Close()
+	return nil
+}
+
+func TestMain(m *testing.M) {
+	installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
+	nodeStorage = filepath.Join(os.Getenv("TEST_TMPDIR"), "stor.img")
+
+	installer := datafile.MustGet("metropolis/installer/test/kernel.efi")
+	bundle := datafile.MustGet("metropolis/installer/test/testos/testos_bundle.zip")
+	iargs := mctl.MakeInstallerImageArgs{
+		Installer:     bytes.NewBuffer(installer),
+		InstallerSize: uint64(len(installer)),
+		TargetPath:    installerImage,
+		NodeParams:    &api.NodeParameters{},
+		Bundle:        bytes.NewBuffer(bundle),
+		BundleSize:    uint64(len(bundle)),
+	}
+	if err := mctl.MakeInstallerImage(iargs); err != nil {
+		log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)
+	}
+	// With common dependencies set up, run the tests.
+	code := m.Run()
+	// Clean up.
+	os.Remove(installerImage)
+	os.Exit(code)
+}
+
+func TestInstallerImage(t *testing.T) {
+	// This test examines the installer image, making sure that the GPT and the
+	// ESP contents are in order.
+	image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly)
+	if err != nil {
+		t.Errorf("Couldn't open the installer image at %q: %s", installerImage, err.Error())
+	}
+	// Verify that GPT exists.
+	ti, err := image.GetPartitionTable()
+	if ti.Type() != "gpt" {
+		t.Error("Couldn't verify that the installer image contains a GPT.")
+	}
+	// Check that the first partition is likely to be a valid ESP.
+	pi := ti.GetPartitions()
+	esp := (pi[0]).(*gpt.Partition)
+	if esp.Start == 0 || esp.End == 0 {
+		t.Error("The installer's ESP GPT entry looks off.")
+	}
+	// Verify that the image contains only one partition.
+	second := (pi[1]).(*gpt.Partition)
+	if second.Name != "" || second.Start != 0 || second.End != 0 {
+		t.Error("It appears the installer image contains more than one partition.")
+	}
+	// Verify the ESP contents.
+	if err := checkEspContents(image); err != nil {
+		t.Error(err.Error())
+	}
+}
+
+func TestNoBlockDevices(t *testing.T) {
+	ctx, ctxC := context.WithCancel(context.Background())
+	defer ctxC()
+
+	// No block devices are passed to QEMU aside from the install medium. Expect
+	// the installer to fail at the device probe stage rather than attempting to
+	// use the medium as the target device.
+	expectedOutput := "Couldn't find a suitable block device"
+	result, err := runQemuWithInstaller(ctx, nil, expectedOutput)
+	if err != nil {
+		t.Error(err.Error())
+	}
+	if result != true {
+		t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
+	}
+}
+
+func TestBlockDeviceTooSmall(t *testing.T) {
+	ctx, ctxC := context.WithCancel(context.Background())
+	defer ctxC()
+
+	// Prepare the block device the installer will install to. This time the
+	// target device is too small to host a Metropolis installation.
+	imagePath, err := getStorage(64)
+	defer os.Remove(imagePath)
+	if err != nil {
+		t.Errorf(err.Error())
+	}
+
+	// Run QEMU. Expect the installer to fail with a predefined error string.
+	expectedOutput := "Couldn't find a suitable block device"
+	result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput)
+	if err != nil {
+		t.Error(err.Error())
+	}
+	if result != true {
+		t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
+	}
+}
+
+func TestInstall(t *testing.T) {
+	ctx, ctxC := context.WithCancel(context.Background())
+	defer ctxC()
+
+	// Prepare the block device image the installer will install to.
+	storagePath, err := getStorage(4096 + 128 + 128 + 1)
+	defer os.Remove(storagePath)
+	if err != nil {
+		t.Errorf(err.Error())
+	}
+
+	// Run QEMU. Expect the installer to succeed.
+	expectedOutput := "Installation completed"
+	result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput)
+	if err != nil {
+		t.Error(err.Error())
+	}
+	if result != true {
+		t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
+	}
+
+	// Verify the resulting node image. Check whether the node GPT was created.
+	storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly)
+	if err != nil {
+		t.Errorf("Couldn't open the resulting node image at %q: %s", storagePath, err.Error())
+	}
+	// Verify that GPT exists.
+	ti, err := storage.GetPartitionTable()
+	if ti.Type() != "gpt" {
+		t.Error("Couldn't verify that the resulting node image contains a GPT.")
+	}
+	// Check that the first partition is likely to be a valid ESP.
+	pi := ti.GetPartitions()
+	esp := (pi[0]).(*gpt.Partition)
+	if esp.Name != osimage.ESPVolumeLabel || esp.Start == 0 || esp.End == 0 {
+		t.Error("The node's ESP GPT entry looks off.")
+	}
+	// Verify the system partition's GPT entry.
+	system := (pi[1]).(*gpt.Partition)
+	if system.Name != osimage.SystemVolumeLabel || system.Start == 0 || system.End == 0 {
+		t.Error("The node's system partition GPT entry looks off.")
+	}
+	// Verify the data partition's GPT entry.
+	data := (pi[2]).(*gpt.Partition)
+	if data.Name != osimage.DataVolumeLabel || data.Start == 0 || data.End == 0 {
+		t.Errorf("The node's data partition GPT entry looks off.")
+	}
+	// Verify that there are no more partitions.
+	fourth := (pi[3]).(*gpt.Partition)
+	if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 {
+		t.Error("The resulting node image contains more partitions than expected.")
+	}
+	// Verify the ESP contents.
+	if err := checkEspContents(storage); err != nil {
+		t.Error(err.Error())
+	}
+	// Run QEMU again. Expect TestOS to launch successfully.
+	expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
+	result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
+	if err != nil {
+		t.Error(err.Error())
+	}
+	if result != true {
+		t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
+	}
+}
diff --git a/metropolis/installer/test/testos/BUILD b/metropolis/installer/test/testos/BUILD
new file mode 100644
index 0000000..b264527
--- /dev/null
+++ b/metropolis/installer/test/testos/BUILD
@@ -0,0 +1,43 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
+load("//metropolis/node/build:def.bzl", "erofs_image")
+load("//metropolis/node/build:efi.bzl", "efi_unified_kernel_image")
+load("@rules_pkg//:pkg.bzl", "pkg_zip")
+
+erofs_image(
+    name = "rootfs",
+    files = {
+        ":testos": "/init",
+    },
+)
+
+efi_unified_kernel_image(
+    name = "kernel_efi",
+    cmdline = "loglevel=0 console=ttyS0 root=PARTLABEL=METROPOLIS-SYSTEM rootfstype=erofs init=/init",
+    kernel = "//third_party/linux",
+)
+
+# An intermediary "bundle" format until we finalize the actual bundle format. This is NOT stable until migrated
+# to the actual bundle format.
+# TODO(lorenz): Replace this
+pkg_zip(
+    name = "testos_bundle",
+    srcs = [
+        ":kernel_efi",
+        ":rootfs",
+    ],
+    visibility = ["//metropolis/installer/test:__subpackages__"],
+)
+
+go_library(
+    name = "go_default_library",
+    srcs = ["main.go"],
+    importpath = "source.monogon.dev/metropolis/installer/test/testos",
+    visibility = ["//visibility:private"],
+    deps = ["@org_golang_x_sys//unix:go_default_library"],
+)
+
+go_binary(
+    name = "testos",
+    embed = [":go_default_library"],
+    visibility = ["//visibility:public"],
+)
diff --git a/metropolis/installer/test/testos/main.go b/metropolis/installer/test/testos/main.go
new file mode 100644
index 0000000..f0a6e80
--- /dev/null
+++ b/metropolis/installer/test/testos/main.go
@@ -0,0 +1,15 @@
+// TestOS is a tiny "operating system" which is packaged the exact same way as
+// an actual Metropolis node but only outputs a single flag before exiting.
+// It's used for decoupling the installer tests from the Metropolis Node code.
+package main
+
+import (
+	"fmt"
+
+	"golang.org/x/sys/unix"
+)
+
+func main() {
+	fmt.Println("TestOS launched successfully! _TESTOS_LAUNCH_SUCCESS_")
+	unix.Reboot(unix.LINUX_REBOOT_CMD_POWER_OFF)
+}