Commit 8c68e347 authored by nimrod's avatar nimrod
Browse files

Per btrfs device scrub job.

Instead of a single Systemd service and timer and a script to scrub all
of the btrfs devices. Easier to find faults that way.
parent 7da2cea9
Loading
Loading
Loading
Loading
+0 −18
Original line number Diff line number Diff line
#!/bin/sh
set -eu

for mp in $(lsblk --list --noheadings --output fstype,mountpoint | \
            awk '$1 == "btrfs" {print $2}' | \
            sort -u)
do
    # In case there are no btrfs volumes, the for loop will execute once with an
    # empty string for mp. Validate that indeed we're passed a mountpoint.
    [ -d "$mp" ] || continue
    status="$(btrfs scrub status "$mp" | awk '$1 == "Status:" {print $2}')"
    if [ "$status" = 'running' ]
    then
        echo "Scrub already running on $mp, skipping." >&2
        continue
    fi
    btrfs scrub start "$mp" >&2
done
+25 −14
Original line number Diff line number Diff line
@@ -235,23 +235,34 @@
    name: update.timer
    state: started

- name: Copy the btrfs scrub script
  ansible.builtin.copy:
    dest: /usr/local/sbin/btrfs-scrub
    mode: 0o0755
    src: btrfs-scrub
- name: Create btrfs scrub services
  # yamllint disable rule:line-length
  loop: &btrfs_devices |-
    {{ ansible_mounts|selectattr("fstype", "equalto", "btrfs")|map(attribute="device")|unique }}
  # yamllint enable rule:line-length
  ansible.builtin.template:
    dest: /etc/systemd/system/scrub{{ name }}.service
    mode: 0o0644
    src: scrub.service.j2
  vars: &btrfs_vars
    device: '{{ item }}'
    name: '{{ item|replace("/", "_") }}'
  tags: [scrub]

- name: Copy btrfs scrub service and timer
  loop:
    - scrub.service
    - scrub.timer
  ansible.builtin.copy:
    dest: /etc/systemd/system
- name: Create btrfs scrub timers
  loop: *btrfs_devices
  ansible.builtin.template:
    dest: /etc/systemd/system/scrub{{ name }}.timer
    mode: 0o0644
    src: '{{ item }}'
    src: scrub.timer.j2
  vars: *btrfs_vars
  tags: [scrub]

- name: Enable the btrfs scrub timer
- name: Enable the btrfs scrub timers
  loop: *btrfs_devices
  ansible.builtin.systemd:
    enabled: true
    name: scrub.timer
    name: scrub{{ name }}.timer
    state: started
  vars: *btrfs_vars
  tags: [scrub]
+4 −3
Original line number Diff line number Diff line
# vim: filetype=systemd
[Unit]
Description=Scrub btrfs volumes
Description=Scrub btrfs device {{ device }}
ConditionACPower=true
After=local-fs.target

[Service]
Type=oneshot
ExecStart=btrfs-scrub
Type=exec
ExecStart=btrfs scrub start -B {{ device }}
+2 −1
Original line number Diff line number Diff line
# vim: filetype=systemd
[Unit]
Description=Scrub btrfs volumes
Description=Scrub btrfs device {{ device }}

[Timer]
OnCalendar=weekly