Custom WSL Distributions

One thing I've noticed is there isn't many distributions available for WSL (Windows Subsystem for Linux), while for most the small selection of Ubuntu, Kali, SUSE, Debian, Alpine, and Fedora is sufficient, others have chosen to add support for more distributions by the use of installers.

Custom WSL Distributions
Photo by Sai Kiran Anagani / Unsplash

One thing I've noticed is there isn't many distributions available for WSL (Windows Subsystem for Linux), while for most the small selection of Ubuntu, Kali, SUSE, Debian, Alpine, and Fedora is sufficient, others have chosen to add support for more distributions by the use of installers, the most popular of which is WSLDL which already has a growing selection of distribution support. If neither of these have the distribution you're looking for, then this article is going to explain how to setup your own distribution of choice for WSL.

Step 1. Choosing a Distribution

For this guide we're going to first of course need to choose the distribution we're going to be porting to WSL, unfortunately depending on the route you choose this can get fairly complicated, but fear not as we're going to be going with the easier, and yet still fully working route.

What you're going to want to do is choose a distribution that has a docker container, this is important as in a later step we are going to extract the filesystem from the docker image and use it as our rootfs that is installed into WSL.

Step 2. Preparing for Extraction

After you have chosen your distribution of choice, we're now going to start preparations to extract the filesystem, the first thing we're going to need to do is install Go as the tool we're going to be using to extract it is built using it, assuming you're on Ubuntu on a server or Ubuntu WSL doing this, this can be done with the following

sudo apt install -y golang

After go has finished installing you're going to want to clone this repository and build the executable, this tool will allow us to extract the root filesystem from the docker image into a directory of our choosing.

git clone https://github.com/ForAllSecure/rootfs_builder
cd rootfs_builder && make

After this has finished compiling, you can continue to step 3 and start the extraction process.

Step 3. Extracting the RootFS

To extract the rootfs of the image is now fairly simple, you're going to want to create a config.json file with the following contents:

{
    "Name": "docker image label here",
    "Spec": {
        "Dest": "/tmp/rootfs",
        "User": "root"
    }
}

After you have created the configuration file, we can now use the rootfs_builder tool to extract the root filesystem from the image, to do this run the following:

sudo ./rootfs_builder config.json

Once the tool has finished running, you should have the root filesystem of the image inside /tmp/rootfs/rootfs/, the last step we need to do here is compress it using tar, this can be done with the following:

cd /tmp/rootfs/rootfs
sudo tar -zcpf rootfs.tar.gz *

Step 4. Installing the Distribution

For installing the distribution we'll be using WSLDL, start by downloading the latest version of the Launcher.exe executable, rename it to your distribution's name of choice, such as oraclelinux.exe and place your created rootfs.tar.gz file beside it, move them into a directory where you want to install your WSL filesystem, and finish off by running the launcher executable we downloaded and renamed earlier, and.. you're done, the distribution is now installed into WSL and you can configure and setup everything as you need.

Conclusion

The reason I made this post is because I personally really like Oracle Linux, and unfortunately there wasn't any installer or anything to set it up with WSL, so I decided to go my own way and create a installer myself using tools available to me, hope this guide treats you well, and that you have a great holiday for those who celebrate Christmas or any other!