Whamcloud - gitweb
e2scrub: check to make sure lvm2 is installed
[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 a 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 snap_size_mb=256
29 fstrim=0
30 reap=0
31 e2fsck_opts=""
32 conffile="@root_sysconfdir@/e2scrub.conf"
33
34 test -f "${conffile}" && . "${conffile}"
35
36 print_help() {
37         echo "Usage: $0 [OPTIONS] mountpoint | device"
38         echo
39         echo "mountpoint must be on a LVM-managed block device"
40         echo "-r: Remove e2scrub snapshot and exit, do not check anything."
41         echo "-t: Run fstrim if successful."
42         echo "-V: Print version information and exit."
43 }
44
45 print_version() {
46         echo "e2scrub @E2FSPROGS_VERSION@ (@E2FSPROGS_DATE@)"
47 }
48
49 exitcode() {
50         ret="$1"
51
52         # If we're being run as a service, the return code must fit the LSB
53         # init script action error guidelines, which is to say that we
54         # compress all errors to 1 ("generic or unspecified error", LSB 5.0
55         # section 22.2) and hope the admin will scan the log for what
56         # actually happened.
57
58         # We have to sleep 2 seconds here because journald uses the pid to
59         # connect our log messages to the systemd service.  This is critical
60         # for capturing all the log messages if the scrub fails, because the
61         # fail service uses the service name to gather log messages for the
62         # error report.
63         if [ -n "${SERVICE_MODE}" ]; then
64                 test "${ret}" -ne 0 && ret=1
65                 sleep 2
66         fi
67
68         exit "${ret}"
69 }
70
71 while getopts "rtV" opt; do
72         case "${opt}" in
73         "r") reap=1;;
74         "t") fstrim=1;;
75         "V") print_version; exitcode 0;;
76         *) print_help; exitcode 2;;
77         esac
78 done
79 shift "$((OPTIND - 1))"
80
81 arg="$1"
82 if [ -z "${arg}" ]; then
83         print_help
84         exitcode 1
85 fi
86
87 if ! type lsblk >& /dev/null ; then
88     echo "e2scrub: can't find lsblk --- is util-linux installed?"
89     exitcode 1
90 fi
91
92 if ! type lvcreate >& /dev/null ; then
93     echo "e2scrub: can't find lvcreate --- is lvm2 installed?"
94     exitcode 1
95 fi
96
97 # Find the device for a given mountpoint
98 dev_from_mount() {
99         local mountpt="$(realpath "$1")"
100
101         lsblk -o NAME,FSTYPE,MOUNTPOINT -p -P -n 2> /dev/null | while read vars; do
102                 eval "${vars}"
103                 if [ "${mountpt}" != "${MOUNTPOINT}" ]; then
104                         continue
105                 fi
106                 case "${FSTYPE}" in
107                 ext[234])
108                         echo "${NAME}"
109                         return 0
110                         ;;
111                 esac
112         done
113         return 1
114 }
115
116 # Check a device argument
117 dev_from_arg() {
118         local dev="$1"
119         local fstype="$(lsblk -o FSTYPE -n "${dev}" 2> /dev/null)"
120
121         case "${fstype}" in
122         ext[234])
123                 echo "${dev}"
124                 return 0
125                 ;;
126         esac
127         return 1
128 }
129
130 mnt_from_dev() {
131         local dev="$1"
132
133         if [ -n "${dev}" ]; then
134                 lsblk -o MOUNTPOINT -n "${dev}"
135         fi
136 }
137
138 # Construct block device path and mountpoint from argument
139 if [ -b "${arg}" ]; then
140         dev="$(dev_from_arg "${arg}")"
141         mnt="$(mnt_from_dev "${dev}")"
142 else
143         dev="$(dev_from_mount "${arg}")"
144         mnt="${arg}"
145 fi
146 if [ ! -e "${dev}" ]; then
147         echo "${arg}: Not an ext[234] filesystem."
148         print_help
149         exitcode 16
150 fi
151
152 # Make sure this is an LVM device we can snapshot
153 lvm_vars="$(lvs --nameprefixes -o name,vgname,lv_role --noheadings "${dev}" 2> /dev/null)"
154 eval "${lvm_vars}"
155 if [ -z "${LVM2_VG_NAME}" ] || [ -z "${LVM2_LV_NAME}" ] ||
156    echo "${LVM2_LV_ROLE}" | grep -q "snapshot"; then
157         echo "${arg}: Not connnected to a LVM logical volume."
158         print_help
159         exitcode 16
160 fi
161 start_time="$(date +'%Y%m%d%H%M%S')"
162 snap="${LVM2_LV_NAME}.e2scrub"
163 snap_dev="/dev/${LVM2_VG_NAME}/${snap}"
164
165 teardown() {
166         # Remove and wait for removal to succeed.
167         ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" 3>&-
168         while [ -e "${snap_dev}" ] && [ "$?" -eq "5" ]; do
169                 sleep 0.5
170                 ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" 3>&-
171         done
172 }
173
174 check() {
175         # First we recover the journal, then we see if e2fsck tries any
176         # non-optimization repairs.  If either of these two returns a
177         # non-zero status (errors fixed or remaining) then this fs is bad.
178         E2FSCK_FIXES_ONLY=1
179         export E2FSCK_FIXES_ONLY
180         ${DBG} "@root_sbindir@/e2fsck" -E journal_only -p ${e2fsck_opts} "${snap_dev}" || return $?
181         ${DBG} "@root_sbindir@/e2fsck" -f -y ${e2fsck_opts} "${snap_dev}"
182 }
183
184 mark_clean() {
185         ${DBG} "@root_sbindir@/tune2fs" -C 0 -T "${start_time}" "${dev}"
186 }
187
188 mark_corrupt() {
189         ${DBG} "@root_sbindir@/tune2fs" -E force_fsck "${dev}"
190 }
191
192 setup() {
193         # Try to remove snapshot for 30s, bail out if we can't remove it.
194         lveremove_deadline="$(( $(date "+%s") + 30))"
195         ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" 3>&- 2>/dev/null
196         while [ -e "${snap_dev}" ] && [ "$?" -eq "5" ] &&
197               [ "$(date "+%s")" -lt "${lvremove_deadline}" ]; do
198                 sleep 0.5
199                 ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" 3>&-
200         done
201         if [ -e "${snap_dev}" ]; then
202                 echo "${arg}: e2scrub snapshot is in use, cannot check!"
203                 return 1
204         fi
205         # Create the snapshot, wait for device to appear.
206         ${DBG} lvcreate -s -L "${snap_size_mb}m" -n "${snap}" "${LVM2_VG_NAME}/${LVM2_LV_NAME}" 3>&-
207         if [ $? -ne 0 ]; then
208                 echo "${arg}: e2scrub snapshot FAILED, will not check!"
209                 return 1
210         fi
211         ${DBG} udevadm settle 2> /dev/null
212         return 0
213 }
214
215 if [ "${reap}" -gt 0 ]; then
216         if [ -e "${snap_dev}" ]; then
217                 teardown 2> /dev/null
218         fi
219         exit 0
220 fi
221 if ! setup; then
222         exitcode 8
223 fi
224 trap "teardown; exit 1" EXIT INT QUIT TERM
225
226 # Check and react
227 check
228 case "$?" in
229 "0")
230         # Clean check!
231         echo "${arg}: Scrub succeeded."
232         mark_clean
233         teardown
234         trap '' EXIT
235
236         # Trim the free space, which requires the snapshot be deleted.
237         if [ "${fstrim}" -eq 1 ] && [ -d "${mnt}" ] && type fstrim > /dev/null 2>&1; then
238                 echo "${arg}: Trimming free space."
239                 fstrim -v "${mnt}"
240         fi
241
242         ret=0
243         ;;
244 "8")
245         # Operational error, what now?
246         echo "${arg}: e2fsck operational error."
247         teardown
248         trap '' EXIT
249         ret=8
250         ;;
251 *)
252         # fsck failed.  Check if the snapshot is invalid; if so, make a
253         # note of that at the end of the log.  This isn't necessarily a
254         # failure because the mounted fs could have overflowed the
255         # snapshot with regular disk writes /or/ our repair process
256         # could have done it by repairing too much.
257         #
258         # If it's really corrupt we ought to fsck at next boot.
259         is_invalid="$(lvs -o lv_snapshot_invalid --noheadings "${snap_dev}" | awk '{print $1}')"
260         if [ -n "${is_invalid}" ]; then
261                 echo "${arg}: Scrub FAILED due to invalid snapshot."
262                 ret=8
263         else
264                 echo "${arg}: Scrub FAILED due to corruption!  Unmount and run e2fsck -y."
265                 mark_corrupt
266                 ret=6
267         fi
268         teardown
269         trap '' EXIT
270         ;;
271 esac
272
273 exitcode "${ret}"