-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathqemu.nix
More file actions
361 lines (325 loc) · 10.7 KB
/
qemu.nix
File metadata and controls
361 lines (325 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
{ pkgs
, microvmConfig
, macvtapFds
}:
let
inherit (pkgs) lib;
inherit (pkgs.stdenv) system;
enableLibusb = pkg: pkg.overrideAttrs (oa: {
configureFlags = oa.configureFlags ++ [
"--enable-libusb"
];
buildInputs = oa.buildInputs ++ (with pkgs; [
libusb1
]);
});
minimizeQemuClosureSize = pkg: (pkg.override (oa: {
# standin for disabling everything guilike by hand
nixosTestRunner =
if graphics.enable
then oa.nixosTestRunner or false
else true;
enableDocs = false;
})).overrideAttrs (oa: {
postFixup = ''
${oa.postFixup or ""}
# This particular firmware causes 192mb of closure size
${lib.optionalString (system != "aarch64-linux") "rm -rf $out/share/qemu/edk2-arm-*"}
'';
});
overrideQemu = x: lib.pipe x (
lib.optional requireUsb enableLibusb
++ lib.optional microvmConfig.optimize.enable minimizeQemuClosureSize
);
qemuPkg =
if microvmConfig.cpu == null
then
# When cross-compiling for a target host, select qemu for the target:
pkgs.qemu_kvm
else
# When cross-compiling for CPU emulation, select qemu for the host:
pkgs.buildPackages.qemu;
qemu = overrideQemu qemuPkg;
inherit (microvmConfig) hostName vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces shares socket forwardPorts devices vsock graphics storeOnDisk kernel initrdPath storeDisk;
inherit (microvmConfig.qemu) machine extraArgs serialConsole;
inherit (import ../. { inherit (pkgs) lib; }) withDriveLetters;
volumes = withDriveLetters microvmConfig;
requireUsb =
graphics.enable ||
lib.any ({ bus, ... }: bus == "usb") microvmConfig.devices;
arch = builtins.head (builtins.split "-" system);
cpuArgs = [
"-cpu"
(
if microvmConfig.cpu != null
then microvmConfig.cpu
else if system == "x86_64-linux"
# qemu crashes when sgx is used on microvm machines: https://gitlab.com/qemu-project/qemu/-/issues/2142
then "host,+x2apic,-sgx"
else "host"
) ];
accel =
if microvmConfig.cpu == null
then "kvm:tcg"
else "tcg";
# PCI required by vfio-pci for PCI passthrough
pciInDevices = lib.any ({ bus, ... }: bus == "pci") devices;
requirePci =
graphics.enable ||
(! lib.hasPrefix "microvm" machine) ||
shares != [] ||
pciInDevices;
machineOpts =
if microvmConfig.qemu.machineOpts != null
then microvmConfig.qemu.machineOpts
else {
x86_64-linux = {
inherit accel;
mem-merge = "on";
acpi = "on";
} // lib.optionalAttrs (machine == "microvm") {
pit = "off";
pic = "off";
pcie = if requirePci then "on" else "off";
usb = if requireUsb then "on" else "off";
};
aarch64-linux = {
inherit accel;
gic-version = "max";
};
}.${system};
machineConfig = builtins.concatStringsSep "," (
[ machine ] ++
map (name:
"${name}=${machineOpts.${name}}"
) (builtins.attrNames machineOpts)
);
devType =
if requirePci
then "pci"
else "device";
kernelPath = "${kernel.out}/${pkgs.stdenv.hostPlatform.linux-kernel.target}";
enumerate = n: xs:
if xs == []
then []
else [
(builtins.head xs // { index = n; })
] ++ (enumerate (n + 1) (builtins.tail xs));
canSandbox =
# Don't let qemu sandbox itself if it is going to call qemu-bridge-helper
! lib.any ({ type, ... }:
type == "bridge"
) microvmConfig.interfaces;
tapMultiQueue = vcpu > 1;
forwardingOptions = lib.concatMapStrings ({ proto, from, host, guest }: {
host = "hostfwd=${proto}:${host.address}:${toString host.port}-" +
"${guest.address}:${toString guest.port},";
guest = "guestfwd=${proto}:${guest.address}:${toString guest.port}-" +
"cmd:${pkgs.netcat}/bin/nc ${host.address} ${toString host.port},";
}.${from}) forwardPorts;
writeQmp = data: ''
echo '${builtins.toJSON data}'
'';
kernelConsole =
if !microvmConfig.qemu.serialConsole
then ""
else if system == "x86_64-linux"
then "earlyprintk=ttyS0 console=ttyS0"
else if system == "aarch64-linux"
then "console=ttyAMA0"
else "";
in
lib.warnIf (mem == 2048) ''
QEMU hangs if memory is exactly 2GB
<https://github.com/astro/microvm.nix/issues/171>
''
{
inherit tapMultiQueue;
command = if initialBalloonMem != 0
then throw "qemu does not support initialBalloonMem"
else if hotplugMem != 0
then throw "qemu does not support hotplugMem"
else if hotpluggedMem != 0
then throw "qemu does not support hotpluggedMem"
else lib.escapeShellArgs (
[
"${qemu}/bin/qemu-system-${arch}"
"-name" hostName
"-M" machineConfig
"-m" (toString mem)
"-smp" (toString vcpu)
"-nodefaults" "-no-user-config"
# qemu just hangs after shutdown, allow to exit by rebooting
"-no-reboot"
"-kernel" "${kernelPath}"
"-initrd" initrdPath
"-chardev" "stdio,id=stdio,signal=off"
"-device" "virtio-rng-${devType}"
] ++
lib.optionals serialConsole [
"-serial" "chardev:stdio"
] ++
lib.optionals (microvmConfig.cpu == null) [
"-enable-kvm"
] ++
cpuArgs ++
lib.optionals (system == "x86_64-linux") [
"-device" "i8042"
"-append" "${kernelConsole} reboot=t panic=-1 ${builtins.unsafeDiscardStringContext (toString microvmConfig.kernelParams)}"
] ++
lib.optionals (system == "aarch64-linux") [
"-append" "${kernelConsole} reboot=t panic=-1 ${builtins.unsafeDiscardStringContext (toString microvmConfig.kernelParams)}"
] ++
lib.optionals storeOnDisk [
"-drive" "id=store,format=raw,read-only=on,file=${storeDisk},if=none,aio=io_uring"
"-device" "virtio-blk-${devType},drive=store${lib.optionalString (devType == "pci") ",disable-legacy=on"}"
] ++
(if graphics.enable
then [
"-display" "gtk,gl=on"
"-device" "virtio-vga-gl"
"-device" "qemu-xhci"
"-device" "usb-tablet"
"-device" "usb-kbd"
]
else [
"-nographic"
]) ++
lib.optionals canSandbox [
"-sandbox" "on"
] ++
lib.optionals (user != null) [ "-user" user ] ++
lib.optionals (socket != null) [ "-qmp" "unix:${socket},server,nowait" ] ++
lib.optionals balloon [
"-device" ("virtio-balloon,free-page-reporting=on,id=balloon0" + lib.optionalString (deflateOnOOM) ",deflate-on-oom=on")
] ++
builtins.concatMap ({ image, letter, serial, direct, readOnly, ... }:
[ "-drive"
"id=vd${letter},format=raw,file=${image},if=none,aio=io_uring,discard=unmap${
lib.optionalString (direct != null) ",cache=none"
},read-only=${if readOnly then "on" else "off"}"
"-device"
"virtio-blk-${devType},drive=vd${letter}${
lib.optionalString (serial != null) ",serial=${serial}"
}"
]
) volumes ++
lib.optionals (shares != []) (
[
"-object" "memory-backend-memfd,id=mem,size=${toString mem}M,share=on"
"-numa" "node,memdev=mem"
] ++
builtins.concatMap ({ proto, index, socket, source, tag, securityModel, ... }: {
"virtiofs" = [
"-chardev" "socket,id=fs${toString index},path=${socket}"
"-device" "vhost-user-fs-${devType},chardev=fs${toString index},tag=${tag}"
];
"9p" = [
"-fsdev" "local,id=fs${toString index},path=${source},security_model=${securityModel}"
"-device" "virtio-9p-${devType},fsdev=fs${toString index},mount_tag=${tag}"
];
}.${proto}) (enumerate 0 shares)
)
++
lib.warnIf (
forwardPorts != [] &&
! builtins.any ({ type, ... }: type == "user") interfaces
) "${hostName}: forwardPortsOptions only running with user network" (
builtins.concatMap ({ type, id, mac, bridge, ... }: [
"-netdev" (
lib.concatStringsSep "," (
[
(if type == "macvtap" then "tap" else "${type}")
"id=${id}"
]
++ lib.optionals (type == "user" && forwardPorts != []) [
forwardingOptions
]
++ lib.optionals (type == "bridge") [
"br=${bridge}" "helper=/run/wrappers/bin/qemu-bridge-helper"
]
++ lib.optionals (type == "tap") [
"ifname=${id}"
"script=no" "downscript=no"
]
++ lib.optionals (type == "macvtap") [ (
let
fds = macvtapFds.${id};
in
if builtins.length fds == 1
then "fd=${toString (builtins.head fds)}"
else "fds=${lib.concatMapStringsSep ":" toString fds}"
) ]
++ lib.optionals (type == "tap" && tapMultiQueue) [
"queues=${toString vcpu}"
]
)
)
"-device" "virtio-net-${devType},netdev=${id},mac=${mac}${
# romfile= does not work with x86_64-linux and -M microvm
# setting or -cpu different than host
lib.optionalString (
requirePci ||
(microvmConfig.cpu == null && system != "x86_64-linux")
) ",romfile="
}${
lib.optionalString (tapMultiQueue && requirePci) ",mq=on,vectors=${toString (2 * vcpu + 2)}"
}"
]) interfaces
)
++
lib.optionals requireUsb [
"-device" "qemu-xhci"
]
++
lib.optionals (vsock.cid != null) [
"-device"
"vhost-vsock-${devType},guest-cid=${toString vsock.cid}"
]
++
extraArgs
)
+ " " + # Move vfio-pci outside of
lib.concatStringsSep " " (lib.concatMap ({ bus, path, qemu,... }: {
pci = [
"-device" "vfio-pci,host=${path},multifunction=on${
# Allow to pass additional arguments to pci device
lib.optionalString (qemu.deviceExtraArgs != null) ",${qemu.deviceExtraArgs}"
}"
];
usb = [
"-device" "usb-host,${path}"
];
}.${bus}) devices);
canShutdown = socket != null;
shutdownCommand =
if socket != null
then
''
(
${writeQmp { execute = "query-commands"; }}
${writeQmp { execute = "system_powerdown"; }}
# wait for exit
cat
) | \
${pkgs.socat}/bin/socat STDIO UNIX:${socket},shut-none
''
else throw "Cannot shutdown without socket";
setBalloonScript =
if balloon
then
if socket != null then
''
VALUE=$(( $SIZE * 1024 * 1024 ))
SIZE=$( (
${writeQmp { execute = "balloon"; arguments.value = 987; }}
) | sed -e s/987/$VALUE/ | \
${pkgs.socat}/bin/socat STDIO UNIX:${socket},shut-none | \
${pkgs.jq}/bin/jq -r .data.actual \
)
echo $(( $SIZE / 1024 / 1024 ))
''
else throw "Cannot balloon without socket"
else null;
requiresMacvtapAsFds = true;
}