/kvm-single-gpu-hooks

Modified hooks to passthough Single NVIDIA GPU (RTX 2080Ti tested)

Primary LanguageShellGNU General Public License v2.0GPL-2.0

kvm-single-gpu-hooks

Modified hooks to passthough Single NVIDIA GPU (RTX 2080Ti tested)

README was generated by ChatGPT.

Script to Detach GPU Devices from Host and Load VFIO Module

This script is designed to automatically run when a virtual machine is powered on. It is added to the /etc/libvirt/hooks/qemu.d/vm_name/prepare/begin file, where vm_name is the name of the virtual machine. Usage

This script should not be run manually by the user. Commands

systemctl stop sddm.service

This command stops the SDDM (Simple Desktop Display Manager) service, which manages user sessions and handles the graphical login screen.

killall gdm-x-session waybar swaybg Hyprland xdg-desktop-portal-hyprland xdg-desktop-portal

This command kills any processes that might interfere with the script's operation. These processes include the GDM (Gnome Display Manager) X session, Waybar (a status bar for Wayland), swaybg (a wallpaper manager for the Sway window manager), and several desktop portals for Hyprland (a desktop environment).

echo 0 > /sys/class/vtconsole/vtcon0/bind
echo 0 > /sys/class/vtconsole/vtcon1/bind

These commands unbind VT (Virtual Terminal) consoles from the host. Depending on the system configuration, these commands might not be needed.

echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind

This command unbinds the EFI framebuffer from the host. Depending on the system configuration, this command might not be needed.

modprobe -r nvidia_drm nvidia_modeset nvidia_uvm nvidia

This command unloads the NVIDIA kernel modules, which provide support for NVIDIA graphics cards. If you are not using an NVIDIA GPU, comment out this command.

modprobe -r amdgpu

This command unloads the AMD kernel module, which provides support for AMD graphics cards. If you are not using an AMD GPU, comment out this command.

virsh nodedev-detach pci_0000_0c_00_0
virsh nodedev-detach pci_0000_0c_00_1
virsh nodedev-detach pci_0000_0c_00_2
virsh nodedev-detach pci_0000_0c_00_3

These commands detach GPU devices from the host. Replace pci_0000_0c_00_0, pci_0000_0c_00_1, pci_0000_0c_00_2, and pci_0000_0c_00_3 with the actual names of your GPU and HDMI audio PCI host devices.

modprobe vfio-pci

This command loads the VFIO kernel module, which provides support for virtualizing PCI devices.

Notes:

This script is designed to automatically run when a virtual machine is powered on.

Replace vm_name in /etc/libvirt/hooks/qemu.d/vm_name/prepare/begin with the actual name of your virtual machine.

Depending on your system configuration, some commands might not be needed.

Script to Attach GPU Devices to Host and Rebind Framebuffer to Host

This script is designed to automatically run when a virtual machine is shut down. It is added to the /etc/libvirt/hooks/qemu.d/vm_name/release/end file, where vm_name is the name of the virtual machine. Usage

This script should not be run manually by the user. It is used to automatically attach GPU devices to the host and rebind the framebuffer to the host when a virtual machine is shut down. Commands

set -x

This command enables debugging mode for the script.

virsh nodedev-reattach pci_0000_0c_00_3
virsh nodedev-reattach pci_0000_0c_00_2
virsh nodedev-reattach pci_0000_0c_00_1
virsh nodedev-reattach pci_0000_0c_00_0

These commands reattach the PCI host devices that were detached from the host and attached to the virtual machine. This allows the host to access the GPU devices again.

modprobe -r vfio-pci

This command removes the vfio-pci kernel module, which is used to assign devices to virtual machines.

echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind

This command rebinds the EFI framebuffer device to the host.

modprobe nvidia_drm
modprobe nvidia_modeset
modprobe nvidia_uvm
modprobe nvidia

These commands load the NVIDIA kernel modules required for the host to use NVIDIA graphics cards.

echo 1 > /sys/class/vtconsole/vtcon0/bind

This command binds the VT console to the host.

systemctl start sddm.service
gdm-x-session
xdg-desktop-portal-hyprland
xdg-desktop-portal
waybar
swaybg
Hyprland

These commands start the Display Manager and its related services. The Display Manager is responsible for launching the graphical interface and user sessions. The specific services that are started here may vary depending on the system configuration.