Skip to content

Commit

Permalink
Add tests for qemu-tcg backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ereslibre authored and astro committed Jan 14, 2024
1 parent f554d2f commit 088ba56
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
19 changes: 19 additions & 0 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ let
modules = [ {
microvm.hypervisor = "qemu";
} ];
} {
id = "qemu-tcg";
modules = let
# Emulate a different guest system than the host one
guestSystem = if "${system}" == "x86_64-linux" then "aarch64-unknown-linux-gnu"
else "x86_64-linux";
in [
{
microvm = {
hypervisor = "qemu";
# Force the CPU to be something else than the current
# system, and thus, emulated with qemu's Tiny Code Generator
# (TCG)
cpu = if "${system}" == "x86_64-linux" then "cortex-a53"
else "Westmere";
};
nixpkgs.crossSystem.config = guestSystem;
}
];
} {
id = "cloud-hypervisor";
modules = [ {
Expand Down
42 changes: 32 additions & 10 deletions checks/startup-shutdown.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ let
};
microvm = {
volumes = [ {
label = "var";
mountPoint = "/var";
image = "var.img";
image = "output.img";
label = "output";
mountPoint = "/output";
size = 32;
} ];
crosvm.pivotRoot = "/build/empty";
Expand All @@ -35,7 +35,9 @@ let
kvmtool = "reboot";
}.${config.microvm.hypervisor};
in ''
${pkgs.coreutils}/bin/uname > /var/OK
${pkgs.coreutils}/bin/uname > /output/kernel-name
${pkgs.coreutils}/bin/uname -m > /output/machine-name
${exit}
'';
};
Expand All @@ -53,14 +55,34 @@ builtins.mapAttrs (_: nixos:
];
requiredSystemFeatures = [ "kvm" ];
meta.timeout = 120;
} ''
} (let
expectedMachineName = (crossSystem:
if crossSystem == null then
expectedMachineName { config = system; }
else if crossSystem.config == "aarch64-unknown-linux-gnu" then
"aarch64"
else if crossSystem.config == "x86_64-linux" then
"x86_64"
else throw "unknown machine name (${crossSystem.config})"
);
in ''
microvm-run
7z e var.img OK
if [ "$(cat OK)" != "Linux" ] ; then
echo Output does not match
7z e output.img kernel-name machine-name
EXPECTED_KERNEL_NAME="Linux"
if [ "$(cat kernel-name)" != "$EXPECTED_KERNEL_NAME" ] ; then
echo "Kernel does not match (got: $(cat kernel-name); expected: $EXPECTED_KERNEL_NAME)"
exit 1
fi
EXPECTED_MACHINE_NAME="${expectedMachineName nixos.config.nixpkgs.crossSystem}"
if [ "$(cat machine-name)" != "$EXPECTED_MACHINE_NAME" ] ; then
echo "Machine does not match (got: $(cat machine-name); expected: $EXPECTED_MACHINE_NAME)"
exit 1
fi
cp OK $out
''
mkdir $out
cp {kernel-name,machine-name} $out
'')
) configs
4 changes: 2 additions & 2 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ rec {
# Mark NOCOW
chattr +C '${image}' || true
fallocate -l${toString size}MiB '${image}'
mkfs.${fsType} ${pkgs.lib.optionalString (label != null) "-L '${label}'"} '${image}'
mkfs.${fsType} ${labelArgument} '${image}'
fi
''));
'')));

buildRunner = import ./runner.nix;

Expand Down
12 changes: 6 additions & 6 deletions lib/runner.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ let
preStart = hypervisorConfig.preStart or microvmConfig.preStart;
tapMultiQueue = hypervisorConfig.tapMultiQueue or false;

runScriptBin = pkgs.writeScriptBin "microvm-run" ''
#! ${pkgs.runtimeShell} -e
runScriptBin = pkgs.buildPackages.writeScriptBin "microvm-run" ''
#! ${pkgs.buildPackages.runtimeShell} -e
${preStart}
${createVolumesScript pkgs microvmConfig.volumes}
${createVolumesScript pkgs.buildPackages microvmConfig.volumes}
${lib.optionalString (hypervisorConfig.requiresMacvtapAsFds or false) openMacvtapFds}
exec ${command}
'';

shutdownScriptBin = pkgs.writeScriptBin "microvm-shutdown" ''
shutdownScriptBin = pkgs.buildPackages.writeScriptBin "microvm-shutdown" ''
#! ${pkgs.runtimeShell} -e
${shutdownCommand}
'';

balloonScriptBin = pkgs.writeScriptBin "microvm-balloon" ''
balloonScriptBin = pkgs.buildPackages.writeScriptBin "microvm-balloon" ''
#! ${pkgs.runtimeShell} -e
if [ -z "$1" ]; then
Expand All @@ -48,7 +48,7 @@ let
'';
in

pkgs.runCommand "microvm-${microvmConfig.hypervisor}-${microvmConfig.hostName}"
pkgs.buildPackages.runCommand "microvm-${microvmConfig.hypervisor}-${microvmConfig.hostName}"
{
# for `nix run`
meta.mainProgram = "microvm-run";
Expand Down

0 comments on commit 088ba56

Please sign in to comment.