Nixerus/configurations/host/materusPC/vm/win10/default.nix

129 lines
3.3 KiB
Nix
Raw Normal View History

2023-07-03 21:49:46 +02:00
{ config, pkgs, lib, inputs, materusFlake, ... }:
2023-08-31 11:01:38 +02:00
let
2023-09-17 14:37:07 +02:00
startHook = /*''
2023-09-17 13:00:37 +02:00
2023-08-31 11:01:38 +02:00
# Debugging
2023-09-17 14:37:07 +02:00
exec 19>/home/materus/startlogfile
BASH_XTRACEFD=19
set -x
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>/home/materus/startlogfile.out 2>&1
''
+*/
''
2023-09-17 13:00:37 +02:00
# Make sure nothing renders on gpu to prevent "sysfs: cannot create duplicate filename" after rebinding to amdgpu
chmod 0 /dev/dri/renderD128
fuser -k /dev/dri/renderD128
2023-07-03 21:49:46 +02:00
2023-09-17 13:00:37 +02:00
# Seems to fix reset bug for 7900 XTX
echo "0" > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/d3cold_allowed"
2023-07-03 21:49:46 +02:00
2023-09-17 13:00:37 +02:00
systemctl stop mountWin10Share.service
2023-08-31 11:01:38 +02:00
echo ''$VIRSH_GPU_VIDEO > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/driver/unbind"
echo ''$VIRSH_GPU_AUDIO > "/sys/bus/pci/devices/''${VIRSH_GPU_AUDIO}/driver/unbind"
2023-07-03 21:49:46 +02:00
2023-08-31 11:01:38 +02:00
sleep 1s
2023-07-03 21:49:46 +02:00
2023-09-17 14:37:07 +02:00
echo "10" > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/resource0_resize"
echo "8" > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/resource2_resize"
2023-07-03 21:49:46 +02:00
2023-09-17 13:00:37 +02:00
systemctl set-property --runtime -- user.slice AllowedCPUs=12-15,28-31
systemctl set-property --runtime -- system.slice AllowedCPUs=12-15,28-31
systemctl set-property --runtime -- init.scope AllowedCPUs=12-15,28-31
2023-08-31 11:01:38 +02:00
'';
stopHook = ''
2023-07-03 21:49:46 +02:00
2023-08-31 11:01:38 +02:00
# Debugging
# exec 19>/home/materus/stoplogfile
# BASH_XTRACEFD=19
# set -x
2023-07-03 21:49:46 +02:00
2023-08-31 11:01:38 +02:00
# exec 3>&1 4>&2
# trap 'exec 2>&4 1>&3' 0 1 2 3
# exec 1>/home/materus/stoplogfile.out 2>&1
2023-07-03 21:49:46 +02:00
2023-09-17 13:00:37 +02:00
sleep 1s
2023-08-31 11:01:38 +02:00
echo ''$VIRSH_GPU_VIDEO > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/driver/unbind"
echo ''$VIRSH_GPU_AUDIO > "/sys/bus/pci/devices/''${VIRSH_GPU_AUDIO}/driver/unbind"
2023-07-03 21:49:46 +02:00
2023-08-31 11:01:38 +02:00
2023-09-17 13:00:37 +02:00
2023-08-31 11:01:38 +02:00
echo "15" > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/resource0_resize"
echo "8" > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/resource2_resize"
2023-09-17 13:00:37 +02:00
echo "1" > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/d3cold_allowed"
2023-08-31 11:01:38 +02:00
echo ''$VIRSH_GPU_VIDEO > /sys/bus/pci/drivers/amdgpu/bind
echo ''$VIRSH_GPU_AUDIO > /sys/bus/pci/drivers/snd_hda_intel/bind
systemctl start mountWin10Share.service
2023-09-17 13:00:37 +02:00
systemctl set-property --runtime -- user.slice AllowedCPUs=0-31
systemctl set-property --runtime -- system.slice AllowedCPUs=0-31
systemctl set-property --runtime -- init.scope AllowedCPUs=0-31
2023-08-31 11:01:38 +02:00
'';
in
{
2023-07-03 21:49:46 +02:00
2023-08-31 11:01:38 +02:00
virtualisation.libvirtd.hooks.qemu = {
"win10" = pkgs.writeShellScript "win10.sh" ''
VIRSH_GPU_VIDEO="0000:03:00.0"
VIRSH_GPU_AUDIO="0000:03:00.1"
VIRSH_USB1="0000:10:00.0"
2023-09-17 13:00:37 +02:00
if [ ''$1 = "win10" ] || [ ''$1 = "win11" ]; then
2023-08-31 11:01:38 +02:00
if [ ''$2 = "prepare" ] && [ ''$3 = "begin" ]; then
${startHook}
fi
if [ ''$2 = "release" ] && [ ''$3 = "end" ]; then
${stopHook}
fi
fi
'';
};
2023-07-03 21:49:46 +02:00
systemd.services.mountWin10Share = {
wantedBy = [ "multi-user.target" ];
2023-09-17 14:37:07 +02:00
path = [ config.virtualisation.libvirtd.qemu.package pkgs.util-linux pkgs.kmod pkgs.coreutils ];
2023-07-03 21:49:46 +02:00
serviceConfig.Type = "oneshot";
serviceConfig.RemainAfterExit = true;
script = ''
modprobe nbd max_part=16
sleep 1
qemu-nbd -c /dev/nbd0 /materus/data/VM/data.qcow2 --cache=unsafe --discard=unmap
sleep 1
mount /dev/nbd0p1 /materus/data/Windows -o uid=1000,gid=100
'';
preStop = ''
umount /materus/data/Windows
qemu-nbd -d /dev/nbd0
'';
};
}