Skip to content

Commit

Permalink
Add simple VM test to assert booting from disk works
Browse files Browse the repository at this point in the history
More to come :D
  • Loading branch information
jmbaur committed Jul 18, 2024
1 parent d8b1030 commit 3631960
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 80 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn build(b: *std.Build) !void {
.strip = optimize != std.builtin.OptimizeMode.Debug,
});
tboot_loader.root_module.addAnonymousImport("test_key", .{
.root_source_file = b.path("test/keys/tboot/key.der"),
.root_source_file = b.path("tests/keys/tboot/key.der"),
});
tboot_loader.root_module.addImport("linux_headers", linux_headers_module);

Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 4 additions & 27 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -73,38 +73,15 @@
qemu
zon2nix
];
# https://github.com/NixOS/nixpkgs/issues/270415
shellHook = ''
unset ZIG_GLOBAL_CACHE_DIR
'';
env.TINYBOOT_KERNEL = ''${pkgs."tinyboot-qemu-${pkgs.stdenv.hostPlatform.qemuArch}".linux}/kernel'';
};
}) inputs.self.legacyPackages;
apps = inputs.nixpkgs.lib.mapAttrs (
system: pkgs:
(
let
nixosSystem = inputs.nixpkgs.lib.nixosSystem {
modules = [
inputs.self.nixosModules.default
./test/module.nix
({ nixpkgs.hostPlatform = system; })
];
};
in
{
"${system}-disk" = {
type = "app";
program = toString (
pkgs.writeShellScript "make-disk-image" ''
dd status=progress if=${nixosSystem.config.system.build.qcow2}/nixos.qcow2 of=nixos-${system}.qcow2
''
);
};
}
)
// {
default = inputs.self.apps.${system}."${system}-disk";
}
) inputs.self.legacyPackages;
checks = inputs.nixpkgs.lib.mapAttrs (_: pkgs: {
simple = pkgs.callPackage ./tests/simple { };
}) inputs.self.legacyPackages;
};
}
12 changes: 6 additions & 6 deletions options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,27 @@ in
};
tbootPublicCertificate = mkOption {
type = types.path;
default = ./test/keys/tboot/key.der;
default = ./tests/keys/tboot/key.der;
};
tbootPrivateKey = mkOption {
type = types.path;
default = ./test/keys/tboot/key.pem;
default = ./tests/keys/tboot/key.pem;
};
vbootRootKey = mkOption {
type = types.path;
default = ./test/keys/root/key.vbpubk;
default = ./tests/keys/root/key.vbpubk;
};
vbootFirmwarePrivkey = mkOption {
type = types.path;
default = ./test/keys/firmware/key.vbprivk;
default = ./tests/keys/firmware/key.vbprivk;
};
vbootFirmwareKey = mkOption {
type = types.path;
default = ./test/keys/firmware/key.vbpubk;
default = ./tests/keys/firmware/key.vbpubk;
};
vbootKeyblock = mkOption {
type = types.path;
default = ./test/keys/firmware/key.keyblock;
default = ./tests/keys/firmware/key.keyblock;
};
};
extraInitrdContents = mkOption {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/tinyboot/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ stdenv.mkDerivation (
../../build.zig
../../build.zig.zon
../../src
../../test/keys/tboot/key.der
../../tests/keys/tboot/key.der
];
};

Expand Down
2 changes: 1 addition & 1 deletion src/runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn main() !void {
const kernel = args.next().?;

// get path to tboot key before changing directory
const tboot_key = try std.fs.cwd().realpathAlloc(allocator, "test/keys/tboot/key.der");
const tboot_key = try std.fs.cwd().realpathAlloc(allocator, "tests/keys/tboot/key.der");

try std.posix.chdir(tmpdir);

Expand Down
41 changes: 0 additions & 41 deletions test/module.nix

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions tests/simple/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{ testers, writeText }:

testers.runNixOSTest {
name = "simple";
nodes.machine =
{
config,
lib,
pkgs,
...
}:
{
virtualisation = {
# The bios option wants a package with a bios.bin file in it.
bios = pkgs.runCommand "tinyboot-bios.bin" { } ''
install -D ${pkgs."tinyboot-qemu-${pkgs.stdenv.hostPlatform.qemuArch}"} $out/bios.bin
'';
qemu.options = lib.optionals pkgs.stdenv.hostPlatform.isx86_64 [ "-machine q35" ];
qemu.drives = [
{
file = "fat:rw:$ESP";
driveExtraOpts = {
"if" = "virtio";
format = "raw";
};
}
];
};
};
testScript =
{ nodes, ... }:
let
bootEntry = writeText "boot-entry" ''
title simple
linux /linux
initrd /initrd
options init=${nodes.machine.system.build.toplevel}/init ${toString nodes.machine.boot.kernelParams}
'';
in
''
import os
import shutil
import tempfile
esp = tempfile.TemporaryDirectory()
os.environ["ESP"] = esp.name
os.makedirs(os.path.join(esp.name, "loader/entries"))
shutil.copyfile("${nodes.machine.system.build.kernel}/${nodes.machine.system.boot.loader.kernelFile}", os.path.join(esp.name, "linux"))
shutil.copyfile("${nodes.machine.system.build.initialRamdisk}/${nodes.machine.system.boot.loader.initrdFile}", os.path.join(esp.name, "initrd"))
shutil.copyfile("${bootEntry}", os.path.join(esp.name, "loader/entries/simple.conf"))
machine.wait_for_unit("multi-user.target")
'';
}

0 comments on commit 3631960

Please sign in to comment.