Skip to main content

WSL: One Home to host them all

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 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 <<<

Comments

  1. It's missing a step to copy your *existing* /home/user stuff into the mounted home. Otherwise AweSOME! I'm trying it now.

    ReplyDelete
    Replies
    1. ohhh 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

      Delete
  2. I 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

     chris  /  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  

    ReplyDelete

Post a Comment

Popular posts from this blog

Docker + WSL: Get 2 daemon for the price of 1

Introduction almost two years ago, Docker announced the capability of switching between the Linux and Windows containers "mode" (far from the right click that we have today). At that time, I wrote a blog post on how to run both daemons at the same time ( http://darthnunix.blogspot.ch/2016/10/docker-for-windows-2-daemons-enter-in.html ) Fast forward to 2018, and while we were blogging on how to get the TLS connection from WSL docker client with  Rory McCune  ( https://raesene.github.io/blog/2018/03/29/WSL-And-Docker/ ), another blog post, by Stefan Stranger drew my attention (read: blew my mind) as I was trying to reproduce the same: how could I "bind" the docker socket in WSL with the Docker for Windows Linux mode socket ( https://blogs.technet.microsoft.com/stefan_stranger/2018/04/02/access-my-docker-for-windows-kubernetes-cluster-from-debian-wsl/ ) From 1 to 2 daemon: DemonHunter mode achieved Now that we have all the required setup resources, let's b...

VSCode + WSL: the SSH way

The initial idea early 2017, Rich Turner made a demo where Visual Studio was used to develop C++ code, however the debugging environment targeted was WSL! With some magic, he showed something that he had forbidden from the initial release of WSL: writting directly to the WSL filesystem (still a big no-no actually). Of course, this triggered a lot of curiosity and the WSL Corsair had to loot from it too! SSH to save the day the biggest challenge was to find a way that not only could be easy to setup on both ends, and also would be approved  by the WSL team.  TLDR: if you use the Windows Path, You Die! #ThereBeDragons Another way, more Linux-oriented, needed to be used and hopefully a solution already existed and was simply waiting on being used: SSH🔐 The setup the setup is done in 2 distinct parts: Setup the SSH server on WSL Setup the SSH client on VSCode  While the SSH server is a standard procedure in Linux, finding the right  plugin for VSCode w...