Producing any Linux Image Suitable for WSL

[ad_1]
WSL, the abbreviation of Windows Subsystem for Linux, means Windows Subsystem for Linux. Allows developers to run the GNU/Linux environment, including most command line tools, utilities and applications, as-is, without the overhead of a traditional virtual machine or dual-boot setup.
How to install WSL on Windows 10 version 2004 and later or Windows 11, you can refer to Microsoft'sOfficial documentation。
There are already some Linux distributions on the Microsoft Store, such as Ubuntu, etc., but there are basically only a few of them, or the versions are relatively old.
Microsoft officially hasDocumentation example, import into any Linux distribution by using a tar file. Import the Linux distribution into WSL by using a Docker container to obtain its tar file.
This article introduces another method of making any Linux image suitable for WSL, based on the qcow2 image officially provided by each Linux distribution.
The following uses Rocky Linux 8 as an example to introduce the specific production process.
1. Download the official qcow2 file
Rocky Linux provides the latest version of qcow2 file download address:
https://download.rockylinux.org/pub/rocky/8/images/x86_64/Rocky-8-GenericCloud-Base.latest.x86_64.qcow2
2. Make rootfs.tar.gz
All the following operations depend on the following commands:kpartx
、qemu-img
、losetup
、fdisk
、mount
、umount
if the command does not exist, please install it yourself.
Convert qcow2 files to raw files
qemu-img convert -f qcow2 -O raw Rocky-8-GenericCloud-Base.latest.x86_64.qcow2 Rocky8.raw
Use losetup to virtualize raw files into block devices
losetup /dev/loop1 Rocky8.raw
Use kpartx to mount a virtual file system
kpartx -a /dev/loop1
Use fdisk to check the mounted partition status
fdisk -l /dev/loop1
Return the following information
Disk /dev/loop1: 10 GiB, 10737418240 bytes, 20971520 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x2cc9c27e Device Boot Start End Sectors Size Id Type /dev/loop1p1 * 2048 16386047 16384000 7.8G 83 Linux
Here's/dev/loop1p1
Just wait for the specific name of the block device to be mounted.
Mount block device to local path
mount /dev/mapper/loop1p1 /mnt
Enter the mount point/mnt and view the files
cd /mnt ls -la
Return the following information
total 16 dr-xr-xr-x. 17 root root 224 Nov 15 03:17 . dr-xr-xr-x. 18 root root 256 Nov 17 09:09 .. lrwxrwxrwx. 1 root root 7 Oct 11 08:48 bin -> usr/bin dr-xr-xr-x. 6 root root 4096 Nov 15 03:24 boot drwxr-xr-x. 2 root root 6 Nov 15 02:50 dev drwxr-xr-x. 94 root root 8192 Nov 15 03:24 etc drwxr-xr-x. 2 root root 6 Oct 11 08:48 home lrwxrwxrwx. 1 root root 7 Oct 11 08:48 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Oct 11 08:48 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 Oct 11 08:48 media drwxr-xr-x. 2 root root 6 Oct 11 08:48 mnt drwxr-xr-x. 2 root root 6 Oct 11 08:48 opt drwxr-xr-x. 2 root root 6 Nov 15 02:50 proc dr-xr-x---. 2 root root 114 Nov 15 03:47 root drwxr-xr-x. 2 root root 6 Nov 15 02:50 run lrwxrwxrwx. 1 root root 8 Oct 11 08:48 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Oct 11 08:48 srv drwxr-xr-x. 2 root root 6 Nov 15 02:50 sys drwxrwxrwt. 2 root root 84 Nov 15 03:47 tmp drwxr-xr-x. 13 root root 158 Nov 15 03:01 usr drwxr-xr-x. 20 root root 262 Nov 15 03:00 var
Pack and compress /mnt. The entire directory is rootfs.tar.gz and save it to /root.
tar zcf /root/rootfs.tar.gz *
Check the packaged file to confirm that the file exists. The size is usually several hundred MB.
ll -h /root/rootfs.tar.gz
At this point, the file system of Rocky Linux 8 is basically completed.
3. Follow-up
Unmount mount point
cd ~ umount /mnt
Use kpartx to unmount the virtual file system
kpartx -d /dev/loop1
Use losetup to unmount a virtual block device
losetup -d /dev/loop1
4. Use with wsldl
wsldl, Advanced WSL launcher/installer, is a versatile and advanced WSL/WSL2 launcher and installer written in Go. It can be used to install any rootfs package or act as a launcher for an existing installed distribution.
project address:https://github.com/yuk7/wsldl
Download the latest version of Release icons.zip document
After unzipping icons.zip, you will find a series of exe files for Linux distributions. As shown below:
Find the Rocky.exe file, and then download the rootfs.tar.gz file packaged and compressed in the previous step back to your local computer. Put these 2 files in the same directory.
Double-click Rocky.exe to install Rocky Linux 8. For the specific process, please refer to another article “Installing Rocky Linux System on Windows 10”
The production process for the rest of the distributions is much the same. If you find it troublesome, you can directly download some commonly used Linux distributions that I have made.
5. Prepared Linux distribution
The download link is as follows:
CentOS 7.9
CentOS 8.4
Fedora 35
Fedora 36
Fedora 37
AlmaLinux 8
AlmaLinux 9
Rocky Linux 8.5
Rocky Linux 9.0
Alpine 3.15
Ubuntu 18.04
Ubuntu 20.04
Ubuntu 22.04
Debian 10
Debian 11
Debian 12
Amazon Linux 2
Browse the catalog:
https://dl.lamp.sh/wsl