Whamcloud - gitweb
e2scrub: create a script to scrub all ext* filesystems
[tools/e2fsprogs.git] / scrub / e2scrub_all.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 scrub_all=0
22 conffile="@root_sysconfdir@/e2scrub.conf"
23
24 test -f "${conffile}" && . "${conffile}"
25
26 scrub_args=""
27
28 print_help() {
29         echo "Usage: $0 [OPTIONS]"
30         echo " -A: Scrub all ext[234] filesystems even if not mounted."
31         echo " -r: Remove e2scrub snapshots."
32         echo " -V: Print version information and exit."
33 }
34
35 print_version() {
36         echo "e2scrub_all @E2FSPROGS_VERSION@ (@E2FSPROGS_DATE@)"
37 }
38
39 while getopts "ArV" opt; do
40         case "${opt}" in
41         "A") scrub_all=1;;
42         "r") scrub_args="${scrub_args} -r";;
43         "V") print_version; exit 0;;
44         *) print_help; exit 2;;
45         esac
46 done
47 shift "$((OPTIND - 1))"
48
49 # Find scrub targets, make sure we only do this once.
50 ls_scrub_targets() {
51         lsblk -o NAME,FSTYPE,MOUNTPOINT -p -P -n | while read vars; do
52                 eval "${vars}"
53
54                 # Skip non-ext[234]
55                 case "${FSTYPE}" in
56                 ext[234])       ;;
57                 *)              continue;;
58                 esac
59
60                 # Skip unmounted filesystems unless -A
61                 if [ "${scrub_all}" -eq 0 ] && [ -z "${MOUNTPOINT}" ]; then
62                         continue;
63                 fi
64
65                 # Skip non-lvm devices and lvm snapshots
66                 lvm_vars="$(lvs --nameprefixes -o vg_name,lv_name,lv_role --noheadings "${NAME}" 2> /dev/null)"
67                 test $? -ne 0 && continue
68                 eval "${lvm_vars}"
69                 echo "${LVM2_LV_ROLE}" | grep -q "snapshot" && continue
70
71                 if [ -n "${MOUNTPOINT}" ]; then
72                         echo "${MOUNTPOINT}"
73                 else
74                         echo "${NAME}"
75                 fi
76         done | sort | uniq
77 }
78
79 # Scrub any mounted fs on lvm by creating a snapshot and fscking that.
80 stdin="$(realpath /dev/stdin)"
81 ls_scrub_targets | while read tgt; do
82         ${DBG} "@root_sbindir@/e2scrub" ${scrub_args} "${tgt}" < "${stdin}"
83 done
84
85 exit 0