McGarrah Technical Blog

Sharing file systems between WSLv2 instances

I have a significant investment in my WSLv2 Ubuntu 22.04.3 LTS installation. It has my Nvidia GPU setup nicely integrated and several machine learning demos and tests I’ve built and use for keeping current on machine learning. With Ubuntu 24.04 LTS released, I now want to play around in the newer version but don’t want to move or worse copy my entire set of models and repositories across. I have well over 500Gb of content and absolutely don’t want two copies of those floating around. I’m looking for a solution to this and figure others have encountered it.

Explorer WSL Filesystems

The first option is that you can use Windows Explorer to copy between the two WSLv2 instances. I did a bit of that while moving some small custom bits between the two linux instances. It was super easy to do but misses the direct sharing that I’d like to do between them especially for larger files and repos.

Explorer WSL Filesystems

Enter a nifty SuperUser Stack Overflow question “Is there a way to access files from one WSL 2 distro/image in another one?” and the interesting set of answers that took a couple of tries for me to get right.

Run this in both your WSL instances from the command line.

echo "/ /mnt/wsl/instances/$WSL_DISTRO_NAME none defaults,bind,X-mount.mkdir 0 0" | sudo tee -a /etc/fstab
sudo mkdir -p /mnt/wsl/instances/$WSL_DISTRO_NAME

Update your wsl.conf file to delay mounting from /etc/fstab entries until /mnt/wsl is ready for the sub-mount point to occur.

nano /etc/wsl.conf
[automount]
mountFsTab = false

[boot]
command = sleep 5; mount -a
systemd=true

At this point, you have to stop both the WSLv2 instances completely.

You can do this from a Command or Powershell console using the following command.

C:\> wsl --shutdown

I personally use the easyWSL utility from the Microsoft Store to do this with a Windowed GUI. You can also retrieve it from Github easyWSL as well. I’m super surprised it wasn’t on Chocolatey. You also have to install the .NET Desktop Runtimes 6.0.3+ x64 which are a dependency of easyWSL and I did that using Chocolatey.

C:\> choco install dotnet-6.0-desktopruntime

EasyWSL Utility

Restart the distro using the easyWSL “Stop distro” for each one and you should see both the file systems between the instances of WSLv2 after restarting them.

After restarting the WSLv2 instances, you can access across the two Ubuntu instances via the /mnt/wsl/instances/ and the instance name.

➜  ~ ls -al /mnt/wsl/instances
total 8
drwxr-xr-x  4 root root   80 Jun 21 16:42 .
drwxrwxrwt  3 root root   80 Jun 20 23:31 ..
drwxr-xr-x 19 root root 4096 Jun 21 19:06 Ubuntu-22.04
drwxr-xr-x 22 root root 4096 Jun 20 23:31 Ubuntu-24.04

One of the first issues you will hit is the home directory may have different UID and GID values between the two installs. So you will have to use sudo or root access to see into the file system. You can see this issue below in my two installs of Ubuntu.

➜  ~ sudo grep mcgarrah /mnt/wsl/instances/Ubuntu-22.04/etc/passwd
mcgarrah:x:1000:1000:,,,:/home/mcgarrah:/usr/bin/zsh
➜  ~ sudo grep mcgarrah /mnt/wsl/instances/Ubuntu-24.04/etc/passwd
mcgarrah:x:1002:1002:,,,:/home/mcgarrah:/usr/bin/zsh

I was able to work around this for a simple check like a diff between two repositories with this command.

➜  ~ sudo diff -r /mnt/wsl/instances/Ubuntu-22.04/home/mcgarrah/github/mcgarrah.github.io/  /mnt/wsl/instances/Ubuntu-24.04/home/mcgarrah/github/mcgarrah.github.io

This is not a perfect solution but it gets me closer to what I wanted. I can share the machine learning models between the two instances with decent performance. I’ll likely work on getting the UID and GID synchronized between the different instances to make this work more easily.

I hope this helps someone else in my same boat as they upgrade WSLv2 instances.