Skip to content

Commit 9ad2b03

Browse files
SuperSandro2000astro
authored andcommitted
Split balloon and hot plug memory options, remove balloonMem, add balloon, initialBalloonMem, hotplugMem, hotpluggedMem options, no longer add balloonMem to normal memory
- ballooning and hot plug memory are totally different things. - balloonMem didn't make much sense as all except one vmm only support enabling balloning. For cloud-hypervisor there is a new initialBalloonMem option. - For cloud-hypervisor hot plug memory there is a new and separate hotpluggedMem option to control the initial size. - balloon is described as way to remove memory and decrease the configured memory. Adding it to the normal memory doesn't make much sense in that regard.
1 parent 247bcfd commit 9ad2b03

File tree

8 files changed

+108
-30
lines changed

8 files changed

+108
-30
lines changed

lib/runners/alioth.nix

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ let
77
inherit (pkgs) lib;
88
inherit (microvmConfig)
99
user
10-
vcpu mem interfaces volumes shares devices vsock
10+
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces volumes shares devices vsock
1111
kernel initrdPath
1212
storeDisk storeOnDisk;
1313
in {
1414
command =
1515
if user != null
1616
then throw "alioth will not change user"
17+
else if balloon
18+
then throw "balloon not implemented for alioth"
19+
else if initialBalloonMem != 0
20+
then throw "initialBalloonMem not implemented for alioth"
21+
else if hotplugMem != 0
22+
then throw "alioth does not support hotplugMem"
23+
else if hotpluggedMem != 0
24+
then throw "alioth does not support hotpluggedMem"
1725
else builtins.concatStringsSep " " (
1826
[
1927
"${pkgs.alioth}/bin/alioth" "run"

lib/runners/cloud-hypervisor.nix

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
let
77
inherit (pkgs) lib;
8-
inherit (microvmConfig) vcpu mem balloonMem deflateOnOOM user interfaces volumes shares socket devices hugepageMem graphics storeDisk storeOnDisk kernel initrdPath;
8+
inherit (microvmConfig) vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces volumes shares socket devices hugepageMem graphics storeDisk storeOnDisk kernel initrdPath;
99
inherit (microvmConfig.cloud-hypervisor) extraArgs;
1010

1111
kernelPath = {
@@ -20,8 +20,7 @@ let
2020
then "console=ttyAMA0"
2121
else "";
2222

23-
# balloon
24-
useBallooning = balloonMem > 0;
23+
useHotPlugMemory = hotplugMem > 0;
2524

2625
useVirtiofs = builtins.any ({ proto, ... }: proto == "virtiofs") shares;
2726

@@ -41,19 +40,19 @@ let
4140
shared = if useVirtiofs || graphics.enable then "on" else "off";
4241
}
4342
# add ballooning options and override 'size' key
44-
// lib.optionalAttrs useBallooning {
45-
size = "${toString (mem + balloonMem)}M";
43+
// lib.optionalAttrs useHotPlugMemory {
44+
size = "${toString (mem + hotplugMem)}M";
4645
hotplug_method = "virtio-mem";
47-
hotplug_size = "${toString balloonMem}M";
48-
hotplugged_size = "${toString balloonMem}M";
46+
hotplug_size = "${toString hotplugMem}M";
47+
hotplugged_size = "${toString hotpluggedMem}M";
4948
}
5049
# enable hugepages (shared option is ignored by CHV)
5150
// lib.optionalAttrs hugepageMem {
5251
hugepages = "on";
5352
});
5453

5554
balloonOps = opsMapped ({
56-
size = "${toString balloonMem}M";
55+
size = "${toString initialBalloonMem}M";
5756
free_page_reporting = "on";
5857
}
5958
# enable deflating memory balloon on out-of-memory
@@ -158,7 +157,7 @@ in {
158157
"--gpu" "socket=${graphics.socket}"
159158
]
160159
++
161-
lib.optionals useBallooning [ "--balloon" balloonOps ]
160+
lib.optionals balloon [ "--balloon" balloonOps ]
162161
++
163162
arg "--disk" (
164163
lib.optional storeOnDisk (opsMapped ({

lib/runners/crosvm.nix

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let
77
inherit (pkgs) lib;
88
inherit (pkgs.stdenv) system;
99
inherit (microvmConfig)
10-
vcpu mem balloonMem user volumes shares
10+
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem user volumes shares
1111
socket devices vsock graphics
1212
kernel initrdPath storeDisk storeOnDisk;
1313
inherit (microvmConfig.crosvm) pivotRoot extraArgs;
@@ -46,15 +46,23 @@ in {
4646
command =
4747
if user != null
4848
then throw "crosvm will not change user"
49+
else if initialBalloonMem != 0
50+
then throw "crosvm does not support initialBalloonMem"
51+
else if hotplugMem != 0
52+
then throw "crosvm does not support hotplugMem"
53+
else if hotpluggedMem != 0
54+
then throw "crosvm does not support hotpluggedMem"
4955
else lib.escapeShellArgs (
5056
[
5157
"${pkgs.crosvm}/bin/crosvm" "run"
52-
"-m" (toString (mem + balloonMem))
58+
"-m" (toString mem)
5359
"-c" (toString vcpu)
5460
"--serial" "type=stdout,console=true,stdin=true"
5561
"-p" "console=ttyS0 reboot=k panic=1 ${builtins.unsafeDiscardStringContext (toString microvmConfig.kernelParams)}"
5662
]
5763
++
64+
lib.optional (!balloon) "--no-balloon"
65+
++
5866
lib.optionals storeOnDisk [
5967
"-r" storeDisk
6068
]

lib/runners/firecracker.nix

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let
77
inherit (pkgs) lib system;
88
inherit (microvmConfig)
99
hostName user socket preStart
10-
vcpu mem
10+
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem
1111
interfaces volumes shares devices
1212
kernel initrdPath
1313
storeDisk;
@@ -72,6 +72,14 @@ in {
7272
then throw "9p/virtiofs shares not implemented for Firecracker"
7373
else if devices != []
7474
then throw "devices passthrough not implemented for Firecracker"
75+
else if balloon
76+
then throw "balloon not implemented for Firecracker"
77+
else if initialBalloonMem != 0
78+
then throw "initialBalloonMem not implemented for Firecracker"
79+
else if hotplugMem != 0
80+
then throw "hotplugMem not implemented for Firecracker"
81+
else if hotpluggedMem != 0
82+
then throw "hotpluggedMem not implemented for Firecracker"
7583
else lib.escapeShellArgs [
7684
"${pkgs.firecracker}/bin/firecracker"
7785
"--config-file" configFile

lib/runners/kvmtool.nix

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let
77
inherit (pkgs) lib;
88
inherit (microvmConfig)
99
hostName preStart user
10-
vcpu mem balloonMem interfaces volumes shares devices vsock
10+
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces volumes shares devices vsock
1111
kernel initrdPath
1212
storeDisk storeOnDisk;
1313
in {
@@ -19,11 +19,17 @@ in {
1919
command =
2020
if user != null
2121
then throw "kvmtool will not change user"
22+
else if initialBalloonMem != 0
23+
then throw "kvmtool does not support initialBalloonMem"
24+
else if hotplugMem != 0
25+
then throw "kvmtool does not support hotplugMem"
26+
else if hotpluggedMem != 0
27+
then throw "kvmtool does not support hotpluggedMem"
2228
else builtins.concatStringsSep " " (
2329
[
2430
"${pkgs.kvmtool}/bin/lkvm" "run"
2531
"--name" (lib.escapeShellArg hostName)
26-
"-m" (toString (mem + balloonMem))
32+
"-m" (toString mem)
2733
"-c" (toString vcpu)
2834
"--console" "serial"
2935
"--rng"
@@ -36,9 +42,9 @@ in {
3642
"-d" (lib.escapeShellArg "${storeDisk},ro")
3743
]
3844
++
39-
lib.optionals (balloonMem > 0) [ "--balloon" ]
45+
lib.optionals balloon [ "--balloon" ]
4046
++
41-
builtins.concatMap ({ image, serial, direct, readOnly, ... }:
47+
builtins.concatMap ({ serial, direct, readOnly, ... }:
4248
lib.warnIf (serial != null) ''
4349
Volume serial is not supported for kvmtool
4450
''

lib/runners/qemu.nix

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let
3939
qemu = overrideQemu (if microvmConfig.cpu == null then
4040
pkgs.qemu_kvm else pkgs.buildPackages.qemu_full);
4141

42-
inherit (microvmConfig) hostName cpu vcpu mem balloonMem deflateOnOOM user interfaces shares socket forwardPorts devices vsock graphics storeOnDisk kernel initrdPath storeDisk;
42+
inherit (microvmConfig) hostName vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces shares socket forwardPorts devices vsock graphics storeOnDisk kernel initrdPath storeDisk;
4343
inherit (microvmConfig.qemu) machine extraArgs serialConsole;
4444

4545
inherit (import ../. { inherit (pkgs) lib; }) withDriveLetters;
@@ -156,12 +156,18 @@ lib.warnIf (mem == 2048) ''
156156
{
157157
inherit tapMultiQueue;
158158

159-
command = lib.escapeShellArgs (
159+
command = if initialBalloonMem != 0
160+
then throw "qemu does not support initialBalloonMem"
161+
else if hotplugMem != 0
162+
then throw "qemu does not support hotplugMem"
163+
else if hotpluggedMem != 0
164+
then throw "qemu does not support hotpluggedMem"
165+
else lib.escapeShellArgs (
160166
[
161167
"${qemu}/bin/qemu-system-${arch}"
162168
"-name" hostName
163169
"-M" machineConfig
164-
"-m" (toString (mem + balloonMem))
170+
"-m" (toString mem)
165171
"-smp" (toString vcpu)
166172
"-nodefaults" "-no-user-config"
167173
# qemu just hangs after shutdown, allow to exit by rebooting
@@ -208,7 +214,7 @@ lib.warnIf (mem == 2048) ''
208214
] ++
209215
lib.optionals (user != null) [ "-user" user ] ++
210216
lib.optionals (socket != null) [ "-qmp" "unix:${socket},server,nowait" ] ++
211-
lib.optionals (balloonMem > 0) [
217+
lib.optionals balloon [
212218
"-device" ("virtio-balloon,free-page-reporting=on,id=balloon0" + lib.optionalString (deflateOnOOM) ",deflate-on-oom=on")
213219
] ++
214220
builtins.concatMap ({ image, letter, serial, direct, readOnly, ... }:
@@ -224,7 +230,7 @@ lib.warnIf (mem == 2048) ''
224230
) volumes ++
225231
lib.optionals (shares != []) (
226232
[
227-
"-object" "memory-backend-memfd,id=mem,size=${toString (mem + balloonMem)}M,share=on"
233+
"-object" "memory-backend-memfd,id=mem,size=${toString mem}M,share=on"
228234
"-numa" "node,memdev=mem"
229235
] ++
230236
builtins.concatMap ({ proto, index, socket, source, tag, securityModel, ... }: {

lib/runners/stratovirt.nix

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let
88

99
inherit (microvmConfig)
1010
hostName
11-
vcpu mem interfaces shares socket forwardPorts devices
11+
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces shares socket forwardPorts devices
1212
kernel initrdPath
1313
storeOnDisk storeDisk;
1414

@@ -70,7 +70,15 @@ let
7070
in {
7171
inherit tapMultiQueue;
7272

73-
command = lib.escapeShellArgs (
73+
command = if balloon
74+
then throw "balloon not implemented for stratovirt"
75+
else if initialBalloonMem != 0
76+
then throw "initialBalloonMem not implemented for stratovirt"
77+
else if hotplugMem != 0
78+
then throw "stratovirt does not support hotplugMem"
79+
else if hotpluggedMem != 0
80+
then throw "stratovirt does not support hotpluggedMem"
81+
else lib.escapeShellArgs (
7482
[
7583
"${pkgs.expect}/bin/unbuffer"
7684
"${pkgs.stratovirt}/bin/stratovirt"

nixos-modules/microvm/options.nix

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,47 @@ in
111111
'';
112112
};
113113

114-
balloonMem = mkOption {
114+
hotplugMem = mkOption {
115115
description = ''
116-
Amount of balloon memory in megabytes
116+
Amount of hotplug memory in megabytes.
117117
118-
The way virtio-balloon works is that this is the memory size
119-
that the host can request to be freed by the VM. Initial
120-
booting of the VM allocates mem+balloonMem megabytes of RAM.
118+
This describes the maximum amount of memory that can be dynamically added to the VM with virtio-mem.
121119
'';
122120
default = 0;
123-
type = types.int;
121+
type = types.ints.unsigned;
122+
};
123+
124+
hotpluggedMem = mkOption {
125+
description = ''
126+
Amount of hotplugged memory in megabytes.
127+
128+
This basically describes the amount of hotplug memory the VM starts with.
129+
'';
130+
default = config.microvm.hotplugMem;
131+
type = types.ints.unsigned;
132+
};
133+
134+
balloon = mkOption {
135+
description = ''
136+
Whether to enable ballooning.
137+
138+
By "inflating" or increasing the balloon the host can reduce the VMs
139+
memory amount and reclaim it for itself.
140+
When "deflating" or decreasing the balloon the host can give the memory
141+
back to the VM.
142+
143+
virtio-mem is recommended over ballooning if supported by the hypervisor.
144+
'';
145+
default = false;
146+
type = types.bool;
147+
};
148+
149+
initialBalloonMem = mkOption {
150+
description = ''
151+
Amount of initial balloon memory in megabytes.
152+
'';
153+
default = 0;
154+
type = types.ints.unsigned;
124155
};
125156

126157
deflateOnOOM = mkOption {
@@ -614,6 +645,10 @@ in
614645
};
615646
};
616647

648+
imports = [
649+
(lib.mkRemovedOptionModule ["microvm" "balloonMem"] "The balloonMem option has been removed and replaced by the boolean option balloon")
650+
];
651+
617652
config = lib.mkMerge [ {
618653
microvm.qemu.machine =
619654
lib.mkIf (pkgs.stdenv.system == "x86_64-linux") (

0 commit comments

Comments
 (0)