imelamory - Fotolia
When a virtual desktop needs to run graphically intensive workloads, admins may want to assign a GPU to those virtual desktops.
As a virtual desktop administrator, you can ensure the VM or virtual desktop makes direct use of your server's physical graphics card. The exact method for mapping a physical GPU to a virtual desktop varies, depending on the type of virtualization host that you are using. This article examines a procedure that you can use for virtual desktops running on top of Microsoft's Hyper-V.
At one point, the preferred method for tying a GPU to a Hyper-V virtual desktop involved RemoteFX. Starting with Microsoft Windows Server 2016, however, this is no longer an option. If you have Hyper-V virtual desktops that are currently configured to use RemoteFX, you can continue to do so. However, you will not be able to assign a RemoteFX 3D video adapter to a newly created VM (Figure 1).
The new method of assigning a GPU to a Hyper-V virtual desktop -- also known as GPU passthrough -- relies on Discrete Device Assignment (DDA). DDA enables Peripheral Component Interconnect Express hardware -- particularly graphics adapters and non-volatile memory express (NVMe) storage devices -- to be directly accessible with a virtual desktop.
GPU passthrough setup with Hyper-V
The first step in GPU passthrough for Hyper-V is to set the virtual desktop's automatic stop action to Turn Off. One approach you could take is to map the VM name to a variable named VM$ because assigning a GPU is a per-VM operation and GPU passthrough will reference the VM name several times. If you wanted to set the VM name to Demo, for example, you could use this command:
While you are at it, you will need to enable write combining for the CPU. Here is the command that you will need to use:
Set-VM $VM -GuestControlledCacheTypes $true
Next, you will need to determine the GPU's location. The easiest way to do this is to open Hyper-V Device Manager and then locate the specific GPU that you plan to allocate.
Next, you will need to right-click on the device and choose the Properties command from the shortcut menu. When the resulting Properties sheet opens, go to the Details tab, and select the Location Paths option from the Property drop-down. Now, right-click on the value that begins with PCIROOT, and then choose the Copy command from the shortcut menu (Figure 2).
Once you have copied the device location to the Windows clipboard, open an elevated PowerShell session. You will want to map the location to a variable. The command should look something like this, with your GPU location following the = sign:
$Location = 'PCIROOT(0)#PCI(0300)#PCI(0000)'
The next step in the process is to configure threshold values for memory-mapped I/O (MMIO). This is memory that will allow the device to be accessible. You will need to configure this threshold on a per-virtual desktop basis.
The Windows OS already reserves some MMIO space by default. It sets the low value to 128 MB and the high value to 512 MB. Depending on the GPU that your virtual desktop will use, this may be sufficient. However, higher-end GPUs -- such as some of the Nvidia models -- require you to assign additional MMIO. Microsoft provides a Machine Profile Script that you can download and run to determine how much MMIO your GPU will require.
Once you know how much MMIO is needed, it's time to assign the MMIO to the virtual desktop that will be using the GPU. For example, say that you need to set the low range of the MMIO to 512 MB and the high range MMIO to 1 GB (Figure 3). The commands for doing so would look like this:
Set-VM $VM -LowMemoryMappedIoSpace 512MB
Set-VM $VM -HighMemoryMappedIoSpace 1GB
The last steps in the process are to disassociate the device from the host OS and then associate it with the VM. Here are the commands used to do so:
Dismount-VMHostAssignableDevice -force -LocationPath $Location
Add-VMAssignableDevice -LocationPath $Location -VMName VMName
As you can see, GPU passthrough for a Hyper-V VM can be a tedious process. If you need some extra help, check out this script provided by Microsoft, which can help automate the process. The script is geared toward a virtual server environment, but it is easy for IT admins to adapt it for use of virtual desktops.