I finished the draft
parent
e3abb34fc1
commit
06619ab5b9
@ -1,123 +0,0 @@
|
||||
# Making my music connection available anywhere
|
||||
; description: I sat down to make all of my music available from anywhere using some SSH tunnels, even though everything lives on my LAN.
|
||||
|
||||
I have been collecting music for about ten years already. Because of that, my whole library is hunderds of gigabytes. I also revel in obscure things, so I have a lot of songs that just aren't available on Spotify, iTunes, YouTube or any other platform. But over the years, keeping everything together was becoming more and more of a nuisance; at first I just had everything on my desktop, since I didn't really have any portable devices on which I listened to music. But then I started using a smartphone, and I had to keep a copy of everything there too. After a while another device joined my flock - a laptop which I used when travelling.
|
||||
|
||||
Keeping the desktop and laptop in sync was very straight forward, I just `rsync`ed everything over SSH, but my phone was much more of a nuisance, until I started using [`rsync` over ADB](/article/rsync-over-adb.html). Moving from one phone to another wasn't much of a pain, because I stuffed everything on an SD card that I could just take out and put in another phone. At least until a few months ago, when I bought a new phone, this time without a memory card slot, which has added a new challenge: I couldn't fit everything into my the phone's internal memory. Also I lately added a few more albums in there, and decided, that it was time to stop doing that, and put my skills to use.
|
||||
|
||||
## Explaining my set-up
|
||||
|
||||
The VPS that I use for hosting my own things has around 50 gigabytes, so there is no way I can keep everything there. But I don't have to, because a long time ago I connected an old ThinkPad to my local network, I use it as a print server and to host some small things that I want accessible only to devices connected to my LAN.
|
||||
|
||||
But there was one main issue - I am behind my ISP's NAT, so I can't just connect to my gateway using my IP. I thought about this for a while, but then the solution dawned on me: SSH!
|
||||
|
||||
I `ssh`ed into the ThinkPad, and tested out if I could forward the port 22 from it to my VPS.
|
||||
```shell
|
||||
user@thinkpad ~> ssh -R 9999:localhost:22 admin@80.211.240.40 -N
|
||||
```
|
||||
Then I logged into my server, and tested out logging into the ThinkPad
|
||||
```shell
|
||||
admin@yuno ~> ssh user@localhost -p 9999
|
||||
```
|
||||
Turned out it worked, and I could SSH into a device on my LAN just using a tunnel. This is great news, because now I can use SSHFS to mount a directory from that device on my server 🤤
|
||||
```shell
|
||||
admin@yuno ~> sshfs user@localhost:/var/music -p 9999 /mnt/music
|
||||
admin@yuno ~> ls /mnt/music
|
||||
# A lot of directories
|
||||
```
|
||||
|
||||
The only thing that was left was automating that.
|
||||
|
||||
## Creating the SSH tunnel automatically
|
||||
|
||||
I can't just schedule creating the SSH tunnel from the ThinkPad at reboot, because if something happens (for example, I reboot my server), the connection will be broken and won't be reestablished. So I went with the following systemd service:
|
||||
```ini
|
||||
#/etc/systemd/system/ssh-tunnel.service
|
||||
[Unit]
|
||||
Description=Estabilish a connection between this computer and the yunohost server
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/bin/ssh -R 9999:localhost:22 admin@80.211.240.40 -N
|
||||
User=user
|
||||
Group=user
|
||||
Restart=on-failure
|
||||
RestartSec=20s
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
and then I enabled it with `sudo systemctl enable ssh-tunnel`. Because of that approach, it just works™ when an SSH tunnel gets established without issues, but if anything goes wrong, it retries doing that every 20 seconds.
|
||||
|
||||
I tested it out by rebooting both the laptop and server a couple of times, and the SSH tunnel was properly reestablished every time.
|
||||
|
||||
## Auto-mounting on the server
|
||||
|
||||
The first thing I tried when doing that was to just configure it by putting the appropriate line in `/etc/fstab`:
|
||||
```
|
||||
user@localhost:/var/music /mnt/music fuse.sshfs auto,_netdev,uid=984,gid=33,identityfile=/root/.ssh/id_rsa,port=9999,reconnect,delay_connect,allow_other 0 0
|
||||
```
|
||||
but after rebooting the server it didn't work, and I wasn't able to find any logs related to why. After tinkering with it for about an hour, I went to sleep, and the next morning I woke up with one thought in my mind: systemd mounts. I didn't know anything about the topic, but fortunately the [official documentation](https://www.freedesktop.org/software/systemd/man/systemd.mount.html) was easy enough to understand.
|
||||
|
||||
After a bit of tinkering, I had the following service file:
|
||||
```ini
|
||||
#/etc/systemd/system/mnt-music.mount
|
||||
[Unit]
|
||||
Description=Mount the music library from ThinkPad
|
||||
|
||||
[Mount]
|
||||
What=user@localhost:/var/music
|
||||
Where=/mnt/music
|
||||
Type=fuse.sshfs
|
||||
Options=auto,_netdev,nofail,uid=984,gid=33,identityfile=/root/.ssh/id_rsa,port=9999,reconnect,defaults,allow_other
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
Note: remember to name the `.mount` file after the mount point it will be using; in my case, I'm mounting to `/mnt/music`, so my file is named `mnt-music.mount`.
|
||||
|
||||
As per the documentation, I created another service file to enable it in systemd, so the directory is mounted automatically:
|
||||
```ini
|
||||
#/etc/systemd/system/mnt-music.automount
|
||||
[Unit]
|
||||
Description=Automount music directory
|
||||
|
||||
[Automount]
|
||||
Where=/mnt/music
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
I ran `sudo systemctl enable mnt-music.automount`, and again tested it out by rebooting both devices; this proved I had a working setup.
|
||||
|
||||
## Setting up a digital library service
|
||||
|
||||
To manage my library, I decided to use [Funkwhale](https://funkwhale.audio/), a great piece of software that basically allows me to do everything I want with my music. It is available in the YunoHost app directory, so setting it up was very easy. After that, I had to import my music, which wasn't so straight-forward anymore. Thankfully, @drlab from the Funkwhale's Matrix room pointed me towards the [appropriate docs page](https://docs.funkwhale.audio/admin/importing-music.html); I knew I wanted an in-place import.
|
||||
|
||||
Because Funkwhale is a Python app, YunoHost uses a virtual environment to not pollute global packages with dependencies. Depending on how you install Funkwhale yourself, running the `manage.py`, you may encounter an error like that:
|
||||
```
|
||||
Traceback (most recent call last):
|
||||
File "/var/www/funkwhale/api/manage.py", line 2, in <module>
|
||||
import django
|
||||
ImportError: No module named django
|
||||
```
|
||||
if you do, it means you have to first activate the virtualenv. To do that, you have to `source` an appropriate file, in my case it is `/var/www/funkwhale/virtualenv/bin/activate`.
|
||||
|
||||
After the environment is set up, I ran the import like so:
|
||||
```shell
|
||||
python /var/www/funkwhale/api/manage.py import_files "18b9dd15-40ad-4a68-a48d-79b7c8200e4c" "/home/yunohost.app/funkwhale/data/music/watykan/Samuel Baron" --recursive --noinput --in-place
|
||||
```
|
||||
when Funkwhale processed all of my music, I had everything accessible via the web interface.
|
||||
|
||||
## Additional notes
|
||||
|
||||
I already all the needed SSH keys generated, but if you don't you will need to run `ssh-keygen`, to generate an SSH key. If you put a password on it, you will have to somehow pass it to the systemd services, but there are various ways to do that. Depending on your approach, different ones may be usable.
|
||||
|
||||
When SSH keys are ready, you can copy them using `ssh-copy-id`, like so `ssh-copy-id user@server`. The script will probably prompt you for the password of the remote user (and may ask for the password to the SSH key itself, if you used that), and if everything went okay, you will be able to log into the box without using a password for the remote user.
|
||||
|
||||
If you get permission denied errors during import/playback, you may want to verify whether you have correct permissions on your files; `uid=984,gid=33` in my systemd mount service file is related to IDs of the user and group that runs the app, and they may differ in your case.
|
||||
|
||||
; tags: english technical shell bash self-hosted hosting music funkwhale workaround tutorial
|
Loading…
Reference in New Issue