← All TILs · linux

Lire en français traduction automatique Google Traduire sur votre ordinateur Firefox

Loadable modules for a custom WSL2 kernel: a `modules.vhdx` and one `.wslconfig` line

linux - 2026-11-10

A custom WSL2 kernel built as in Building a custom WSL2 kernel on GitHub Actions boots fine, but every feature its config marks =m is missing. modprobe wireguard answers Module wireguard not found in directory /lib/modules/6.18.54.1-wsl2-x86_64-v3+. Microsoft's config-wsl has 744 such options, among them the ones Kubernetes, VMs and VPNs need.

Some terms first:

A WSL2 VM runs one kernel for every distro. The modules have to be there for that exact build in each distro, without writing into each distro's /lib/modules.

Two ways out

Everything =y Modules in a VHDX
Change to the build sed -i 's/=m$/=y/' .config before make olddefconfig make modules_install, then package
Size (x86-64-v3) bzImage 33 MB (6.18.54.1), 0 options left =m bzImage 16 MB + modules.vhdx 143 MB (6.18.58.1, 748 modules, debug info stripped)
.wslconfig kernel= only kernel= and kernelModules=
Loaded at boot all 2,591 options, used or not only what something asks for
Must match nothing the VHDX belongs to one build: same uname -r, same compile

Both work for every distro at once. The rest of this entry is the VHDX route.

How WSL uses the VHDX

kernelModules= in the [wsl2] section of %UserProfile%\.wslconfig takes "an absolute Windows path to a custom Linux kernel modules VHD" (wsl-config). WSL's own init (src/linux/init/main.cpp in microsoft/WSL) then:

On the Windows host, .wslconfig points kernel= to a bzImage and kernelModules= to modules.vhdx. In the WSL2 virtual machine, the bzImage boots as the one Linux kernel, and the VHDX is attached and mounted read-only at /modules. Each distro, here Ubuntu 26.04 and NixOS, gets an overlay on /lib/modules/release: a writable upper layer in the distro over the read-only modules from /modules. A module loaded with modprobe in Ubuntu shows up in lsmod in NixOS, because both share the kernel.
One .wslconfig line attaches the VHDX to the VM (as a SCSI disk). WSL mounts it once, then gives every distro an overlay on top of it.

The workflow

Microsoft's repository ships the packaging script: Microsoft/scripts/gen_artifacts_vhdx.sh <modules dir> <headers dir> <perf dir> <kernelrelease> <output>. Older guides call it gen_modules_vhdx.sh; it was renamed on 2026-07-31, when it started bundling headers and perf. It uses mke2fs -d (e2fsprogs) to build an ext4 image and qemu-img convert -O vhdx (qemu-utils) to convert it.

Added to the build of the previous entry (full file: build-wsl2-kernel-modules.yml on the modules-vhdx branch of abdelhousni/WSL2-Linux-Kernel):

      - run: sudo apt-get install -y qemu-utils e2fsprogs kmod cpio

      # after: make -j"$(nproc)" KCFLAGS="-march=x86-64-v3"   (no target: kernel + modules)
      - name: Package modules.vhdx
        run: |
          KREL=$(make -s kernelrelease)
          make INSTALL_MOD_PATH="$PWD/out/modules" INSTALL_MOD_STRIP=1 modules_install
          make headers_install INSTALL_HDR_PATH="$PWD/out/headers"
          mkdir -p out/perf     # left empty, see below
          ./Microsoft/scripts/gen_artifacts_vhdx.sh \
            out/modules out/headers out/perf "$KREL" modules.vhdx

Upload arch/x86/boot/bzImage and modules.vhdx from the same run: a VHDX from one build doesn't fit a bzImage from another, even from the same source.

The perf directory stays empty on purpose. WSL 3.0.5 removed its bundled perf integration (microsoft/WSL#41904), but the script still requires the argument.

Using it

[wsl2]
kernel=G:\\wsl\\kernels\\20261110b-bzImage-wsl2-x86_64-v3
kernelModules=G:\\wsl\\kernels\\20261110b-modules-wsl2-x86_64-v3.vhdx
# optional: load these at every boot
# loadKernelModules=wireguard,kvm_amd

Then wsl --shutdown (it stops every distro) and, in any distro:

$ uname -r
6.18.58.1-wsl2-x86_64-v3+
$ mount | grep /lib/modules
none on /usr/lib/modules/6.18.58.1-wsl2-x86_64-v3+ type overlay (rw,nosuid,nodev,noatime,lowerdir=/modules/6.18.58.1-wsl2-x86_64-v3+/modules,upperdir=/lib/modules/6.18.58.1-wsl2-x86_64-v3+/rw/upper,workdir=/lib/modules/6.18.58.1-wsl2-x86_64-v3+/rw/work,uuid=on)
$ lsmod | grep br_netfilter      # loaded by WSL at boot
br_netfilter           32768  0
$ sudo modprobe -a wireguard kvm_amd vhost_net ip_vs macvlan ipvlan nfs cifs nft_ct erofs && echo ok
ok
$ ls -l /dev/kvm; cat /proc/sys/net/bridge/bridge-nf-call-iptables
crw-rw---- 1 root kvm 10, 232 Nov 10 09:18 /dev/kvm
1

Use kvm_intel on an Intel CPU. On a Ryzen 7 5700X, kvm_amd loads and /dev/kvm appears.

Pitfalls

Sources

The build ran on a GitHub-hosted ubuntu-latest runner (about 35 minutes, 32 of them compiling); the boot and modprobe checks ran in WSL 3.0.5 on Windows 11, in Ubuntu 26.04 and NixOS.

Created 2026-11-10T10:41:01+01:00, updated 2026-11-10T23:19:35+01:00 · History · Edit