{"id":9121,"date":"2024-09-12T08:10:51","date_gmt":"2024-09-12T08:10:51","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=9121"},"modified":"2024-09-13T07:05:21","modified_gmt":"2024-09-13T07:05:21","slug":"how-to-use-gpu-passthrough-in-kvm-qemu-and-display-on-host","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/how-to-use-gpu-passthrough-in-kvm-qemu-and-display-on-host\/","title":{"rendered":"How to Use GPU Passthrough in KVM\/QEMU and Display on Host"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 11,441<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p class=\"wp-block-paragraph\">Using GPU passthrough in KVM\/QEMU and displaying it in a window on the host OS involves configuring your virtual machine (VM) to leverage the dedicated GPU for better performance, while also ensuring you can interact with the VM from a window on your host system. Here\u2019s a guide on how to achieve this:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step-by-Step Guide to Use GPU Passthrough in KVM\/QEMU:<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Prerequisites:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Compatible Hardware<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A CPU that supports <strong>VT-d<\/strong> (Intel) or <strong>AMD-Vi<\/strong> (AMD) for IOMMU (Input-Output Memory Management Unit).<\/li>\n\n\n\n<li>A compatible motherboard with IOMMU support (ensure it&#8217;s enabled in the BIOS\/UEFI).<\/li>\n\n\n\n<li>A dedicated GPU for passthrough and another GPU for your host system display.<\/li>\n\n\n\n<li><strong>Virtualization software<\/strong>: KVM\/QEMU with libvirt.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">     2. <strong>Install Required Packages<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install the necessary packages for virtualization:<br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">bash sudo dnf install qemu-kvm libvirt virt-manager virt-viewer<\/mark><\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">     3. <strong>Enable IOMMU in the BIOS\/UEFI<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reboot your computer and enter the BIOS\/UEFI settings.<\/li>\n\n\n\n<li>Look for <strong>Intel VT-d<\/strong> or <strong>AMD IOMMU<\/strong> and enable it.<\/li>\n\n\n\n<li>Save changes and restart.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">    4. <strong>Enable IOMMU in the Kernel<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Edit the GRUB configuration:<br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">bash sudo nano \/etc\/default\/grub<\/mark><\/code><\/li>\n\n\n\n<li>Add <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">intel_iommu=on<\/mark><\/code> (for Intel) or <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">amd_iommu=on<\/mark><\/code> (for AMD) to the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">GRUB_CMDLINE_LINUX<\/mark><\/code> line:<br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">bash GRUB_CMDLINE_LINUX=\"... intel_iommu=on\"<\/mark><\/code><\/li>\n\n\n\n<li>Update the GRUB configuration:<br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">bash sudo grub2-mkconfig -o \/boot\/efi\/EFI\/fedora\/grub.cfg # For UEFI systems sudo grub2-mkconfig -o \/boot\/grub2\/grub.cfg # For BIOS systems<\/mark><\/code><\/li>\n\n\n\n<li>Reboot your system.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">     5. <strong>Check IOMMU Groups<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Check if the GPU and its associated devices (like audio) are in separate IOMMU groups:<br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">bash find \/sys\/kernel\/iommu_groups\/ -type l<\/mark><\/code><\/li>\n\n\n\n<li>Each GPU component should be in its own IOMMU group.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">     6. <strong>Bind the GPU to VFIO-PCI Driver<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Identify the PCI IDs of your GPU and its associated audio device using:<br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">bash lspci -nn | grep VGA lspci -nn | grep Audio<\/mark><\/code><\/li>\n\n\n\n<li>Create a configuration file for the VFIO driver:<br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">bash sudo nano \/etc\/modprobe.d\/vfio.conf<\/mark><\/code><\/li>\n\n\n\n<li>Add the GPU PCI IDs to the file:<br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">bash options vfio-pci ids=1002:67df,1002:aaf0<\/mark><\/code><\/li>\n\n\n\n<li>Replace <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">1002:67df<\/mark><\/code> and <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">1002:aaf0<\/mark><\/code> with your GPU\u2019s PCI IDs.<\/li>\n\n\n\n<li>Regenerate the initial RAM filesystem to include the VFIO modules:<br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">bash sudo dracut -f --kver `uname -r`<\/mark><\/code><\/li>\n\n\n\n<li>Reboot the system.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">    7. <strong>Configure the Virtual Machine for GPU Passthrough<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open <strong>Virt-Manager<\/strong> (<code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">virt-manager<\/mark><\/code>) and create or edit a VM.<\/li>\n\n\n\n<li>Go to <strong>Add Hardware<\/strong> &gt; <strong>PCI Host Device<\/strong> and select your GPU and its audio device.<\/li>\n\n\n\n<li>Ensure <strong>PCIe<\/strong> is selected, and the <strong>ROM bar<\/strong> is enabled if needed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">    8. <strong>Install the Necessary Drivers in the Guest OS<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you\u2019re using Windows as the guest OS, install the GPU drivers for your specific hardware after booting the VM.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">     9. <strong>Configure Spice and QXL Display for Host Output<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go to the VM\u2019s configuration in Virt-Manager.<\/li>\n\n\n\n<li>In the <strong>Video<\/strong> section, add a <strong>QXL<\/strong> display device.<\/li>\n\n\n\n<li>Set the primary display to <strong>QXL<\/strong>.<\/li>\n\n\n\n<li>Enable <strong>Spice Server<\/strong> or <strong>VNC Server<\/strong> for the display in a window on the host.<\/li>\n\n\n\n<li>Install the <strong>Spice Guest Tools<\/strong> or <strong>Virtio drivers<\/strong> in the guest OS to ensure seamless integration and input handling.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">  10. <strong>Launch the VM with Passthrough and Windowed Display<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start the VM from <strong>Virt-Manager<\/strong>.<\/li>\n\n\n\n<li>You should see a window on your host that shows the VM&#8217;s display output via the Spice\/VNC server, while the GPU passthrough works for hardware acceleration in the guest.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Additional Tips:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Monitor Performance<\/strong>: Use tools like <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">virt-top<\/mark><\/code> or <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">htop<\/mark><\/code> to monitor the resource usage and ensure the GPU is properly utilized by the guest.<\/li>\n\n\n\n<li><strong>Troubleshoot Errors<\/strong>: If you encounter errors related to device access or driver conflicts, check the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">dmesg<\/mark><\/code> logs for kernel messages.<\/li>\n\n\n\n<li><strong>Security Note<\/strong>: Using GPU passthrough can expose your hardware directly to the guest OS. Make sure you trust the guest OS or run it in a secure environment.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using GPU passthrough, especially in KVM\/QEMU, and also showing it in a window of the host OS involves arranging your virtual machine (VM) to use the <a href=\"https:\/\/www.infinitivehost.com\/gpu-dedicated-server\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\"><strong>best dedicated GPU server<\/strong><\/mark><\/a> for robust performance, while also making sure that you can easily interact with the machine from a window on your main system. The above-mentioned steps help you to understand this easily.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>11,441 Views Using GPU passthrough in KVM\/QEMU and displaying it in a window on the host OS involves configuring your virtual machine (VM) to leverage the dedicated GPU for better performance, while also ensuring you can interact with the VM from a window on your host system. Here\u2019s a guide on how to achieve this: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[202],"tags":[],"class_list":["post-9121","post","type-post","status-publish","format-standard","hentry","category-gpu-server"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=9121"}],"version-history":[{"count":2,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9121\/revisions"}],"predecessor-version":[{"id":9127,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9121\/revisions\/9127"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=9121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=9121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=9121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}