Skip to content

Instantly share code, notes, and snippets.

@MarioHewardt
Last active April 27, 2024 09:27
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save MarioHewardt/5759641727aae880b29c8f715ba4d30f to your computer and use it in GitHub Desktop.
Save MarioHewardt/5759641727aae880b29c8f715ba4d30f to your computer and use it in GitHub Desktop.
Enable EBPF on WSL2
By default, EBPF programs will not run on WSL2 due to required kernel modules missing. The following example error is an
indication of this problem:
modprobe: ERROR: ../libkmod/libkmod.c:586 kmod_search_moddep() could not open moddep file '/lib/modules/4.19.84-microso
ft-standard/modules.dep.bin'
modprobe: FATAL: Module kheaders not found in directory /lib/modules/4.19.84-microsoft-standard
chdir(/lib/modules/4.19.84-microsoft-standard/build): No such file or directory
To fix this you need to rebuild the WSL2 kernel with the missing kernel modules. The below instructions are for Ubuntu 18.04 WSL2.
1. git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
2. cd WSL2-Linux-Kernel
3. sudo apt install flex bison build-essential libelf-dev libncurses-dev libssl-dev
4. cp Microsoft/config-wsl .config
5. Add the kernel flags specified in https://github.com/iovisor/bcc/blob/master/INSTALL.md#kernel-configuration to .config
6. export KERNELRELEASE=4.19.84-microsoft-standard
NOTE: Make sure the version corresponds to the version of WSL2. Run: uname -a
7. make KERNELRELEASE=$KERNELRELEASE -j 4
8. make KERNELRELEASE=$KERNELRELEASE modules -j 4
9. sudo make KERNELRELEASE=$KERNELRELEASE modules_install
10. (confirm you can see files under /lib/modules/$(uname -r))
11. sudo mount -t debugfs debugfs /sys/kernel/debug (for any apps that uses it since its not mounted by default)
@daneb
Copy link

daneb commented Jan 5, 2021

Really helpful thank you!

@ReySys
Copy link

ReySys commented May 13, 2021

I'm point 6. in WSL2 Ubuntu 20.04 LTS I'm trying to use sysinternals from Microsoft in here:

xxxxxxx@NUCCANYON-8:~/WSL2-Linux-Kernel$ sudo procmon
[sudo] password for xxxxxxx:
modprobe: FATAL: Module kheaders not found in directory /lib/modules/5.10.16.3-microsoft-standard-WSL2
Unable to find kernel headers. Try rebuilding kernel with CONFIG_IKHEADERS=m (module) or installing the kernel development package for your running kernel version.
chdir(/lib/modules/5.10.16.3-microsoft-standard-WSL2/build): No such file or directory
terminate called after throwing an instance of 'std::invalid_argument'
what(): Table '' is not an array table
Aborted

Here is my run

xxxxxxx@NUCCANYON-8:$ git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
Cloning into 'WSL2-Linux-Kernel'...
remote: Enumerating objects: 8921264, done.
remote: Total 8921264 (delta 0), reused 0 (delta 0), pack-reused 8921264
Receiving objects: 100% (8921264/8921264), 1.63 GiB | 2.10 MiB/s, done.
Resolving deltas: 100% (7539238/7539238), done.
Updating files: 100% (61777/61777), done.
reynaldoru@NUCCANYON-8:
$ cd WSL2-Linux-Kernel
reynaldoru@NUCCANYON-8:/WSL2-Linux-Kernel$ sudo apt install flex bison build-essential libelf-dev libncurses-dev libssl-dev
[sudo] password for reynaldoru:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-9 dpkg-dev fakeroot g++ g++-9 gcc gcc-9 gcc-9-base
libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan5 libatomic1 libbinutils
libc-dev-bin libc6-dev libcc1-0 libcrypt-dev libctf-nobfd0 libctf0 libdpkg-perl libfakeroot libfile-fcntllock-perl
libfl-dev libgcc-9-dev libgomp1 libisl22 libitm1 liblsan0 libmpc3 libquadmath0 libstdc++-9-dev libtsan0 libubsan1
linux-libc-dev m4 make manpages-dev zlib1g-dev
Suggested packages:
binutils-doc bison-doc cpp-doc gcc-9-locales debian-keyring flex-doc g++-multilib g++-9-multilib gcc-9-doc
gcc-multilib autoconf automake libtool gdb gcc-doc gcc-9-multilib glibc-doc bzr ncurses-doc libssl-doc
libstdc++-9-doc m4-doc make-doc
The following NEW packages will be installed:
binutils binutils-common binutils-x86-64-linux-gnu bison build-essential cpp cpp-9 dpkg-dev fakeroot flex g++ g++-9
gcc gcc-9 gcc-9-base libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan5 libatomic1
libbinutils libc-dev-bin libc6-dev libcc1-0 libcrypt-dev libctf-nobfd0 libctf0 libdpkg-perl libelf-dev libfakeroot
libfile-fcntllock-perl libfl-dev libgcc-9-dev libgomp1 libisl22 libitm1 liblsan0 libmpc3 libncurses-dev libquadmath0
libssl-dev libstdc++-9-dev libtsan0 libubsan1 linux-libc-dev m4 make manpages-dev zlib1g-dev
0 upgraded, 49 newly installed, 0 to remove and 0 not upgraded.
Need to get 43.3 MB of archives.
After this operation, 190 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 m4 amd64 1.4.18-4 [199 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 flex amd64 2.6.4-6.2 [317 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 binutils-common amd64 2.34-6ubuntu1.1 [207 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libbinutils amd64 2.34-6ubuntu1.1 [475 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libctf-nobfd0 amd64 2.34-6ubuntu1.1 [47.1 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libctf0 amd64 2.34-6ubuntu1.1 [46.6 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 binutils-x86-64-linux-gnu amd64 2.34-6ubuntu1.1 [1613 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 binutils amd64 2.34-6ubuntu1.1 [3380 B]
Get:9 http://archive.ubuntu.com/ubuntu focal/main amd64 bison amd64 2:3.5.1+dfsg-1 [657 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libc-dev-bin amd64 2.31-0ubuntu9.2 [71.8 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 linux-libc-dev amd64 5.4.0-73.82 [1130 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal/main amd64 libcrypt-dev amd64 1:4.4.10-10ubuntu4 [104 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libc6-dev amd64 2.31-0ubuntu9.2 [2520 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 gcc-9-base amd64 9.3.0-17ubuntu1
20.04 [19.1 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal/main amd64 libisl22 amd64 0.22.1-1 [592 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal/main amd64 libmpc3 amd64 1.1.0-1 [40.8 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 cpp-9 amd64 9.3.0-17ubuntu120.04 [7494 kB]
Get:18 http://archive.ubuntu.com/ubuntu focal/main amd64 cpp amd64 4:9.3.0-1ubuntu2 [27.6 kB]
Get:19 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libcc1-0 amd64 10.2.0-5ubuntu1
20.04 [41.1 kB]
Get:20 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libgomp1 amd64 10.2.0-5ubuntu120.04 [102 kB]
Get:21 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libitm1 amd64 10.2.0-5ubuntu1
20.04 [26.4 kB]
Get:22 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libatomic1 amd64 10.2.0-5ubuntu120.04 [9300 B]
Get:23 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libasan5 amd64 9.3.0-17ubuntu1
20.04 [394 kB]
Get:24 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 liblsan0 amd64 10.2.0-5ubuntu120.04 [144 kB]
Get:25 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libtsan0 amd64 10.2.0-5ubuntu1
20.04 [320 kB]
Get:26 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libubsan1 amd64 10.2.0-5ubuntu120.04 [136 kB]
Get:27 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libquadmath0 amd64 10.2.0-5ubuntu1
20.04 [146 kB]
Get:28 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libgcc-9-dev amd64 9.3.0-17ubuntu120.04 [2360 kB]
Get:29 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 gcc-9 amd64 9.3.0-17ubuntu1
20.04 [8241 kB]
Get:30 http://archive.ubuntu.com/ubuntu focal/main amd64 gcc amd64 4:9.3.0-1ubuntu2 [5208 B]
Get:31 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libstdc++-9-dev amd64 9.3.0-17ubuntu120.04 [1714 kB]
Get:32 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 g++-9 amd64 9.3.0-17ubuntu1
20.04 [8405 kB]
Get:33 http://archive.ubuntu.com/ubuntu focal/main amd64 g++ amd64 4:9.3.0-1ubuntu2 [1604 B]
Get:34 http://archive.ubuntu.com/ubuntu focal/main amd64 make amd64 4.2.1-1.2 [162 kB]
Get:35 http://archive.ubuntu.com/ubuntu focal/main amd64 libdpkg-perl all 1.19.7ubuntu3 [230 kB]
Get:36 http://archive.ubuntu.com/ubuntu focal/main amd64 dpkg-dev all 1.19.7ubuntu3 [679 kB]
Get:37 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 build-essential amd64 12.8ubuntu1.1 [4664 B]
Get:38 http://archive.ubuntu.com/ubuntu focal/main amd64 libfakeroot amd64 1.24-1 [25.7 kB]
Get:39 http://archive.ubuntu.com/ubuntu focal/main amd64 fakeroot amd64 1.24-1 [62.6 kB]
Get:40 http://archive.ubuntu.com/ubuntu focal/main amd64 libalgorithm-diff-perl all 1.19.03-2 [46.6 kB]
Get:41 http://archive.ubuntu.com/ubuntu focal/main amd64 libalgorithm-diff-xs-perl amd64 0.04-6 [11.3 kB]
Get:42 http://archive.ubuntu.com/ubuntu focal/main amd64 libalgorithm-merge-perl all 0.08-3 [12.0 kB]
Get:43 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 zlib1g-dev amd64 1:1.2.11.dfsg-2ubuntu1.2 [155 kB]
Get:44 http://archive.ubuntu.com/ubuntu focal/main amd64 libelf-dev amd64 0.176-1.1build1 [57.0 kB]
Get:45 http://archive.ubuntu.com/ubuntu focal/main amd64 libfile-fcntllock-perl amd64 0.22-3build4 [33.1 kB]
Get:46 http://archive.ubuntu.com/ubuntu focal/main amd64 libfl-dev amd64 2.6.4-6.2 [6316 B]
Get:47 http://archive.ubuntu.com/ubuntu focal/main amd64 libncurses-dev amd64 6.2-0ubuntu2 [339 kB]
Get:48 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libssl-dev amd64 1.1.1f-1ubuntu2.3 [1582 kB]
Get:49 http://archive.ubuntu.com/ubuntu focal/main amd64 manpages-dev all 5.05-1 [2266 kB]
Fetched 43.3 MB in 13s (3297 kB/s)
Extracting templates from packages: 100%
Selecting previously unselected package m4.
(Reading database ... 32317 files and directories currently installed.)
Preparing to unpack .../00-m4_1.4.18-4_amd64.deb ...
Unpacking m4 (1.4.18-4) ...
Selecting previously unselected package flex.
Preparing to unpack .../01-flex_2.6.4-6.2_amd64.deb ...
Unpacking flex (2.6.4-6.2) ...
Selecting previously unselected package binutils-common:amd64.
Preparing to unpack .../02-binutils-common_2.34-6ubuntu1.1_amd64.deb ...
Unpacking binutils-common:amd64 (2.34-6ubuntu1.1) ...
Selecting previously unselected package libbinutils:amd64.
Preparing to unpack .../03-libbinutils_2.34-6ubuntu1.1_amd64.deb ...
Unpacking libbinutils:amd64 (2.34-6ubuntu1.1) ...
Selecting previously unselected package libctf-nobfd0:amd64.
Preparing to unpack .../04-libctf-nobfd0_2.34-6ubuntu1.1_amd64.deb ...
Unpacking libctf-nobfd0:amd64 (2.34-6ubuntu1.1) ...
Selecting previously unselected package libctf0:amd64.
Preparing to unpack .../05-libctf0_2.34-6ubuntu1.1_amd64.deb ...
Unpacking libctf0:amd64 (2.34-6ubuntu1.1) ...
Selecting previously unselected package binutils-x86-64-linux-gnu.
Preparing to unpack .../06-binutils-x86-64-linux-gnu_2.34-6ubuntu1.1_amd64.deb ...
Unpacking binutils-x86-64-linux-gnu (2.34-6ubuntu1.1) ...
Selecting previously unselected package binutils.
Preparing to unpack .../07-binutils_2.34-6ubuntu1.1_amd64.deb ...
Unpacking binutils (2.34-6ubuntu1.1) ...
Selecting previously unselected package bison.
Preparing to unpack .../08-bison_2%3a3.5.1+dfsg-1_amd64.deb ...
Unpacking bison (2:3.5.1+dfsg-1) ...
Selecting previously unselected package libc-dev-bin.
Preparing to unpack .../09-libc-dev-bin_2.31-0ubuntu9.2_amd64.deb ...
Unpacking libc-dev-bin (2.31-0ubuntu9.2) ...
Selecting previously unselected package linux-libc-dev:amd64.
Preparing to unpack .../10-linux-libc-dev_5.4.0-73.82_amd64.deb ...
Unpacking linux-libc-dev:amd64 (5.4.0-73.82) ...
Selecting previously unselected package libcrypt-dev:amd64.
Preparing to unpack .../11-libcrypt-dev_1%3a4.4.10-10ubuntu4_amd64.deb ...
Unpacking libcrypt-dev:amd64 (1:4.4.10-10ubuntu4) ...
Selecting previously unselected package libc6-dev:amd64.
Preparing to unpack .../12-libc6-dev_2.31-0ubuntu9.2_amd64.deb ...
Unpacking libc6-dev:amd64 (2.31-0ubuntu9.2) ...
Selecting previously unselected package gcc-9-base:amd64.
Preparing to unpack .../13-gcc-9-base_9.3.0-17ubuntu120.04_amd64.deb ...
Unpacking gcc-9-base:amd64 (9.3.0-17ubuntu1
20.04) ...
Selecting previously unselected package libisl22:amd64.
Preparing to unpack .../14-libisl22_0.22.1-1_amd64.deb ...
Unpacking libisl22:amd64 (0.22.1-1) ...
Selecting previously unselected package libmpc3:amd64.
Preparing to unpack .../15-libmpc3_1.1.0-1_amd64.deb ...
Unpacking libmpc3:amd64 (1.1.0-1) ...
Selecting previously unselected package cpp-9.
Preparing to unpack .../16-cpp-9_9.3.0-17ubuntu120.04_amd64.deb ...
Unpacking cpp-9 (9.3.0-17ubuntu1
20.04) ...
Selecting previously unselected package cpp.
Preparing to unpack .../17-cpp_4%3a9.3.0-1ubuntu2_amd64.deb ...
Unpacking cpp (4:9.3.0-1ubuntu2) ...
Selecting previously unselected package libcc1-0:amd64.
Preparing to unpack .../18-libcc1-0_10.2.0-5ubuntu120.04_amd64.deb ...
Unpacking libcc1-0:amd64 (10.2.0-5ubuntu1
20.04) ...
Selecting previously unselected package libgomp1:amd64.
Preparing to unpack .../19-libgomp1_10.2.0-5ubuntu120.04_amd64.deb ...
Unpacking libgomp1:amd64 (10.2.0-5ubuntu1
20.04) ...
Selecting previously unselected package libitm1:amd64.
Preparing to unpack .../20-libitm1_10.2.0-5ubuntu120.04_amd64.deb ...
Unpacking libitm1:amd64 (10.2.0-5ubuntu1
20.04) ...
Selecting previously unselected package libatomic1:amd64.
Preparing to unpack .../21-libatomic1_10.2.0-5ubuntu120.04_amd64.deb ...
Unpacking libatomic1:amd64 (10.2.0-5ubuntu1
20.04) ...
Selecting previously unselected package libasan5:amd64.
Preparing to unpack .../22-libasan5_9.3.0-17ubuntu120.04_amd64.deb ...
Unpacking libasan5:amd64 (9.3.0-17ubuntu1
20.04) ...
Selecting previously unselected package liblsan0:amd64.
Preparing to unpack .../23-liblsan0_10.2.0-5ubuntu120.04_amd64.deb ...
Unpacking liblsan0:amd64 (10.2.0-5ubuntu1
20.04) ...
Selecting previously unselected package libtsan0:amd64.
Preparing to unpack .../24-libtsan0_10.2.0-5ubuntu120.04_amd64.deb ...
Unpacking libtsan0:amd64 (10.2.0-5ubuntu1
20.04) ...
Selecting previously unselected package libubsan1:amd64.
Preparing to unpack .../25-libubsan1_10.2.0-5ubuntu120.04_amd64.deb ...
Unpacking libubsan1:amd64 (10.2.0-5ubuntu1
20.04) ...
Selecting previously unselected package libquadmath0:amd64.
Preparing to unpack .../26-libquadmath0_10.2.0-5ubuntu120.04_amd64.deb ...
Unpacking libquadmath0:amd64 (10.2.0-5ubuntu1
20.04) ...
Selecting previously unselected package libgcc-9-dev:amd64.
Preparing to unpack .../27-libgcc-9-dev_9.3.0-17ubuntu120.04_amd64.deb ...
Unpacking libgcc-9-dev:amd64 (9.3.0-17ubuntu1
20.04) ...
Selecting previously unselected package gcc-9.
Preparing to unpack .../28-gcc-9_9.3.0-17ubuntu120.04_amd64.deb ...
Unpacking gcc-9 (9.3.0-17ubuntu1
20.04) ...
Selecting previously unselected package gcc.
Preparing to unpack .../29-gcc_4%3a9.3.0-1ubuntu2_amd64.deb ...
Unpacking gcc (4:9.3.0-1ubuntu2) ...
Selecting previously unselected package libstdc++-9-dev:amd64.
Preparing to unpack .../30-libstdc++-9-dev_9.3.0-17ubuntu120.04_amd64.deb ...
Unpacking libstdc++-9-dev:amd64 (9.3.0-17ubuntu1
20.04) ...
Selecting previously unselected package g++-9.
Preparing to unpack .../31-g++-9_9.3.0-17ubuntu120.04_amd64.deb ...
Unpacking g++-9 (9.3.0-17ubuntu1
20.04) ...
Selecting previously unselected package g++.
Preparing to unpack .../32-g++_4%3a9.3.0-1ubuntu2_amd64.deb ...
Unpacking g++ (4:9.3.0-1ubuntu2) ...
Selecting previously unselected package make.
Preparing to unpack .../33-make_4.2.1-1.2_amd64.deb ...
Unpacking make (4.2.1-1.2) ...
Selecting previously unselected package libdpkg-perl.
Preparing to unpack .../34-libdpkg-perl_1.19.7ubuntu3_all.deb ...
Unpacking libdpkg-perl (1.19.7ubuntu3) ...
Selecting previously unselected package dpkg-dev.
Preparing to unpack .../35-dpkg-dev_1.19.7ubuntu3_all.deb ...
Unpacking dpkg-dev (1.19.7ubuntu3) ...
Selecting previously unselected package build-essential.
Preparing to unpack .../36-build-essential_12.8ubuntu1.1_amd64.deb ...
Unpacking build-essential (12.8ubuntu1.1) ...
Selecting previously unselected package libfakeroot:amd64.
Preparing to unpack .../37-libfakeroot_1.24-1_amd64.deb ...
Unpacking libfakeroot:amd64 (1.24-1) ...
Selecting previously unselected package fakeroot.
Preparing to unpack .../38-fakeroot_1.24-1_amd64.deb ...
Unpacking fakeroot (1.24-1) ...
Selecting previously unselected package libalgorithm-diff-perl.
Preparing to unpack .../39-libalgorithm-diff-perl_1.19.03-2_all.deb ...
Unpacking libalgorithm-diff-perl (1.19.03-2) ...
Selecting previously unselected package libalgorithm-diff-xs-perl.
Preparing to unpack .../40-libalgorithm-diff-xs-perl_0.04-6_amd64.deb ...
Unpacking libalgorithm-diff-xs-perl (0.04-6) ...
Selecting previously unselected package libalgorithm-merge-perl.
Preparing to unpack .../41-libalgorithm-merge-perl_0.08-3_all.deb ...
Unpacking libalgorithm-merge-perl (0.08-3) ...
Selecting previously unselected package zlib1g-dev:amd64.
Preparing to unpack .../42-zlib1g-dev_1%3a1.2.11.dfsg-2ubuntu1.2_amd64.deb ...
Unpacking zlib1g-dev:amd64 (1:1.2.11.dfsg-2ubuntu1.2) ...
Selecting previously unselected package libelf-dev:amd64.
Preparing to unpack .../43-libelf-dev_0.176-1.1build1_amd64.deb ...
Unpacking libelf-dev:amd64 (0.176-1.1build1) ...
Selecting previously unselected package libfile-fcntllock-perl.
Preparing to unpack .../44-libfile-fcntllock-perl_0.22-3build4_amd64.deb ...
Unpacking libfile-fcntllock-perl (0.22-3build4) ...
Selecting previously unselected package libfl-dev:amd64.
Preparing to unpack .../45-libfl-dev_2.6.4-6.2_amd64.deb ...
Unpacking libfl-dev:amd64 (2.6.4-6.2) ...
Selecting previously unselected package libncurses-dev:amd64.
Preparing to unpack .../46-libncurses-dev_6.2-0ubuntu2_amd64.deb ...
Unpacking libncurses-dev:amd64 (6.2-0ubuntu2) ...
Selecting previously unselected package libssl-dev:amd64.
Preparing to unpack .../47-libssl-dev_1.1.1f-1ubuntu2.3_amd64.deb ...
Unpacking libssl-dev:amd64 (1.1.1f-1ubuntu2.3) ...
Selecting previously unselected package manpages-dev.
Preparing to unpack .../48-manpages-dev_5.05-1_all.deb ...
Unpacking manpages-dev (5.05-1) ...
Setting up manpages-dev (5.05-1) ...
Setting up libfile-fcntllock-perl (0.22-3build4) ...
Setting up libalgorithm-diff-perl (1.19.03-2) ...
Setting up binutils-common:amd64 (2.34-6ubuntu1.1) ...
Setting up linux-libc-dev:amd64 (5.4.0-73.82) ...
Setting up libctf-nobfd0:amd64 (2.34-6ubuntu1.1) ...
Setting up m4 (1.4.18-4) ...
Setting up libgomp1:amd64 (10.2.0-5ubuntu120.04) ...
Setting up libfakeroot:amd64 (1.24-1) ...
Setting up fakeroot (1.24-1) ...
update-alternatives: using /usr/bin/fakeroot-sysv to provide /usr/bin/fakeroot (fakeroot) in auto mode
Setting up make (4.2.1-1.2) ...
Setting up libquadmath0:amd64 (10.2.0-5ubuntu1
20.04) ...
Setting up libssl-dev:amd64 (1.1.1f-1ubuntu2.3) ...
Setting up libmpc3:amd64 (1.1.0-1) ...
Setting up libatomic1:amd64 (10.2.0-5ubuntu120.04) ...
Setting up libdpkg-perl (1.19.7ubuntu3) ...
Setting up libubsan1:amd64 (10.2.0-5ubuntu1
20.04) ...
Setting up libcrypt-dev:amd64 (1:4.4.10-10ubuntu4) ...
Setting up bison (2:3.5.1+dfsg-1) ...
update-alternatives: using /usr/bin/bison.yacc to provide /usr/bin/yacc (yacc) in auto mode
Setting up libisl22:amd64 (0.22.1-1) ...
Setting up libbinutils:amd64 (2.34-6ubuntu1.1) ...
Setting up libc-dev-bin (2.31-0ubuntu9.2) ...
Setting up libalgorithm-diff-xs-perl (0.04-6) ...
Setting up libcc1-0:amd64 (10.2.0-5ubuntu120.04) ...
Setting up liblsan0:amd64 (10.2.0-5ubuntu1
20.04) ...
Setting up libitm1:amd64 (10.2.0-5ubuntu120.04) ...
Setting up gcc-9-base:amd64 (9.3.0-17ubuntu1
20.04) ...
Setting up libalgorithm-merge-perl (0.08-3) ...
Setting up libtsan0:amd64 (10.2.0-5ubuntu120.04) ...
Setting up libctf0:amd64 (2.34-6ubuntu1.1) ...
Setting up flex (2.6.4-6.2) ...
Setting up libfl-dev:amd64 (2.6.4-6.2) ...
Setting up libasan5:amd64 (9.3.0-17ubuntu1
20.04) ...
Setting up cpp-9 (9.3.0-17ubuntu120.04) ...
Setting up libc6-dev:amd64 (2.31-0ubuntu9.2) ...
Setting up binutils-x86-64-linux-gnu (2.34-6ubuntu1.1) ...
Setting up libncurses-dev:amd64 (6.2-0ubuntu2) ...
Setting up binutils (2.34-6ubuntu1.1) ...
Setting up dpkg-dev (1.19.7ubuntu3) ...
Setting up libgcc-9-dev:amd64 (9.3.0-17ubuntu1
20.04) ...
Setting up zlib1g-dev:amd64 (1:1.2.11.dfsg-2ubuntu1.2) ...
Setting up cpp (4:9.3.0-1ubuntu2) ...
Setting up gcc-9 (9.3.0-17ubuntu120.04) ...
Setting up libstdc++-9-dev:amd64 (9.3.0-17ubuntu1
20.04) ...
Setting up gcc (4:9.3.0-1ubuntu2) ...
Setting up libelf-dev:amd64 (0.176-1.1build1) ...
Setting up g++-9 (9.3.0-17ubuntu120.04) ...
Setting up g++ (4:9.3.0-1ubuntu2) ...
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode
Setting up build-essential (12.8ubuntu1.1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for install-info (6.7.0.dfsg.2-5) ...
xxxxxxx@NUCCANYON-8:
/WSL2-Linux-Kernel$ cp Microsoft/config-wsl .config
xxxxxxx@NUCCANYON-8:/WSL2-Linux-Kernel$ export KERNELRELEASE=4.19.84-microsoft-standard
xxxxxxx@NUCCANYON-8:
/WSL2-Linux-Kernel$ uname -a
Linux NUCCANYON-8 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Is fine to continue with version 5.1 is indicated above?

@msftnadavbh
Copy link

Is there a solution for Ubuntu 20?

@ahmedalhulaibi
Copy link

Just adding my experience here with ubuntu 20 and wsl kernel version 5.10.16.3-microsoft-standard

  1. Add the kernel flags specified in https://github.com/iovisor/bcc/blob/master/INSTALL.md#kernel-configuration to .config

Followed as instructed, and modified these config values

CONFIG_BPF=y
CONFIG_BPF_SYSCALL=y
# [optional, for tc filters]
CONFIG_NET_CLS_BPF=m
# [optional, for tc actions]
CONFIG_NET_ACT_BPF=m
CONFIG_BPF_JIT=y
# [for Linux kernel versions 4.1 through 4.6]
CONFIG_HAVE_BPF_JIT=y
# [for Linux kernel versions 4.7 and later]
CONFIG_HAVE_EBPF_JIT=y
# [optional, for kprobes]
CONFIG_BPF_EVENTS=y
# Need kernel headers through /sys/kernel/kheaders.tar.xz
CONFIG_IKHEADERS=y
CONFIG_NET_SCH_SFQ=m
CONFIG_NET_ACT_POLICE=m
CONFIG_NET_ACT_GACT=m
CONFIG_DUMMY=m
CONFIG_VXLAN=m
  1. make KERNELRELEASE=$KERNELRELEASE -j 4 I ran into this error:
BTF: .tmp_vmlinux.btf: pahole (pahole) is not available
Failed to generate BTF for vmlinux
Try to disable CONFIG_DEBUG_INFO_BTF
make: *** [Makefile:1179: vmlinux] Error 1

Solution is to install dwarves sudo apt install dwarves and repeat step 7. make KERNELRELEASE=$KERNELRELEASE -j 4

Otherwise no other issues!

@StephanDollberg
Copy link

StephanDollberg commented Mar 14, 2022

This actually working for you? BCC tools don't seem to work for me as kernel modules are missing (I am on preview WSL).

@baoqger
Copy link

baoqger commented Apr 18, 2022

Just adding my experience here with ubuntu 20 and wsl kernel version 5.10.16.3-microsoft-standard

  1. Add the kernel flags specified in https://github.com/iovisor/bcc/blob/master/INSTALL.md#kernel-configuration to .config

Followed as instructed, and modified these config values

CONFIG_BPF=y
CONFIG_BPF_SYSCALL=y
# [optional, for tc filters]
CONFIG_NET_CLS_BPF=m
# [optional, for tc actions]
CONFIG_NET_ACT_BPF=m
CONFIG_BPF_JIT=y
# [for Linux kernel versions 4.1 through 4.6]
CONFIG_HAVE_BPF_JIT=y
# [for Linux kernel versions 4.7 and later]
CONFIG_HAVE_EBPF_JIT=y
# [optional, for kprobes]
CONFIG_BPF_EVENTS=y
# Need kernel headers through /sys/kernel/kheaders.tar.xz
CONFIG_IKHEADERS=y
CONFIG_NET_SCH_SFQ=m
CONFIG_NET_ACT_POLICE=m
CONFIG_NET_ACT_GACT=m
CONFIG_DUMMY=m
CONFIG_VXLAN=m
  1. make KERNELRELEASE=$KERNELRELEASE -j 4 I ran into this error:
BTF: .tmp_vmlinux.btf: pahole (pahole) is not available
Failed to generate BTF for vmlinux
Try to disable CONFIG_DEBUG_INFO_BTF
make: *** [Makefile:1179: vmlinux] Error 1

Solution is to install dwarves sudo apt install dwarves and repeat step 7. make KERNELRELEASE=$KERNELRELEASE -j 4

Otherwise no other issues!

I build a customer wsl2 kernel with EBPF config added. And use it as wsl2 kernel.

But when i tried to run ebp program in samples/bpf (inside linux kernel), still get error message as following:

baoqger:/usr/src/linux-5.10.16/samples/bpf$ sudo ./cpustat
[sudo] password for baoqger:
libbpf: elf: skipping unrecognized data section(22) .eh_frame
libbpf: elf: skipping relo section(23) .rel.eh_frame for section(22) .eh_frame
libbpf: failed to determine tracepoint 'power/cpu_idle' perf event ID: No such file or directory
libbpf: prog 'bpf_prog1': failed to create tracepoint 'power/cpu_idle' perf event: No such file or directory
ERROR: bpf_program__attach failed

@baoqger
Copy link

baoqger commented Apr 18, 2022

Another thing need mention is: in the latest version of WSL2-Linux-Kernel, most of the EBPF flag required in this link: https://github.com/iovisor/bcc/blob/master/INSTALL.md#kernel-configuration is enabled by default. The only one need your setting is:

CONFIG_IKHEADERS=y

@baoqger
Copy link

baoqger commented Apr 18, 2022

Can anyone give one hello-world demo to verify that bpf program can run on WSL2? In this way, we can troubleshoot that the issue is in bpf program aspect or wsl2 aspect?

@narimantos
Copy link

narimantos commented Apr 23, 2022

@baoqger here is a hello-world i used, and it worked.


#!/usr/bin/python3
from bcc import BPF
from time import sleep

program = """
BPF_HASH(clones);

int hello_world(void *ctx) {
    u64 uid;
    u64 counter = 0;
    u64 *p;

    uid = bpf_get_current_uid_gid() & 0xFFFFFFFF;
    p = clones.lookup(&uid);
    if (p != 0){
        counter = *p;
    }

    counter++;
    clones.update(&uid, &counter);

    return 0;
}
"""

b = BPF(text=program)
clone = b.get_syscall_fnname("clone")
b.attach_kprobe(event=clone, fn_name="hello_world")
b.trace_print()

sleep(1000)

while True:
    sleep(2)
    s = ""
    if len(b["clones"].items()):
        for k,v in b["clones"].items():
            s += "ID {}: {}\t".format(k.value, v.value)
        print(s)
    else:
        print("No entries yet")

@MetaT1an
Copy link

I still encountered such problem after rebuilding the kernel.

modprobe: FATAL: Module kheaders not found in directory /lib/modules/5.10.102.1-microsoft-standard-WSL2

@satanshiro
Copy link

I still encountered such problem after rebuilding the kernel.

modprobe: FATAL: Module kheaders not found in directory /lib/modules/5.10.102.1-microsoft-standard-WSL2

I have exact same issue also did what they said to do here https://gist.github.com/cerebrate/d40c89d3fa89594e1b1538b2ce9d2720
and I get the same error of modules not found.
tried configuring CONFIG_IKHEADERS=m and also CONFIG_IKHEADERS=y both didn't work
just running make, or the commands listed in steps 7-9 also did not change anything.
I will note that in all trials I did not see anything under /lib/modules/$(uname -r) per step 10
step 11 I do not understand doesn't seem to make a difference either.
also I am sure I am running the custom built kernel as I changed CONFIG_LOCALVERSION to make sure.

 Unable to find kernel headers. Try rebuilding kernel with CONFIG_IKHEADERS=m (module) or installing the kernel development package for your running kernel version.
chdir(/lib/modules/5.10.102.1-microsoft-ebpf-WSL2+/build): No such file or directory

@MarioHewardt
Copy link
Author

This gist was written quite a while ago. If you are still having problems enabling eBPF on WSL2 I would recommend filing an issue in their repo. If I end up trying it again in the future I'll update this gist with any changes that may be needed.

@jerviscui
Copy link

Can anyone give one hello-world demo to verify that bpf program can run on WSL2? In this way, we can troubleshoot that the issue is in bpf program aspect or wsl2 aspect?

Hi @baoqger,
I have a mistake:

FAILED: load BTF from vmlinux: Unknown error -2make: *** [Makefile:1179: vmlinux] Error 255

Has that ever happened to you?

@benjaminbuzek
Copy link

Thank you for the nice and well structured guide, worked without any issues for me. 😃

@cheneytianx
Copy link

cheneytianx commented Aug 16, 2022

Hi, @MetaT1an @satanshiro,

Have you tried using the following command

export KERNELRELEASE=$(uname -r)

to replace the original one in https://gist.github.com/MarioHewardt/5759641727aae880b29c8f715ba4d30f#file-enable_ebpf_on_wsl2-L15

For me, I can see the new content in /lib/modules/$(uname -r) after rebuilding the kernel.

The kernel KERNELRELEASE should have -WSL2 as the suffix.

@aminmiri
Copy link

@baoqger here is a hello-world i used, and it worked.


#!/usr/bin/python3
from bcc import BPF
from time import sleep

program = """
BPF_HASH(clones);

int hello_world(void *ctx) {
    u64 uid;
    u64 counter = 0;
    u64 *p;

    uid = bpf_get_current_uid_gid() & 0xFFFFFFFF;
    p = clones.lookup(&uid);
    if (p != 0){
        counter = *p;
    }

    counter++;
    clones.update(&uid, &counter);

    return 0;
}
"""

b = BPF(text=program)
clone = b.get_syscall_fnname("clone")
b.attach_kprobe(event=clone, fn_name="hello_world")
b.trace_print()

sleep(1000)

while True:
    sleep(2)
    s = ""
    if len(b["clones"].items()):
        for k,v in b["clones"].items():
            s += "ID {}: {}\t".format(k.value, v.value)
        print(s)
    else:
        print("No entries yet")

Here is a simpler one:

#!/usr/bin/python3  
from bcc import BPF

program = r"""
int hello(void *ctx) {
    bpf_trace_printk("Hello World!");
    return 0;
}
"""

b = BPF(text=program)
syscall = b.get_syscall_fnname("execve")
b.attach_kprobe(event=syscall, fn_name="hello")

b.trace_print()

then open a second terminal and run any linux command (ls, echo...etc) and you should see in the trace output Hello World

@Joly0
Copy link

Joly0 commented Oct 15, 2023

Can anyone give one hello-world demo to verify that bpf program can run on WSL2? In this way, we can troubleshoot that the issue is in bpf program aspect or wsl2 aspect?

Hi @baoqger, I have a mistake:

FAILED: load BTF from vmlinux: Unknown error -2make: *** [Makefile:1179: vmlinux] Error 255

Has that ever happened to you?

Hey, have you ever found a solution to this? I am having the same issue

@gambitaw
Copy link

Can anyone give one hello-world demo to verify that bpf program can run on WSL2? In this way, we can troubleshoot that the issue is in bpf program aspect or wsl2 aspect?

Hi @baoqger, I have a mistake:

FAILED: load BTF from vmlinux: Unknown error -2make: *** [Makefile:1179: vmlinux] Error 255

Has that ever happened to you?

Hey, habe you ever found a solution to this? I am having the same issue

Same :(

@NexViolentus
Copy link

I have been trying to unfuck this for several days. This is a tough son of a bitch to solve. I have followed several guides and still FAILED: load BTF from vmlinux: Unknown error -22make: *** [Makefile:1179: vmlinux] Error 255

@deadash
Copy link

deadash commented Jan 26, 2024

I successfully launched eBPF following the guide at https://learn.microsoft.com/en-us/community/content/wsl-user-msft-kernel-v6. However, I used Debian which required the installation of the following packages:

sudo apt install git python3 bc dwarves rsync

Additionally, to run the example Python code provided above, I had to install:

sudo apt install bpfcc-tools python3-bpfcc

Running it initially failed, but I managed to fix it by mounting debugfs with the command:

sudo mount -t debugfs debugfs /sys/kernel/debug

Finally, after opening a new Debian terminal, it printed the following:

b'           <...>-275     [004] d...1    91.948441: bpf_trace_printk: Hello World!'
b'            bash-276     [007] d...1    91.950385: bpf_trace_printk: Hello World!'
b'           <...>-277     [007] d...1    94.875099: bpf_trace_printk: Hello World!'

@Slach
Copy link

Slach commented Jan 26, 2024

@deadash thank you!

@NexViolentus
Copy link

It finally worked even after I tried several guides and got errors every time. I typed usbip and the menu appeared. I got my device to forward to the VM but I cant connect it to WSL1. I can only get WSL1 to work on the VM so im thinking about trying this with rasberrypi instead

@WCY-dt
Copy link

WCY-dt commented Mar 27, 2024

Hi, @MetaT1an @satanshiro,

Have you tried using the following command

export KERNELRELEASE=$(uname -r)

to replace the original one in https://gist.github.com/MarioHewardt/5759641727aae880b29c8f715ba4d30f#file-enable_ebpf_on_wsl2-L15

For me, I can see the new content in /lib/modules/$(uname -r) after rebuilding the kernel.

The kernel KERNELRELEASE should have -WSL2 as the suffix.

Thank you! It works!

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