#!/usr/bin/env bash
# scripts/trim-zfs.sh
# Issue `zpool trim` on each SSD-backed pool; skip anything not
# supporting it. Runs monthly from cron.
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=/dev/null
. "${HERE}/lib/log.sh"
if ! command -v zpool >/dev/null 2>&1; then
log_info "zpool not installed, skipping"
exit 0
fi
mapfile -t pools < <(zpool list -H -o name)
for pool in "${pools[@]}"; do
if zpool get -Hp -o value autotrim "${pool}" 2>/dev/null | grep -q '^on$'; then
log_info "${pool} has autotrim on, skipping manual run"
continue
fi
log_info "zpool trim ${pool}"
if ! zpool trim "${pool}"; then
log_warn "trim failed on ${pool}"
continue
fi
while zpool status -t "${pool}" 2>/dev/null | grep -qE 'trim.*in progress'; do
sleep 30
done
log_info "${pool} trim done"
done