Mount share only if connected to Tailscale

Setup: Pretty common ig, systemd, network manager, systemd-resolved. Pretty much always connected to Tailscale.

Problem: Mount an NFS/Samba share only if connected through tailscale. I am aware that 100.x.y.z is unroutable though local networks due to NAT and thus can directly be used in /etc/fstab but I would like no packet to exit the machine unnecessarily.

Do you have any solutions? Or am I barking up the wrong tree and there’s an alternative to expecting an auto-mount? Closest I’ve come is a systemd.path triggering a systemd.service that checks the status of the tailscale0 interface and mounts if it is up. Thoughts? Any guidance would be appreciated, thanks!

1 Like

It’s definitely a wishlist item for me as well to offer a definable list or even automatically mount shares on a Tailscale connection.

Maybe a quick script that runs a ping check against hello.ts.net; if it can reasonably reach it, utilize autofs to mount the share? Wrap that up in a systemd service that triggers whenever networking comes back online

2 Likes

Was thinking pretty much in the same vein but since I was spending time on it anyways, figured it should be extendable.

Quick rundown of my solution:

Set up a 10s timer to test

systemctl is-active --quiet tailscaled.service && [[ $(tailscale status --peers=false --json=true | jq -r '.Self.Online') = "true" ]]

and start / stop tailscale-online.target. Now, any service that hooks to the target will start and stop with tailscale.

So all that is left is to write out <mountpoint>.automount (with RequiredBy=tailscale-online.target) and <mountpoint>.mount to get autofs mounts on directory access! Just rebooted a couple times and tested manually, seems to do the trick, will update here if I find any corner cases as time passes.

Bonus Tip:

If testing with tailscale status --peers=true, we get the online status of every device and then one can feasibly create something like <hostname>-online.target which will be active only if a particular device is up and connected to the tailnet, great for people who host stuff from laptops / Pis etc.

2 Likes