]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add --qemu-kvm option
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 12 Apr 2022 11:58:58 +0000 (13:58 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 19 Apr 2022 06:59:44 +0000 (08:59 +0200)
Allows running without KVM acceleration on machines that support KVM.
Useful when trying to reproduce CI issues where KVM is not supported.

NEWS.md
mkosi.md
mkosi/__init__.py
mkosi/backend.py
tests/test_config_parser.py

diff --git a/NEWS.md b/NEWS.md
index 8054657f7c0a475e30ad64e5ae5a375e1d9ccbe9..247b99d788f0a0981754274b48aee05597e87c66 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
@@ -15,6 +15,7 @@
   mkosi won't automatically build new unified kernel images anymore when a kernel is updated or installed. To keep this
   behavior, you can install the kernel-install script manually via a skeleton tree. The original script can be found
   [here](https://github.com/systemd/mkosi/blob/3798eb0c2ebcdf7dac207a559a3cb5a65cdb77b0/mkosi/resources/dracut_unified_kernel_install.sh).
+- Added QemuKvm option to configure whether to use KVM or not when running `mkosi qemu`.
 
 ## v12
 
index 0ad3f43be76fa51a1f16b6ae50d0333e33190c52..9e54317d89834e626c0939c1404711f6c8cc9b8e 100644 (file)
--- a/mkosi.md
+++ b/mkosi.md
@@ -1095,6 +1095,12 @@ a machine ID.
 : When used with the `qemu` verb, this options sets `qemu`'s `-m`
   argument which controls the amount of guest's RAM. Defaults to `1G`.
 
+`QemuKvm=`, `--qemu-kvm=`
+
+: When used with the `qemu` verb, this option specifies whether QEMU
+  should use KVM acceleration. Defaults to yes if the host machine
+  supports KVM acceleration, no otherwise.
+
 `NspawnKeepUnit=`, `--nspawn-keep-unit`
 
 : When used, this option instructs underlying calls of systemd-nspawn to
index 96cded114356e41fe8de3c407977242bad6bbe7a..3e343e4716b64c4272fc7d85b9495aae4392d3aa 100644 (file)
@@ -5624,6 +5624,8 @@ def create_parser() -> ArgumentParserMkosi:
     group.add_argument("--qemu-headless", action=BooleanAction, help="Configure image for qemu's -nographic mode")
     group.add_argument("--qemu-smp", help="Configure guest's SMP settings", metavar="SMP", default="2")
     group.add_argument("--qemu-mem", help="Configure guest's RAM size", metavar="MEM", default="1G")
+    group.add_argument("--qemu-kvm", action=BooleanAction, help="Configure whether to use KVM or not",
+                       default=qemu_check_kvm_support())
     group.add_argument(
         "--nspawn-keep-unit",
         action=BooleanAction,
@@ -6638,6 +6640,9 @@ def load_args(args: argparse.Namespace) -> MkosiArgs:
     if args.base_image is not None:
         args.base_packages = False
 
+    if args.qemu_kvm and not qemu_check_kvm_support():
+        die("Sorry, the host machine does not support KVM acceleration.")
+
     return MkosiArgs(**vars(args))
 
 
@@ -7636,8 +7641,7 @@ def qemu_check_kvm_support() -> bool:
 
 @contextlib.contextmanager
 def run_qemu_cmdline(args: MkosiArgs) -> Iterator[List[str]]:
-    has_kvm = qemu_check_kvm_support()
-    accel = "kvm" if has_kvm else "tcg"
+    accel = "kvm" if args.qemu_kvm else "tcg"
 
     firmware, fw_supports_sb = find_qemu_firmware()
 
@@ -7655,7 +7659,7 @@ def run_qemu_cmdline(args: MkosiArgs) -> Iterator[List[str]]:
         "virtio-rng-pci,rng=rng0,id=rng-device0",
     ]
 
-    if has_kvm:
+    if args.qemu_kvm:
         cmdline += ["-cpu", "host"]
 
     if args.qemu_headless:
index 36c856f99f73fdb87867d067a43b2c40a33d050e..d424a27174ccb297dc6c4d69222f4af8242824f1 100644 (file)
@@ -516,6 +516,7 @@ class MkosiArgs:
     qemu_headless: bool
     qemu_smp: str
     qemu_mem: str
+    qemu_kvm: bool
 
     # systemd-nspawn specific options
     nspawn_keep_unit: bool
index 7f8e1bd56e8f7b0dca049fdbd4f844aab3509b6a..ee1b01a0d287f9c043b025c9b8e0d661c59d3ddf 100644 (file)
@@ -132,6 +132,7 @@ class MkosiConfig:
             "qemu_headless": False,
             "qemu_smp": "2",
             "qemu_mem": "1G",
+            "qemu_kvm": mkosi.qemu_check_kvm_support(),
             "nspawn_keep_unit": False,
             "netdev": False,
             "ephemeral": False,