Skip to content

Instantly share code, notes, and snippets.

@jacky9813
Last active March 31, 2024 09:19
Show Gist options
  • Save jacky9813/927261020bb1dacc1a7baedef657b732 to your computer and use it in GitHub Desktop.
Save jacky9813/927261020bb1dacc1a7baedef657b732 to your computer and use it in GitHub Desktop.
Running LXC VM in WSL2

Running LXC/LXD VM in WSL2 (Windows 11 Only)

If you just running LXC/LXD containers, the default settings for WSL2 (both Windows 10 and Windows 11) should be adequate.

However, if trying something like lxc launch images:alpine/3.18 --vm, LXC will complain that vhost_vsock module is not loaded.

Here's how I configure WSL2 so I can run VM with LXC/LXD.

Unfrotunate news for Windows 10 users

LXC/LXD uses QEMU under the hood with kvm as requirement for virtual machines.

Windows 10 does not support nested virtualization for WSL2. Setting nestedVirtualization=true in .wslconfig will report wsl: Nested virtualization is not supported on this machine.

This article will only work on Windows 11 or later machines.

That being said, Windows 10 can still run LXC/LXD container without kernel modification.

Step 1: Prepare kernel compile environment

I'm using Debian 11. Ubuntu or any Debian/Ubuntu based WSL image should have similar dependencies.

sudo apt update
sudo apt install git bc build-essential flex bison libssl-dev libelf-dev dwarves

Step 2: Get source code of current running kernel

In theory, using newer kernel with the same major version should work fine. But I don't want to take my time to verify whole kernel feature for just a minor change.

I'll use the same kernel version as what Microsoft came with. uname -r gives me 5.15.120-microsoft-standard-WSL2. The kernel version to use here will be 5.15.120.

Now download kernel source from kernel.org:

wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.120.tar.xz
tar xf linux-5.15.120.tar.xz

Step 3: Get original configuration file for WSL2 kernel

pushd linux-5.15.120
wget -O arch/x86/configs/config-wsl https://raw.githubusercontent.com/microsoft/WSL2-Linux-Kernel/linux-msft-wsl-5.15.y/arch/x86/configs/config-wsl
# Create a duplicate to modify
cp arch/x86/configs/config-wsl arch/x86/configs/config-wsl-custom
popd

Step 4: Modify compile configuration

To prevent human error, it is highly suggested use menuconfig to modify the kernel compile configuration.

pushd linux-5.15.120
make KCONFIG_CONFIG=arch/x86/configs/config-wsl-custom menuconfig
popd

Inside the config UI:

  • Use <Up> and <Down> key to navigate through options.
  • Use <Left> and <Right> key to select action at the bottom when <Enter> key is pressed.
  • Use <Space> to change the compile mode.
  • An option with ---> indicates it has submenu, choose Select action and press <Enter> key to get inside the submenu.

The target is to compile vhost_vsock as an kernel module, which should be marked as <M>.

The compile option is at Device Drivers -> [*] VHOST drivers -> <M> vhost virtio-vsock driver.

After vhost virtio-vsock driver is marked as compile as module <M>, save the config file.

Optional Step: Change the version name suffix

To make the changes being obvious, you can change the kernel version suffix in General Setup -> Local version

I'm changing it to -microsoft-custom-WSL2 so I can verify the corrent kernel has been loaded.

Step 5: Compile and install the kernel and its modules

pushd linux-5.15.120
make KCONFIG_CONFIG=arch/x86/configs/config-wsl-custom -j$(nproc)
sudo make KCONFIG_CONFIG=arch/x86/configs/config-wsl-custom modules_install
mkdir -p /mnt/c/Users/user/wsl2-kernel
cp arch/x86/boot/bzImage /mnt/c/Users/user/wsl2-kernel/bzImage-5.15.120-WSL2-custom

# WSL2 seems not to load the module without some easy configuration.
echo vhost_vsock | sudo tee -a /etc/modules

# If you already have .wslconfig setup, the following lines will remove what has been done.
# Modify the file manually if required.
cat << EOF > /mnt/c/Users/user/.wslconfig
[wsl2]
kernel=C:\\\\Users\\\\user\\\\wsl2-kernel\\\\bzImage-5.15.120-WSL2-custom
EOF

popd

Step 6: Restart WSL2

wsl --shutdown
wsl

At this moment, uname -r should give us 5.15.120-microsoft-custom-WSL2.

If vhost_vsock not in the result of lsmod, try load the module manually:

sudo modprobe vhost_vsock

If vhost_vsock is still not loaded, the system may have some other issue.

I'm not sure if LXC needs virtualization support or not, as Windows 11 already support this feature and I've seen /dev/kvm since upgraded to Windows 11.

@jacky9813
Copy link
Author

Last time I checked my WSL2 disk size, which is well over 100 GB.

I've moved over to Fedora 39 for my new laptop for work. (I still have a Windows 11 desktop for gaming).

Previously I have to run Optimize-VHD once in a while to shrink WSL disk.

But it seems like there's some great news for WSL2 September 2023 release that support auto shrink disk using sparse disk.

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