800.scrub-zfs revision 292673
1#!/bin/sh
2#
3# $FreeBSD: stable/10/etc/periodic/daily/800.scrub-zfs 292673 2015-12-23 21:59:38Z lidl $
4#
5
6# If there is a global system configuration file, suck it in.
7#
8
9newline="
10" # A single newline
11
12if [ -r /etc/defaults/periodic.conf ]
13then
14    . /etc/defaults/periodic.conf
15    source_periodic_confs
16fi
17
18: ${daily_scrub_zfs_default_threshold=35}
19
20case "$daily_scrub_zfs_enable" in
21    [Yy][Ee][Ss])
22	echo
23	echo 'Scrubbing of zfs pools:'
24
25	if [ -z "${daily_scrub_zfs_pools}" ]; then
26		daily_scrub_zfs_pools="$(zpool list -H -o name)"
27	fi
28
29	rc=0
30	for pool in ${daily_scrub_zfs_pools}; do
31		# sanity check
32		_status=$(zpool list "${pool}" 2> /dev/null)
33		if [ $? -ne 0 ]; then
34			rc=2
35			echo "   WARNING: pool '${pool}' specified in"
36			echo "            '/etc/periodic.conf:daily_scrub_zfs_pools'"
37			echo "            does not exist"
38			continue
39		fi
40		_status=${_status##*$newline}
41		case ${_status} in
42		*FAULTED*)
43			rc=3
44			echo "Skipping faulted pool: ${pool}"
45			continue ;;
46		*UNAVAIL*)
47			rc=4
48			echo "Skipping unavailable pool: ${pool}"
49			continue ;;
50		esac
51
52		# determine how many days shall be between scrubs
53		eval _pool_threshold=\${daily_scrub_zfs_$(echo "${pool}"|tr  ".:-" "_")_threshold}
54		if [ -z "${_pool_threshold}" ];then
55			_pool_threshold=${daily_scrub_zfs_default_threshold}
56		fi
57
58		_last_scrub=$(zpool history ${pool} | \
59		    egrep "^[0-9\.\:\-]{19} zpool scrub ${pool}\$" | tail -1 |\
60		    cut -d ' ' -f 1)
61		if [ -z "${_last_scrub}" ]; then
62			# creation time of the pool if no scrub was done
63			_last_scrub=$(zpool history ${pool} | \
64			    sed -ne '2s/ .*$//p')
65		fi
66
67		# Now minus last scrub (both in seconds) converted to days.
68		_scrub_diff=$(expr -e \( $(date +%s) - \
69		    $(date -j -f %F.%T ${_last_scrub} +%s) \) / 60 / 60 / 24)
70		if [ ${_scrub_diff} -lt ${_pool_threshold} ]; then
71			echo "   skipping scrubbing of pool '${pool}':"
72			echo "      last scrubbing is ${_scrub_diff} days ago, threshold is set to ${_pool_threshold} days"
73			continue
74		fi
75
76		_status="$(zpool status ${pool} | grep scrub:)"
77		case "${_status}" in
78			*"scrub in progress"*)
79				echo "   scrubbing of pool '${pool}' already in progress, skipping:"
80				;;
81			*"none requested"*)
82				echo "   starting first scrub (since reboot) of pool '${pool}':"
83				zpool scrub ${pool}
84				[ $rc -eq 0 ] && rc=1
85				;;
86			*)
87				echo "   starting scrub of pool '${pool}':"
88				zpool scrub ${pool}
89				[ $rc -eq 0 ] && rc=1
90				;;
91		esac
92
93		echo "      consult 'zpool status ${pool}' for the result"
94	done
95	;;
96
97    *)
98	rc=0
99	;;
100esac
101
102exit $rc
103