#Enabling SocketCAN on WSL2 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
This just means that WSL2 doesn't come with the CAN interface support enabled, so we need to build the wsl kernel and enable it.
Get the latest WSL2 kernel and configure it for can and vcan support.
sudo apt install build-essential flex bison libssl-dev libelf-dev
cd ~
git clone https://github.com/microsoft/WSL2-Linux-Kernel
cd WSL2-Linux-Kernel
git checkout 'uname -r'
cat /proc/config.gz | gunzip > .config
make prepare modules_prepare
make menuconfig
The config cli app will pop up and we need to enable the following settings. Enter Networking support Change CAN bus subsystem support to M and enter Change Raw CAN Protocol to M Enter CAN Device Drivers Change Virtual Local CAN Interface to M Make sure CAN bit-timing calculation is set to * Optionally change CAN devices debugging messages to * Save and exit
And now we will build the kernel and export it to your Windows user lcoation.
make modules
sudo make modules_install
make -j $(nproc)
sudo make install
cp vmlinux /mnt/c/Users/<yourwindowsloginname>/
The file vmlinux was created and now we will load it.
We need to create config file for wsl to load the custom kernel.
Create the file .wslconfig
in your Windows user folder C:/Users// with the following content:
[wsl2]
kernel=C:\\Users\\<yourwindowsloginname>\\vmlinux
Now you can reset WSL with the new kenrel. We need to enable the modules every time we restart the WSL kernel:
sudo modprobe can
sudo modprobe can-raw
sudo modprobe vcan
And now you are able to create virtual can devices to run. 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
Sources:
I see any error when doing
make modules
and need to install this package:sudo apt install dwarves