From 9c3759ddc35608024200a46ecd50c1e268fd4b2f Mon Sep 17 00:00:00 2001 From: Martin Schwaighofer Date: Sun, 6 Jun 2021 12:45:40 +0200 Subject: [PATCH 1/2] qemu, runInLinuxVM: fix KVM availability check KVM should only be considered abailable if /dev/kvm exists and is read-writable by the user that is trying to launch it. The previous check for existance only had the consequence that on some Linux distributions running VMs with Nix's QEMU only worked if KVM was NOT installed. fixes #124371 (cherry picked from commit 046392279ca41abc36e61a839d4679e40f6e9a3c) --- pkgs/applications/virtualization/qemu/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/applications/virtualization/qemu/default.nix b/pkgs/applications/virtualization/qemu/default.nix index 776bb75531867..828cf6c31930f 100644 --- a/pkgs/applications/virtualization/qemu/default.nix +++ b/pkgs/applications/virtualization/qemu/default.nix @@ -236,7 +236,7 @@ stdenv.mkDerivation rec { if [ -x $out/bin/qemu-system-${stdenv.hostPlatform.qemuArch} ]; then makeWrapper $out/bin/qemu-system-${stdenv.hostPlatform.qemuArch} \ $out/bin/qemu-kvm \ - --add-flags "\$([ -e /dev/kvm ] && echo -enable-kvm)" + --add-flags "\$([ -r /dev/kvm -a -w /dev/kvm ] && echo -enable-kvm)" fi ''; From 4852a70a66d10f9be0312993c3a1278355c6909f Mon Sep 17 00:00:00 2001 From: Martin Schwaighofer Date: Fri, 5 Nov 2021 19:13:47 +0100 Subject: [PATCH 2/2] qemu: emit warnings when KVM acceleration is not usable This fixes the qemu-kvm wrapper we add for convenience silently not using KVM, when the system would support it by at least leaving an indication in the log that the build ran slower because it ran without KVM. (cherry picked from commit 5718276542100f9e779f2c2113e3d0f6c45da054) --- .../virtualization/qemu/default.nix | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/pkgs/applications/virtualization/qemu/default.nix b/pkgs/applications/virtualization/qemu/default.nix index 828cf6c31930f..2f79a2b5dcb19 100644 --- a/pkgs/applications/virtualization/qemu/default.nix +++ b/pkgs/applications/virtualization/qemu/default.nix @@ -1,7 +1,7 @@ { lib, stdenv, fetchurl, fetchpatch, python, zlib, pkg-config, glib , perl, pixman, vde2, alsaLib, texinfo, flex , bison, lzo, snappy, libaio, gnutls, nettle, curl, ninja, meson -, makeWrapper, autoPatchelfHook +, makeWrapper, autoPatchelfHook, runtimeShell , attr, libcap, libcap_ng , CoreServices, Cocoa, Hypervisor, rez, setfile , numaSupport ? stdenv.isLinux && !stdenv.isAarch32, numactl @@ -233,9 +233,12 @@ stdenv.mkDerivation rec { # Add a ‘qemu-kvm’ wrapper for compatibility/convenience. postInstall = '' + cp -- $emitKvmWarningsPath $out/libexec/emit-kvm-warnings + chmod a+x -- $out/libexec/emit-kvm-warnings if [ -x $out/bin/qemu-system-${stdenv.hostPlatform.qemuArch} ]; then makeWrapper $out/bin/qemu-system-${stdenv.hostPlatform.qemuArch} \ $out/bin/qemu-kvm \ + --run $out/libexec/emit-kvm-warnings \ --add-flags "\$([ -r /dev/kvm -a -w /dev/kvm ] && echo -enable-kvm)" fi ''; @@ -247,6 +250,26 @@ stdenv.mkDerivation rec { # Builds in ~3h with 2 cores, and ~20m with a big-parallel builder. requiredSystemFeatures = [ "big-parallel" ]; + emitKvmWarnings = '' + #!${runtimeShell} + WARNCOL='\033[1;35m' + NEUTRALCOL='\033[0m' + WARNING="''${WARNCOL}warning:''${NEUTRALCOL}" + if [ ! -e /dev/kvm ]; then + echo -e "''${WARNING} KVM is not available - execution will be slow" >&2 + echo "Consider installing KVM for hardware-accelerated execution." >&2 + echo "If KVM is already installed make sure the kernel module is loaded." >&2 + elif [ ! -r /dev/kvm -o ! -w /dev/kvm ]; then + echo -e "''${WARNING} /dev/kvm is not read-/writable - execution will be slow" >&2 + echo "/dev/kvm needs to be read-/writable by the user executing QEMU." >&2 + echo "" >&2 + echo "For hardware-acceleration inside the nix build sandbox /dev/kvm" >&2 + echo "must be world-read-/writable (rw-rw-rw-)." >&2 + fi + ''; + + passAsFile = [ "emitKvmWarnings" ]; + meta = with lib; { homepage = "http://www.qemu.org/"; description = "A generic and open source machine emulator and virtualizer";