impl single-gpu-passthru

This commit is contained in:
zack 2025-01-13 17:07:11 -05:00
parent a8219d7148
commit 82c3c45428
No known key found for this signature in database
GPG key ID: EE8A2B709E2401D1
8 changed files with 312 additions and 130 deletions

View file

@ -18,8 +18,8 @@ in {
package = pkgs.rofi;
# Basic configuration
terminal = "ghostty";
font = "Lexend Medium 16px";
terminal = "kitty";
font = "Cantarell";
extraConfig = {
modi = "drun";
@ -138,7 +138,7 @@ in {
cursor = mkLiteral "inherit";
"vertical-align" = mkLiteral "0.5";
"horizontal-align" = mkLiteral "0.0";
font = mkLiteral ''"Lexend Medium 16px"'';
font = mkLiteral ''"Cantarell 16px"'';
};
};
};

View file

@ -58,6 +58,10 @@ in {
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
if command -v tmux &> /dev/null && [ -z "$TMUX" ]; then
tmux attach-session -t default || tmux new-session -s default
fi
'';
# oh my zsh

View file

@ -0,0 +1,202 @@
{
inputs,
lib,
config,
pkgs,
...
}:
with lib;
with lib.custom; let
cfg = config.hardware.gpu-passthru;
in {
options.hardware.gpu-passthru = with types; {
enable = mkBoolOpt false "Enable support for single gpu-passthru";
};
config = mkIf cfg.enable {
boot.kernelParams = ["intel_iommu=on" "iommu=pt"];
boot.kernelModules = ["vfio-pci"];
virtualisation.libvirtd = {
enable = true;
onBoot = "ignore";
onShutdown = "shutdown";
qemuOvmf = true;
qemuRunAsRoot = true;
};
systemd.services.libvirtd = {
path = let
env = pkgs.buildEnv {
name = "qemu-hook-env";
paths = with pkgs; [
bash
libvirt
kmod
systemd
ripgrep
sd
];
};
in [env];
};
system.activationScripts.libvirt-hooks.text = ''
ln -Tfs /etc/libvirt/hooks /var/lib/libvirt/hooks
'';
# environment.systemPackages = with pkgs; [
# virt-manager
# gnome3.dconf
# libguestfs
# ];
environment.etc = {
"/libvirt/hooks/qemu" = {
text = ''
#!/run/current-system/sw/bin/bash
#
# Author: Sebastiaan Meijer (sebastiaan@passthroughpo.st)
#
# Copy this file to /etc/libvirt/hooks, make sure it's called "qemu".
# After this file is installed, restart libvirt.
# From now on, you can easily add per-guest qemu hooks.
# Add your hooks in /etc/libvirt/hooks/qemu.d/vm_name/hook_name/state_name.
# For a list of available hooks, please refer to https://www.libvirt.org/hooks.html
#
GUEST_NAME="$1"
HOOK_NAME="$2"
STATE_NAME="$3"
MISC="''${@:4}"
BASEDIR="$(dirname $0)"
HOOKPATH="$BASEDIR/qemu.d/$GUEST_NAME/$HOOK_NAME/$STATE_NAME"
set -e # If a script exits with an error, we should as well.
# check if it's a non-empty executable file
if [ -f "$HOOKPATH" ] && [ -s "$HOOKPATH"] && [ -x "$HOOKPATH" ]; then
eval \"$HOOKPATH\" "$@"
elif [ -d "$HOOKPATH" ]; then
while read file; do
# check for null string
if [ ! -z "$file" ]; then
eval \"$file\" "$@"
fi
done <<< "$(find -L "$HOOKPATH" -maxdepth 1 -type f -executable -print;)"
fi
'';
mode = "0755";
};
"libvirt/hooks/kvm.conf" = {
text = ''
VIRSH_GPU_VIDEO=pci_0000_0B_00_0
VIRSH_GPU_AUDIO=pci_0000_0B_00_1
'';
mode = "0755";
};
"libvirt/hooks/qemu.d/win10/prepare/begin/start.sh" = {
text = ''
#!/run/current-system/sw/bin/bash
# Debugging
exec 19>/home/zoey/Desktop/startlogfile
BASH_XTRACEFD=19
set -x
# Load variables we defined
source "/etc/libvirt/hooks/kvm.conf"
# Change to performance governor
echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
# Isolate host to core 0
systemctl set-property --runtime -- user.slice AllowedCPUs=0-6
systemctl set-property --runtime -- system.slice AllowedCPUs=0-6
systemctl set-property --runtime -- init.scope AllowedCPUs=0-6
# Logout
# source "/home/owner/Desktop/Sync/Files/Tools/logout.sh"
# Stop display manager
systemctl stop display-manager.service
killall gdm-wayland-session
# Unbind VTconsoles
echo 0 > /sys/class/vtconsole/vtcon0/bind
echo 0 > /sys/class/vtconsole/vtcon1/bind
# Unbind EFI Framebuffer
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
# Avoid race condition
sleep 5
# Unload NVIDIA kernel modules
modprobe -r nvidia_drm nvidia_modeset nvidia_uvm nvidia
# Detach GPU devices from host
virsh nodedev-detach $VIRSH_GPU_VIDEO
virsh nodedev-detach $VIRSH_GPU_AUDIO
# Load vfio module
modprobe vfio-pci
'';
mode = "0755";
};
"libvirt/hooks/qemu.d/win10/release/end/stop.sh" = {
text = ''
#!/run/current-system/sw/bin/bash
# Debugging
exec 19>/home/zoey/Desktop/stoplogfile
BASH_XTRACEFD=19
set -x
# Load variables we defined
source "/etc/libvirt/hooks/kvm.conf"
# Unload vfio module
modprobe -r vfio-pci
# Attach GPU devices from host
virsh nodedev-reattach $VIRSH_GPU_VIDEO
virsh nodedev-reattach $VIRSH_GPU_AUDIO
# Read nvidia x config
nvidia-xconfig --query-gpu-info > /dev/null 2>&1
# Load NVIDIA kernel modules
modprobe nvidia_drm nvidia_modeset nvidia_uvm nvidia
# Avoid race condition
sleep 5
# Bind EFI Framebuffer
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
# Bind VTconsoles
echo 1 > /sys/class/vtconsole/vtcon0/bind
echo 1 > /sys/class/vtconsole/vtcon1/bind
# Start display manager
systemctl start display-manager.service
# Return host to all cores
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
# Change to powersave governor
echo powersave | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
'';
mode = "0755";
};
};
};
}