Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share directory between distros #5177

Closed
giggio opened this issue May 6, 2020 · 22 comments
Closed

Share directory between distros #5177

giggio opened this issue May 6, 2020 · 22 comments

Comments

@giggio
Copy link

giggio commented May 6, 2020

Describe the solution you'd like
In WSL2, I'd like to be able to share a directory between distros. I can see it is possible, as Docker for Windows is doing it by creating devices (/dev/sdd, /dev/sdc on my system, using ext4) and mounting them on /mnt.

I see 2 options:

  1. A data drive, that does not belong to any distro and is shared by the ones I want.
  2. A directory could belong to a distro, and be shared with others.

You could also try to change something on the namespaces and maybe use symbolic links between distros, that would be good enough, and wouldn't need mounting. I'm not sure how feasible that would be. I don't really care about the implementation.

Describe alternatives you've considered
I can't see a clear alternative.

Additional context
My use case it that I'd like to share /home/user. It would save space and help a lot.

@dseynhae
Copy link

dseynhae commented May 6, 2020

I currently already share a directory available on the Windows platform, as my home directory for the different distros I have. This looks like what you're after:

  1. Somewhere in my Windows 10 infrastructure, I have a ...WSL_HOME/<user> directory.
  2. Note that this directory is on one of my drives (i.e. C:), and therefore show up in WSL (under /mnt/c)
  3. In my WSL distros, I bind that directory to a directory in my WSL infrastructure (in the /etc/fstab file):
#   /etc/fstab
...
C: /mnt/c drvfs rw,noatime,metadata,uid=1000,gid=1000,umask=022,fmask=011
/mnt/c/.../WSL_Home /<user> /home/<user> drvfs bind

This requires that the /home/<user> directory is empty..., so you need to move all the files and directories from your original WSL created home directory, to the mounted location in Windows first...

You could also bind this directory to any other directory (e.g. /mnt/h), and then change your /etc/passwd file to make that directory your home directory...

@onomatopellan
Copy link

The second option is actually implemented. You can create a directory in /mnt/wsl/, mount whatever you want on it and it will appear on every distro installed.

For example, just adding this to my Ubuntu ~/.profile it automatically mounts on launch an external Alpine /home folder that appears in every distro under /mnt/wsl/Alpine.

# mount external /home folder from Alpine distro
if [ ! -d /mnt/wsl/Alpine ]; then
    mkdir /mnt/wsl/Alpine
    wsl.exe -d Alpine mount --bind /home/ /mnt/wsl/Alpine/
fi
@dseynhae
Copy link

dseynhae commented May 6, 2020

I was not aware of the second option, thanks @onomatopellan !
I did now find #4577, which explains why I'm not aware of it :-).
Also, it seems that this crossDistro option is only available for WSL2?
I'm still stuck with WSL...

@onomatopellan
Copy link

onomatopellan commented May 7, 2020

@beltaurus Yes, it's a WSL2 only thing, since all the distros in WSL2 are just "containers" running on a single kernel/lightweight VM.

@giggio
Copy link
Author

giggio commented May 7, 2020

I was not aware of this functionality, @onomatopellan, very cool.
Unfortunately, /mnt/wsl uses tmpfs, so when wsl shutsdown you loose everything that was there. What I could do is when Ubuntu (my main distro) comes up, it adds a symlink from /home/user to /mnt/wsl/homeuser/, and when OpenSuse comes up, it does the oposite, it mounts from /mnt/wsl/homeuser/ to /home/user.
The problem is that if OpenSuse starts before Ubuntu, there will be nothing at /mnt/wsl/homeuser/.
So it kinda works, but there are issues.

@dseynhae
Copy link

dseynhae commented May 7, 2020

@giggio : Can you use my suggestion then? Or are there other obstacles with sharing a given windows directory over the different distros?

@onomatopellan
Copy link

onomatopellan commented May 7, 2020

@giggio Correct, it's tmpfs but you only loses the folder used as mount point. In my case I can modify the Alpine home files from Ubuntu and, of course, the changes are preserved after wsl shutdown.

I can understand why this feature is undocumented and so far it's only used by Docker devs. Things can become dangerous fast, especially with different user file permissions. But it's still very useful and the most important thing to know is this:

Every time you launch a distro it attaches that distro's VHDX to the running WSL2 VM as a new sdx device.

You can observe that with sudo lsblk for example. After knowing that it's the user's decision when and where to mount them although /mnt/wsl/ will still be the only folder truly shared by all distros.

@beltaurus For WSL2 users /mnt/c/ like folders are terribly slow. So it's better to save things in the /home folder.

@giggio
Copy link
Author

giggio commented May 7, 2020

Yes, I'm not using Windows because it's slow at the moment.

I don't like that I have to have another distro running to be able to mount. There should be a way to mount a device to multiple distros, a device that does not belong to any of the distros.
They are just separated by namespaces, they could share that.
For example, I create a device (using wsl.exe), and it is auto mounted to any distro that starts up.

@onomatopellan
Copy link

onomatopellan commented May 9, 2020

@giggio It looks like the Docker team already asked for something similar #4730.

Something like that solution would be great.

@beardedeagle
Copy link

@onomatopellan I seem to only be able to mount a single distro with this method. Do we happen to know if this by design?

if [ ! -d /mnt/wsl/NewishUbu ]; then
    mkdir /mnt/wsl/NewishUbu
    wsl.exe -d NewishUbu mount --bind /home/beardedeagle/ /mnt/wsl/NewishUbu/
fi

if [ ! -d /mnt/wsl/OldUbu ]; then
    mkdir /mnt/wsl/OldUbu
    wsl.exe -d OldUbu mount --bind /home/beardedeagle/ /mnt/wsl/OldUbu/
fi

which produces the following:

mount: /mnt/wsl/OldUbu/: mount point does not exist.
@onomatopellan
Copy link

onomatopellan commented Jul 28, 2020

@beardedeagle are you sure both distros are WSL2? If OldUbu is WSL1 it won't work since there is no sharing /mnt/wsl in WSL1.

@beardedeagle
Copy link

@onomatopellan yup, they are all wsl2:

PS C:\Windows\System32> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu                 Running         2
  OldUbu                 Stopped         2
  NewishUbu              Stopped         2

To clarify both NewishUbu and OldUbu started their life as wsl1 containers on another machine. I imported them as wsl2 though.

@onomatopellan
Copy link

onomatopellan commented Jul 29, 2020

@beardedeagle I just tried with two distros and it works. I think the mount command should be called by the root user which is default in Alpine but not in Ubuntu.
Try with this:
wsl.exe -d OldUbu -u root mount --bind /home/beardedeagle/ /mnt/wsl/OldUbu/

After that you should see if it did work with lsblk

$ lsblk
NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda    8:0    0  256G  0 disk
sdb    8:16   0  256G  0 disk /
sdc    8:32   0  256G  0 disk /mnt/wsl/Alpine
sdd    8:48   0  256G  0 disk /mnt/wsl/OldUbu
@therealkenc
Copy link
Collaborator

The shared /mnt/wsl directory behavior mentioned in #4577 (message) shipped to Insiders sometime last fall and landed in 20H1 aka 2004 aka 19041.

Creating a bind mount inside a Ubuntu-18.04 instance would look something like...

$ sudo mkdir /mnt/wsl/Ubuntu-18.04
$ sudo mount --bind / /mnt/wsl/Ubuntu-18.04  # share all of /

... but you can pick whatever you want for the source and target of the mount, so long as the target is under /mnt/wsl. The feature is only available in WSL2.

/fixed 19041

@ghost ghost closed this as completed Sep 1, 2020
@ghost ghost added the fixedininsiderbuilds label Sep 1, 2020
@ghost
Copy link

ghost commented Sep 1, 2020

This bug or feature request originally submitted has been addressed in whole or in part. Related or ongoing bug or feature gaps should be opened as a new issue submission if one does not already exist.

Thank you!

@giggio
Copy link
Author

giggio commented Sep 1, 2020

@therealkenc this issue has not been addressed.

Yes, you can share a drive from a distro to another, but I don't want that. I want a drive that is not part of any distros, so I can add my home directory there and use it in any distro that boots first. I don't want to have to boot them in order.

Also, please see this comment with other issues: #5177 (comment)

It is not possible to do what I need with today's implementation.

@therealkenc
Copy link
Collaborator

That's #689. [If this issue was interpreted as "I want a drive that is not part of any distros" contrast issue title "Share directory between distros" it wouldn't have survived a dupe-closed in May.]

Unfortunately, /mnt/wsl uses tmpfs, so when wsl shutsdown you loose everything that was there.

Yeah, there's that. Feel free to open a new feature request "persist /mnt/wsl mountpoints" or somesuch.

@justinmchase
Copy link

In my default distro (which was upgraded from wsl1 a long time ago) I do not have the folder /mnt/wsl. I have no idea why not.

Simply creating the folder and then following the instructions in this thread do not seem to work, the mounted folder is not available in other distros.

Is there a way to properly repair the distro such that the /mnt/wsl folder is mounted properly?

@onomatopellan
Copy link

@justinmchase Make sure you are in a WSL2 distro with wsl.exe -l -v and you have latest linux kernel installed with uname -a. Running mount you should see something like tmpfs on /mnt/wsl type tmpfs (rw,relatime)

@justinmchase
Copy link

In the end to fix it I had to completely uninstall Ubuntu and then re-install it from the store. This created a new distro which had the /mnt/wsl folder functioning correctly.

I have no idea how I got in that bad state but it was possibly an error in the upgrade process and I never noticed the issue until now.

@horror-proton
Copy link

horror-proton commented Sep 9, 2022

But what if one (or both) of them are WSL1, is there a better way of sharing their filesystems. I guess mounting over network is the only option, but any suggestion on which one to use.
Is it possible to have something like sshfs but piping file through wsl.exe instead of the real sftp1, it could be annoying to have a daemon running on all of the WSL instances.

Mounting something like \\wsl$\a-wsl1-distro as drvfs is possible in WSL2, but it is mounted as Windows filesystem and I don't think it is safe to do so.

a related stackexchance question

Footnotes

  1. I'm not sure if that is how sshfs works

@didibus
Copy link

didibus commented Jul 10, 2023

I'm not very Linux saavy, so how to I make it so my home dir is shared between my WSL 2 distros? Without it being a slow windows mount?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
8 participants