3

I don't want to permanently map remote disks from production systems, I want to click on shortcut to map the disk and than click another shortcut to unmap it. The sshfs works fine when used from Bash. However when I run the command from shortcut with wsl.exe, it maps the disk but as soon as the command ends, mapping is removed. This can be replicated by running wsl.exe from PS:

PS C:\Users\user> wsl.exe --user root -- sshfs -o allow_other,default_permissions,ro user@machine:/path /mnt/path `&`& read -p "Press enter to continue"

After the sshfs is executed, I can see that the disk is mapped but when I press enter, the disk is unmapped. It seems that there is some kind of "console session" and the mapping is related to the session only, once the session ends, the mapping ends.

I have no idea what's going on, I am missing something simple but fundamental. Thank you for any pointers. And no, using bash.exe does not fix the problem.

jan_bar
  • 33
  • Thanks for posting this over here. Don't forget to delete the one on Super User. One question as I'm looking into a solution -- Are you entering the password each time you launch it? Or using agent or a key without a passphrase? – NotTheDr01ds May 19 '21 at 21:43
  • Also, help me understand the use-case a little bit better. When you have the mount "active", what will you be doing with it? Will you be in a different WSL session working with the mount? Or are you going to be using the files through something else like VSCode? I was thinking maybe \\wsl$\..., but that doesn't seem to work for mounts. – NotTheDr01ds May 19 '21 at 22:07
  • Sorry, first comment should have been "Don't forget to delete the one on Stack Overflow". – NotTheDr01ds May 20 '21 at 00:50
  • @NotTheDr01ds post deleted. I prefer password as the Linuxes are linked to DC controller via Realm. I run WSL all the time and just want to map and eventually unmap server logs so I can use my Windows tools to analyze them. Yes, I access the mapped folders with \wsl$\Ubuntu-20.04\mnt – jan_bar May 20 '21 at 11:48
  • @NotTheDr01ds Thank you for your time. I also tried with fstab noauto and mount. maybe the ssh session is the problem. – jan_bar May 21 '21 at 06:47

3 Answers3

1

So I came up with two possible solutions for this. There's the "hard way" that works more like you seem to be expecting, and an "easy way" that works almost like you seem to want.

This is the "hard way", but it's not too bad, really. I'm just verbose ;-) ...

I'm making an assumption that you can at least use a passphrase-protected keypair for connecting, rather than just a simple password. As long as we can get the password/passphrase before launching sshfs, it gets a whole lot easier.

I'm also making an assumption that you are using Ubuntu, but the basic steps here should work in any distribution.

  • Set up your ssh keys on the client (WSL) and the server

  • Install keychain (sudo apt-get install keychain)

  • Install tmux if needed (should already be installed in the default WSL Ubuntu distro)

  • Make sure you can connect using keychain/ssh-agent as the user (root in this case). From inside the WSL instance (bash):

    eval $(keychain --eval ~/.ssh/id_rsa) # Should ask for key passphrase
    sshfs -o allow_other,default_permissions,ro user@machine:/path /mnt/path # Should connect without requiring a password or passphrase
    
  • Assuming that works, for your "connection script/command" in PowerShell:

    wsl -u root -e bash -c 'eval $(keychain --eval ~/.ssh/id_rsa) tmux new-session -s sshfs -d sshfs -f -o allow_other,default_permissions,ro user@machine:/path /mnt/path'
    
  • Your "disconnect script" is:

    wsl -u root umount /mnt/path
    wsl -u root keychain --stop all
    

Breaking that down, the first command:

  • Requests your passphrase via keychain so that the sshfs command itself doesn't require any user input.
  • Sets up a disconnected tmux session using -d (so WSL keeps running in the background)
  • Starts sshfs in the foreground of a window in that tmux session (using -f). However, since the tmux session is disconnected, you won't be able to directly enter the password there; thus the need for keychain above.

When the disconnect is run, it simply unmounts the connection, which causes the sshfs running in that tmux window to exit, thus causing the tmux session and server to end. All that's left to clean up at that point is the ssh-agent left by keychain. Once that's gone, the WSL instance itself will terminate itself, assuming you don't have anything else running in it.

NotTheDr01ds
  • 21,923
  • Thank you @NotTheDr01ds for the in depth explanation. I did not mentioned that I run WSL instance all the time, so there is no need to power/terminate it with mount/umount command. That is maybe part of your "easy way" almost solution? Not sure I understand the underlying problem - it seems that WSL keeps a hidden terminal connected to running instance and uses that terminal to run linux commands. When I run another wsl.exe, it creates another hidden terminal that terminates after command ends. The solution is to use tmux running on background. – jan_bar May 20 '21 at 07:28
  • Honestly, I can't determine the reason this is happening either. WSL doesn't seem to "keep a hidden terminal" or anything; it spins one up for each invoked instance. For some reason sshfs behaves differently than other services, though. For instance, starting sshd through the wsl command works fine and continues running as a daemon after the command exits. sshfs seems to spawn two processes, though, and it may be that the way they are spawned is causing the parent process (init in this case, it seems) to think that it can exit. – NotTheDr01ds May 20 '21 at 17:57
1

So I came up with two possible solutions for this. There's the "hard way" that works more like you seem to be expecting, and an "easy way" that works almost like you seem to want.

This is the "easy way":

Just run sshfs using the -f flag to keep it from exiting:

wsl -u root sshfs -o allow_other,default_permissions,ro user@machine:/path /mnt/path

That's going to keep a window or PowerShell window open, of course. But if you are setting it up as a shortcut, you could always set it to run Minimized.

To terminate the connection, run:

wsl -u root umount /mnt/path

Once the connection is terminated through unmounting, the sshfs command will exit, and the previous window will close.

Since you already had a similar solution with the read command, I'm guessing that's not quite what you want, though. Hence the "hard way" solution that I provided as well.

NotTheDr01ds
  • 21,923
0

I finally settled with the two shortcuts using detached screen and I need to type the password. The two solutions above might work better for someone else but helped me to find my solution.

Mount server:

C:\Windows\System32\wsl.exe --user root -- screen -S "server" bash -c "mkdir -p /mnt/server; sshfs -o allow_other,default_permissions,ro user@server:/folder /mnt/server; echo 'Press Ctrl-a, Ctrl-d'; exec sh;"

Explanation - screen will create new session with name "server" and run the bash command in quotes. Bash will create mount folder (optional), sshfs will prompt for password, echo will ask user to detach the session and exec sh will run indefinitely. The Ctrl-a, Ctrl-d will detach screen and it will run on background.

Umount server:

C:\Windows\System32\wsl.exe --user root -- screen -X -S "server" quit

Explanation - this will quit the detached session and also the sshfs mount,

jan_bar
  • 33
  • Right - I was thinking about mentioning the "detach" option with my tmux answer, but it didn't sound like that was quite what you wanted. Good to hear you found a solution, though. – NotTheDr01ds May 22 '21 at 16:09
  • Also, it may not matter, but I still recommend using the umount command to terminate the session, rather than forcing it closed by killing the parent. Since you are dealing with a filesystem mount, it's typically considered "safer", I think. Using umount should wait until open files are closed, etc. Forcing disconnect could cause file correction. Running with the -f will exit, as I mentioned in my answers, when you umount. – NotTheDr01ds May 22 '21 at 16:12
  • You are right with umount. Thank you for your time. – jan_bar May 24 '21 at 11:38