Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QEMU balloon script abstraction improvement #248

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/runner.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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}
'';
Expand Down
1 change: 1 addition & 0 deletions lib/runners/cloud-hypervisor.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions lib/runners/crosvm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
)
Expand Down
14 changes: 8 additions & 6 deletions lib/runners/qemu.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -339,16 +339,18 @@ 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; }}
) | sed -e s/987/$VALUE/ | \
(${writeQmp { execute = "balloon"; arguments.value = 987; }}) | sed -e s/987/$VALUE/
${pkgs.coreutils}/bin/sleep ${toString balloonDelay}
${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 $(( $SIZE / 1024 / 1024 ))
echo $(( ${toString (mem + balloonMem)} - $SIZE / 1024 / 1024 ))
''
else null;

Expand Down
12 changes: 12 additions & 0 deletions nixos-modules/microvm/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down