1 { config, lib, pkgs, ... }:
6 cfg = config.virtualisation.kvmgt;
8 kernelPackages = config.boot.kernelPackages;
12 type = with types; listOf str;
13 description = "UUID(s) of VGPU device. You can generate one with `libossp_uuid`.";
19 virtualisation.kvmgt = {
20 enable = mkEnableOption ''
21 KVMGT (iGVT-g) VGPU support. Allows Qemu/KVM guests to share host's Intel integrated graphics card.
22 Currently only one graphical device can be shared. To allow users to access the device without root add them
23 to the kvm group: `users.extraUsers.<yourusername>.extraGroups = [ "kvm" ];`
25 # multi GPU support is under the question
28 default = "0000:00:02.0";
29 description = "PCI ID of graphics card. You can figure it with {command}`ls /sys/class/mdev_bus`.";
33 type = with types; attrsOf (submodule [ { options = vgpuOptions; } ]);
35 Virtual GPUs to be used in Qemu. You can find devices via {command}`ls /sys/bus/pci/devices/*/mdev_supported_types`
36 and find info about device via {command}`cat /sys/bus/pci/devices/*/mdev_supported_types/i915-GVTg_V5_4/description`
39 i915-GVTg_V5_8.uuid = [ "a297db4a-f4c2-11e6-90f6-d3b88d6c9525" ];
45 config = mkIf cfg.enable {
46 assertions = singleton {
47 assertion = versionAtLeast kernelPackages.kernel.version "4.16";
48 message = "KVMGT is not properly supported for kernels older than 4.16";
51 boot.kernelModules = [ "kvmgt" ];
52 boot.kernelParams = [ "i915.enable_gvt=1" ];
54 services.udev.extraRules = ''
55 SUBSYSTEM=="vfio", OWNER="root", GROUP="kvm"
59 vgpus = listToAttrs (flatten (mapAttrsToList
60 (mdev: opt: map (id: nameValuePair "kvmgt-${id}" { inherit mdev; uuid = id; }) opt.uuid)
63 paths = mapAttrs (_: opt:
65 description = "KVMGT VGPU ${opt.uuid} path";
66 wantedBy = [ "multi-user.target" ];
68 PathExists = "/sys/bus/pci/devices/${cfg.device}/mdev_supported_types/${opt.mdev}/create";
72 services = mapAttrs (_: opt:
74 description = "KVMGT VGPU ${opt.uuid}";
77 RemainAfterExit = true;
78 ExecStart = "${pkgs.runtimeShell} -c 'echo ${opt.uuid} > /sys/bus/pci/devices/${cfg.device}/mdev_supported_types/${opt.mdev}/create'";
79 ExecStop = "${pkgs.runtimeShell} -c 'echo 1 > /sys/bus/pci/devices/${cfg.device}/${opt.uuid}/remove'";
85 meta.maintainers = with maintainers; [ patryk27 ];