xref: /freebsd/usr.sbin/periodic/etc/daily/801.trim-zfs (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
1#!/bin/sh
2#
3#
4
5# If there is a global system configuration file, suck it in.
6#
7
8if [ -r /etc/defaults/periodic.conf ]
9then
10    . /etc/defaults/periodic.conf
11    source_periodic_confs
12fi
13
14case "$daily_trim_zfs_enable" in
15    [Yy][Ee][Ss])
16	echo
17	echo 'Trimming of zfs pools:'
18
19	if [ -z "${daily_trim_zfs_pools}" ]; then
20		daily_trim_zfs_pools="$(zpool list -H -o name)"
21	fi
22
23	rc=0
24	for pool in ${daily_trim_zfs_pools}; do
25		# sanity check
26		_status=$(zpool list -Hohealth "${pool}" 2> /dev/null)
27		if [ $? -ne 0 ]; then
28			rc=2
29			echo "   WARNING: pool '${pool}' specified in"
30			echo "            '/etc/periodic.conf:daily_trim_zfs_pools'"
31			echo "            does not exist"
32			continue
33		fi
34		case ${_status} in
35		FAULTED)
36			rc=3
37			echo "Skipping faulted pool: ${pool}"
38			continue ;;
39		UNAVAIL)
40			rc=4
41			echo "Skipping unavailable pool: ${pool}"
42			continue ;;
43		esac
44
45		if ! zpool status "${pool}" | grep -q '(trimming)'; then
46			echo "    starting trim of pool '${pool}'"
47			zpool trim ${daily_zfs_trim_flags} "${pool}"
48		else
49			echo "    trim of pool '${pool}' already in progress, skipping"
50		fi
51	done
52	;;
53
54    *)
55	rc=0
56	;;
57esac
58
59exit $rc
60