about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/virtualisation/kvmgt.nix
blob: 36ef6d17df69df166b1c06fa702a45756eb36517 (plain) (blame)
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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.virtualisation.kvmgt;

  kernelPackages = config.boot.kernelPackages;

  vgpuOptions = {
    uuid = mkOption {
      type = types.str;
      description = "UUID of VGPU device. You can generate one with <package>libossp_uuid</package>.";
    };
  };

in {
  options = {
    virtualisation.kvmgt = {
      enable = mkEnableOption ''
        KVMGT (iGVT-g) VGPU support. Allows Qemu/KVM guests to share host's Intel integrated graphics card.
        Currently only one graphical device can be shared
      '';
      # multi GPU support is under the question
      device = mkOption {
        type = types.str;
        default = "0000:00:02.0";
        description = "PCI ID of graphics card. You can figure it with <command>ls /sys/class/mdev_bus</command>.";
      };
      vgpus = mkOption {
        default = {};
        type = with types; attrsOf (submodule [ { options = vgpuOptions; } ]);
        description = ''
          Virtual GPUs to be used in Qemu. You can find devices via <command>ls /sys/bus/pci/devices/*/mdev_supported_types</command>
          and find info about device via <command>cat /sys/bus/pci/devices/*/mdev_supported_types/i915-GVTg_V5_4/description</command>
        '';
        example = {
          i915-GVTg_V5_8 = {
            uuid = "a297db4a-f4c2-11e6-90f6-d3b88d6c9525";
          };
        };
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = singleton {
      assertion = versionAtLeast kernelPackages.kernel.version "4.16";
      message = "KVMGT is not properly supported for kernels older than 4.16";
    };

    boot.kernelModules = [ "kvmgt" ];

    boot.extraModprobeConfig = ''
      options i915 enable_gvt=1
    '';

    systemd.paths = mapAttrs' (name: value:
      nameValuePair "kvmgt-${name}" {
        description = "KVMGT VGPU ${name} path";
        wantedBy = [ "multi-user.target" ];
        pathConfig = {
          PathExists = "/sys/bus/pci/devices/${cfg.device}/mdev_supported_types/${name}/create";
        };
      }
    ) cfg.vgpus;

    systemd.services = mapAttrs' (name: value:
      nameValuePair "kvmgt-${name}" {
        description = "KVMGT VGPU ${name}";
        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
          ExecStart = "${pkgs.runtimeShell} -c 'echo ${value.uuid} > /sys/bus/pci/devices/${cfg.device}/mdev_supported_types/${name}/create'";
          ExecStop = "${pkgs.runtimeShell} -c 'echo 1 > /sys/bus/pci/devices/${cfg.device}/${value.uuid}/remove'";
        };
      }
    ) cfg.vgpus;
  };

  meta.maintainers = with maintainers; [ gnidorah ];
}