-
Notifications
You must be signed in to change notification settings - Fork 300
/
qemu.py
1045 lines (853 loc) · 38.2 KB
/
qemu.py
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: LGPL-2.1+
import asyncio
import base64
import contextlib
import enum
import errno
import fcntl
import hashlib
import json
import logging
import os
import random
import shutil
import socket
import struct
import subprocess
import sys
import tempfile
import uuid
from collections.abc import Iterator
from pathlib import Path
from typing import NamedTuple, Optional
from mkosi.config import (
Args,
Config,
ConfigFeature,
Network,
OutputFormat,
QemuDrive,
QemuFirmware,
QemuVsockCID,
format_bytes,
systemd_tool_version,
want_selinux_relabel,
)
from mkosi.log import ARG_DEBUG, die
from mkosi.partition import finalize_root, find_partitions
from mkosi.run import AsyncioThread, find_binary, fork_and_wait, run, spawn
from mkosi.sandbox import Mount
from mkosi.tree import copy_tree, rmtree
from mkosi.types import PathString
from mkosi.user import INVOKING_USER, become_root
from mkosi.util import StrEnum, flatten, flock, flock_or_die
from mkosi.versioncomp import GenericVersion
QEMU_KVM_DEVICE_VERSION = GenericVersion("9.0")
VHOST_VSOCK_SET_GUEST_CID = 0x4008af60
class QemuDeviceNode(StrEnum):
kvm = enum.auto()
vhost_vsock = enum.auto()
def device(self) -> Path:
return Path("/dev") / str(self)
def description(self) -> str:
return {
QemuDeviceNode.kvm: "KVM acceleration",
QemuDeviceNode.vhost_vsock: "a VSock device",
}[self]
def feature(self, config: Config) -> ConfigFeature:
return {
QemuDeviceNode.kvm: config.qemu_kvm,
QemuDeviceNode.vhost_vsock: config.qemu_vsock,
}[self]
def open(self) -> int:
return os.open(self.device(), os.O_RDWR|os.O_CLOEXEC|os.O_NONBLOCK)
def available(self, log: bool = False) -> bool:
try:
os.close(self.open())
except OSError as e:
if e.errno not in (errno.ENOENT, errno.ENODEV, errno.EPERM, errno.EACCES):
raise e
if log and e.errno in (errno.ENOENT, errno.ENODEV):
logging.warning(f"{self.device()} not found. Not adding {self.description()} to the virtual machine.")
if log and e.errno in (errno.EPERM, errno.EACCES):
logging.warning(
f"Permission denied to access {self.device()}. "
f"Not adding {self.description()} to the virtual machine. "
"(Maybe a kernel module could not be loaded?)"
)
return False
return True
def hash_output(config: Config) -> "hashlib._Hash":
p = os.fspath(config.output_dir_or_cwd() / config.output)
return hashlib.sha256(p.encode())
def hash_to_vsock_cid(hash: "hashlib._Hash") -> int:
cid = int.from_bytes(hash.digest()[:4], byteorder='little')
# Make sure we don't return any of the well-known CIDs.
return max(3, min(cid, 0xFFFFFFFF - 1))
def vsock_cid_in_use(vfd: int, cid: int) -> bool:
try:
fcntl.ioctl(vfd, VHOST_VSOCK_SET_GUEST_CID, struct.pack("=Q", cid))
except OSError as e:
if e.errno != errno.EADDRINUSE:
raise
return True
return False
def find_unused_vsock_cid(config: Config, vfd: int) -> int:
hash = hash_output(config)
for i in range(64):
cid = hash_to_vsock_cid(hash)
if not vsock_cid_in_use(vfd, cid):
return cid
hash.update(i.to_bytes(length=4, byteorder='little'))
for i in range(64):
cid = random.randint(0, 0xFFFFFFFF - 1)
if not vsock_cid_in_use(vfd, cid):
return cid
die("Failed to find an unused VSock connection ID")
class KernelType(StrEnum):
pe = enum.auto()
uki = enum.auto()
unknown = enum.auto()
@classmethod
def identify(cls, config: Config, path: Path) -> "KernelType":
if not find_binary("bootctl", root=config.tools()):
logging.warning("bootctl is not installed, assuming 'unknown' kernel type")
return KernelType.unknown
if systemd_tool_version(config, "bootctl") < 253:
logging.warning("bootctl doesn't know kernel-identify verb, assuming 'unknown' kernel type")
return KernelType.unknown
type = run(
["bootctl", "kernel-identify", path],
stdout=subprocess.PIPE,
sandbox=config.sandbox(mounts=[Mount(path, path, ro=True)]),
).stdout.strip()
try:
return cls(type)
except ValueError:
logging.warning(f"Unknown kernel type '{type}', assuming 'unknown'")
return KernelType.unknown
def find_qemu_binary(config: Config) -> str:
binaries = [f"qemu-system-{config.architecture.to_qemu()}"]
binaries += ["qemu", "qemu-kvm"] if config.architecture.is_native() else []
for binary in binaries:
if find_binary(binary, root=config.tools()) is not None:
return binary
die("Couldn't find QEMU/KVM binary")
class OvmfConfig(NamedTuple):
description: Path
firmware: Path
format: str
vars: Path
vars_format: str
def find_ovmf_firmware(config: Config, firmware: QemuFirmware) -> Optional[OvmfConfig]:
if not firmware.is_uefi():
return None
desc = flatten(
p.glob("*")
for p in (
config.tools() / "etc/qemu/firmware",
config.tools() / "usr/share/qemu/firmware",
)
)
arch = config.architecture.to_qemu()
machine = config.architecture.default_qemu_machine()
for p in sorted(desc):
if p.is_dir():
continue
j = json.loads(p.read_text())
if "uefi" not in j["interface-types"]:
logging.debug(f"{p.name} firmware description does not target UEFI, skipping")
continue
for target in j["targets"]:
if target["architecture"] != arch:
continue
# We cannot use fnmatch as for example our default machine for x86-64 is q35 and the firmware description
# lists "pc-q35-*" so we use a substring check instead.
if any(machine in glob for glob in target["machines"]):
break
else:
logging.debug(
f"{p.name} firmware description does not target architecture {arch} or machine {machine}, skipping"
)
continue
if firmware == QemuFirmware.uefi_secure_boot and "secure-boot" not in j["features"]:
logging.debug(f"{p.name} firmware description does not include secure boot, skipping")
continue
if firmware != QemuFirmware.uefi_secure_boot and "secure-boot" in j["features"]:
logging.debug(f"{p.name} firmware description includes secure boot, skipping")
continue
if config.qemu_firmware_variables == Path("microsoft") and "enrolled-keys" not in j["features"]:
logging.debug(f"{p.name} firmware description does not have enrolled Microsoft keys, skipping")
continue
if config.qemu_firmware_variables != Path("microsoft") and "enrolled-keys" in j["features"]:
logging.debug(f"{p.name} firmware description has enrolled Microsoft keys, skipping")
continue
logging.debug(f"Using {p.name} firmware description")
return OvmfConfig(
description=Path("/") / p.relative_to(config.tools()),
firmware=Path(j["mapping"]["executable"]["filename"]),
format=j["mapping"]["executable"]["format"],
vars=Path(j["mapping"]["nvram-template"]["filename"]),
vars_format=j["mapping"]["nvram-template"]["format"],
)
die("Couldn't find matching OVMF UEFI firmware description")
@contextlib.contextmanager
def start_swtpm(config: Config) -> Iterator[Path]:
with tempfile.TemporaryDirectory(prefix="mkosi-swtpm") as state:
# swtpm_setup is noisy and doesn't have a --quiet option so we pipe it's stdout to /dev/null.
run(["swtpm_setup", "--tpm-state", state, "--tpm2", "--pcr-banks", "sha256", "--config", "/dev/null"],
sandbox=config.sandbox(mounts=[Mount(state, state)]),
stdout=None if ARG_DEBUG.get() else subprocess.DEVNULL)
cmdline = ["swtpm", "socket", "--tpm2", "--tpmstate", f"dir={state}"]
# We create the socket ourselves and pass the fd to swtpm to avoid race conditions where we start qemu before
# swtpm has had the chance to create the socket (or where we try to chown it first).
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
path = Path(state) / Path("sock")
sock.bind(os.fspath(path))
sock.listen()
cmdline += ["--ctrl", f"type=unixio,fd={sock.fileno()}"]
with spawn(
cmdline,
pass_fds=(sock.fileno(),),
sandbox=config.sandbox(mounts=[Mount(state, state)]),
) as proc:
yield path
proc.terminate()
def find_virtiofsd(*, tools: Path = Path("/")) -> Optional[Path]:
if p := find_binary("virtiofsd", root=tools):
return p
if (p := tools / "usr/libexec/virtiofsd").exists():
return Path("/") / p.relative_to(tools)
if (p := tools / "usr/lib/virtiofsd").exists():
return Path("/") / p.relative_to(tools)
return None
@contextlib.contextmanager
def start_virtiofsd(config: Config, directory: Path, *, uidmap: bool) -> Iterator[Path]:
virtiofsd = find_virtiofsd(tools=config.tools())
if virtiofsd is None:
die("virtiofsd must be installed to boot directory images or use RuntimeTrees= with mkosi qemu")
cmdline: list[PathString] = [
virtiofsd,
"--shared-dir", directory,
"--xattr",
# qemu's client doesn't seem to support announcing submounts so disable the feature to avoid the warning.
"--no-announce-submounts",
"--sandbox=chroot",
"--cache=always",
f"--inode-file-handles={'prefer' if os.getuid() == 0 and not uidmap else 'never'}",
]
if not uidmap and want_selinux_relabel(config, directory, fatal=False):
cmdline += ["--security-label"]
# We create the socket ourselves and pass the fd to virtiofsd to avoid race conditions where we start qemu
# before virtiofsd has had the chance to create the socket (or where we try to chown it first).
with (
tempfile.TemporaryDirectory(prefix="mkosi-virtiofsd") as context,
socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock,
):
# Make sure virtiofsd can access the socket in this directory.
os.chown(context, INVOKING_USER.uid, INVOKING_USER.gid)
# Make sure we can use the socket name as a unique identifier for the fs as well but make sure it's not too
# long as virtiofs tag names are limited to 36 bytes.
path = Path(context) / f"sock-{uuid.uuid4().hex}"[:35]
sock.bind(os.fspath(path))
sock.listen()
# Make sure virtiofsd can connect to the socket.
os.chown(path, INVOKING_USER.uid, INVOKING_USER.gid)
cmdline += ["--fd", str(sock.fileno())]
with spawn(
cmdline,
pass_fds=(sock.fileno(),),
# When not invoked as root, bubblewrap will automatically map the current uid/gid to the requested uid/gid
# in the user namespace it spawns, so by specifying --uid 0 --gid 0 we'll get a userns with the current
# uid/gid mapped to root in the userns. --cap-add=all is required to make virtiofsd work. Since it drops
# capabilities itself, we don't bother figuring out the exact set of capabilities it needs.
user=INVOKING_USER.uid if uidmap else None,
group=INVOKING_USER.gid if uidmap else None,
preexec_fn=become_root if not uidmap else None,
sandbox=config.sandbox(
mounts=[Mount(directory, directory)],
options=["--uid", "0", "--gid", "0", "--cap-add", "all"],
),
) as proc:
yield path
proc.terminate()
@contextlib.contextmanager
def vsock_notify_handler() -> Iterator[tuple[str, dict[str, str]]]:
"""
This yields a vsock address and a dict that will be filled in with the notifications from the VM. The
dict should only be accessed after the context manager has been finalized.
"""
with socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM) as vsock:
vsock.bind((socket.VMADDR_CID_ANY, socket.VMADDR_PORT_ANY))
vsock.listen()
vsock.setblocking(False)
num_messages = 0
num_bytes = 0
messages = {}
async def notify() -> None:
nonlocal num_messages
nonlocal num_bytes
loop = asyncio.get_running_loop()
while True:
s, _ = await loop.sock_accept(vsock)
num_messages += 1
with s:
data = []
try:
while (buf := await loop.sock_recv(s, 4096)):
data.append(buf)
except ConnectionResetError:
logging.debug("vsock notify listener connection reset by peer")
for msg in b"".join(data).decode().split("\n"):
if not msg:
continue
num_bytes += len(msg)
k, _, v = msg.partition("=")
messages[k] = v
with AsyncioThread(notify()):
yield f"vsock-stream:{socket.VMADDR_CID_HOST}:{vsock.getsockname()[1]}", messages
logging.debug(f"Received {num_messages} notify messages totalling {format_bytes(num_bytes)} bytes")
for k, v in messages.items():
logging.debug(f"- {k}={v}")
@contextlib.contextmanager
def copy_ephemeral(config: Config, src: Path) -> Iterator[Path]:
if not config.ephemeral or config.output_format in (OutputFormat.cpio, OutputFormat.uki):
with flock_or_die(src):
yield src
return
src = src.resolve()
# tempfile doesn't provide an API to get a random filename in an arbitrary directory so we do this
# instead. Limit the size to 16 characters as the output name might be used in a unix socket path by vmspawn and
# needs to fit in 108 characters.
tmp = src.parent / f"{src.name}-{uuid.uuid4().hex[:16]}"
try:
def copy() -> None:
if config.output_format == OutputFormat.directory:
become_root()
copy_tree(
src, tmp,
preserve=config.output_format == OutputFormat.directory,
use_subvolumes=config.use_subvolumes,
tools=config.tools(),
sandbox=config.sandbox,
)
with flock(src):
fork_and_wait(copy)
yield tmp
finally:
def rm() -> None:
if config.output_format == OutputFormat.directory:
become_root()
rmtree(tmp, tools=config.tools(), sandbox=config.sandbox)
fork_and_wait(rm)
def qemu_version(config: Config) -> GenericVersion:
return GenericVersion(run([find_qemu_binary(config), "--version"],
stdout=subprocess.PIPE, sandbox=config.sandbox()).stdout.split()[3])
def want_scratch(config: Config) -> bool:
return config.runtime_scratch == ConfigFeature.enabled or (
config.runtime_scratch == ConfigFeature.auto and
find_binary(f"mkfs.{config.distribution.filesystem()}", root=config.tools()) is not None
)
@contextlib.contextmanager
def generate_scratch_fs(config: Config) -> Iterator[Path]:
with tempfile.NamedTemporaryFile(dir="/var/tmp", prefix="mkosi-scratch") as scratch:
scratch.truncate(1024**4)
fs = config.distribution.filesystem()
extra = config.environment.get(f"SYSTEMD_REPART_MKFS_OPTIONS_{fs.upper()}", "")
run(
[f"mkfs.{fs}", "-L", "scratch", *extra.split(), scratch.name],
stdout=subprocess.DEVNULL,
sandbox=config.sandbox(mounts=[Mount(scratch.name, scratch.name)]),
)
yield Path(scratch.name)
def finalize_qemu_firmware(config: Config, kernel: Optional[Path]) -> QemuFirmware:
if config.qemu_firmware == QemuFirmware.auto:
if kernel:
return (
QemuFirmware.uefi_secure_boot
if KernelType.identify(config, kernel) != KernelType.unknown
else QemuFirmware.linux
)
elif (
config.output_format in (OutputFormat.cpio, OutputFormat.directory) or
config.architecture.to_efi() is None
):
return QemuFirmware.linux
else:
return QemuFirmware.uefi_secure_boot
else:
return config.qemu_firmware
def finalize_firmware_variables(config: Config, ovmf: OvmfConfig, stack: contextlib.ExitStack) -> tuple[Path, str]:
ovmf_vars = stack.enter_context(tempfile.NamedTemporaryFile(prefix="mkosi-ovmf-vars"))
if config.qemu_firmware_variables in (None, Path("custom"), Path("microsoft")):
ovmf_vars_format = ovmf.vars_format
else:
ovmf_vars_format = "raw"
if config.qemu_firmware_variables == Path("custom"):
assert config.secure_boot_certificate
run(
[
"virt-fw-vars",
"--input", ovmf.vars,
"--output", ovmf_vars.name,
"--enroll-cert", config.secure_boot_certificate,
"--add-db", "OvmfEnrollDefaultKeys", config.secure_boot_certificate,
"--no-microsoft",
"--secure-boot",
"--loglevel", "WARNING",
],
sandbox=config.sandbox(
mounts=[
Mount(ovmf_vars.name, ovmf_vars.name),
Mount(config.secure_boot_certificate, config.secure_boot_certificate, ro=True),
],
),
)
else:
vars = (
config.tools() / ovmf.vars.relative_to("/")
if config.qemu_firmware_variables == Path("microsoft") or not config.qemu_firmware_variables
else config.qemu_firmware_variables
)
shutil.copy2(vars, Path(ovmf_vars.name))
return Path(ovmf_vars.name), ovmf_vars_format
def apply_runtime_size(config: Config, image: Path) -> None:
if config.output_format != OutputFormat.disk or not config.runtime_size:
return
run(
[
"systemd-repart",
"--definitions", "",
"--no-pager",
f"--size={config.runtime_size}",
"--pretty=no",
"--offline=yes",
image,
],
sandbox=config.sandbox(mounts=[Mount(image, image)]),
)
@contextlib.contextmanager
def finalize_drive(drive: QemuDrive) -> Iterator[Path]:
with tempfile.NamedTemporaryFile(dir=drive.directory or "/var/tmp", prefix=f"mkosi-drive-{drive.id}") as file:
file.truncate(drive.size)
yield Path(file.name)
@contextlib.contextmanager
def finalize_state(config: Config, cid: int) -> Iterator[None]:
(INVOKING_USER.runtime_dir() / "machine").mkdir(parents=True, exist_ok=True)
if INVOKING_USER.is_regular_user():
os.chown(INVOKING_USER.runtime_dir(), INVOKING_USER.uid, INVOKING_USER.gid)
os.chown(INVOKING_USER.runtime_dir() / "machine", INVOKING_USER.uid, INVOKING_USER.gid)
with flock(INVOKING_USER.runtime_dir() / "machine"):
if (p := INVOKING_USER.runtime_dir() / "machine" / f"{config.machine_or_name()}.json").exists():
die(f"Another virtual machine named {config.machine_or_name()} is already running",
hint="Use --machine to specify a different virtual machine name")
p.write_text(
json.dumps(
{
"Machine": config.machine_or_name(),
"ProxyCommand": f"socat - VSOCK-CONNECT:{cid}:%p",
"SshKey": os.fspath(config.ssh_key) if config.ssh_key else None,
},
sort_keys=True,
indent=4,
)
)
if INVOKING_USER.is_regular_user():
os.chown(p, INVOKING_USER.uid, INVOKING_USER.gid)
try:
yield
finally:
with flock(INVOKING_USER.runtime_dir() / "machine"):
p.unlink(missing_ok=True)
def allocate_scope(config: Config, pid: int) -> None:
if os.getuid() != 0 and "DBUS_SESSION_BUS_ADDRESS" not in os.environ:
return
if (
os.getuid() == 0 and
"DBUS_SYSTEM_ADDRESS" not in os.environ and
not Path("/run/dbus/system_bus_socket").exists()
):
return
name = config.machine_or_name().replace("_", "-")
scope = run(
["systemd-escape", "--mangle", f"{name}.scope"],
stdout=subprocess.PIPE,
foreground=False,
).stdout.strip()
run(
[
"busctl",
"call",
"--system" if os.getuid() == 0 else "--user",
"--quiet",
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartTransientUnit",
"ssa(sv)a(sa(sv))",
scope,
"fail",
"4",
"Description", "s", f"mkosi Virtual Machine {name}",
"CollectMode", "s", "inactive-or-failed",
"PIDs", "au", "1", str(pid),
"AddRef", "b", "1",
"0",
],
foreground=False,
env=os.environ | config.environment,
sandbox=config.sandbox(relaxed=True),
)
def register_machine(config: Config, pid: int, fname: Path) -> None:
if (
os.getuid() != 0 or
("DBUS_SYSTEM_ADDRESS" not in os.environ and not Path("/run/dbus/system_bus_socket").exists())
):
return
run(
[
"busctl",
"call",
"--quiet",
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"RegisterMachine",
"sayssus",
config.machine_or_name().replace("_", "-"),
"0",
"mkosi",
"vm",
str(pid),
fname if fname.is_dir() else "",
],
foreground=False,
env=os.environ | config.environment,
sandbox=config.sandbox(relaxed=True),
)
def run_qemu(args: Args, config: Config) -> None:
if config.output_format not in (
OutputFormat.disk,
OutputFormat.cpio,
OutputFormat.uki,
OutputFormat.esp,
OutputFormat.directory,
):
die(f"{config.output_format} images cannot be booted in qemu")
if (
config.output_format in (OutputFormat.cpio, OutputFormat.uki, OutputFormat.esp) and
config.qemu_firmware not in (QemuFirmware.auto, QemuFirmware.linux) and
not config.qemu_firmware.is_uefi()
):
die(f"{config.output_format} images cannot be booted with the '{config.qemu_firmware}' firmware")
if config.runtime_trees and config.qemu_firmware == QemuFirmware.bios:
die("RuntimeTrees= cannot be used when booting in BIOS firmware")
if config.qemu_kvm == ConfigFeature.enabled and not config.architecture.is_native():
die(f"KVM acceleration requested but {config.architecture} does not match the native host architecture")
if config.qemu_firmware_variables == Path("custom") and not config.secure_boot_certificate:
die("SecureBootCertificate= must be configured to use QemuFirmwareVariables=custom")
# After we unshare the user namespace to sandbox qemu, we might not have access to /dev/kvm or related device nodes
# anymore as access to these might be gated behind the kvm group and we won't be part of the kvm group anymore
# after unsharing the user namespace. To get around this, open all those device nodes early can pass them as file
# descriptors to qemu later. Note that we can't pass the kvm file descriptor to qemu until version 9.0.
qemu_device_fds = {
d: d.open()
for d in QemuDeviceNode
if d.feature(config) != ConfigFeature.disabled and d.available(log=True)
}
have_kvm = ((qemu_version(config) < QEMU_KVM_DEVICE_VERSION and QemuDeviceNode.kvm.available()) or
(qemu_version(config) >= QEMU_KVM_DEVICE_VERSION and QemuDeviceNode.kvm in qemu_device_fds))
if config.qemu_kvm == ConfigFeature.enabled and not have_kvm:
die("KVM acceleration requested but cannot access /dev/kvm")
if config.qemu_vsock == ConfigFeature.enabled and QemuDeviceNode.vhost_vsock not in qemu_device_fds:
die("VSock requested but cannot access /dev/vhost-vsock")
if config.qemu_kernel:
kernel = config.qemu_kernel
elif "-kernel" in args.cmdline:
kernel = Path(args.cmdline[args.cmdline.index("-kernel") + 1])
else:
kernel = None
if config.output_format in (OutputFormat.uki, OutputFormat.esp) and kernel:
logging.warning(
f"Booting UKI output, kernel {kernel} configured with QemuKernel= or passed with -kernel will not be used"
)
kernel = None
if kernel and not kernel.exists():
die(f"Kernel not found at {kernel}")
firmware = finalize_qemu_firmware(config, kernel)
if (
not kernel and
(
firmware == QemuFirmware.linux or
config.output_format in (OutputFormat.cpio, OutputFormat.directory, OutputFormat.uki)
)
):
if firmware.is_uefi():
name = config.output if config.output_format == OutputFormat.uki else config.output_split_uki
kernel = config.output_dir_or_cwd() / name
else:
kernel = config.output_dir_or_cwd() / config.output_split_kernel
if not kernel.exists():
die(
f"Kernel or UKI not found at {kernel}, please install a kernel in the image "
"or provide a -kernel argument to mkosi qemu"
)
ovmf = find_ovmf_firmware(config, firmware)
# A shared memory backend might increase ram usage so only add one if actually necessary for virtiofsd.
shm = []
if config.runtime_trees or config.output_format == OutputFormat.directory:
shm = ["-object", f"memory-backend-memfd,id=mem,size={config.qemu_mem // 1024**2}M,share=on"]
machine = f"type={config.architecture.default_qemu_machine()}"
if firmware.is_uefi() and config.architecture.supports_smm():
machine += f",smm={'on' if firmware == QemuFirmware.uefi_secure_boot else 'off'}"
if shm:
machine += ",memory-backend=mem"
cmdline: list[PathString] = [
find_qemu_binary(config),
"-machine", machine,
"-smp", str(config.qemu_smp),
"-m", f"{config.qemu_mem // 1024**2}M",
"-object", "rng-random,filename=/dev/urandom,id=rng0",
"-device", "virtio-rng-pci,rng=rng0,id=rng-device0",
"-no-user-config",
*shm,
]
if config.runtime_network == Network.user:
cmdline += ["-nic", f"user,model={config.architecture.default_qemu_nic_model()}"]
elif config.runtime_network == Network.interface:
if os.getuid() != 0:
die("RuntimeNetwork=interface requires root privileges")
cmdline += ["-nic", "tap,script=no,model=virtio-net-pci"]
elif config.runtime_network == Network.none:
cmdline += ["-nic", "none"]
if config.qemu_kvm != ConfigFeature.disabled and have_kvm and config.architecture.can_kvm():
accel = "kvm"
if qemu_version(config) >= QEMU_KVM_DEVICE_VERSION:
cmdline += ["--add-fd", f"fd={qemu_device_fds[QemuDeviceNode.kvm]},set=1,opaque=/dev/kvm"]
accel += ",device=/dev/fdset/1"
else:
accel = "tcg"
cmdline += ["-accel", accel]
cid: Optional[int] = None
if QemuDeviceNode.vhost_vsock in qemu_device_fds:
if config.qemu_vsock_cid == QemuVsockCID.auto:
cid = find_unused_vsock_cid(config, qemu_device_fds[QemuDeviceNode.vhost_vsock])
elif config.qemu_vsock_cid == QemuVsockCID.hash:
cid = hash_to_vsock_cid(hash_output(config))
else:
cid = config.qemu_vsock_cid
if vsock_cid_in_use(qemu_device_fds[QemuDeviceNode.vhost_vsock], cid):
die(f"VSock connection ID {cid} is already in use by another virtual machine",
hint="Use QemuVsockConnectionId=auto to have mkosi automatically find a free vsock connection ID")
cmdline += [
"-device",
f"vhost-vsock-pci,guest-cid={cid},vhostfd={qemu_device_fds[QemuDeviceNode.vhost_vsock]}"
]
cmdline += ["-cpu", "max"]
if config.qemu_gui:
cmdline += ["-vga", "virtio"]
else:
# -nodefaults removes the default CDROM device which avoids an error message during boot
# -serial mon:stdio adds back the serial device removed by -nodefaults.
cmdline += [
"-nographic",
"-nodefaults",
"-chardev", "stdio,mux=on,id=console,signal=off",
"-serial", "chardev:console",
"-mon", "console",
]
# QEMU has built-in logic to look for the BIOS firmware so we don't need to do anything special for that.
if firmware.is_uefi():
assert ovmf
cmdline += ["-drive", f"if=pflash,format={ovmf.format},readonly=on,file={ovmf.firmware}"]
notifications: dict[str, str] = {}
with contextlib.ExitStack() as stack:
if firmware.is_uefi():
assert ovmf
ovmf_vars, ovmf_vars_format = finalize_firmware_variables(config, ovmf, stack)
cmdline += ["-drive", f"file={ovmf_vars},if=pflash,format={ovmf_vars_format}"]
if firmware == QemuFirmware.uefi_secure_boot:
cmdline += [
"-global", "ICH9-LPC.disable_s3=1",
"-global", "driver=cfi.pflash01,property=secure,value=on",
]
if config.qemu_cdrom and config.output_format in (OutputFormat.disk, OutputFormat.esp):
# CD-ROM devices have sector size 2048 so we transform disk images into ones with sector size 2048.
src = (config.output_dir_or_cwd() / config.output_with_compression).resolve()
fname = src.parent / f"{src.name}-{uuid.uuid4().hex}"
run(
[
"systemd-repart",
"--definitions", "",
"--no-pager",
"--pretty=no",
"--offline=yes",
"--empty=create",
"--size=auto",
"--sector-size=2048",
"--copy-from", src,
fname,
],
sandbox=config.sandbox(mounts=[Mount(fname.parent, fname.parent), Mount(src, src, ro=True)]),
)
stack.callback(lambda: fname.unlink())
else:
fname = stack.enter_context(
copy_ephemeral(config, config.output_dir_or_cwd() / config.output_with_compression)
)
apply_runtime_size(config, fname)
if (
kernel and
(
KernelType.identify(config, kernel) != KernelType.uki or
not config.architecture.supports_smbios(firmware)
)
):
kcl = config.kernel_command_line + config.kernel_command_line_extra
else:
kcl = config.kernel_command_line_extra
if kernel:
cmdline += ["-kernel", kernel]
if any(s.startswith("root=") for s in kcl):
pass
elif config.output_format == OutputFormat.disk:
# We can't rely on gpt-auto-generator when direct kernel booting so synthesize a root=
# kernel argument instead.
root = finalize_root(find_partitions(fname, sandbox=config.sandbox))
if not root:
die("Cannot perform a direct kernel boot without a root or usr partition")
kcl += [root]
elif config.output_format == OutputFormat.directory:
sock = stack.enter_context(start_virtiofsd(config, fname, uidmap=False))
cmdline += [
"-chardev", f"socket,id={sock.name},path={sock}",
"-device", f"vhost-user-fs-pci,queue-size=1024,chardev={sock.name},tag=root",
]
kcl += ["root=root", "rootfstype=virtiofs", "rw"]
for tree in config.runtime_trees:
sock = stack.enter_context(start_virtiofsd(config, tree.source, uidmap=True))
tag = tree.target.name if tree.target else tree.source.name
cmdline += [
"-chardev", f"socket,id={sock.name},path={sock}",
"-device", f"vhost-user-fs-pci,queue-size=1024,chardev={sock.name},tag={tag}",
]
target = Path("/root/src") / (tree.target or tree.source.name)
kcl += [f"systemd.mount-extra={tag}:{target}:virtiofs"]
if want_scratch(config) or config.output_format in (OutputFormat.disk, OutputFormat.esp):
cmdline += ["-device", "virtio-scsi-pci,id=scsi"]
if want_scratch(config):
scratch = stack.enter_context(generate_scratch_fs(config))
cmdline += [
"-drive", f"if=none,id=scratch,file={scratch},format=raw",
"-device", "scsi-hd,drive=scratch",
]
kcl += [f"systemd.mount-extra=LABEL=scratch:/var/tmp:{config.distribution.filesystem()}"]
if config.output_format == OutputFormat.cpio:
cmdline += ["-initrd", fname]
elif (
kernel and KernelType.identify(config, kernel) != KernelType.uki and
"-initrd" not in args.cmdline and
(config.output_dir_or_cwd() / config.output_split_initrd).exists()
):
cmdline += ["-initrd", config.output_dir_or_cwd() / config.output_split_initrd]
if config.output_format in (OutputFormat.disk, OutputFormat.esp):
cmdline += ["-drive", f"if=none,id=mkosi,file={fname},format=raw",
"-device", f"scsi-{'cd' if config.qemu_cdrom else 'hd'},drive=mkosi,bootindex=1"]
if (
firmware.is_uefi() and
config.qemu_swtpm != ConfigFeature.disabled and
find_binary("swtpm", root=config.tools()) is not None
):
sock = stack.enter_context(start_swtpm(config))
cmdline += ["-chardev", f"socket,id=chrtpm,path={sock}",
"-tpmdev", "emulator,id=tpm0,chardev=chrtpm"]
if config.architecture.is_x86_variant():
cmdline += ["-device", "tpm-tis,tpmdev=tpm0"]
elif config.architecture.is_arm_variant():
cmdline += ["-device", "tpm-tis-device,tpmdev=tpm0"]
credentials = dict(config.credentials)
if QemuDeviceNode.vhost_vsock in qemu_device_fds:
addr, notifications = stack.enter_context(vsock_notify_handler())
credentials["vmm.notify_socket"] = addr
for k, v in credentials.items():
payload = base64.b64encode(v.encode()).decode()
if config.architecture.supports_smbios(firmware):
cmdline += ["-smbios", f"type=11,value=io.systemd.credential.binary:{k}={payload}"]
elif config.architecture.supports_fw_cfg():
f = stack.enter_context(tempfile.NamedTemporaryFile(prefix="mkosi-fw-cfg", mode="w"))
f.write(v)
f.flush()
cmdline += ["-fw_cfg", f"name=opt/io.systemd.credentials/{k},file={f.name}"]
elif kernel:
kcl += [f"systemd.set_credential_binary={k}:{payload}"]
if (
kernel and
(
KernelType.identify(config, kernel) != KernelType.uki or
not config.architecture.supports_smbios(firmware)
)
):
cmdline += ["-append", " ".join(kcl)]
elif config.architecture.supports_smbios(firmware):
cmdline += [
"-smbios",
f"type=11,value=io.systemd.stub.kernel-cmdline-extra={' '.join(kcl)}",
"-smbios",
f"type=11,value=io.systemd.boot.kernel-cmdline-extra={' '.join(kcl)}",
]
for drive in config.qemu_drives:
file = stack.enter_context(finalize_drive(drive))
arg = f"if=none,id={drive.id},file={file},format=raw"
if drive.options:
arg += f",{drive.options}"
cmdline += ["-drive", arg]
cmdline += config.qemu_args
cmdline += args.cmdline
if cid is not None:
stack.enter_context(finalize_state(config, cid))
with spawn(
cmdline,
stdin=sys.stdin,
stdout=sys.stdout,
pass_fds=qemu_device_fds.values(),
env=os.environ | config.environment,
log=False,
foreground=True,
sandbox=config.sandbox(network=True, devices=True, relaxed=True),
) as qemu: