WSL & Hyper-V
Windows can run Linux programs and whole other operating systems side by side with Windows apps. Two technologies make that possible: Hyper-V, Microsoft's hypervisor, and the Windows Subsystem for Linux (WSL). This lesson explains how each works inside, why WSL 2 replaced WSL 1's design, and what that means for performance and day-to-day use.
Hyper-V: a type 1 hypervisor
A hypervisor (or virtual machine monitor) lets several operating systems share one physical machine. Hyper-V is a type 1 ("bare-metal") hypervisor: when it is enabled, the boot loader starts hvix64.exe (Intel) or hvax64.exe (AMD) before the Windows kernel. The hypervisor takes over the CPU's virtualization features (Intel VT-x / AMD-V, plus second-level address translation for memory) and then starts Windows in a partition.
+--------------------------+ +------------------+ +------------------+
| Root partition | | Child partition | | Child partition |
| Windows 11 (your desktop)| | Ubuntu VM | | WSL 2 utility VM |
| VM worker processes, | | (enlightened | | (Microsoft Linux |
| VSP drivers, vmms.exe | | kernel, VSCs) | | kernel) |
+------------+-------------+ +--------+---------+ +--------+---------+
| VMBus (shared-memory channels) |
+============+===============================================+=========+
| Hyper-V hypervisor (hvix64 / hvax64) |
+=======================================================================+
CPU (VT-x / AMD-V, SLAT), RAM, devicesKey ideas:
- The root partition is your normal Windows. It still owns the physical devices and runs the management stack (
vmms.exe, onevmwp.exeworker process per VM). - Child partitions are VMs. They see synthetic devices: a guest driver (VSC, virtualization service client) sends requests over VMBus, a shared-memory channel, to a VSP (provider) in the root partition, which uses the real driver. That's much faster than emulating old hardware.
- Enlightened guests (modern Windows and Linux kernels) know they run under Hyper-V and call the hypervisor directly (hypercalls) instead of triggering expensive traps.
Because Hyper-V sits under Windows itself, Windows uses it for its own protection too: virtualization-based security (Memory integrity/HVCI, Credential Guard) runs a small secure kernel in a separate trust level that even the normal kernel can't read (see Windows NT Architecture). Type 2 hypervisors such as VirtualBox and VMware Workstation now run on top of the Windows hypervisor platform when it is active.
Turning on Hyper-V needs Windows Pro, Enterprise or Education (sample output — yours will differ):
PS C:\Windows\system32> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
Do you want to restart the computer to complete this operation now?
[Y] Yes [N] No [?] Help (default is "Y"): Y
PS C:\Windows\system32> Get-VM
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status Version
---- ----- ----------- ----------------- ------ ------ -------
Ubuntu Running 2 4096 01:12:44.5210000 Operating normally 12.0WSL 1: translating system calls
The first WSL (2016) had no Linux kernel at all. Linux ELF binaries ran as pico processes, and two kernel drivers (lxss.sys, lxcore.sys) caught every Linux system call and translated it into NT kernel operations: Linux open() became an NT file open, fork() was implemented on top of NT's process creation, and so on. It was an impressive revival of NT's original "multiple environment subsystems" idea.
Problems appeared at the edges. Linux has more than 350 system calls with subtle semantics; some (full ptrace, inotify across the boundary, FUSE, kernel modules, cgroups for Docker) were hard or impossible to translate, and file operations had to go through NTFS and Windows' file-system filter drivers, which made git status or npm install on large trees slow.
WSL 2: a real Linux kernel in a lightweight VM
WSL 2 (2019) takes the opposite approach: it boots a real Linux kernel (built and updated by Microsoft) inside a special lightweight Hyper-V VM, the utility VM. It starts in about a second, grows and shrinks its memory dynamically, and all your distributions run in it as separate containers sharing that one kernel.
| WSL 1 | WSL 2 | |
|---|---|---|
| Kernel | none: syscalls translated to NT | real Linux kernel in a utility VM |
| System call compatibility | partial | full (Docker, FUSE, eBPF, systemd) |
| Linux file system | stored on NTFS | ext4 inside a virtual disk (ext4.vhdx) |
| Speed on Linux files | slower | near native |
Speed on Windows files (/mnt/c) |
fast | slower (crosses the VM boundary via the 9P protocol) |
| Networking | shares Windows' IP | NAT behind a virtual switch, or mirrored mode |
| Needs Hyper-V platform | no | yes ("Virtual Machine Platform") |
| GUI apps | no | yes (WSLg: Wayland + RDP) |
Working with WSL from PowerShell (sample output — yours will differ):
PS C:\Users\ada> wsl --install -d Ubuntu-24.04
PS C:\Users\ada> wsl --list --verbose
NAME STATE VERSION
* Ubuntu-24.04 Running 2
Debian Stopped 2
PS C:\Users\ada> wsl --set-version Debian 1
PS C:\Users\ada> wsl -d Ubuntu-24.04 -- uname -r
6.6.87.2-microsoft-standard-WSL2
PS C:\Users\ada> wsl --shutdownGlobal VM settings live in %UserProfile%\.wslconfig:
[wsl2]
memory=8GB # cap the VM's RAM
processors=4
networkingMode=mirroredWhere do system calls go?
This model contrasts the two designs. WSL 1 must have a translation for every system call it supports; WSL 2 simply forwards it to a real Linux kernel, and only file access to Windows drives has to cross into Windows:
WSL1_TABLE = { # Linux syscall -> NT kernel operation
"open": "NtCreateFile",
"read": "NtReadFile",
"write": "NtWriteFile",
"fork": "NtCreateUserProcess (clone address space)",
"mmap": "NtMapViewOfSection",
}
def wsl1(call, path=""):
if call in WSL1_TABLE:
return f"lxcore.sys translates -> {WSL1_TABLE[call]}"
return "ENOSYS: not implemented in WSL 1"
def wsl2(call, path=""):
if path.startswith("/mnt/c/"):
return "Linux kernel -> 9P over VMBus -> Windows file server -> NTFS"
return "Linux kernel handles it (ext4 in ext4.vhdx)"
calls = [("open", "/home/ada/app.py"), ("open", "/mnt/c/Users/ada/doc.txt"),
("fork", ""), ("bpf", ""), ("fanotify_init", "")]
for call, path in calls:
print(f"{call} {path}".strip())
print(" WSL 1:", wsl1(call, path))
print(" WSL 2:", wsl2(call, path))open /home/ada/app.py
WSL 1: lxcore.sys translates -> NtCreateFile
WSL 2: Linux kernel handles it (ext4 in ext4.vhdx)
open /mnt/c/Users/ada/doc.txt
WSL 1: lxcore.sys translates -> NtCreateFile
WSL 2: Linux kernel -> 9P over VMBus -> Windows file server -> NTFS
fork
WSL 1: lxcore.sys translates -> NtCreateUserProcess (clone address space)
WSL 2: Linux kernel handles it (ext4 in ext4.vhdx)
bpf
WSL 1: ENOSYS: not implemented in WSL 1
WSL 2: Linux kernel handles it (ext4 in ext4.vhdx)
fanotify_init
WSL 1: ENOSYS: not implemented in WSL 1
WSL 2: Linux kernel handles it (ext4 in ext4.vhdx)
The practical rule follows from the last line of each open: keep Linux projects in the Linux file system (~/projects, reachable from Windows as \\wsl$\Ubuntu-24.04\home\ada\projects or \\wsl.localhost\...), and Windows projects on C:. Crossing the boundary for thousands of small files is what makes builds slow.
A cost model for the boundary
Each file operation that crosses the VM boundary pays a round trip. With many small files, the fixed cost dominates. The numbers below are illustrative, not measurements:
NATIVE_US = 4 # microseconds per stat/open on ext4 inside the VM
CROSS_US = 120 # microseconds per operation over 9P to NTFS
def scan(files, ops_per_file, cost_us):
return files * ops_per_file * cost_us / 1_000_000 # seconds
for files in [100, 10_000, 200_000]:
inside = scan(files, 3, NATIVE_US)
across = scan(files, 3, CROSS_US)
print(f"{files:>7} files: ~/project {inside:7.3f}s /mnt/c/project {across:7.3f}s x{across / inside:.0f}") 100 files: ~/project 0.001s /mnt/c/project 0.036s x30
10000 files: ~/project 0.120s /mnt/c/project 3.600s x30
200000 files: ~/project 2.400s /mnt/c/project 72.000s x30
The ratio is the same at every size, but the absolute cost is what you feel: a fraction of a second for a small project, more than a minute for a big node_modules tree.
Detecting WSL from a script
Scripts sometimes need to know whether they run under WSL (for example to call explorer.exe or clip.exe). WSL kernels contain microsoft in their version string, and WSL sets the WSL_DISTRO_NAME variable. In this course's Linux sandbox (a container, not WSL) the check says so:
if grep -qi microsoft /proc/version; then
echo "running under WSL (${WSL_DISTRO_NAME:-unknown distro})"
else
echo "not WSL: kernel is $(uname -s)"
finot WSL: kernel is Linux
From inside WSL, Windows programs run directly (notepad.exe file.txt, explorer.exe .), and wslpath converts paths: wslpath 'C:\Users\ada' prints /mnt/c/Users/ada.
Choosing a tool
| You want to… | Use |
|---|---|
| run Linux CLI tools, compilers, Docker on Windows | WSL 2 |
| test on several full OSes, other kernels, Windows guests | Hyper-V VMs |
| run untrusted software in a throwaway Windows | Windows Sandbox (Hyper-V based, deleted on close) |
| run Linux containers | Docker Desktop or Podman, which use the WSL 2 VM as the Linux host |
Learn more: Virtualization & Containers · Linux vs Windows · The Windows Boot Process
Test yourself
How did WSL 1 run Linux programs?
- In a full Linux VM
- By translating Linux system calls into NT kernel operations in kernel drivers
- By recompiling them for Windows
- Through Cygwin
Where should a large Node.js project live for the fastest builds with WSL 2 tools?
- On C: via /mnt/c
- In the Linux file system, e.g. ~/projects
- On a network share
- It makes no difference
Why is Hyper-V called a type 1 hypervisor?
- It only runs one VM
- It runs directly on the hardware, below the Windows kernel, which itself runs in the root partition
- It is the first version
- It runs as an app on top of Windows