DEV Community

Cover image for Easy Distro-hopping with WSL and Share you Data across Distros
Nathachai B.
Nathachai B.

Posted on

Easy Distro-hopping with WSL and Share you Data across Distros

Ever since Windows for Subsystem Linux (WSL) came out, my life has been so graceful and effortless as I am a System/DevOps Engineer, Developer as a hobby, especially on Windows 🪟.

I can use tools and software that familiar to me in the Linux world without me needing another computer or virtual machine to spawn an instance of Linux and get in the loophole of managing those machines and selecting a distro of choice.

As I also have another life in creative works that utilize tools like Adobe Suite. So, Windows is a platform of my choice.

Choosing a Linux distro and stick with it is something that I wouldn't like to get into and WSL has advantages of giving me a freedom to choose and uncompromised.

Linux on Microsoft Store

Why Multiple Linux Distro?

There're many reason to move across Linux distro (or Distro Hopping) but from one of my experiences, there are many occasion that a software you're looking to try is not available on your default package manager.

For example, k9s (at least from the first GitHub page) does not distribute on apt so, you need to download and maintain yourself that binary which to my preference, would avoid it as it is much simpler to just let package manager do the job.

Downloading K9s from GitHub Guide

Everything should be much simpler if you just distro hopping but if you think about it on bare-hardware, it often requires you a dual (or multiple) booting whereas WSL doesn't require you to restart your computer to hop distro.

WSL with multiple Linux distro installed, I just open a terminal session for another distro, and done!

For this instance, I go with Arch Linux and install k9s straight with pacman.

I use Arch, btw

Opening another Linux distro on Windows Terminal

Kudos to: ArchWSL

Alright Multiple Distro, but we got another problem…

As we can have so many distros installed but they all separated. Each of WSL Linux distro has its own disk. (From my own perspective and what I understand). Think of each a virtual machine but more integrated per say.

Let say that you have a source code and you have been working on a Linux distro that you choose to daily drive but you maybe need to build or run on a different runtime version or even or using different tool or toolchain.

I wouldn't talk about version manager stuffs like for npm with nvm to switch between Node versions but you go by running another Linux distro and just install a separate runtime on it

In my case, I need a different tool for my work. I often connect to different Kubernetes Cluster with .yaml files.

Where I daily drive Ubuntu where typical kubectl commands are executed but I also use k9s for TUI-interface but with the condition that I've mentioned about k9s.

So, I am looking for a way to put these .yaml file in one place and share across Linux distros.

But WSL actually mount Windows Filesystem to Linux 🪟

WSL actually accommodate us by mounting Windows Filesystem on Linux system by default. I just store them on Windows right? Problem solve!

Good choice! But there's a catch 🥶

Long story short, the performance is not really good while accessing Windows filesystem (for WSL2) because of a protocol that was used to mount the drive.

See: Why is WSL extremely slow when compared with native Windows NPM/Yarn processing? - Stack Overflow

So, I need something that is native to Linux and also need to be external from one distro and requires portability.

From that point, I decided making myself a separate (virtual) drive that is native to Linux (ext4) to solve the performance problem and match to what I need.

Windows has Virtual Hard Disk (VHD) which comes in handy where I don't need to spare a partition for a separate drive and as it is a file, it offers portability!

Remarks: This is actually a part of my WSL portable setup. That's why it needs to be portable and using a virtual drive method.

How to Setup

Fun fact: For a fortunate occasion, one of my co-worker bought a new computer to office and he must move his work from old to new so, I share him this WSL setup and also to you readers.

Let's go ahead and create one.

1). Go to Windows Settings App > System > Storage > Disks & volumes > Create a virtual hard disk (VHD)

Create a virtual hard disk (VHD)

2). Create a VHD of a size of your choice without initialisation

Create a 60GB VHD

I named it Supernova with .vhdx Format, Dynamic Disk Type.
Mine is 60 GB (can be extend later, don't worry)

After you create, don't initialise them yet. Just click Cancel to skip it

Skip Initialising VHD

VHD File on Disk

3). Mount the Uninitialised Virtual Disk to WSL

You need to get the Virtual Disk Identifier

# PowerShell on Windows
GET-CimInstance -query "SELECT * from Win32_DiskDrive"
Enter fullscreen mode Exit fullscreen mode

Get Disk Identifier

Mine is \\.\PHYSICALDRIVE3; It should be in the same order as the settings page.

And then mount the Virtual Disk to WSL

As it is not initialised, must mount it as is with --bare option

# PowerShell on Windows
sudo wsl --mount \\.\PHYSICALDRIVE3 --name Supernova --bare
Enter fullscreen mode Exit fullscreen mode

If you don't have sudo for Windows yet, run Terminal with Administrator Privileges first

In Linux Shell, Using sudo fdisk -l to check it out.

4). Initialise Disk on Linux

Simple create a disk partition and format it to ext4, I am using fdisk

Using fdisk with a drive

Create GPT Partition

Create Primary Partition

Formating to ext4

5). Unmount and Re-mount the Disk

After we initialise it with fdisk on Linux.

Go back to PowerShell on Windows, Unmount and Re-mount it with the first partition.

# PowerShell on Windows
sudo wsl --unmount \\.\PhysicalDrive3
sudo wsl --mount \\.\PhysicalDrive3 --name Supernova -p 1
Enter fullscreen mode Exit fullscreen mode

Unmount and Re-mount

This will be automatically mounted to /mnt/wsl/<Drive Name> as it is the WSL default mounting path.

  • <Drive Name> is corresponding to --name parameter when you executing wsl --mount

Mounting Path

In my uses, I store all of my work, data on that Virtual Drive and then make symlinks to data on the Virtual Drive.

Create Symlinks

I wish I can do is storing my entire home directory with Virtual Drive.

This is the same story as when you're installing Linux on bare-hardware with a separate /home on a separate partition.

Optional: Setup auto-mounting when logging in with Task Scheduler with the same command.

Automounting with Task Scheduler

Let see in action!

I am writing file on Arch and instantly appear on Ubuntu and OpenSUSE!

Image description


Thanks for reading, hopping this is useful to y'all 😇

Top comments (0)