Whamcloud - gitweb
e2scrub: fix grammar nit: "a LVM" -> "an LVM"
[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 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
22
23 if (( $EUID != 0 )); then
24     echo "e2scrub_all must be run as root"
25     exit 1
26 fi
27
28 scrub_all=0
29 run_from_cron=0
30 snap_size_mb=256
31 reap=0
32 conffile="@root_sysconfdir@/e2scrub.conf"
33
34 test -f "${conffile}" && . "${conffile}"
35
36 scrub_args=""
37
38 print_help() {
39         echo "Usage: $0 [OPTIONS]"
40         echo " -n: Show what commands e2scrub_all would execute."
41         echo " -r: Remove e2scrub snapshots."
42         echo " -A: Scrub all ext[234] filesystems even if not mounted."
43         echo " -V: Print version information and exit."
44 }
45
46 print_version() {
47         echo "e2scrub_all @E2FSPROGS_VERSION@ (@E2FSPROGS_DATE@)"
48 }
49
50 exitcode() {
51         ret="$1"
52
53         # If we're being run as a service, the return code must fit the LSB
54         # init script action error guidelines, which is to say that we
55         # compress all errors to 1 ("generic or unspecified error", LSB 5.0
56         # section 22.2) and hope the admin will scan the log for what
57         # actually happened.
58
59         # We have to sleep 2 seconds here because journald uses the pid to
60         # connect our log messages to the systemd service.  This is critical
61         # for capturing all the log messages if the scrub fails, because the
62         # fail service uses the service name to gather log messages for the
63         # error report.
64         if [ -n "${SERVICE_MODE}" ]; then
65                 test "${ret}" -ne 0 && ret=1
66                 sleep 2
67         fi
68
69         exit "${ret}"
70 }
71
72 while getopts "nrAV" opt; do
73         case "${opt}" in
74         "n") DBG="echo Would execute: " ;;
75         "r") scrub_args="${scrub_args} -r"; reap=1;;
76         "A") scrub_all=1;;
77         "C") run_from_cron=1;;
78         "V") print_version; exitcode 0;;
79         *) print_help; exitcode 2;;
80         esac
81 done
82 shift "$((OPTIND - 1))"
83
84 # If some prerequisite packages are not installed, exit with a code
85 # indicating success to avoid spamming the sysadmin with fail messages
86 # when e2scrub_all is run out of cron or a systemd timer.
87
88 if ! type lsblk >& /dev/null ; then
89     if [ "${run_from_cron}" -eq 1 ] ; then
90         exitcode 0
91     fi
92     echo "e2scrub_all: can't find lsblk --- is util-linux installed?"
93     exitcode 1
94 fi
95
96 if ! type lvcreate >& /dev/null ; then
97     if [ "${run_from_cron}" -eq 1 ] ; then
98         exitcode 0
99     fi
100     echo "e2scrub_all: can't find lvcreate --- is lvm2 installed?"
101     exitcode 1
102 fi
103
104 # Find scrub targets, make sure we only do this once.
105 ls_scan_targets() {
106     local devices=$(lvs -o lv_path --noheadings -S "lv_active=active,lv_role=public,lv_role!=snapshot,vg_free>${snap_size_mb}")
107
108     if [ -z "$devices" ]; then
109         return 0;
110     fi
111     lsblk -o NAME,MOUNTPOINT,FSTYPE -P -n -p $devices | \
112             grep FSTYPE=\"ext\[234\]\" | while read vars ; do
113                 eval "${vars}"
114
115                 if [ "${scrub_all}" -eq 1 ] || [ -n "${MOUNTPOINT}" ]; then
116                     echo ${MOUNTPOINT:-${NAME}}
117                 fi
118         done
119 }
120
121 # Find leftover scrub snapshots
122 ls_reap_targets() {
123         lvs -o lv_path -S lv_role=snapshot -S lv_name=~\(e2scrub$\) --noheadings
124 }
125
126 # Figure out what we're targeting
127 ls_targets() {
128         if [ "${reap}" -eq 1 ]; then
129                 ls_reap_targets
130         else
131                 ls_scan_targets
132         fi
133 }
134
135 # systemd doesn't know to do path escaping on the instance variable we pass
136 # to the e2scrub service, which breaks things if there is a dash in the path
137 # name.  Therefore, do the path escaping ourselves if needed.
138 #
139 # systemd path escaping also drops the initial slash so we add that back in so
140 # that log messages from the service units preserve the full path and users can
141 # look up log messages using full paths.  However, for "/" the escaping rules
142 # do /not/ drop the initial slash, so we have to special-case that here.
143 escape_path_for_systemd() {
144         local path="$1"
145
146         if [ "${path}" != "/" ]; then
147                 echo "-$(systemd-escape --path "${path}")"
148         else
149                 echo "-"
150         fi
151 }
152
153 # Scrub any mounted fs on lvm by creating a snapshot and fscking that.
154 stdin="$(realpath /dev/stdin)"
155 ls_targets | while read tgt; do
156         # If we're not reaping and systemd is present, try invoking the
157         # systemd service.
158         if [ "${reap}" -ne 1 ] && type systemctl > /dev/null 2>&1; then
159                 tgt_esc="$(escape_path_for_systemd "${tgt}")"
160                 ${DBG} systemctl start "e2scrub@${tgt_esc}" 2> /dev/null < "${stdin}"
161                 res=$?
162                 if [ "${res}" -eq 0 ] || [ "${res}" -eq 1 ]; then
163                         continue;
164                 fi
165         fi
166
167         # Otherwise use direct invocation
168         ${DBG} "@root_sbindir@/e2scrub" ${scrub_args} "${tgt}" < "${stdin}"
169 done
170
171 exitcode 0