Compiling custom kernel for WSL2

I would like to use USB devices inside WSL2, however, it doesn't support USB pass-through yet. VirtualHere allows to pass USB devices from windows to WSL2 through the network but it requires USBIP support from Linux kernel. Unfortunately default kernel provided by Microsoft doesn't have required components built-in. So let's build a custom one!

All instructions are given for Ubuntu 20.04 on the WSL side and Windows 10 20H2 as a host.

To build the kernel first install a compiler and required libs:

sudo apt update -y
sudo apt install -y \
  autoconf \
  bison \
  build-essential \
  flex \
  libelf-dev \
  libncurses-dev \
  libssl-dev \
  libtool \
  libudev-dev

Get the right version of the kernel. In my case it's 4.19.104-microsoft-standard

sudo git clone --branch $(uname -r) https://github.com/microsoft/WSL2-Linux-Kernel.git /usr/src/$(uname -r)

And copy current kernel config:

cd /usr/src/$(uname -r) && sudo cp /proc/config.gz config.gz && sudo mv config .config

Now it's time to configure the kernel:

sudo make menuconfig

My final .config with basic support of USB (networking and USB-UART) and USBIP is in this gist

Now it's time to build the kernel and modules:

sudo make -j $(nproc) && sudo make modules_install && sudo make install

Copy kernel to windows partition:

cp vmlinux /mnt/c/Users/<UserName>/

And create a wsl config file: /mnt/c/Users/<UserName>/.wslconfig with the content:

[wsl2]
kernel=C:\\Users\\<UserName>\\vmlinux

The last step is to restart the WSL:

wsl.exe --shutdown

Show Comments