Preface: this walkthrough is a hand-holdy step by step tutorial to enable SocketCan on your WSL2 instance (Ubuntu 20.04).
To enable SocketCAN's can-utils on WSL we need to enable the CAN interface module in the WSL, to do so requires a re-building of the WSL kernel.
Requirements:
- WSL2
- back up your wsl image (optional) follow here (wsl2-backup-and-restore-images) Steps: First we will update wsl and our linux to the latest versions.
From cmd / powershell
wsl --shutdown
wsl --update
From wsl
sudo apt update
sudo apt install can-utils
If you try now to use candump for example you'd get this error message:
socket: Address family not supported by protocol
By default, WSL2 doesn't support the CAN interface, so we need to build the wsl kernel where we can enable it.
sudo apt-get install libncurses5-dev
sudo apt install bc
sudo apt install dwarves
sudo apt install build-essential flex bison libssl-dev libelf-dev
- Download the kernel to your Linux home directory:
cd ~
git clone https://github.com/microsoft/WSL2-Linux-Kernel
cd WSL2-Linux-Kernel
- Check out the correct tag:
git checkout $(git tag --list | grep $(uname -r | grep -oE '[0-9]+(\.[0-9]+)+' | head -n 1))
or git checkout 'linux-msft-wsl-YOUR_VERSION_HERE'
- find the correct version number in uname -r
- Generate the config and run
make prepare
:
cat /proc/config.gz | gunzip > .config
make prepare modules_prepare
- Leave the prompts at default values
- run
make menuconfig
- The config command line interface will pop up now. Make sure the following settings under
Networking Support
are checked/enabled:
- Network Support
- CAN bus subsystem support => M -> Enter
- Raw CAN protocol => M
- CAN device drivers => Enter
- Virtual Local CAN Interface (vcan) => M
- CAN bit-timing calculation => *
- CAN bus subsystem support => M -> Enter
- Save and exit the CLI
- Build the kernel (this can take a while):
make modules
- Keep building the kernel:
sudo make modules_install
make -j $(nproc)
sudo make install
- Copy the newly created
vmlinux
file to your Windows user’s home directory:
cp vmlinux /mnt/c/Users/<yourwindowsloginname>/
Now that the vmlinux file was created, we will load it.
-
Create a config file for wsl to load the custom kernel (or edit it if it exists):
C:\Users\YOURWINDOWSLOGINNAME\.wslconfig
-
Put or add the following content:
[wsl2]
kernel=C:\\Users\\<yourwindowsloginname>\\vmlinux
- Restart WSL: From a Windows cmd prompt, run
wsl --shutdown
wsl
- Now you can use WSL with the new kernel:
sudo modprobe can
sudo modprobe can-raw
sudo modprobe vcan
- And now you are able to create virtual can devices, for example:
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
sudo ip link add dev vcan1 type vcan
sudo ip link set up vcan1
As it is, you need to enable the modules every time you restart WSL. This section will show you how to automate that.
- Create
/etc/init.d/init-modprobe
and make it executable:
sudo touch /etc/init.d/init-modprobe
sudo chmod +x /etc/init.d/init-modprobe
- Set the file content:
#!/usr/bin/env bash
sudo modprobe can
sudo modprobe can-raw
sudo modprobe vcan
Allow your current user to execute the new script without a password:
sudo visudo
Add the following line (your_user_name being the linux login name):
your_user_name ALL=(ALL) NOPASSWD:/etc/init.d/init-modprobe
Add the following lines to $HOME/.profile
:
sudo /etc/init.d/init-modprobe
Apply the changes via source $HOME/.profile
The CAN tools should now be reinitialized with each WSL session, and it should not be necessary to enter a password.
Example of error:
arch/x86/Makefile:142: CONFIG_X86_X32 enabled but no binutils support
sh ./arch/x86/boot/install.sh 5.15.167.4-microsoft-standard-WSL2+ \
arch/x86/boot/bzImage System.map "/boot"
run-parts: executing /etc/kernel/postinst.d/unattended-upgrades 5.15.167.4-micro
- Try updating binutils (
sudo apt-get install binutils
) - If that doesn't help, disable the config. Quick and dirty way to just remove it from the makefile:
sed -i -e 's/CONFIG_X86_X32/CONFIG_XDOESNOTEXIST_XINVALID/g' ./arch/x86/Makefile
- If you want to use this, please find out how to make it work and update the gist.
Step 8:
Build the kernel (this can take a while):
make_modules
should be
make modules