]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - etc/periodic/daily/800.scrub-zfs
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / etc / periodic / daily / 800.scrub-zfs
1 #!/bin/sh
2 #
3 # $FreeBSD$
4 #
5
6 # If there is a global system configuration file, suck it in.
7 #
8
9 newline="
10 " # A single newline
11
12 if [ -r /etc/defaults/periodic.conf ]
13 then
14     . /etc/defaults/periodic.conf
15     source_periodic_confs
16 fi
17
18 : ${daily_scrub_zfs_default_threshold=35}
19
20 case "$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                 esac
47
48                 # determine how many days shall be between scrubs
49                 eval _pool_threshold=\${daily_scrub_zfs_$(echo "${pool}"|tr  ".:-" "_")_threshold}
50                 if [ -z "${_pool_threshold}" ];then
51                         _pool_threshold=${daily_scrub_zfs_default_threshold}
52                 fi
53
54                 _last_scrub=$(zpool history ${pool} | \
55                     egrep "^[0-9\.\:\-]{19} zpool scrub ${pool}\$" | tail -1 |\
56                     cut -d ' ' -f 1)
57                 if [ -z "${_last_scrub}" ]; then
58                         # creation time of the pool if no scrub was done
59                         _last_scrub=$(zpool history ${pool} | \
60                             sed -ne '2s/ .*$//p')
61                 fi
62
63                 # Now minus last scrub (both in seconds) converted to days.
64                 _scrub_diff=$(expr -e \( $(date +%s) - \
65                     $(date -j -f %F.%T ${_last_scrub} +%s) \) / 60 / 60 / 24)
66                 if [ ${_scrub_diff} -lt ${_pool_threshold} ]; then
67                         echo "   skipping scrubbing of pool '${pool}':"
68                         echo "      last scrubbing is ${_scrub_diff} days ago, threshold is set to ${_pool_threshold} days"
69                         continue
70                 fi
71
72                 _status="$(zpool status ${pool} | grep scrub:)"
73                 case "${_status}" in
74                         *"scrub in progress"*)
75                                 echo "   scrubbing of pool '${pool}' already in progress, skipping:"
76                                 ;;
77                         *"none requested"*)
78                                 echo "   starting first scrub (since reboot) of pool '${pool}':"
79                                 zpool scrub ${pool}
80                                 [ $rc -eq 0 ] && rc=1
81                                 ;;
82                         *)
83                                 echo "   starting scrub of pool '${pool}':"
84                                 zpool scrub ${pool}
85                                 [ $rc -eq 0 ] && rc=1
86                                 ;;
87                 esac
88
89                 echo "      consult 'zpool status ${pool}' for the result"
90         done
91         ;;
92
93     *)
94         rc=0
95         ;;
96 esac
97
98 exit $rc