You are not logged in.

#1 2016-09-22 05:15:26

dtjohnst
Member
Registered: 2007-03-01
Posts: 85

[SOLVED] Unit Files with automount dependency

Hi Folks,

I'm hoping someone can help me with this, given that I'm new to systemd and google/forum search hasn't seemed to provide any insight...  Which I thought was odd, given that surely someone has done what I'm trying already. Maybe it's just really trivial and I'm not seeing it.

I had this working in the days before systemd and I could probably make it work again but it wouldn't be quite as graceful, so I'm trying to adjust everything for the new paradigm. Of relevance to this project, I have a NAS server and a standalone Linux box that does some basic "moving around of files" including your basic newsgroup downloader. Some of this stuff happens with sftp/scp but if you can help me get it working for newsgroups, I am sure I can figure the rest on my own. Essentially, I want to modify my newsgroup service so it depends on my "downloads" folder which resides on my NAS being mounted. I've already get my fstab set up so that systemd-fstab-generator is creating my mnt-downloads.mount and mnt-downloads.automount unit files. Ideally, I'd like my newsgroup service (sabnzbd.service) to start when the drives are mounted, and stop when they are dropped, but I'm not quiet sure how to work my drop-in file to make this happen.

In the OpenRC days, I just had a script that, whenever my NAS needed to be updated or drives rebuilt, autmoatically stopped my downloading/transfer services and unmounted the drives. Once the NAS was back up, it sent a command again to remount the drives and fire up the services. With .mount and .automount service files, I'm hoping this can be more robust. For example, "in the old days", if my NAS stopped for some other reason (maybe it crashed and didn't shutdown gracefully or reboot, or my cat chewed it's network cable and the connection dropped), things got pretty messy. My understanding with systemd's  mount and automount is that this should not be an issue provided I can tell my services to "stop writing to those folders" when the drives can't be accessed.

I guess my first question is: Am I understanding this correctly? Or do the old problems still exist?
Secondly: do I want to have my drop-in files create dependencies on mnt-downloads.mount or mnt-downloads.automount? Or something else?
Thirdly: Is a requires dependency what I want, or should I BindTo the mount/autmount unit instead to make sure the service stopes? I feel like BindTo is the right answer, but confirmation is always nice.

On the flip side, is all this a terrible idea, and I should remove systemd autmounting from my fstab and just use the same scripts I did back in the OpenRC days that make it fairly idiot proof except when my cat gets hungry for CAT5?

Last edited by dtjohnst (2016-09-22 20:45:32)

Offline

#2 2016-09-22 05:18:05

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] Unit Files with automount dependency

Not a Sysadmin issue, moving to NC...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2016-09-22 20:45:20

dtjohnst
Member
Registered: 2007-03-01
Posts: 85

Re: [SOLVED] Unit Files with automount dependency

In case anyone finds this later and is looking for the answer, it actually is completely trivial and I'm ashamed to say I can't believe I even posted it. I guess that's what I get for trying to get creative about automation at 1AM after a bottle of wine. Took me about 30 seconds to figure out which services I needed to create dependencies for.

First step: make sure my directory is mounted and check service status:

> mount /mnt/downloads
> systemctl status mnt-downloads.automount | grep Active
Active: active (mounted) since Thu 2016-09-22 13:51:53 EDT; 5min ago
> systemctl status mnt-downloads.mount | grep Active
Active: active (mounted) since Thu 2016-09-22 13:51:53 EDT; 5min ago

Next umount the drive and check again:

> umount /mnt/downloads
> systemctl status mnt-downloads.automount | grep Active
Active: active (waiting) since Tue 2016-09-20 05:48:11 EDT; 2 days ago
> systemctl status mnt-downloads.mount | grep Active
Active: inactive (dead) since Thu 2016-09-22 14:00:19 EDT; 38s ago

So. If you want a service to be terminated when an automount drive is dismounted, a simple drop-in file that creates a BindTo the appropriate .mount unit will do the trick. In my case, a drop-in for sabnzbd looks like this:

> cat /etc/systemd/system/sabnzbd.service.d/mnt-downloads-bind.conf
[Unit]
BindsTo=mnt-downloads.mount
After=mnt-downloads.mount

The "After" may not be needed, the services starting in parallel might not cause any issues, but I figured it didn't hurt to wait the milliseconds it takes to mount a drive before firing up my newsgroup downloader.

Offline

Board footer

Powered by FluxBB