at 24.11-pre 3.0 kB view raw
1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 cfg = config.virtualisation.kvmgt; 7 8 kernelPackages = config.boot.kernelPackages; 9 10 vgpuOptions = { 11 uuid = mkOption { 12 type = with types; listOf str; 13 description = "UUID(s) of VGPU device. You can generate one with `libossp_uuid`."; 14 }; 15 }; 16 17in { 18 options = { 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" ];` 24 ''; 25 # multi GPU support is under the question 26 device = mkOption { 27 type = types.str; 28 default = "0000:00:02.0"; 29 description = "PCI ID of graphics card. You can figure it with {command}`ls /sys/class/mdev_bus`."; 30 }; 31 vgpus = mkOption { 32 default = {}; 33 type = with types; attrsOf (submodule [ { options = vgpuOptions; } ]); 34 description = '' 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` 37 ''; 38 example = { 39 i915-GVTg_V5_8.uuid = [ "a297db4a-f4c2-11e6-90f6-d3b88d6c9525" ]; 40 }; 41 }; 42 }; 43 }; 44 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"; 49 }; 50 51 boot.kernelModules = [ "kvmgt" ]; 52 boot.kernelParams = [ "i915.enable_gvt=1" ]; 53 54 services.udev.extraRules = '' 55 SUBSYSTEM=="vfio", OWNER="root", GROUP="kvm" 56 ''; 57 58 systemd = let 59 vgpus = listToAttrs (flatten (mapAttrsToList 60 (mdev: opt: map (id: nameValuePair "kvmgt-${id}" { inherit mdev; uuid = id; }) opt.uuid) 61 cfg.vgpus)); 62 in { 63 paths = mapAttrs (_: opt: 64 { 65 description = "KVMGT VGPU ${opt.uuid} path"; 66 wantedBy = [ "multi-user.target" ]; 67 pathConfig = { 68 PathExists = "/sys/bus/pci/devices/${cfg.device}/mdev_supported_types/${opt.mdev}/create"; 69 }; 70 }) vgpus; 71 72 services = mapAttrs (_: opt: 73 { 74 description = "KVMGT VGPU ${opt.uuid}"; 75 serviceConfig = { 76 Type = "oneshot"; 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'"; 80 }; 81 }) vgpus; 82 }; 83 }; 84 85 meta.maintainers = with maintainers; [ patryk27 ]; 86}