Introduction
This blog post will explain how a single home mount can be shared accross all the WSL instances.
It was quite fun to find the idea and then make it happen.
It will use all the tools currently available from WSL, so please don't expect a "Linux only" as Interopability will be heavily used.
And before I start, here is my point of view about WSL: It's not only Linux!
It's different, just like GNU is different from Unix (yes I said it).
We have finally the strength of both worlds combined. And while I do understand the "dev" aspect is meant to be reproducible in a Linux prod environment, the "ops" side is meant to take advantages of everything that can make the full environment feeling Home.
Setup requirements
In order to really enjoy this solution, I do recommend having 2 or more WSL distros installed or, if you fell like a real WSLCorsair, try @bketelsen crazy setup (I love it)!
While the distros are downloading, let's prepare the Windows side (read: Interopability) of the solution:
- Create a folder in any physical drive (C, D, X, ...) and give it a nice name (i.e. home)
- Right click on it and choose "Properties" option
- Click on the "Sharing" tab
- Click on "Share" button
- Normally, your login name and "Everyone" should be listed
- Remove "Everyone" from the list, keeping only your login name
- Click the "Share" button
- Back on the "Sharing" tab, you will now see the Network path of your folder
- It will look like: \\<computer name>\<folder name>
- Close the "Properties" window
Once the directory is shared, what we will do is "map it" as a new drive in Windows explorer:
- In Windows Explorer, click on "This PC"
- Click on the top menu "Computer" and click on "Map network drive"
- Pick a "Drive" letter (i.e. H:)
- Write the "Network path" in the "Path" textbox
- Ensure "Reconnect at sign-in" is checked
- Click on the "Finish" button
Setting up our new Home
Finally, we will be able to mount our "home drive" and thanks to this gem called WSLconfig, it will be possible to mount automatically every time a new terminal is opened (with a little help as described below):
Open a terminal of your favorite distro (i.e. Ubuntu)
PS client> ubuntu.exe
[Optional] Edit the sudoers file to be password less for your account
client:~$ sudo visudo
...
<username> [tab] ALL=(ALL:ALL) [tab] NOPASSWD: ALL
...
:wq!
WSLconfig needs a config file to be created "inside" the distro
client:~$ sudo vi /etc/wsl.conf
# Enable extra metadata options by default
[automount]
enabled = true
options = "metadata,umask=22,fmask=11"
mountFsTab = true
# Enable DNS – even though these are turned on by default, we’ll specify here just to be explicit.
[network]
generateHosts = true
generateResolvConf = true
:wq!
In the config file, we enable the "fstab" to be read when the terminal is launched, so the next step is setup the Home mount point in it
client:~$ sudo vi /etc/fstab
H: /home drvfs rw,noatime,uid=1000,gid=1000,umask=22,fmask=11 0 0
:wq!
If you close the terminal now and launch a new one, unfortunately the "fstab" is not read and the "home" filesystem is not mounted. Here is a small hack how to do it (for all users)
client:~$ sudo vi /etc/bash.bashrc
...
sudo mount -a && cd
:wq!
The "mount -a" will mount all the filesystems listed in "fstab"
And because we are already logged in, we need to "move" in the mounted filesystem by simply doing a "cd"
And because we are already logged in, we need to "move" in the mounted filesystem by simply doing a "cd"
And now, repeat for the other distros and you will get a single home for all your WSL instances:
Bonus
as the now the "home" filesystem is mounted as the "windir" (i.e. /mnt/c), you are free (to be confirmed by the WSL team) to edit the files in it with Windows programs (Interopabilitiy).So if you have VSCode installed on your Windows instance, then you can simply do
client:~$ code mySourceCodes/hat.go
Enjoy and let me know in Twitter if this solution helps you and/or if you came with your own solution.
>>> Nunix Out <<<
It's missing a step to copy your *existing* /home/user stuff into the mounted home. Otherwise AweSOME! I'm trying it now.
ReplyDeleteohhh a reply, sorry was not even awaiting one :) ... but you shared it in your blog post which is way better, but thanks, I will correct it
DeleteI must be missing something because those mount options fail with bad superblock... in fstab or command line... without any mount options it mounts but obviously as root:root
ReplyDeletechris / mnt sudo mount -t drvfs -o rw,uid=1000,gid=1000,umask=22,fmask=11 H: /mnt/h
mount: wrong fs type, bad option, bad superblock on H:,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
chris / mnt sudo mount -t drvfs -o rw,umask=22,fmask=11 H: /mnt/h
mount: wrong fs type, bad option, bad superblock on H:,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
chris / mnt sudo mount -t drvfs H: /mnt/h
chris / mnt