Skip to content

Instantly share code, notes, and snippets.

@Nislaco
Last active January 31, 2025 01:52
Show Gist options
  • Save Nislaco/19e0cd3ce4bad94820d5a8085cabd5dd to your computer and use it in GitHub Desktop.
Save Nislaco/19e0cd3ce4bad94820d5a8085cabd5dd to your computer and use it in GitHub Desktop.
Build script for dxgkrnl kernel module from WSL linux-msft-wsl-6.6.y for Debian Trixie guest on Hyper-V for GPU-PV
#!/bin/bash -e
BRANCH=linux-msft-wsl-6.6.y
if [ "$EUID" -ne 0 ]; then
echo "Swithing to root..."
exec sudo $0 "$@"
fi
apt-get install -y git dkms curl
cd /tmp
git clone -b $BRANCH --no-checkout --depth=1 https://github.com/microsoft/WSL2-Linux-Kernel.git
cd WSL2-Linux-Kernel
git sparse-checkout set --no-cone /drivers/hv/dxgkrnl /include/uapi/misc/d3dkmthk.h /include/linux/hyperv.h /include/linux/eventfd.h
git checkout
BATCH='test'
RUN=$(git rev-parse --short HEAD)
VERSION="${RUN}${BATCH}"
cp -r drivers/hv/dxgkrnl /usr/src/dxgkrnl-$VERSION
mkdir -p /usr/src/dxgkrnl-$VERSION/include/uapi/misc
mkdir -p /usr/src/dxgkrnl-$VERSION/include/linux
cp include/uapi/misc/d3dkmthk.h /usr/src/dxgkrnl-$VERSION/include/uapi/misc/d3dkmthk.h
cp include/linux/hyperv.h /usr/src/dxgkrnl-$VERSION/include/linux/hyperv_dxgkrnl.h
cp include/linux/eventfd.h /usr/src/dxgkrnl-$VERSION/include/linux/eventfd.h
sed -i 's/\$(CONFIG_DXGKRNL)/m/' /usr/src/dxgkrnl-$VERSION/Makefile
sed -i 's#<uapi/linux/eventfd.h>#<linux/eventfd.h>#g' /usr/src/dxgkrnl-$VERSION/include/linux/eventfd.h
sed -i 's#linux/hyperv.h#linux/hyperv_dxgkrnl.h#' /usr/src/dxgkrnl-$VERSION/dxgmodule.c
echo "EXTRA_CFLAGS=-I\$(PWD)/include -D_MAIN_KERNEL_" >> /usr/src/dxgkrnl-$VERSION/Makefile
cat > /usr/src/dxgkrnl-$VERSION/dkms.conf <<EOF
PACKAGE_NAME="dxgkrnl"
PACKAGE_VERSION="$VERSION"
BUILT_MODULE_NAME="dxgkrnl"
DEST_MODULE_LOCATION="/kernel/drivers/hv/dxgkrnl/"
AUTOINSTALL="yes"
EOF
#wget https://raw.githubusercontent.com/thexperiments/dxgkrnl-dkms-git/main/dxgkrnl.h.patch
# Does Compile with or without this patch ^
#patch -u /usr/src/dxgkrnl-$VERSION/dxgkrnl.h ./dxgkrnl.h.patch
# Does Compile with or without this patch ^
sed -i 's/l(event->cpu_event, 1)/l(event->cpu_event)/g' /usr/src/dxgkrnl-$VERSION/dxgmodule.c
dkms add dxgkrnl/$VERSION
dkms build dxgkrnl/$VERSION
dkms install dxgkrnl/$VERSION
@Nislaco
Copy link
Author

Nislaco commented Jul 29, 2024

Tested working on Linux debian 6.9.10-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.9.10-1 (2024-07-19) x86_64 GNU/Linux

/usr/lib/wsl/lib/nvidia-smi reports passthrough gpus correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment