HomeTechnologyLinuxNested Virtualization with WSL2...

Nested Virtualization with WSL2 – Linux Magazine

WSL is super handy, versatile and in full development. There are two flavors of WSL; WSL-1 which translates System Calls intended for the Linux Kernel into WinNT Kernel System Calls (say WINE, but the other way around) and WSL2 which uses a so-called Lightweight Utility VM. (Henk den Hartog)

The Lightweight Utility VM offers many possibilities and a nice one of these is Nested Virtualization, or the ability to run Virtual Machines in a Virtual Machine (the Lightweight Utility VM in the case of WSL2).

This is one of the new features that will be introduced in the upcoming update of WSL2, but with some skill you can already play with it based on the current version of WSL2. In this article you will install KVM on an Ubuntu WSL-2 distribution. The method has been tested on hardware with an Intel processor (support for AMD will follow later) with the following Windows10 releases:

Windows Professional 10.0.19042 N/A Build 19042

Windows Enterprise 10.0.19042 N/A Build 19042

Windows Home 10.0.19042 Build 19042

Note that the installation of WSL-2 falls outside the scope of this article, but fortunately you can find a clear howto on the Microsoft site:

https://docs.microsoft.com/en-us/windows/wsl/install-win10

Third party virtualization software

Before you start it is good to realize that WSL-2 uses virtualization and will therefore activate Hyper-V, albeit a stripped down version. This means that virtual machines created with third party virtualization software like VMWare Player and VirtualBox will no longer work as long as the WSL-2 feature is enabled. Microsoft offers an additional feature called “Windows Hypervisor Platform” which provides an API for this purpose. The latest version of VirtualBox can handle this, but it is still in the experimental phase and doesn’t seem to work well in practice.

Install an additional distribution

You start by creating an additional WSL distribution. You don’t want to change your current WSL Ubuntu installation for this experiment, but you can use it if you want.

Getting the same distribution from the Microsoft Store again won’t work, so we use Powershell to download an extra instance of Ubuntu20.04.

Start powershell and create a wsl-distros directory in your home directory:

cd ~

mkdir wsl distros

cd wsl distros

Now download Ubuntu using the Invoke-WebRequest cmdlet:

Invoke-WebRequest -Uri https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-wsl.rootfs.tar.gz -OutFile kvm.tar.gz -UseBasicParsing

Nb1. Instead of using Invoke-WebRequest you can of course also manually download an image from https://cloud-images.ubuntu.com.

Nb2. This takes a while; in the meantime you can proceed with installing WinDbg Preview from the Microsoft Store and downloading the necessary scripts (described later in this article)

Import the Ubuntu WSL distribution under the name “kvm” with wsl import:

wsl –import kvm ./kvm kvm.tar.gz –version 2

  1. Make sure you import as version 2

You can now start your new “kvm” WSL distribution

wsl -d kvm

Update and then install the following packages:

apt update

apt upgrade

apt -y install build-essential libncurses-dev bison flex libssl-dev libelf-dev cpu-checker qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils libvirt-daemon-system-sysv virt-manager

Now when you run the command ‘kvm-ok’ you will see the following:

INFO: Your CPU does not support KVM extensions

KVM acceleration can NOT be used

[Afb1: WSL-2 heeft out of the box geen virtualisatie actief staan]

You will now fix that by making the Lightweight Utility VM suitable for Nested Virtualization.

Nested virtualization

Now you are going to make the Lightweight Utility VM suitable for Nested Virtualization.

For a normal Hyper-V VM you can enable this with the “Set-VMProcessor” Powershell CMDlet, but the Lightweight Utility VM is managed in a different way (with HCS and therefore you will not find it in the Hyper-V manager for example ).

In addition, the Lightweigt Utility VM is recreated every time the first WSL instance is started, so you need to activate nested virtualization in a different way.

You will do this using WinDbg Preview and a number of scripts which can be found on the following GitLab page:

https://gitlab.com/conclusionxforce/automation/public/wsl2-nested-virtualization

  1. These scripts are derived from the original, which can be found at:

https://gist.github.com/steffengy/62a0b5baa124830a4b0fe4334ccc2606.

Go to the Microsoft Store, search for “WinDbg Preview” and install it.

[Afb2: Windows Debugger is makkelijk te installeren vanuit de  Microsoft Store]

Then stop the Lightweight Utility VM, but BEWARE(!) this also implies that all your already running WSL distributions will be killed. So close it properly first!

Launch Powershell and type in:

wsl–shutdown

Get the run-luvm.bat and script.js from the Conclusion Xforce git repository and place them somewhere on your system. Where doesn’t really matter, as long as you can find them again and they’re both together in the same folder

Now start Powershell again, but this time with “Run as Administrator”, because we are going to restart the LxssManager Windows Services in the run-luvm.bat script and that is a protected process.

[Afb3: Start Powershell als Administrator]

Run the run-luvm.bat script in this Powershell with Administrator rights:

./run-luvm.bat

Wait for the Windows Debugger to be fully started by this script. However, sometimes, especially the first time, the error message “The RPC server is unavailable” appears. This is because the WinDBG is not initialized fast enough. So pay close attention to this message, because that means it doesn’t work. The solution is simple though, try again after making sure you’ve run Powershell as Administrator and done a “wsl –shutdown”.

[afb4: Soms start WinDB niet snel genoeg, de oplossing is simpel; probeer het nog een keer]

Launch WSL KVM distro

Now you can start the previously created “kvm” WSL distribution again:

wsl -d kvm

This does not have to be done in the same Powershell window with Administrator rights (rather not even). When you now start “kvm-ok”, you will see:

INFO: /dev/kvm exists

KVM acceleration can be used

The above means that virtualization is possible, but there are still some small things to do before we can start KVM.

Qemu logs to systemd-logd by default, but a WSL distribution does not use Systemd. Therefore, add the following to /etc/libvirt/qemu.conf:

stdio_handler = “file”

Some permissions need to be set on /dev/kvm

chown root:kvm /dev/kvm

chmod 660 /dev/kvm

Now you can start libvirtd:

service libvirtd starts

Virtual Machine

When you have installed an X server on Windows you can start virt-manager and create a VM. How to install an X Server under Windows is also outside the scope of this article, but in the Linux Magazine number 6 of 2019 you can read what is required for this based on WSL-1 and VcXsrv. If you follow the steps in this article, it’s worth looking for the GWSL program in the Microsoft Store. This is also a free X Server for Windows, but which is more based on WSL and therefore also integrates better. For example, you can start your graphical applications from GWSL and configure D-bus on your distro. You also have to set your DISPLAY differently for WSL-2. For a WSL-2 distribution, the X server does not run on localhost but on an external ip address. From the point of view of the WSL-2 distribution, this is the IP address of your default gateway. This is how you set this ip address as the value for the DISPLAY variable:

export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk ‘{print $2}’):0

[afb5: Centos8 Stream VM op KVM met WSL-2 op Windows 10]

Did you know that in the upcoming major update of Windows 10, an X server will be integrated with WSL-2 as standard?

Finally

In this article you have already taken a look into the future when it comes to Nested Virtualization with WSL-2.

WSL-2 and its Lightweight Utility VM which will support out-of-the-box Nested-Virtualization in the next release offers endless possibilities, especially if you subsequently install KVM.

How about running MacOS on Windows10 with WSL-2, for example? I found this link, but haven’t tested it myself:

https://dev.to/nicole/running-macos-on-windows-10-with-wsl2-kvm-and-qemu-21e1

Can you get it going?

- A word from our sponsors -

Most Popular

LEAVE A REPLY

Please enter your comment!
Please enter your name here

More from Author

An Unprecedented 190% Quantum Efficiency – New Material Could Drastically Increase the Efficiency of Solar Panels

Lehigh University researchers have created a revolutionary solar cell material with...

Even Brief Secondhand Smoke Exposure Increases Risk of Dangerous Heart Rhythm Disorder

New research indicates that even minimal exposure to secondhand smoke increases...

Neuronal Crossroads: Decoding Brain Development

New research uncovers the developmental pathways of inhibitory neurons in the...

Quantum Control Unlocked: Creating Resistance-Free Electron Channels

New research demonstrates control over quantum states that could revolutionize energy...

- A word from our sponsors -

Read Now

An Unprecedented 190% Quantum Efficiency – New Material Could Drastically Increase the Efficiency of Solar Panels

Lehigh University researchers have created a revolutionary solar cell material with up to 190% external quantum efficiency, pushing beyond conventional efficiency limits and showing great promise for enhancing future solar energy systems. Further development is required for practical application, supported by a U.S. Department of Energy grant.It...

Even Brief Secondhand Smoke Exposure Increases Risk of Dangerous Heart Rhythm Disorder

New research indicates that even minimal exposure to secondhand smoke increases the risk of atrial fibrillation, a common heart rhythm disorder. The study, involving over 400,000 adults from the UK Biobank, found a progressive increase in risk with longer exposure durations, regardless of the environment. The findings...

Neuronal Crossroads: Decoding Brain Development

New research uncovers the developmental pathways of inhibitory neurons in the brain, highlighting the roles of proteins like MEIS2 and DLX5 in neuron differentiation and the potential link to neurodevelopmental disorders through genetic mutations. Credit: SciTechDaily.comStudy reveals how proteins direct nerve cell precursors to turn into specialized...