From fc1f88671eeaebde73187c0a2f3e7e7f1cf9070e Mon Sep 17 00:00:00 2001 From: Santtu Lakkala Date: Thu, 6 Jun 2024 12:53:15 +0300 Subject: [PATCH 1/6] qemu: Improve ballooning abstraction The balloon command of qemu expects the total guest size. Alter the qemu balloon script to calculate the correct value from passed balloon size to make functionality closer to that with other hypervisors. Signed-off-by: Santtu Lakkala --- lib/runners/qemu.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/runners/qemu.nix b/lib/runners/qemu.nix index a2786f5d..b76c3653 100644 --- a/lib/runners/qemu.nix +++ b/lib/runners/qemu.nix @@ -339,7 +339,7 @@ in { setBalloonScript = if socket != null then '' - VALUE=$(( $SIZE * 1024 * 1024 )) + VALUE=$(( (${toString (mem + balloonMem)} - $SIZE) * 1024 * 1024 )) SIZE=$( ( ${writeQmp { execute = "qmp_capabilities"; }} ${writeQmp { execute = "balloon"; arguments.value = 987; }} @@ -348,7 +348,7 @@ in { tail -n 1 | \ ${pkgs.jq}/bin/jq -r .data.actual \ ) - echo $(( $SIZE / 1024 / 1024 )) + echo $(( ${toString (mem + balloonMem)} - $SIZE / 1024 / 1024 )) '' else null; From 7acca6fb66a212d727fb38442051c642bcd677fd Mon Sep 17 00:00:00 2001 From: Santtu Lakkala Date: Thu, 6 Jun 2024 12:54:15 +0300 Subject: [PATCH 2/6] qemu: Improve balloon script output Use query-balloon command instead of relying on the changed signal for slightly improved reliability. The 2 second delay between commands is arbitrarily chosen. Signed-off-by: Santtu Lakkala --- lib/runners/qemu.nix | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/runners/qemu.nix b/lib/runners/qemu.nix index b76c3653..eea159fb 100644 --- a/lib/runners/qemu.nix +++ b/lib/runners/qemu.nix @@ -342,11 +342,13 @@ in { VALUE=$(( (${toString (mem + balloonMem)} - $SIZE) * 1024 * 1024 )) SIZE=$( ( ${writeQmp { execute = "qmp_capabilities"; }} - ${writeQmp { execute = "balloon"; arguments.value = 987; }} - ) | sed -e s/987/$VALUE/ | \ + (${writeQmp { execute = "balloon"; arguments.value = 987; }}) | sed -e s/987/$VALUE/ + ${pkgs.coreutils}/bin/sleep 2 + ${writeQmp { execute = "query-balloon"; }} + ) | \ ${pkgs.socat}/bin/socat STDIO UNIX:${socket},shut-none | \ tail -n 1 | \ - ${pkgs.jq}/bin/jq -r .data.actual \ + ${pkgs.jq}/bin/jq -r .return.actual \ ) echo $(( ${toString (mem + balloonMem)} - $SIZE / 1024 / 1024 )) '' From 58aeca4d59218c6def19b8094b489ee8daee997b Mon Sep 17 00:00:00 2001 From: Santtu Lakkala Date: Tue, 11 Jun 2024 15:08:21 +0300 Subject: [PATCH 3/6] lib/runner: Add balloon argument description --- lib/runner.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/runner.nix b/lib/runner.nix index 66512ee6..f48766fe 100644 --- a/lib/runner.nix +++ b/lib/runner.nix @@ -47,6 +47,8 @@ let exit 1 fi + # The amount of total memory the guest is requested to release to host, in megabytes. + # Expected value between 0 and ${toString microvmConfig.balloonMem}. SIZE=$1 ${hypervisorConfig.setBalloonScript} ''; From 60b27ee1a4b95f720500b7b3c3bc5cf22a7117f7 Mon Sep 17 00:00:00 2001 From: Santtu Lakkala Date: Tue, 11 Jun 2024 15:09:04 +0300 Subject: [PATCH 4/6] cloud-hypervisor: Query balloon size after setting --- lib/runners/cloud-hypervisor.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/runners/cloud-hypervisor.nix b/lib/runners/cloud-hypervisor.nix index 56f4d54a..aec56e6d 100644 --- a/lib/runners/cloud-hypervisor.nix +++ b/lib/runners/cloud-hypervisor.nix @@ -216,6 +216,7 @@ in { if socket != null then '' ${pkgs.cloud-hypervisor}/bin/ch-remote --api-socket ${socket} resize --balloon $SIZE"M" + ${pkgs.cloud-hypervisor}/bin/ch-remote --api-socket ${socket} info | ${pkgs.jq}/bin/jq '.config.balloon.size / 1024 / 1024 | round' '' else null; From c4f9be771ffa45c9f7a98561a31c44eff7aaeffc Mon Sep 17 00:00:00 2001 From: Santtu Lakkala Date: Tue, 11 Jun 2024 15:10:56 +0300 Subject: [PATCH 5/6] qemu: Make balloon query delay configurable --- lib/runners/qemu.nix | 4 ++-- nixos-modules/microvm/options.nix | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/runners/qemu.nix b/lib/runners/qemu.nix index eea159fb..b91cbf42 100644 --- a/lib/runners/qemu.nix +++ b/lib/runners/qemu.nix @@ -38,7 +38,7 @@ let qemu = overrideQemu (if microvmConfig.cpu == null then pkgs.qemu_kvm else pkgs.buildPackages.qemu_full); - inherit (microvmConfig) hostName cpu vcpu mem balloonMem user interfaces shares socket forwardPorts devices vsock graphics storeOnDisk kernel initrdPath storeDisk; + inherit (microvmConfig) hostName cpu vcpu mem balloonMem balloonDelay user interfaces shares socket forwardPorts devices vsock graphics storeOnDisk kernel initrdPath storeDisk; inherit (microvmConfig.qemu) machine extraArgs serialConsole; inherit (import ../. { nixpkgs-lib = pkgs.lib; }) withDriveLetters; @@ -343,7 +343,7 @@ in { SIZE=$( ( ${writeQmp { execute = "qmp_capabilities"; }} (${writeQmp { execute = "balloon"; arguments.value = 987; }}) | sed -e s/987/$VALUE/ - ${pkgs.coreutils}/bin/sleep 2 + ${pkgs.coreutils}/bin/sleep ${toString balloonDelay} ${writeQmp { execute = "query-balloon"; }} ) | \ ${pkgs.socat}/bin/socat STDIO UNIX:${socket},shut-none | \ diff --git a/nixos-modules/microvm/options.nix b/nixos-modules/microvm/options.nix index c0d12f61..7671d79c 100644 --- a/nixos-modules/microvm/options.nix +++ b/nixos-modules/microvm/options.nix @@ -103,6 +103,18 @@ in type = types.int; }; + balloonDelay = mkOption { + description = '' + Amount of time to wait for balloon to settle in seconds + + Under some VMMs it takes some time for the reported balloon + size to be final. Adding some delay between setting and querying + allows microvm-balloon to report correct size. + ''; + default = 2; + type = types.int; + }; + forwardPorts = mkOption { type = types.listOf (types.submodule { From e9f2677fc03d1dce834271f540799fe4541b2199 Mon Sep 17 00:00:00 2001 From: Santtu Lakkala Date: Tue, 11 Jun 2024 15:11:25 +0300 Subject: [PATCH 6/6] crosvm: Add delay to balloon script --- lib/runners/crosvm.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/runners/crosvm.nix b/lib/runners/crosvm.nix index 881b09bf..4d61a8f6 100644 --- a/lib/runners/crosvm.nix +++ b/lib/runners/crosvm.nix @@ -157,6 +157,7 @@ in { then '' VALUE=$(( $SIZE * 1024 * 1024 )) ${pkgs.crosvm}/bin/crosvm balloon $VALUE ${socket} + ${pkgs.coreutils}/bin/sleep ${toString microvmConfig.balloonDelay} SIZE=$( ${pkgs.crosvm}/bin/crosvm balloon_stats ${socket} | \ ${pkgs.jq}/bin/jq -r .BalloonStats.balloon_actual \ )