PolarSPARC |
Introduction to WSL 2
Bhaskar S | 01/07/2023 |
Introduction
A typical large Enterprise provisions and deploys Windows machines to its employees (and contractors) for their daily use, while provisioning and deploying Linux servers for hosting containerized applications that serve their clients.
One of the approaches for an application developer to mimic this dual Windows-Linux environment in their Windows machine is to use a Virtual Machine (VM) to host a Linux environment. With this setup, the developer can run containerized application(s) on the virtualized Linux enviroment and have the client application(s) running on Windows host communicate with each other (via networking).
What if there exists an option for both Windows and Linux to co-exist on the same Windows machine, in such a way that they could access user files as well as execute binaries both ways (Windows to Linux and vice versa) seemlessly ???
This is where the Windows Subsystem for Linux or (WSL) comes into play.
WSL was first introduced in late 2016 and used a translation layer for the integration with Linux. However, note that the two OSes behave and operate in very different ways under-the-hood and this resulted in some compatibility issues.
In mid 2019, the next generation of WSL called WSL 2 was introduced, which offered 100% API compatibility with Linux. This implies one could also execute native Linux GUI (X and Wayland) applications on Windows.
The following diagram illustrates the high-level architecture of WSL 2:
From the high-level architecture in Figure.1 above, we see WSL 2 uses a Lightweight Utility VM to host a real Linux kernel to achieve the full compatibility. The Lightweight Utility VM has been optimized to load the Linux kernel into the VM's address space without going through any Boot Loader process, thereby achieving fast startup times.
At the time of this article, WSL 2 currently supports the following Linux distributions:
Debian
Kali Linux
Oracle Linux
SUSE Linux
Ubuntu
Note that WSL 2 loads the Linux kernel from an image file. Under-the-hood, a WSL 2 Image file is nothing more than a TAR file, which the Lightweight Utility VM can use to load the Linux kernel. This means that a Systems Administrator can create a custom WSL 2 image for use in the Enterprise.
However, using the Microsoft provided WSL 2 images has the advantage that the Linux kernel will be serviced by the Windows updates and one can get the latest security fixes and kernel improvements without needing to manage it themselves.
Windows Setup
For the setup, we will use a Windows 10 Home desktop with the latest updates.
Also, the Windows username will be alice.
To setup WSL on the Windows desktop, one needs to enable the following two optional features:
Virtual Machine Platform - this enables the Lightweight Utility VM used by WSL 2
Windows Subsystem for Linux - this enables the WSL capability
Let us first enable the feature for Virtual Machine Platform. In the Windows Search textbox, type the terms "windows features" as shown in the illustration below:
Click on the Settings (3) item from the search matches as shown in the illustration above.
This action will display the option to Turn Windows features on or off as shown in the illustration below:
Click on the highlighted option as shown in the illustration above.
This will display the Turn Windows features on or off window as shown in the illustration below:
Check the boxes for the two options (as highlighted) and then click on the OK button as shown in the illustration above.
This action will prompt the user to Reboot the system as shown in the illustration below:
Click on the highlighted button to restart the system as shown in the illustration above.
For users of the Windows system who are comfortable with the command-line, open a PowerShell window as an Administrator.
To enable the Virtual Machine Platform feature, execute the following command:
PS C:\Windows\system32> Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -All
The following would be a typical output:
Do you want to restart the computer to complete this operation now? [Y] Yes [N] No [?] Help (default is "Y"): n Path : Online : True RestartNeeded : True
Similarly, to enable the Windows Subsystem for Linux feature, execute the following command:
PS C:\Windows\system32> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -All
The following would be a typical output:
Do you want to restart the computer to complete this operation now? [Y] Yes [N] No [?] Help (default is "Y"): n Path : Online : True RestartNeeded : True
Reboot the Windows system for the features to be enabled.
Hands-on WSL 2
In order to get our hands dirty with WSL 2, open a PowerShell to try and execute few commands.
To list all the installed Linux distribution(s), execute the following command:
PS C:\Users\alice> wsl --list --verbose
The following would be a typical output:
Windows Subsystem for Linux has no installed distributions. Distributions can be installed by visiting the Microsoft Store: https://aka.ms/wslstore
To list all the available Linux distribution(s), execute the following command:
PS C:\Users\alice> wsl --list --online
The following would be a typical output:
The following is a list of valid distributions that can be installed. Install using 'wsl --install -d <Distro>'. NAME FRIENDLY NAME Ubuntu Ubuntu Debian Debian GNU/Linux kali-linux Kali Linux Rolling SLES-12 SUSE Linux Enterprise Server v12 SLES-15 SUSE Linux Enterprise Server v15 Ubuntu-18.04 Ubuntu 18.04 LTS Ubuntu-20.04 Ubuntu 20.04 LTS OracleLinux_8_5 Oracle Linux 8.5 OracleLinux_7_9 Oracle Linux 7.9
To enable version 2 of WSL as the default, execute the following command:
PS C:\Users\alice> wsl --set-default-version 2
The following would be a typical output:
For information on key differences with WSL 2 please visit https://aka.ms/wsl2 The operation completed successfully.
To check the status of WSL 2, execute the following command:
PS C:\Users\alice> wsl --status
The following would be a typical output:
Default Version: 2 The Windows Subsystem for Linux kernel can be manually updated with 'wsl --update', but automatic updates cannot occur due to your system settings. To receive automatic kernel updates, please enable the Windows Update setting: 'Receive updates for other Microsoft products when you update Windows'. For more information please visit https://aka.ms/wsl2kernel. The WSL 2 kernel file is not found. To update or restore the kernel please run 'wsl --update'.
To install the Ubuntu distribution in WSL 2, execute the following command:
PS C:\Users\alice> wsl --install --distribution Ubuntu
The following would be a typical output:
Installing: Ubuntu Ubuntu has been installed. Launching Ubuntu...
The installation process will launch a Linux bash shell and result in an error as shown in the illustration below:
To fix the installation issue from above, one needs to update WSL 2 by executing the following command:
PS C:\Users\alice> wsl --update
The following would be a typical output:
Installing: Windows Subsystem for Linux Windows Subsystem for Linux has been installed.
Now, retry the installation of the Ubuntu distribution in WSL 2 by executing the following command:
PS C:\Users\alice> wsl --install --distribution Ubuntu
The following would be a typical output:
Ubuntu is already installed. Launching Ubuntu...
Again, the installation process will launch a Linux bash shell and complete the installtion by prompting the user for some details as shown in the illustration below:
We will refer to the above terminal window as bash window.
To list all the installed Linux distribution(s), execute the following command:
PS C:\Users\alice> wsl --list --verbose
The following would be a typical output:
NAME STATE VERSION * Ubuntu Running 2
In the bash window, execute the following Linux command to display all the Linux processes:
alice@XXXXX:/mnt/c/Users/alice$ htop
The output is as shown in the illustration below:
In the bash window, execute the following command to start the Windows command shell:
alice@XXXXX:/mnt/c/Users/alice$ cmd.exe
The following would be a typical output:
Microsoft Windows [Version 10.0.19045.2364] (c) Microsoft Corporation. All rights reserved. C:\Users\alice>
Notice the seemless integration between Windows and Linux in action here. To exit the Windows command shell, execute the following command:
C:\Users\alice>exit
The following would be a typical output:
alice@XXXXX:/mnt/c/Users/alice$
We are now back in the Linux bash shell.
To shutdown the WSL 2 Ubuntu instance, execute the following command in the PowerShell window:
PS C:\Users\alice> wsl --terminate Ubuntu
The Linux bash shell window will be closed and the Ubuntu instance is shutdown at this point.
To list all the installed Linux distribution(s), execute the following command in the PowerShell window:
PS C:\Users\alice> wsl --list --verbose
The following would be a typical output:
NAME STATE VERSION * Ubuntu Stopped 2
References