Managing ZFS scrub via systemd
Asked Answered
F

1

6

I would like to automate zpool scrubbing on my ZFS server. I have written the following systemd service as /etc/systemd/system/zfs-scrub.service)

[Unit]
Description=Scrub ZFS tank pool

[Service]
Type=simple
ExecStart=/bin/sh -c 'zpool scrub `zpool list -H -o name`'
RemainAfterExit=yes
ExecStop=/bin/sh -c 'zpool scrub -s `zpool list -H -o name`'

As well as a timer (as /etc/system.d/system/zfs-scrub.timer) :

[Unit]
Description=Run zpool scrub every 1st Saturday

[Timer]
OnCalendar=Sat *-*-* 22:00:00

After having started it a few weeks back, I checked to see if it behaved. It seems that systemd still thinks the service is running, so the timer didn't run.

It seems there is no ExecStatus, so systemd doesn't know that the service completed.

  • Am I missing something ? Should I instead write a script that starts the scrub, greps the zpool status line and catch signals to stop the scrub when systemd signals it ?
  • Is it possible to write a OnCalendar line that means "once per month, only on weekends" ?
Flighty answered 22/12, 2016 at 12:5 Comment(0)
A
2

To answer your second question, the following can be read as running the first seven days of each month at a minute past midnight, but only if that day is also a Sunday. Thus, it should run the first Sunday of every month.

OnCalendar=Sun *-*-01..07 00:01:00

Regarding your first question, it would be helpful to see what the journal says, rather than just confirming it ran via the zpool status command. I notice that your service unit does not require the zfs.target. Also I would make the service Type a "oneshot" rather than "simple".

EDIT: This should work for you, though you need to start/enable for each pool:

Try this for your [email protected] file:

[Unit]
Description=Scrub ZFS tank pool

[Service]
Type=oneshot
ExecStart=/bin/zpool scrub %i

Then this for your [email protected] file:

[Unit]
Description=Run zpool scrub every 1st Saturday

[Timer]
OnCalendar=Sun *-*-01..07 00:01:00 

You would then start the service via the timer with:

systemctl start zfs-scrub@[pool name].timer
systemctl enable zfs-scrub@[pool name].timer
Adjutant answered 5/1, 2017 at 5:11 Comment(3)
I chose simple over oneshot because zpool scrub won't stay around (it just triggers the scrubbing and exits). The thing is that there's no text reporting when you trigger scrub (maybe exit codes, but I haven't checked), apart from grepping zpool status from time to time to get status/progress, and I didn't want to write a wrapper.Flighty
I see what you mean. For what it's worth, what I've shown above is pretty much what I use on both my server and primary workstation (although I modified the timer to fit your requirements). I've had no problems, although I don't know how to get the progress of a scrub other than via zpool status.Adjutant
OpenZFS 2.0 introduced a parameter -w to make zpool scrub run in the foreground.Radiothorium

© 2022 - 2024 — McMap. All rights reserved.