8

I'm trying to follow this tutorial to run KVM nested in WSL 2, but after following the directions through compiling the kernel and loading it, the test kvm-ok reports that KVM is not supported.

I've tried multiple options: enabling hyper-v, disabling it, verifying BIOS/CPU support, enabling/disabling Device Guard, other steps in some vmware docs (bcdedit /set hypervisorlaunchtype, suboptions under gpedit/DeviceGuard).

When I do disable hyper-v, set hypervisorlaunchtype off, and disable Device Guard, then I get the following options in system info, but then wsl2 fails to launch at all, saying "Please enable the Virtual Machine Platform Windows feature and ensure virtualization is enabled in the BIOS."

system info enter image description here Any other configuration I try, sys info says that a hypervisor is running and won't show the hyper-v values, but I end up running into the initial problem of KVM not being supported in WSL2. Also if I try enabling "Virtualize Intel VT-x/EPT" in VMWare Player, that fails to launch as well.

Stepping back, ultimately what I'm trying to do is run the firecracker demo on my windows laptop. So if there's an easier way to do this then lmk.

Update

I noticed it works in VMWare Player with an Ubuntu image. Or at least, kvm-ok reports that it's enabled, /dev/kvm exists. I'd still rather use WSL2. But it's refusing to load.

5 Answers 5

10

On WSL2 (Windows 11), nested virtualization is supported but not enabled by default. To enable it, you must:

  1. Add yourself to the kvm group
  2. Change the default group of /dev/kvm
  3. Enable nested virtualization in /etc/wsl.conf
  4. Restart WSL

1. Adding yourself to the kvm group:

This one is easy:

sudo usermod -a -G kvm ${USER}

2. Change the default group of /dev/kvm

This is also easy, but to make it stick across reboots and upgrades, add this section to your /etc/wsl.conf file:

[boot]
command = /bin/bash -c 'chown -v root:kvm /dev/kvm && chmod 660 /dev/kvm'

3. Enable nested virtualization

You don’t need to recompile your WSL distribution to enable nested virtualization, just add this section to your /etc/wsl.conf:

[wsl2]
nestedVirtualization=true

As @athys mentionned, if you prefer you can also enable nested virtualization globally by using the .wlsconfig file in Windows. YMMV depending on your specific global solution <--> local problem dilemma.

4. Restart WSL

You can either restart Windows, or close all of your WSL terminal windows and issue this command in Powershell, CMD, or Windows Run menu (Windows+R)

wsl.exe --shutdown

The next time you open a terminal, WSL will start with the new options and nested virtualization will work. More details about enabling nested virtualization can be found here.

3
  • 1
    Commenting that it works nicely by this date Aug 30, 2023 at 20:58
  • So rebuilding the kernel like what is described here (boxofcables.dev/kvm-optimized-custom-kernel-wsl2-2022) is not necessary ? It gives us no additional performance advantage inside the nested VM ?
    – bdutta74
    Feb 22 at 7:13
  • It is not necessary to rebuild the kernel. I use the default Ubuntu distribution. I guess you could get better performance by rebuilding, carefully selecting flags to match your architecture, but unless you really need that edge, I would dismiss that effort as premature optimisation.
    – ixe013
    Feb 22 at 19:08
2

Top line of the screenshot, and first line of the linked article. It requires an insider dev build of Windows. Mine was running the GA version. Enrolling in the insider program and downloading latest worked.

All the settings in the screenshots are correct, and it should be bcdedit /set hypervisorlaunchtype auto.

I got 400 KVM VM's launched from WSL on my laptop in under 60s! (First 200 in under 15s).

Also note I got KVM working in VMWare Player, but the firecracker test never quite worked. It launched the VM's but they didn't do anything. Apparently VMWare is type-2 and would be slower even if it does work, so I gave up on that approach.

1

I have taken the same steps as you and got

[    0.330695] kvm: no hardware support
[    0.330696] kvm: no hardware support

in dmesg Hyper-V is enabled and in BIOS Virtualization Technology and VT-d Features are enabled.

After I enabled Windows Hypervisor Platform I finally had

[    0.193019] KVM: vmx: using Hyper-V Enlightened VMCS
[    0.195115] kvm: already loaded the other module

In a lot of guides I read WSL requires Virtual Machine Platform. But that alone wasn't sufficient for me. For some reason it only worked when I ticked Hyper-V, Virtual Machine Platform and Windows Hypervisor Platform.

1

Correction Part 3 of ixe013's Answer

As per the provided source:

The wsl.conf file is used to configure the local settings of a wsl distribution and cannot use the [wsl2] global section label.

For this purpose, .wslconfig file has to be used.

As .wslconfig holds the Global WSL settings. In this file you can use the [wsl2] label with the aforementioned nestedVirtualization key, however, said key already has a default value of true.

TL;DR: Step 3 is useless as nestedVirtualization is already enabled by default.

Source: Microsoft's wsl config documentation

1
  • The .wlsconfig file does not exist by default, so its nestedVirtualization flag is not considered. The /etc/wsl2.conf file does not have that same default. But thanks, I'll edit my answer to mention that it is possible to configure it globally.
    – ixe013
    Feb 22 at 19:16
0

You can try the following guide to enable WSL2 nested virtualization in win10 pro/enterprise (note the disclaimer, you should maybe backup your distros with wsl --export):

https://gist.github.com/xtrnc/496da9243bab29629bc0461562bff458

It worked on my win10 (build 19043.1348). You probably won't need to compile the kernel if you've updated Windows (which is how the Linux kernel is updated now).

If you can upgrade your OS, you can also try that. It should be enabled by default in win11 (along with WSLg).

Note: if you follow the guide it will be necessary to run the script at every restart; also it's necessary to add your user to the kvm group for permissions (on ubuntu usermod -a -G kvm <USER>)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .