Skip to content

Instantly share code, notes, and snippets.

@W3SS
Forked from startergo/KVM_in_WSL2.md
Created October 31, 2024 05:38
Show Gist options
  • Save W3SS/b46809098d85e70e827543a7f70662ac to your computer and use it in GitHub Desktop.
Save W3SS/b46809098d85e70e827543a7f70662ac to your computer and use it in GitHub Desktop.
KVM on WSL2 Windows 11
  • In WSL2 run:
sudo apt update
sudo apt install qemu qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils cpu-checker \
network-manager iptables-persistent linux-headers-generic \
qemu uml-utilities virt-manager git \
wget libguestfs-tools p7zip-full make dmg2img tesseract-ocr \
tesseract-ocr-eng genisoimage vim net-tools screen firewalld libncurses-dev -y
sudo apt install virt-manager
sudo addgroup kvm
sudo adduser `id -un` libvirt
sudo adduser `id -un` kvm
newgrp libvirt

https://medium.com/codemonday/setup-virt-manager-qemu-libvert-and-kvm-on-ubuntu-20-04-fa448bdecde3

  • To enable systemd:
sudo nano /etc/wsl.conf

In the file /etc/wsl.conf add these lines:

[boot]
systemd=true
  • Enable nested virtualization. Create .wslcongfig.

  • Add this section to the .wslconfig file

[wsl2]
nestedVirtualization=true
  • Restart WSL:
wsl.exe --shutdown
  • Got an error:
Setting up udev (252.22-1~deb12u1) ...
addgroup: The group `kvm' already exists and is not a system group. Exiting.
dpkg: error processing package udev (--configure):
 installed udev package post-installation script subprocess returned error exit status 1
dpkg: dependency problems prevent configuration of mdevctl:
 mdevctl depends on udev; however:
  Package udev is not configured yet.
  • It can be fixed with:
 sudo mv /var/lib/dpkg/info/udev.postinst /var/lib/dpkg/info/udev.postinst.backup
  • If you get:
/sbin/ldconfig.real: /usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link
  • Then, in WSL the primary fix seems to be to delete libcuda.so and libcuda.so.1 from C:\Windows\System32\lxss\lib as an Administrator in Windows (not from WSL):
cd C:\Windows\System32\lxss\lib && del libcuda.so && del libcuda.so.1 && mklink libcuda.so libcuda.so.1.1 && mklink libcuda.so.1 libcuda.so.1.1
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
export LIBGL_ALWAYS_INDIRECT=1
  • Unregister wsl Pengwin:
wsl --unregister WLinux
  • Link Linux headers to WSL:
#!/bin/bash

set -e

WSL2_VERSION=$(uname -r)
echo "WSL2_VERSION = $WSL2_VERSION"

WSL2_LINK="/lib/modules/$WSL2_VERSION"
if [ -L "${WSL2_LINK}" ]; then
    if [ -e "${WSL2_LINK}" ]; then
        echo "Good link"
        exit 0
    else
        echo "Broken link"
        rm "${WSL2_LINK}"
    fi
elif [ -e "${WSL2_LINK}" ]; then
    echo "Not a link"
    exit 1
else
    echo "Missing"
fi

shopt -s nullglob
for filename in /lib/modules/*; do
    echo "$filename"
    if [ -z "$HEADERS_DIR" ]; then
        HEADERS_DIR="$filename"
    else
        echo "HEADERS_DIR already set to $HEADERS_DIR, fail"
        exit 1
    fi
done

if [ -n "$HEADERS_DIR" ]; then
    echo "Create symbolic link $WSL2_LINK => $HEADERS_DIR"
    ln -s "$HEADERS_DIR" "$WSL2_LINK"
fi
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
git clone https://github.com/microsoft/WSL2-Linux-Kernel --depth=1
cd WSL2-Linux-Kernel
git checkout `uname -r`
cat /proc/config.gz | gunzip > .config
make prepare modules_prepare
make menuconfig 
make modules
make modules_install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment