staralt / dxgkrnl-dkms

GPU-P (dxgkrnl) on Hyper-V Linux VM with Latest Kernel
GNU General Public License v2.0
14 stars 4 forks source link
gpu hyper-v linux

GPU-P (dxgkrnl) on Hyper-V Linux with Latest Kernel

dxgkrnl is a DirectX driver integrated into the WSL2 Linux kernel.

This project makes it simple to use GPU-P on Linux with the latest kernel VM. 

Available on

Requirements

Problems

Instructions

1. Enable GPU-P

First, turn off the VM and run the following commands in PowerShell:

$vm = "ENTER YOUR VM NAME"

# Remove current GPU-P adapter
Remove-VMGpuPartitionAdapter -VMName $vm

# Add GPU-P adapter
Add-VMGpuPartitionAdapter -VMName $vm
Set-VMGpuPartitionAdapter -VMName $vm -MinPartitionVRAM 1
Set-VMGpuPartitionAdapter -VMName $vm -MaxPartitionVRAM 11
Set-VMGpuPartitionAdapter -VMName $vm -OptimalPartitionVRAM 10
Set-VMGpuPartitionAdapter -VMName $vm -MinPartitionEncode 1
Set-VMGpuPartitionAdapter -VMName $vm -MaxPartitionEncode 11
Set-VMGpuPartitionAdapter -VMName $vm -OptimalPartitionEncode 10
Set-VMGpuPartitionAdapter -VMName $vm -MinPartitionDecode 1
Set-VMGpuPartitionAdapter -VMName $vm -MaxPartitionDecode 11
Set-VMGpuPartitionAdapter -VMName $vm -OptimalPartitionDecode 10
Set-VMGpuPartitionAdapter -VMName $vm -MinPartitionCompute 1
Set-VMGpuPartitionAdapter -VMName $vm -MaxPartitionCompute 11
Set-VMGpuPartitionAdapter -VMName $vm -OptimalPartitionCompute 10
Set-VM -GuestControlledCacheTypes $true -VMName $vm
Set-VM -LowMemoryMappedIoSpace 1Gb -VMName $vm
Set-VM -HighMemoryMappedIoSpace 32GB -VMName $vm

To start the VM (required in the next chapters):

Start-VM -VMName $vm


2. Prepare drivers to use the GPU

For Windows guests, GPU-P requires copying the same drivers from the Windows host. This is also true for Linux guests.

First, you need to know the IP of the VM. 

Run the following command and remember the IP(e.g. 192.168.0.161):

ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:c2:3a:0e brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.161/24 metric 100 brd 192.168.0.255 scope global dynamic eth0
       valid_lft 6929sec preferred_lft 6929sec
    inet6 fe80::215:5dff:fec2:3a0e/64 scope link
       valid_lft forever preferred_lft forever


To copy drivers using SSH, enter the following commands in PowerShell:

$username="ENTER YOUR USERNAME"
$ip="192.168.0.161"

# Create a destination folder.
ssh ${username}@${ip} "mkdir -p ~/wsl/drivers; mkdir -p ~/wsl/lib;"

# Copy driver files
# https://github.com/brokeDude2901/dxgkrnl_ubuntu/blob/main/README.md#3-copy-windows-host-gpu-driver-to-ubuntu-vm

(Get-CimInstance -ClassName Win32_VideoController -Property *).InstalledDisplayDrivers | Select-String "C:\\Windows\\System32\\DriverStore\\FileRepository\\[a-zA-Z0-9\\._]+\\" | foreach {
    $l=$_.Matches.Value.Substring(0, $_.Matches.Value.Length-1)
    scp -r $l ${username}@${ip}:~/wsl/drivers/
}

scp -r C:\Windows\System32\lxss\lib ${username}@${ip}:~/wsl/

If multiple GPUs are installed, the command will ask for three or more passwords.


3. Install dxgkrnl on the guest VM

Please log in to the shell as the user used in the previous chapter and follow the next chapters.

To install dxgkrnl, please run the following command in the VM shell:

curl -fsSL https://content.staralt.dev/dxgkrnl-dkms/main/install.sh | sudo bash -es

If you want to clean already installed modules. You can use the following command:

curl -fsSL https://content.staralt.dev/dxgkrnl-dkms/main/install.sh | sudo bash -es -- clean all


4. Configure and complete

Move drivers to specific locations and set permissions:

sudo mv ~/wsl /usr/lib/wsl
sudo chmod -R 555 /usr/lib/wsl
sudo chown -R root:root /usr/lib/wsl

Link dynamic libraries:

sudo sh -c 'echo "/usr/lib/wsl/lib" > /etc/ld.so.conf.d/ld.wsl.conf'
sudo ldconfig

Finally, reboot the system (recommended)


After rebooting, the GPU will be available on your Linux Guest. Enjoy it!

nvidia-smi
Sat Jun  1 12:32:58 2024
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.40.07              Driver Version: 551.52         CUDA Version: 12.4     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  Geforce GTX 106...             On  |   00000000:01:00.0 Off |                  N/A |
| 32%   45C    P8              5W /  140W |       0MiB /   6144MiB |      0%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+
+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI        PID   Type   Process name                              GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|  No running processes found                                                             |
+-----------------------------------------------------------------------------------------+


References

Documents:

Repositories: