From e1beb5e38c80dd15eb17bda6043237674b1bc4b7 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 28 Aug 2022 04:02:18 +0200 Subject: [PATCH] Add zfs scrubbing and filesystem trim --- machine.yml | 86 +++++++++++++++++++ root/etc/systemd/system/status-mail@.service | 8 ++ .../systemd/system/zfs-scrub-monthly@.timer | 12 +++ root/etc/systemd/system/zfs-scrub@.service | 17 ++++ .../systemd/system/zfs-trim-monthly@.timer | 12 +++ root/etc/systemd/system/zfs-trim@.service | 16 ++++ root/usr/local/bin/zpool-status-mail.j2 | 11 +++ 7 files changed, 162 insertions(+) create mode 100644 root/etc/systemd/system/status-mail@.service create mode 100644 root/etc/systemd/system/zfs-scrub-monthly@.timer create mode 100644 root/etc/systemd/system/zfs-scrub@.service create mode 100644 root/etc/systemd/system/zfs-trim-monthly@.timer create mode 100644 root/etc/systemd/system/zfs-trim@.service create mode 100644 root/usr/local/bin/zpool-status-mail.j2 diff --git a/machine.yml b/machine.yml index 8cd3621..c9a181b 100644 --- a/machine.yml +++ b/machine.yml @@ -114,3 +114,89 @@ daemon_reload: true when: systemd_status_mail_service_file is changed + + # ---------------------------------------------------------------------------------------------- + # ZFS scrubbing. + # ---------------------------------------------------------------------------------------------- + + - name: Zpool status mail script + template: + src: ./root/usr/local/bin/zpool-status-mail.j2 + dest: /usr/local/bin/zpool-status-mail + mode: 0755 + + - name: Zfs scrub service file + copy: + src: ./root/etc/systemd/system/zfs-scrub@.service + dest: /etc/systemd/system/zfs-scrub@.service + mode: 0644 + register: systemd_zfs_scrub_service_file + + - name: Zfs scrub timer file + copy: + src: ./root/etc/systemd/system/zfs-scrub-monthly@.timer + dest: /etc/systemd/system/zfs-scrub-monthly@.timer + mode: 0644 + register: systemd_zfs_scrub_monthly_timer_file + + - name: SystemD daemon reload + systemd: + daemon_reload: true + when: + systemd_zfs_scrub_service_file is changed or + systemd_zfs_scrub_monthly_timer_file is changed + + - name: Enable zfs scrub of bpool + service: + name: zfs-scrub-monthly@bpool.timer + enabled: yes + state: started + + - name: Enable zfs scrub of rpool + service: + name: zfs-scrub-monthly@rpool.timer + enabled: yes + state: started + + # ---------------------------------------------------------------------------------------------- + # Filesystem TRIM. + # ---------------------------------------------------------------------------------------------- + + - name: Enable fstrim + service: + name: fstrim.timer + enabled: yes + state: started + + - name: Zfs trim service file + copy: + src: ./root/etc/systemd/system/zfs-trim@.service + dest: /etc/systemd/system/zfs-trim@.service + mode: 0644 + register: systemd_zfs_trim_service_file + + - name: Zfs trim timer file + copy: + src: ./root/etc/systemd/system/zfs-trim-monthly@.timer + dest: /etc/systemd/system/zfs-trim-monthly@.timer + mode: 0644 + register: systemd_zfs_trim_monthly_timer_file + + - name: SystemD daemon reload + systemd: + daemon_reload: true + when: + systemd_zfs_trim_service_file is changed or + systemd_zfs_trim_monthly_timer_file is changed + + - name: Enable zfs trim of bpool + service: + name: zfs-trim-monthly@bpool.timer + enabled: yes + state: started + + - name: Enable zfs trim of rpool + service: + name: zfs-trim-monthly@rpool.timer + enabled: yes + state: started diff --git a/root/etc/systemd/system/status-mail@.service b/root/etc/systemd/system/status-mail@.service new file mode 100644 index 0000000..acb0f5b --- /dev/null +++ b/root/etc/systemd/system/status-mail@.service @@ -0,0 +1,8 @@ +[Unit] +Description=Status email for %i + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/systemd-mail %i +User=nobody +Group=systemd-journal diff --git a/root/etc/systemd/system/zfs-scrub-monthly@.timer b/root/etc/systemd/system/zfs-scrub-monthly@.timer new file mode 100644 index 0000000..9030684 --- /dev/null +++ b/root/etc/systemd/system/zfs-scrub-monthly@.timer @@ -0,0 +1,12 @@ +[Unit] +Description=Monthly zpool scrub timer for %i +Documentation=man:zpool-scrub(8) + +[Timer] +OnCalendar=monthly +Persistent=true +RandomizedDelaySec=1h +Unit=zfs-scrub@%i.service + +[Install] +WantedBy=timers.target diff --git a/root/etc/systemd/system/zfs-scrub@.service b/root/etc/systemd/system/zfs-scrub@.service new file mode 100644 index 0000000..762016f --- /dev/null +++ b/root/etc/systemd/system/zfs-scrub@.service @@ -0,0 +1,17 @@ +[Unit] +Description=Zpool scrub on %i +Documentation=man:zpool-scrub(8) +Requires=zfs.target +After=zfs.target +ConditionACPower=true +ConditionPathIsDirectory=/sys/module/zfs +OnFailure=status-mail@%n.service + +[Service] +EnvironmentFile=-/etc/default/zfs +ExecStart=/bin/sh -c '\ +if /usr/sbin/zpool status %i | grep -q "scrub in progress"; then\ +exec /usr/sbin/zpool wait -t scrub %i;\ +else exec /usr/sbin/zpool scrub -w %i; fi' +ExecStop=-/bin/sh -c '/usr/sbin/zpool scrub -p %i 2>/dev/null || true' +ExecStopPost=/bin/sh -c '/usr/local/bin/zpool-status-mail %i' diff --git a/root/etc/systemd/system/zfs-trim-monthly@.timer b/root/etc/systemd/system/zfs-trim-monthly@.timer new file mode 100644 index 0000000..8c13ffb --- /dev/null +++ b/root/etc/systemd/system/zfs-trim-monthly@.timer @@ -0,0 +1,12 @@ +[Unit] +Description=Monthly zpool trim timer for %i +Documentation=man:zpool-trim(8) + +[Timer] +OnCalendar=monthly +Persistent=true +RandomizedDelaySec=1h +Unit=zfs-trim@%i.service + +[Install] +WantedBy=timers.target diff --git a/root/etc/systemd/system/zfs-trim@.service b/root/etc/systemd/system/zfs-trim@.service new file mode 100644 index 0000000..cb39df5 --- /dev/null +++ b/root/etc/systemd/system/zfs-trim@.service @@ -0,0 +1,16 @@ +[Unit] +Description=Zpool trim on %i +Documentation=man:zpool-trim(8) +Requires=zfs.target +After=zfs.target +ConditionACPower=true +ConditionPathIsDirectory=/sys/module/zfs +OnFailure=status-mail@%n.service + +[Service] +EnvironmentFile=-/etc/default/zfs +ExecStart=/bin/sh -c '\ +if /usr/sbin/zpool status %i | grep -q "(trimming)"; then\ +exec /usr/sbin/zpool wait -t trim %i;\ +else exec /usr/sbin/zpool trim -w %i; fi' +ExecStop=-/bin/sh -c '/usr/sbin/zpool trim -s %i 2>/dev/null || true' diff --git a/root/usr/local/bin/zpool-status-mail.j2 b/root/usr/local/bin/zpool-status-mail.j2 new file mode 100644 index 0000000..6747d83 --- /dev/null +++ b/root/usr/local/bin/zpool-status-mail.j2 @@ -0,0 +1,11 @@ +#!/bin/sh + +/usr/sbin/sendmail -t < +Subject: zpool status $1 +Content-Transfer-Encoding: 8bit +Content-Type: text/plain; charset=UTF-8 + +$(zpool status "$1") +SYSTEMDMAIL