Whamcloud - gitweb
Prevent i_dtime from being mistaken for an inode number post-2038 wraparound
[tools/e2fsprogs.git] / scrub / e2scrub.in
1 #!/bin/bash
2
3 #  Copyright (C) 2018 Oracle.  All Rights Reserved.
4 #
5 #  Author: Darrick J. Wong <darrick.wong@oracle.com>
6 #
7 #  This program is free software; you can redistribute it and/or
8 #  modify it under the terms of the GNU General Public License
9 #  as published by the Free Software Foundation; either version 2
10 #  of the License, or (at your option) any later version.
11 #
12 #  This program is distributed in the hope that it would be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with this program; if not, write the Free Software Foundation,
19 #  Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 # Automatically check an LVM-managed filesystem online.
22 # We use lvm snapshots to do this, which means that we can only
23 # check filesystems in VGs that have at least 256MB (or so) of
24 # free space.
25
26 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
27
28 if (( $EUID != 0 )); then
29     echo "e2scrub must be run as root"
30     exit 1
31 fi
32
33 snap_size_mb=256
34 fstrim=0
35 reap=0
36 e2fsck_opts=""
37 conffile="@root_sysconfdir@/e2scrub.conf"
38
39 test -f "${conffile}" && . "${conffile}"
40
41 print_help() {
42         echo "Usage: $0 [OPTIONS] mountpoint | device"
43         echo
44         echo "mountpoint must be on an LVM-managed block device"
45         echo "-n: Show what commands e2scrub would execute."
46         echo "-r: Remove e2scrub snapshot and exit, do not check anything."
47         echo "-t: Run fstrim if successful."
48         echo "-V: Print version information and exit."
49 }
50
51 print_version() {
52         echo "e2scrub @E2FSPROGS_VERSION@ (@E2FSPROGS_DATE@)"
53 }
54
55 exitcode() {
56         ret="$1"
57
58         # If we're being run as a service, the return code must fit the LSB
59         # init script action error guidelines, which is to say that we
60         # compress all errors to 1 ("generic or unspecified error", LSB 5.0
61         # section 22.2) and hope the admin will scan the log for what
62         # actually happened.
63
64         # We have to sleep 2 seconds here because journald uses the pid to
65         # connect our log messages to the systemd service.  This is critical
66         # for capturing all the log messages if the scrub fails, because the
67         # fail service uses the service name to gather log messages for the
68         # error report.
69         if [ -n "${SERVICE_MODE}" -a "${ret}" -ne 0 ]; then
70                 test "${ret}" -ne 0 && ret=1
71                 sleep 2
72         fi
73
74         exit "${ret}"
75 }
76
77 while getopts "nrtV" opt; do
78     case "${opt}" in
79         "n") DBG="echo Would execute: " ;;
80         "r") reap=1;;
81         "t") fstrim=1;;
82         "V") print_version; exitcode 0;;
83         *) print_help; exitcode 2;;
84         esac
85 done
86 shift "$((OPTIND - 1))"
87
88 arg="$1"
89 if [ -z "${arg}" ]; then
90         print_help
91         exitcode 1
92 fi
93
94 if ! type lsblk >& /dev/null ; then
95     echo "e2scrub: can't find lsblk --- is util-linux installed?"
96     exitcode 1
97 fi
98
99 if ! type lvcreate >& /dev/null ; then
100     echo "e2scrub: can't find lvcreate --- is lvm2 installed?"
101     exitcode 1
102 fi
103
104 # close file descriptor 3 (from cron) since it causes lvm to kvetch
105 exec 3<&-
106
107 # Find the device for a given mountpoint
108 dev_from_mount() {
109         local mountpt="$(realpath "$1")"
110
111         lsblk -o NAME,FSTYPE,MOUNTPOINT -p -P -n 2> /dev/null | while read vars; do
112                 eval "${vars}"
113                 if [ "${mountpt}" != "${MOUNTPOINT}" ]; then
114                         continue
115                 fi
116                 case "${FSTYPE}" in
117                 ext[234])
118                         echo "${NAME}"
119                         return 0
120                         ;;
121                 esac
122         done
123         return 1
124 }
125
126 # Check a device argument
127 dev_from_arg() {
128         local dev="$1"
129         local fstype="$(lsblk -o FSTYPE -n "${dev}" 2> /dev/null)"
130
131         case "${fstype}" in
132         ext[234])
133                 echo "${dev}"
134                 return 0
135                 ;;
136         esac
137         return 1
138 }
139
140 mnt_from_dev() {
141         local dev="$1"
142
143         if [ -n "${dev}" ]; then
144                 lsblk -o MOUNTPOINT -n "${dev}"
145         fi
146 }
147
148 # Construct block device path and mountpoint from argument
149 if [ -b "${arg}" ]; then
150         dev="$(dev_from_arg "${arg}")"
151         mnt="$(mnt_from_dev "${dev}")"
152 else
153         dev="$(dev_from_mount "${arg}")"
154         mnt="${arg}"
155 fi
156 if [ ! -e "${dev}" ]; then
157         echo "${arg}: Not an ext[234] filesystem."
158         print_help
159         exitcode 16
160 fi
161
162 # Do not scrub unjournalled filesystems; they are inconsistent when mounted
163 if [ "${reap}" -eq 0 ] && ! dumpe2fs -h "${dev}" | grep -q 'has_journal'; then
164         echo "${arg}: Filesystem has no journal, cannot check."
165         print_help
166         exitcode 16
167 fi
168
169 # Make sure this is an LVM device we can snapshot
170 lvm_vars="$(lvs --nameprefixes -o name,vgname,lv_role --noheadings "${dev}" 2> /dev/null)"
171 eval "${lvm_vars}"
172 if [ -z "${LVM2_VG_NAME}" ] || [ -z "${LVM2_LV_NAME}" ] ||
173    echo "${LVM2_LV_ROLE}" | grep -q "snapshot"; then
174         echo "${arg}: Not connected to an LVM logical volume."
175         print_help
176         exitcode 16
177 fi
178 start_time="$(date +'%Y%m%d%H%M%S')"
179 snap="${LVM2_LV_NAME}.e2scrub"
180 snap_dev="/dev/${LVM2_VG_NAME}/${snap}"
181
182 teardown() {
183         # Remove and wait for removal to succeed.
184         ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}"
185         while [ -e "${snap_dev}" ] && [ "$?" -eq "5" ]; do
186                 sleep 0.5
187                 ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}"
188         done
189 }
190
191 check() {
192         # First we recover the journal, then we see if e2fsck tries any
193         # non-optimization repairs.  If either of these two returns a
194         # non-zero status (errors fixed or remaining) then this fs is bad.
195         E2FSCK_FIXES_ONLY=1
196         export E2FSCK_FIXES_ONLY
197         ${DBG} "@root_sbindir@/e2fsck" -E journal_only -p ${e2fsck_opts} "${snap_dev}" || return $?
198         ${DBG} "@root_sbindir@/e2fsck" -f -y ${e2fsck_opts} "${snap_dev}"
199 }
200
201 mark_clean() {
202         ${DBG} "@root_sbindir@/tune2fs" -C 0 -T "${start_time}" "${dev}"
203 }
204
205 mark_corrupt() {
206         ${DBG} "@root_sbindir@/tune2fs" -E force_fsck "${dev}"
207 }
208
209 setup() {
210         # Try to remove snapshot for 30s, bail out if we can't remove it.
211         lvremove_deadline="$(( $(date "+%s") + 30))"
212         ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" 2>/dev/null
213         while [ -e "${snap_dev}" ] && [ "$?" -eq "5" ] &&
214               [ "$(date "+%s")" -lt "${lvremove_deadline}" ]; do
215                 sleep 0.5
216                 ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}"
217         done
218         if [ -e "${snap_dev}" ]; then
219                 echo "${arg}: e2scrub snapshot is in use, cannot check!"
220                 return 1
221         fi
222         # Create the snapshot, wait for device to appear.
223         ${DBG} lvcreate -s -L "${snap_size_mb}m" -n "${snap}" "${LVM2_VG_NAME}/${LVM2_LV_NAME}"
224         if [ $? -ne 0 ]; then
225                 echo "${arg}: e2scrub snapshot FAILED, will not check!"
226                 return 1
227         fi
228         ${DBG} udevadm settle 2> /dev/null
229         return 0
230 }
231
232 if [ "${reap}" -gt 0 ]; then
233         if [ -e "${snap_dev}" ]; then
234                 teardown 2> /dev/null
235         fi
236         exit 0
237 fi
238 if ! setup; then
239         exitcode 8
240 fi
241 trap "teardown; exit 1" EXIT INT QUIT TERM
242
243 # Check and react
244 check
245 case "$?" in
246 "0")
247         # Clean check!
248         echo "${arg}: Scrub succeeded."
249         mark_clean
250         teardown
251         trap '' EXIT
252
253         # Trim the free space, which requires the snapshot be deleted.
254         if [ "${fstrim}" -eq 1 ] && [ -d "${mnt}" ] && type fstrim > /dev/null 2>&1; then
255                 echo "${arg}: Trimming free space."
256                 fstrim -v "${mnt}"
257         fi
258
259         ret=0
260         ;;
261 "8")
262         # Operational error, what now?
263         echo "${arg}: e2fsck operational error."
264         teardown
265         trap '' EXIT
266         ret=8
267         ;;
268 *)
269         # fsck failed.  Check if the snapshot is invalid; if so, make a
270         # note of that at the end of the log.  This isn't necessarily a
271         # failure because the mounted fs could have overflowed the
272         # snapshot with regular disk writes /or/ our repair process
273         # could have done it by repairing too much.
274         #
275         # If it's really corrupt we ought to fsck at next boot.
276         is_invalid="$(lvs -o lv_snapshot_invalid --noheadings "${snap_dev}" | awk '{print $1}')"
277         if [ -n "${is_invalid}" ]; then
278                 echo "${arg}: Scrub FAILED due to invalid snapshot."
279                 ret=8
280         else
281                 echo "${arg}: Scrub FAILED due to corruption!  Unmount and run e2fsck -y."
282                 mark_corrupt
283                 ret=6
284         fi
285         teardown
286         trap '' EXIT
287         ;;
288 esac
289
290 exitcode "${ret}"