19

If it is possible, how can I move files from one WSL instance to another directly? This would be useful when trying out new distributions, e.g. for copying /home from Ubuntu 18.04 to 20.04.

I am weary of doing this through explorer by accessing \\wsl$ in the windows host, it doesn't seem to reliably transfer all the files and feels wrong overall.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
BoZenKhaa
  • 782
  • 1
  • 6
  • 22
  • 1
    It seems there is a way: https://github.com/microsoft/WSL/issues/4574 – BoZenKhaa Jan 20 '21 at 20:19
  • Ok, the proper way seems to be this: https://learn.microsoft.com/en-us/windows/wsl/wsl2-mount-disk#mount-a-vhd-in-wsl – BoZenKhaa Jan 20 '21 at 21:29
  • 1
    The last one is (currently) only available if you are running Windows Insider Preview (beta). The `mount --bind` idea in the first comment you posted isn't too bad a workaround in the meantime. I think it's overly complicated by the poster's attempt to automatically set it up each time. If you just want to copy/move between instances, doing a `bind --mount` on one of them to `/mnt/wsl` is probably sufficient. But who am I to judge for being "overly complicated" - I use Ansible to set up my new WSL instances ;-). – NotTheDr01ds Jan 20 '21 at 23:23
  • 1
    I've adapted a method from the Github issue referenced in the first comment, along with a few other methods, in [this Super User answer](https://superuser.com/a/1659355/1210833) where it's on-topic ;-) – NotTheDr01ds Mar 03 '22 at 14:35

2 Answers2

23

How to share data between different WSL instances

1. Create shared directory which is not included in any WSL instances

mkdir /mnt/wsl/share

This will create /mnt/wsl/share directory shared across all WSL instances. You can share data via this shared directory.

WARNING: the shared directory's filesystem lives in memory and thus a wsl.exe --shutdown would wipe them from existence.

However, it is still impossible to directly copy/paste data between WSL instances.

2. Create shared directory included in a WSL instance

You can make existing directory in WSL instance shared across other WSL instances by using bind mount.

For example, you have two WSL instances, Ubuntu-A and Ubuntu-B.

1. Open ~/.profile in Ubuntu-A and add below code.

# bind mount shared directory
if [ ! -d /mnt/wsl/share-a ]; then
  mkdir /mnt/wsl/share-a
  wsl.exe -d Ubuntu-A -u root mount --bind / /mnt/wsl/share-a/
fi

2. Open ~/.profile in Ubuntu-B and add below code.

# bind mount shared directory
if [ ! -d /mnt/wsl/share-b ]; then
  mkdir /mnt/wsl/share-b
  wsl.exe -d Ubuntu-B -u root mount --bind / /mnt/wsl/share-b/
fi

This will automatically mount the root directory (/) of Ubuntu-A to /mnt/wsl/shared-a/ and do the same thing for Ubuntu-B when you launch WSL.

Jinsu Park
  • 246
  • 2
  • 4
  • On item 2, I put the following in my ~/.profile ``` if [[ -n "${WSL_DISTRO_NAME}" ]]; then if [[ -d "/mnt/wsl/"${WSL_DISTRO_NAME}" ]]; then ls "/mnt/wsl/${WSL_DISTRO_NAME}" – intel_chris Jul 04 '21 at 17:06
5

On item 2, I put the following in my ~/.profile (well technically my .bash_profile).

if [[ -n "${WSL_DISTRO_NAME}" ]]; then
   if [[ -d "/mnt/wsl/${WSL_DISTRO_NAME}" ]]; then
      ls "/mnt/wsl/${WSL_DISTRO_NAME}"
   else 
      mkdir "/mnt/wsl/${WSL_DISTRO_NAME}"
      # note the terminating / on the directory name below!
      wsl.exe -d ${WSL_DISTRO_NAME} -u root mount --bind / "/mnt/wsl/${WSL_DISTRO_NAME}/"
   fi
fi

The result is every distro I start gets its root directory mounted with the "obvious" name.

roachsinai
  • 527
  • 5
  • 14
intel_chris
  • 708
  • 1
  • 6
  • 17
  • 2
    I find it cleaner to do the same thing through `/etc/fstab` as documented in [this Super User answer](https://superuser.com/a/1659355/1210833). One line in `/etc/fstab` handles all of that logic. – NotTheDr01ds Mar 03 '22 at 14:38