Friday, March 04, 2016

Easy automounting sshfs

I use sshfs a lot.  Mostly because Norton Commander rocked my world when I first encountered it in the summer of 1994 (bear with me).  I was about to say I was done after that, but that's not quite true.  In 1996 I discovered pushd/popd (unfortunately limited to bash at the time) and the staggering power of the Korn shell's history editing and I had nearly everything I needed to be happy with my interface to computers.  Then I found Midnight Commander on one of those Linux Software Archive CDs in late 1996 and the final piece fell into place.

Alright, enough ancient history.  The point of all of this is that when I discovered MC's Virtual Filesystem functionality I didn't think I'd ever need any other remote tool.  Being able to treat FTP connections like local filesystems was mind-blowing.  Then I started getting into SSH and found I could do the same thing with SFTP and later SSHFS connections.  Absolutely outstanding.

It's a shame the method to connect to these Virtual FS systems was such an EMACS-ish combination of hotkeys and things (at least in the early days) that looked nothing like a URI and didn't really seem to follow any sensible pattern.  So I used it less than I otherwise would unless it was something I already had set a bookmark for.  Because reading help sucks and because all too often you'd be hitting F1 for help in MC and instead get some garbage from your desktop environment or window manager.  I know, solvable problems which I've long-since resolved, but back when it really mattered, it was an issue for me.

That brings us to the last, say, two years.  I don't use MC anymore.  At least not very much.  Probably more than half the machines I work with don't even have mc installed anymore.  But I use sshfs all the time because I love the ability to treat remote systems like they are local.

So this is what I've been doing until recently.

# sshfs joe@meathead:/ meathead/
# sshfs pi@retropie:/ retropie/

(Yeah, I'm one of those guys.)

That works pretty well.

Now here's something you're not going to hear out of me very often.  I kind of miss NFS.

Okay, what I actually miss is auto-mounted NFS shares.  This week I started down the path of creating a script to automatically mount my regular SSHFS locations on boot.  That had two obvious problems right out of the gate:
  • Mounts won't come back automatically if the machine reboots
  • Mounts would only happen automatically on boot of my machine
Plus the third, more subtle but still core-to-my-very-being one:
  • Someone else must have already solved this problem
Because that's how I approach nearly every problem.  First assume I'm not the first one to trip over something.

Turns out I was right.

There's a lot of solutions for this particular problem of varying levels of fugly.  This one (specifically the afuse one) documented over at the Arch Wiki turns out to be just the trick:

# afuse -o mount_template='sshfs -o ServerAliveInterval=10 -o reconnect %r:/ %m' \
> -o unmount_template='fusermount -u -z %m' ~/mnt/ssh

So a little bit of that action and now instead of having a bunch of fixed mounts and a clunky script that'll need a bunch of options and checking to see if something's already mounted or whatever, I just dump the above afuse command in my Openbox autostart script and now I can just do this:

# cd ~/mnt/ssh/pi@retropie/

and magic happens.  Initially I thought it would be a bit of a down-side to dedicating a whole directory to afuse, but it turns out I like it a lot better because now I have all of my 'remote' systems in a single hierarchy but I can still treat them like local filesystems for all intents and purposes.  It's pretty sweet.  Should've done this years ago.

No comments: