From 1a13656f958ebdd855058b524176c1c964420d56 Mon Sep 17 00:00:00 2001 From: waveringana Date: Sun, 15 Jun 2025 05:35:32 -0400 Subject: [PATCH 1/4] add end script --- hosts/focalor/hardware.nix | 6 +++++- hosts/focalor/scripts/end.sh | 20 ++++++++++++++++++++ hosts/focalor/vfio.nix | 3 +++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 hosts/focalor/scripts/end.sh diff --git a/hosts/focalor/hardware.nix b/hosts/focalor/hardware.nix index b290f2f..126d39c 100755 --- a/hosts/focalor/hardware.nix +++ b/hosts/focalor/hardware.nix @@ -9,8 +9,12 @@ ]; boot.initrd.availableKernelModules = [ "nvme" "xhci_pci" "ahci" "uas" "usbhid" "sd_mod" ]; - boot.initrd.kernelModules = [ ]; + boot.initrd.kernelModules = [ "vfio" "vfio_iommu_type1" "vfio_pci" ]; boot.kernelModules = [ "kvm-amd" ]; + boot.kernelParams = [ + "amd_iommu=on" + "vfio-pci.ids=10de:2484,10de228b,1022:149c,15b7:5045,1dbe:5236,1022:149c" + ]; boot.extraModulePackages = [ ]; fileSystems."/" = diff --git a/hosts/focalor/scripts/end.sh b/hosts/focalor/scripts/end.sh new file mode 100644 index 0000000..a08a269 --- /dev/null +++ b/hosts/focalor/scripts/end.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set -x + +virsh nodedev-reattach pci_0000_0a_00_0 +virsh nodedev-reattach pci_0000_0a_00_1 +virsh nodedev-reattach pci_0000_06_00_1 +virsh nodedev-reattach pci_0000_06_00_3 +virsh nodedev-reattach pci_0000_0c_00_3 + +modprobe -r vfio-pci + +modprobe nvidia_modeset +modprobe nvidia_uvm +modprobe nvidia_drm +modprobe nvidia + +modprobe -r xhci_pci +modprobe xhci_pci + +systemctl restart display-manager diff --git a/hosts/focalor/vfio.nix b/hosts/focalor/vfio.nix index eb144df..e0d73f7 100755 --- a/hosts/focalor/vfio.nix +++ b/hosts/focalor/vfio.nix @@ -17,6 +17,9 @@ }).fd]; }; }; + hooks.qemu = { + end = "./scripts/end.sh"; + }; }; users.extraUsers.regent.extraGroups = [ "libvirtd" ]; From e0c6926ba044fdfde43793d47d6a07b564be83ff Mon Sep 17 00:00:00 2001 From: waveringana Date: Sun, 15 Jun 2025 05:57:32 -0400 Subject: [PATCH 2/4] more fixes --- hosts/focalor/scripts/end.sh | 20 --------- hosts/focalor/scripts/vm-win11-hook.sh | 57 ++++++++++++++++++++++++++ hosts/focalor/vfio.nix | 37 ++++++++++++++++- result | 1 + 4 files changed, 94 insertions(+), 21 deletions(-) delete mode 100644 hosts/focalor/scripts/end.sh create mode 100644 hosts/focalor/scripts/vm-win11-hook.sh create mode 120000 result diff --git a/hosts/focalor/scripts/end.sh b/hosts/focalor/scripts/end.sh deleted file mode 100644 index a08a269..0000000 --- a/hosts/focalor/scripts/end.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash -set -x - -virsh nodedev-reattach pci_0000_0a_00_0 -virsh nodedev-reattach pci_0000_0a_00_1 -virsh nodedev-reattach pci_0000_06_00_1 -virsh nodedev-reattach pci_0000_06_00_3 -virsh nodedev-reattach pci_0000_0c_00_3 - -modprobe -r vfio-pci - -modprobe nvidia_modeset -modprobe nvidia_uvm -modprobe nvidia_drm -modprobe nvidia - -modprobe -r xhci_pci -modprobe xhci_pci - -systemctl restart display-manager diff --git a/hosts/focalor/scripts/vm-win11-hook.sh b/hosts/focalor/scripts/vm-win11-hook.sh new file mode 100644 index 0000000..59b39f5 --- /dev/null +++ b/hosts/focalor/scripts/vm-win11-hook.sh @@ -0,0 +1,57 @@ +#!/run/current-system/sw/bin/bash + +readonly GUEST_NAME="$1" +readonly HOOK_NAME="$2" +readonly STATE_NAME="$3" + +function start_hook() { + # Stops GUI + systemctl isolate multi-user.target + + # Avoids race condition + sleep 2 + + # Unloads the NVIDIA drivers + modprobe -r nvidia_drm + modprobe -r nvidia_uvm + modprobe -r nvidia_modeset + modprobe -r nvidia + + # Other code you might want to run +} + +function revert_hook() { + virsh nodedev-reattach pci_0000_0a_00_0 + virsh nodedev-reattach pci_0000_0a_00_1 + virsh nodedev-reattach pci_0000_06_00_1 + virsh nodedev-reattach pci_0000_06_00_3 + virsh nodedev-reattach pci_0000_0c_00_3 + + modprobe -r vfio-pci + + # Loads the NVIDIA drivers + modprobe nvidia_modeset + modprobe nvidia_uvm + modprobe nvidia_drm + modprobe nvidia + + modprobe -r xhci_pci + modprobe xhci_pci + + # Starts the UI again + systemctl restart display-manager + systemctl isolate graphical.target +} + +# I am not using the script from Passthrough-Post +# because hooks option saves it to /var/lib/libvirt/hooks/qemu.d. +# It's simpler to just rewrite it for NixOS. +if [[ "$GUEST_NAME" != "win11" ]]; then + exit 0 +fi + +if [[ "$HOOK_NAME" == "prepare" && "$STATE_NAME" == "begin" ]]; then + start_hook +elif [[ "$HOOK_NAME" == "release" && "$STATE_NAME" == "end" ]]; then + revert_hook +fi \ No newline at end of file diff --git a/hosts/focalor/vfio.nix b/hosts/focalor/vfio.nix index e0d73f7..ebd2e0c 100755 --- a/hosts/focalor/vfio.nix +++ b/hosts/focalor/vfio.nix @@ -18,9 +18,44 @@ }; }; hooks.qemu = { - end = "./scripts/end.sh"; + "win11" = ./scripts/vm-win11-hook.sh; }; }; + systemd.services.libvirtd = { + path = let + env = pkgs.buildEnv { + name = "qemu-hook-env"; + paths = with pkgs; [ + bash + libvirt + kmod + systemd + ripgrep + sd + ]; + }; + in + [ env ]; + + /*preStart = + '' + mkdir -p /var/lib/libvirt/hooks + mkdir -p /var/lib/libvirt/hooks/qemu.d/win10/prepare/begin + mkdir -p /var/lib/libvirt/hooks/qemu.d/win10/release/end + mkdir -p /var/lib/libvirt/vgabios + + ln -sf /home/regent/symlinks/qemu /var/lib/libvirt/hooks/qemu + ln -sf /home/regent/symlinks/kvm.conf /var/lib/libvirt/hooks/kvm.conf + ln -sf /home/regent/symlinks/start.sh /var/lib/libvirt/hooks/qemu.d/win11/prepare/begin/start.sh + ln -sf /home/regent/symlinks/stop.sh /var/lib/libvirt/hooks/qemu.d/win11/release/end/stop.sh + + chmod +x /var/lib/libvirt/hooks/qemu + chmod +x /var/lib/libvirt/hooks/kvm.conf + chmod +x /var/lib/libvirt/hooks/qemu.d/win11/prepare/begin/start.sh + chmod +x /var/lib/libvirt/hooks/qemu.d/win11/release/end/stop.sh + '';*/ + }; + users.extraUsers.regent.extraGroups = [ "libvirtd" ]; } \ No newline at end of file diff --git a/result b/result new file mode 120000 index 0000000..3785af3 --- /dev/null +++ b/result @@ -0,0 +1 @@ +/nix/store/4483jzzk7w649g57g0p1gfnk2skkz85v-nixos-system-focalor-25.05.20250525.7c43f08 \ No newline at end of file From 23bf9ccaa3ed405f9459092bbd8e4d228dca3ba0 Mon Sep 17 00:00:00 2001 From: waveringana Date: Sun, 15 Jun 2025 06:11:29 -0400 Subject: [PATCH 3/4] meow --- hosts/focalor/scripts/vm-win11-hook.sh | 6 +++++- hosts/focalor/vfio.nix | 4 ++-- result | 1 - 3 files changed, 7 insertions(+), 4 deletions(-) delete mode 120000 result diff --git a/hosts/focalor/scripts/vm-win11-hook.sh b/hosts/focalor/scripts/vm-win11-hook.sh index 59b39f5..6c9232e 100644 --- a/hosts/focalor/scripts/vm-win11-hook.sh +++ b/hosts/focalor/scripts/vm-win11-hook.sh @@ -1,5 +1,9 @@ #!/run/current-system/sw/bin/bash +echo "qemu-hook: ${1} ${2}" >> /tmp/qemu-hook.log + +set -x + readonly GUEST_NAME="$1" readonly HOOK_NAME="$2" readonly STATE_NAME="$3" @@ -54,4 +58,4 @@ if [[ "$HOOK_NAME" == "prepare" && "$STATE_NAME" == "begin" ]]; then start_hook elif [[ "$HOOK_NAME" == "release" && "$STATE_NAME" == "end" ]]; then revert_hook -fi \ No newline at end of file +fi diff --git a/hosts/focalor/vfio.nix b/hosts/focalor/vfio.nix index ebd2e0c..07cac37 100755 --- a/hosts/focalor/vfio.nix +++ b/hosts/focalor/vfio.nix @@ -18,7 +18,7 @@ }; }; hooks.qemu = { - "win11" = ./scripts/vm-win11-hook.sh; + win11 = ./scripts/vm-win11-hook.sh; }; }; @@ -58,4 +58,4 @@ }; users.extraUsers.regent.extraGroups = [ "libvirtd" ]; -} \ No newline at end of file +} diff --git a/result b/result deleted file mode 120000 index 3785af3..0000000 --- a/result +++ /dev/null @@ -1 +0,0 @@ -/nix/store/4483jzzk7w649g57g0p1gfnk2skkz85v-nixos-system-focalor-25.05.20250525.7c43f08 \ No newline at end of file From 8cd6aa6afe6414224d309d93b3d2b8f514d7842d Mon Sep 17 00:00:00 2001 From: waveringana Date: Sun, 15 Jun 2025 06:14:33 -0400 Subject: [PATCH 4/4] maybe its permission issues --- hosts/focalor/scripts/vm-win11-hook.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 hosts/focalor/scripts/vm-win11-hook.sh diff --git a/hosts/focalor/scripts/vm-win11-hook.sh b/hosts/focalor/scripts/vm-win11-hook.sh old mode 100644 new mode 100755