You are not logged in.

#1 2015-01-20 00:00:28

o1911
Member
From: Hobart, Australia
Registered: 2009-04-28
Posts: 106

systemd services - automounting, then run rtorrent & nfs-server

Hi all,

My inability to deal with systemd has finally caught up with me.  I'm sure someone will have the perfect, elegant solution, but I can't find anything on these forums or via Google.

I have a server with an attached external drive.  On boot, I want the server to:

1) Automatically mount the device
2) Once mounted, start the nfs-server service and a custom rtorrent in tmux service (found on the archwiki)

Both rtorrent and nfs-server want the data on the external drive.

Frustrated by /etc/fstab, I've resorted to devmon (part of udevil) for mounting devices.  It's really simple and clean -- I'd prefer to use this if possible.  However, because devmon cannot emulate x-systemd.automount like in /etc/fstab, rtorrent and nfs-server services start too quickly, and fail.  Manually restarting them gives me exactly what I want.

I guess the problem could be defined as simply delaying a couple of services until one is completely finished.  I've tried putting Wants=devmon@chj.service and After=devmon@chj.service into each of these services, but they still appear to start too quickly -- I suspect that the devmon service finishes, but has not mounted the drive yet.

Any ideas?


Arch x86_64

Offline

#2 2015-01-20 03:50:03

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,413

Re: systemd services - automounting, then run rtorrent & nfs-server

I think you are going about this the wrong way.  For something like this, devmon is probably not the way to go.  Your external USB HDD sounds as though it is *always* attached since you have services that depend on its presence.  So the right way to go about this would be what you first tried (the fstab).  Either that, or you can write a native systemd unit to do the same thing.

So I think what you should be asking is about your issue with fstab.

Offline

#3 2015-01-20 04:27:29

o1911
Member
From: Hobart, Australia
Registered: 2009-04-28
Posts: 106

Re: systemd services - automounting, then run rtorrent & nfs-server

Sorry, I should've clarified - I don't want the external drive always attached.  Occasionally I want to move it, but still have a running server.  Originally, I had noauto,x-systemd.automount,x-systemd.device-timeout=10s as the mount options within /etc/fstab, but this was frustrating - when a process would stumble across the declared mount point, the process will hang for longer than 10s.  I feel dirty about using /etc/fstab in general, is there no other way?


Arch x86_64

Offline

#4 2015-01-20 06:28:47

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,413

Re: systemd services - automounting, then run rtorrent & nfs-server

So you want to be able to freely remove it, but you also want the dependent services to stop as well?  I think then you will have to have a native systemd.mount unit so that you can apply dependencies to it.  If you are just constanly running devmon, you are going to run into issues when you remove the drive and something like rtorrent is still running and expecting those directories to still be there. 

As far as the starting is concerned, I think you should read the systemd.unit man page.  You probably want the Requires= configuration option rather than Wants=.

Offline

#5 2015-01-21 07:21:17

Padfoot
Member
Registered: 2010-09-03
Posts: 381

Re: systemd services - automounting, then run rtorrent & nfs-server

This may help, though you will need to tweak it to account for the device not always being there.


I have all my media on a NAS. I mount the root folders of the NAS in /mnt via fstab, using the noauto,x-systemd.automount,x-systemd.device-timeout= options. Then I bind mount the specific folders I need from /mnt to /home/<user>.
All works fine on boot. On shutdown, I had major issues in that the network would always come down before the bind mounts and fstab mounts were unmounted (same issue using netctl, NetworkManager and systemd-networkd) resulting in a hang during shutdown.


I resolved the issue of the network going down to early by modifying the wpa_supplicant@.service (my network is currently run by systemd-networkd and wpa_supplicant) - adding the following line:

[Unit]
...........
Before=remote-fs.target systemd-networkd.service systemd-resolved.service
.......

This now resulted in constant failures and circular references while trying to action the bind mounts in fstab. I resolved this by removing the bind mounts from fstab and putting them in a script which uses the parameters up|down to mount|umount as necessary. This script is called by a custom service file that waits until a) the mounts in fstab have been actioned and b) the network is up.

[Unit]
Description=Bind mount NAS directories to local paths
After=local-fs.target network-online.target
Wants=network-online.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/etc/systemd/user/scripts/bindmount up
ExecStop=/etc/systemd/user/scripts/bindmount down

[Install]
WantedBy=multi-user.target

Everything now works as expected.


So I guess I am suggesting looking at something similar to my custom unit file above, particularly the following lines, and adding them to your required services for nfs-server and tmux may solve the issue of the services being started (and failing) before the mount is available.

........
After=local-fs.target network-online.target
Wants=network-online.target

Cheers.

Offline

#6 2015-01-21 10:46:18

o1911
Member
From: Hobart, Australia
Registered: 2009-04-28
Posts: 106

Re: systemd services - automounting, then run rtorrent & nfs-server

Thanks to both of you for your advice.  I've been busy over the last few days, so I haven't really made progress on a solution.  A script with a custom unit really might be the best solution...

For further clarification, I would be OK with shutting down the server to unplug the drive, so long as the services fail upon boot and don't halt the rest of the system - something /etc/fstab can do without noauto.


Arch x86_64

Offline

#7 2015-01-22 12:02:44

o1911
Member
From: Hobart, Australia
Registered: 2009-04-28
Posts: 106

Re: systemd services - automounting, then run rtorrent & nfs-server

I've implemented a solution.  Perhaps not ideally, it uses external scripts and cron.  This could be simplified to one script, but I want a shell script so I don't have to edit the core one, and I'm only really using cron to avoid writing a systemd unit.

The core script (monitor-mount.rb) is as follows:

#!/usr/bin/env ruby
# A simple monitor for a mount point - if mounted, run systemd services.

device_uuid = "aeb66668-fc0b-4488-8f59-7bc02adcc58a"
services = ["nfs-server", "rtorrent@chj"]


# Loop until the drive is mount, and the services are started
loop do
    # Use lsblk to determine mounted partitions
    `lsblk -f`.split("\n").each do |l|
        # Only use the line with the matched UUID
        next unless l.include?(device_uuid)
        # If the last entry of this line is the UUID, we assume that it isn't mounted
        next if l.split[-1].include?(device_uuid)

        # If we've gotten this far, then...
        # ... run another shell script with custom mounts
        system("/root/bind-mounts.sh")

        # ... and start the desired services
        services.each do |s|
            system("systemctl restart #{s}")
        end

        # Exit the script so we don't restart services
        exit
    end

    sleep(10)
end

The syntax of bind-mounts.sh is:

mount -o bind /media/Downloads/public /srv/nfs4/public

And finally the syntax in cron is:

@reboot /usr/bin/ruby /root/monitor-mount.rb

I put the scripts in the /root directory, as only the root user should be able to edit them.  The cron must be specified for root, too.  If user permission to restart desired services is possible, then everything can be run as an underprivileged user.

The basic operation is that the Ruby script will wait until the device with the specified UUID is mounted.  Then it will execute the other script, and restart specified services.  This is working for me.  Hope it helps.

Thanks again for suggestions.


Arch x86_64

Offline

#8 2015-01-22 16:09:07

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: systemd services - automounting, then run rtorrent & nfs-server

Maybe this is relevant?


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

Board footer

Powered by FluxBB