Using sshfs, mpd, ncmpcpp, and VirtualBox to listen to crap I want when I'm not home

I use a bunch of programs with impossible-to-pronounce names to kludge together a way to listen to my music library when I'm away from home.

I don't generally like streaming media. Sure, the convenience is nice, especially if you only ever watch/listen to something one time. Once you watch/listen the second or third or fortieth time, though it turns into a waste of resources to keep downloading and throwing away the thing you're watching/listening to.

Another thing you need to know about me for the context of this article is that I don't generally listen to music while I work. Either at the office or when I'm working at home, I just don't turn music on all that much for 'background noise'1.

But sometimes I do want to hear a particular piece of music for one reason or another. If that urge strikes me when I'm at my work computer, what's a person to do? Just dumping my music collection on my work computer is a dumb idea2. Putting my collection on a thumb drive and plugging it into my work computer is a slightly less dumb idea, but it's inconvenient, especially if I need to use the music player on my computer for work purposes and lose my place or my playlist. My iPod broke a long time ago, so that's out, too. I considered replacing it with a cheape .mp3 player, but 1) cheap .mp3 players that are just .mp3 players don't seem to exist as such these days, and 2) I don't really want to carry around another piece of tech.

It looks like I might have to hold my nose and look at streaming my media back to myself over the Internet. The problem, though, is that my ISP gets really grumpy if you host any kind of server on your own computer, and they get extra grumpy if you try to submit DNS entries for their addresses (unless you pay up for a business plan, which I'm not doing yet).

I had briefly considered spinning up a virtual server and something like OwnCloud, but then I thought that that sounded like a lot more work than I wanted to do. I remembered that I already have ssh access to my home computer so I can serrupticiously browse the Internet, check email, update my website, and so on, without the prying eye of the corporate overlords watching in. They just see some ssh traffic, which isn't unexpected for someone in my line of work.3, and I knew that I could use something like sftp to transfer files around, but I wondered if I could make the connection more like a mounted drive and less like me just downloading a file to my work computer (which I'm trying to avoid).

I have to run Windows at work, and there's no real getting around that (spoiler: there's kind of a way around that). I found a program called Mountain Duck that claimed that it would let me connect to an sftp share and mount it as a Windows folder, but it had a couple of problems. The big one was that it wanted me to pay for it. Out of principle, I won't put software that I've paid for on a work computer. Ever. But another problem is that I don't like leaving things on my computer that are too personal. Like usernames and passwords for my personal services or password managers that are automagically logged in or automatic connections to my home computer's music files, in the event that I leave the company unexpectedly (also, for plausible deniability).

I was going to use the Windows Subsystem for Linux, but that might leave more traces around than I'd like, but I installed VirtualBox some time ago for testing. Testing what? Don't worry about it! The point is that I can use VirtualBox to set up a virtual machine with access to the Internet, and it's really easy to delete the evidence if necessary4.

I set up a new copy of FreeBSD in a new virtual machine. I installed audio/musicpd, audio/musicpc, ncmpcpp, and sysutils/sshfs. I also went into my /boot/loader.conf and added the line fuse_load="YES" to load the fuse kernel module

I created a mountpoint in the root of my filesystem called 'music' mkdir /music, and I changed permissions on the folder so that it belongs to my owner and the mpd user chown myusername:mpd /music. The mpd user needs to have access to this folder so it can play music out of it.

I changed the music_directory line in the musicpd.conf file to point at the /music folder

/usr/local/etc/musicpd.conf
music_directory  "/music"

Next came the magic part: using sshfs to mount a folder on my home computer to /music. To do that, I ran a command like this one:

sshfs -o idmap=user -o port=22 -o allow_other -o reconnect username@000.000.000.000:/path/to/music /music

It looks complicated, but it really isn't.

  • -o idmap=user Maps the UID/GID of the remote user to the UID/GID of the mounting user (this was suggested in the package info when I installed it)
  • -o port=22 I actually use a different port than 22, so I have to specify that here
  • -o allow_other I have to use this to give the mpd user access to the mounted filesystem
  • -o reconnect the connection hiccups a lot and sometimes drops if I leave it idle for a while. This should mitigate that
  • username@000.000.000.000:/path/to/music My username on my home computer, @, the ip address of my home Internet connection, :, the absolute path the the folder where I keep the music I want to listen to
  • /music The directory on the local machine I want to use to mount the remote filesystem

Whew!

It turns out that my SSHFS connection would still drop out occasionally anyway. The problem was that after staying idle for a period of time, the server would decide that the client was gone and just close the connection. The solution was to add the following line to the root user's .ssh/config file, which sends a keepalive signal every five minutes.

/root/.ssh/config
ServerAliveInterval	300

I ran into a few snags here. First is that I'm using password-based authentication because that's what I'm using. That means that when this command is run that I have to type in my password. Not a big deal, but that does mean that I can't just put something into my /etc/fstab and have it connect at boot time. This is also not that big of a deal since if my home computer is down and I start this VM it might take a while to connect.

The second snag is that I couldn't get this command to work for any user other than root no matter what I did. I followed forums, man pages, and anything else I could find. The consensus was that you had to set vfs.usermount=1 (I did), your username has to own the mountpoint (it does), and your user has to have read/write permissions to /dev/fuse (it does). But it still doesn't work. I'll continue working on what I might have done wrong and update this if I ever figure it out.

Once I mounted the music folder and checked it to make sure that I could see stuff in it (by navigating to it and using ls I triggered a database update: mpc update. Then I waited. I have about 30GB of files for it to go through, and refreshing the database took about 20 minutes. Thankfully I shouldn't have to do this too often unless my music library changes (and if it does, I can always pretend that I'm going to go grab a coffee5).

Once the database refresh is complete, I can start up ncmpcpp the usual way and browse my library. Changing songs is slow, as you might expect. I also found that the sound seemed to hiccup occasionally. I'm not sure if this is a result of a momentary dropout of my internet connection or something else. It's not common enough for me to worry about.

I also have to make sure that I reconnect the sshfs folder every time I reboot the virtual machine, but that's not super onerous. I put the command into a one-liner shell script so I can run that instead of trying to find it in my hisotry.

Footnotes

  1. In face, I find the whole concept of having to have some kind of music or video on the background to provide 'background noise' a completely foreign concept that I have never been able to wrap my head around, but this footnote is already long enough.
  2. Though I've seen it done more than once by people that really should know better.
  3. This article assumes that you already have OpenSSH installed and configured. I won't be going over it, sorry.
  4. What counts as 'necessary'? I'll know it when I see it. I hope.
  5. I don't drink coffee.


Read more FreeBSD articles · Go back to the homepage