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 committed Jan 6, 2024
1 parent d5553b1 commit f23f382
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 16 deletions.
23 changes: 22 additions & 1 deletion checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ 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";
};
# This hypervisor requires a crossSystem nixpkgs for the
# guest system
nixpkgs.crossSystem.config = guestSystem;
}
];
} {
id = "cloud-hypervisor";
modules = [ {
Expand Down Expand Up @@ -46,7 +67,7 @@ let
} ];
testing.enableTest = builtins.elem config.microvm.hypervisor [
# Hypervisors that support 9p
"qemu" "crosvm" "kvmtool"
"qemu" "qemu-tcg" "crosvm" "kvmtool"
];
};
}) ];
Expand Down
20 changes: 15 additions & 5 deletions checks/startup-shutdown.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ let
let
exit = {
qemu = "reboot";
qemu-tcg = "reboot";
firecracker = "reboot";
cloud-hypervisor = "poweroff";
crosvm = "reboot";
kvmtool = "reboot";
}.${config.microvm.hypervisor};
in ''
${pkgs.coreutils}/bin/uname > /var/OK
${pkgs.coreutils}/bin/uname > /var/kernel-name
${pkgs.coreutils}/bin/uname -m > /var/machine-name
${exit}
'';
};
Expand All @@ -55,11 +58,18 @@ builtins.mapAttrs (_: nixos:
} ''
microvm-run
7z e var.img OK
if [ "$(cat OK)" != "Linux" ] ; then
echo Output does not match
7z e var.img kernel-name machine-name
if [ "$(cat machine-name)-linux" != "${nixos.config.nixpkgs.system}" ] ; then
echo "Machine does not match (got: $(cat machine-name)-linux; expected: ${nixos.config.nixpkgs.system})"
exit 1
fi
if [ "$(cat kernel-name)" != "Linux" ] ; then
echo "Kernel does not match (got: $(cat kernel-name); expected: Linux"
exit 1
fi
cp OK $out
touch $out
''
) configs
3 changes: 2 additions & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
rec {
hypervisors = [
"qemu"
"qemu-tcg"
"cloud-hypervisor"
"firecracker"
"crosvm"
Expand Down Expand Up @@ -35,7 +36,7 @@ rec {
, autoCreate ? true
, ...
}: nixpkgs-lib.optionalString autoCreate ''
PATH=$PATH:${with pkgs; lib.makeBinPath [ coreutils util-linux e2fsprogs ]}
PATH=$PATH:${with pkgs.buildPackages; lib.makeBinPath [ coreutils util-linux e2fsprogs ]}
if [ ! -e '${image}' ]; then
touch '${image}'
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
4 changes: 2 additions & 2 deletions lib/runners/qemu.nix
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ in {
${writeQmp { execute = "qmp_capabilities"; }}
${writeQmp { execute = "balloon"; arguments.value = 987; }}
) | sed -e s/987/$VALUE/ | \
${pkgs.socat}/bin/socat STDIO UNIX:${socket},shut-none | \
${pkgs.buildPackages.socat}/bin/socat STDIO UNIX:${socket},shut-none | \
tail -n 1 | \
${pkgs.jq}/bin/jq -r .data.actual \
${pkgs.buildPackages.jq}/bin/jq -r .data.actual \
)
echo $(( $SIZE / 1024 / 1024 ))
''
Expand Down
2 changes: 1 addition & 1 deletion nixos-modules/microvm/asserts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ lib.mkIf config.microvm.guest.enable {
[ {
assertion =
config.microvm.forwardPorts != [] -> (
config.microvm.hypervisor == "qemu" &&
(builtins.elem config.microvm.hypervisor ["qemu" "qemu-tcg"]) &&
builtins.any ({ type, ... }: type == "user") config.microvm.interfaces
);
message = ''
Expand Down
1 change: 1 addition & 0 deletions nixos-modules/microvm/optimization.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ in
boot.initrd.systemd.enable = lib.mkDefault (
builtins.elem cfg.hypervisor [
"qemu"
"qemu-tcg"
"cloud-hypervisor"
"firecracker"
"stratovirt"
Expand Down

0 comments on commit f23f382

Please sign in to comment.