Skip to content

Instantly share code, notes, and snippets.

@tatumroaquin
Last active May 10, 2024 02:52
Show Gist options
  • Save tatumroaquin/b51d16be877d66de9bd3c1bac94faec9 to your computer and use it in GitHub Desktop.
Save tatumroaquin/b51d16be877d66de9bd3c1bac94faec9 to your computer and use it in GitHub Desktop.
WSL2 custom kernel for usb support

WSL2 USB/IP Kernel Build

At the time of writing WSL 2 does not have official support for interfacing with USB type devices. To solve this, a fork of the usbip project is modified by Microsoft to allow virtual connectivity between USB devices on a host via the USB/IP protocol.

Though it is recommended to use the usbip from a Windows 11 host, there is a way to build a custom WSL2 Kernel in Windows 10 with usbip driver support enabled for USB Mass Storage devices.

This is a supplementary guide which allows for a more linear step by step instruction to eliminate the confusion introduced by the official documentation.

Install USB/IP Client

from the windows command prompt

alternatively you can just install the MSI

winget install usbipd-win

from within WSL2 terminal

sudo apt install linux-tools-virtual hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/*/usbip 20

WSL Export

export your current distro to a tar file as a failsafe:

wsl --list --verbose`
wsl --export <current-distro> <temporary-path>\wsl2-usbip.tar`

WSL Import

in case the build fails we can restore from this backup using the following commands:

wsl --import wsl2-usbip <install-path> <temporary-path>\wsl2-usbip.tar`  
wsl --distribution wsl2-usbip --user <user>`

Building The Custom Kernel

update your packages

sudo apt update && sudo apt upgrade

install prerequisite tools

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

clone the repo

git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
cd WSL2-Linux-Kernel
git checkout linux-msft-wsl-5.10.y

copy current config.gz

cp /proc/config.gz config.gz
gunzip config.gz
mv config .config

use menuconfig to select kernel features

You may need to set CONFIG_USB=y in .config prior to running menuconfig

sudo make menuconfig

Ensure that the following features are enabled (move = up/down, select = (s)pace)

Device Drivers -> USB Support
Device Drivers -> USB Support -> USB announce new devices
Device Drivers -> USB Support -> USB Modem (CDC ACM) support
Device Drivers -> USB Support -> USB/IP
Device Drivers -> USB Support -> USB/IP -> VHCI HCD
Device Drivers -> USB Support -> USB/IP -> Debug messages for USB/IP
Device Drivers -> USB Support -> USB Mass Storage Support
Device Drivers -> USB Support -> USB Mass Storage Support > USB Mass Storage Debug
Device Drivers -> USB Serial Converter Support
Device Drivers -> USB Serial Converter Support -> USB FTDI Single port Serial Driver

build the kernel

run getconf _NPROCESSORS_ONLN or nproc to find the number of cores.

sudo make -j 8

alternatively you can directly install the kernel

sudo make -j 8 && sudo make modules_install -j 8 && sudo make install -j 8

build usb/ip tools

cd tools/usb/usbip
sudo ./autogen.sh
sudo ./configure
sudo make install -j 8

copy usbip library to /lib

sudo cp libsrc/.libs/libusbip.so.0 /lib/libusbip.so.0

install usb.ids for named display of usb devices

sudo apt install linux-tools-virtual hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/*/usbip 20

from the project root copy the kernel image

cp arch/x86_64/boot/bzImage /path/to/destination/kernel

configure reference to the kernel image from wsl

vim /mnt/c/Users/user/.wslconfig

[wsl2]
kernel=c:\\path\\to\\destination\\kernel

restart WSL 2 from the windows command prompt

wsl --shutdown

sources:

https://aka.ms/wsl-usbip
https://github.com/dorssel/usbipd-win/wiki/WSL-support#building-your-own-usbip-enabled-wsl-2-kernel
https://docs.microsoft.com/en-us/windows/wsl/build-custom-distro
https://docs.microsoft.com/en-us/windows/wsl/use-custom-distro

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