Whamcloud - gitweb
f342faf2d636b51032e5b9288fada962f497ca65
[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 snap_size_mb=256
30 reap=0
31 conffile="@root_sysconfdir@/e2scrub.conf"
32
33 test -f "${conffile}" && . "${conffile}"
34
35 scrub_args=""
36
37 print_help() {
38         echo "Usage: $0 [OPTIONS]"
39         echo " -n: Show what commands e2scrub_all would execute."
40         echo " -r: Remove e2scrub snapshots."
41         echo " -A: Scrub all ext[234] filesystems even if not mounted."
42         echo " -V: Print version information and exit."
43 }
44
45 print_version() {
46         echo "e2scrub_all @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 "nrAV" opt; do
72         case "${opt}" in
73         "n") DBG="echo Would execute: " ;;
74         "r") scrub_args="${scrub_args} -r"; reap=1;;
75         "A") scrub_all=1;;
76         "V") print_version; exitcode 0;;
77         *) print_help; exitcode 2;;
78         esac
79 done
80 shift "$((OPTIND - 1))"
81
82 # If some prerequisite packages are not installed, exit with a code
83 # indicating success to avoid spamming the sysadmin with fail messages
84 # when e2scrub_all is run out of cron or a systemd timer.
85
86 if ! type lsblk >& /dev/null ; then
87     test -n "${SERVICE_MODE}" && exitcode 0
88     echo "e2scrub_all: can't find lsblk --- is util-linux installed?"
89     exitcode 1
90 fi
91
92 if ! type lvcreate >& /dev/null ; then
93     test -n "${SERVICE_MODE}" && exitcode 0
94     echo "e2scrub_all: can't find lvcreate --- is lvm2 installed?"
95     exitcode 1
96 fi
97
98 # Find scrub targets, make sure we only do this once.
99 ls_scan_targets() {
100     local devices=$(lvs -o lv_path --noheadings -S "lv_active=active,lv_role=public,lv_role!=snapshot,vg_free>${snap_size_mb}")
101
102     if [ -z "$devices" ]; then
103         return 0;
104     fi
105     lsblk -o NAME,MOUNTPOINT,FSTYPE,TYPE -P -n -p $devices | \
106         grep FSTYPE=\"ext\[234\]\" | grep TYPE=\"lvm\" | \
107         while read vars ; do
108                 eval "${vars}"
109
110                 if [ "${scrub_all}" -eq 1 ] || [ -n "${MOUNTPOINT}" ]; then
111                     echo ${MOUNTPOINT:-${NAME}}
112                 fi
113         done
114 }
115
116 # Find leftover scrub snapshots
117 ls_reap_targets() {
118     lvs -o lv_path -S lv_role=snapshot -S lv_name=~\(e2scrub$\) \
119         --noheadings | sed -e 's/.e2scrub$//'
120 }
121
122 # Figure out what we're targeting
123 ls_targets() {
124         if [ "${reap}" -eq 1 ]; then
125                 ls_reap_targets
126         else
127                 ls_scan_targets
128         fi
129 }
130
131 # systemd doesn't know to do path escaping on the instance variable we pass
132 # to the e2scrub service, which breaks things if there is a dash in the path
133 # name.  Therefore, do the path escaping ourselves if needed.
134 #
135 # systemd path escaping also drops the initial slash so we add that back in so
136 # that log messages from the service units preserve the full path and users can
137 # look up log messages using full paths.  However, for "/" the escaping rules
138 # do /not/ drop the initial slash, so we have to special-case that here.
139 escape_path_for_systemd() {
140         local path="$1"
141
142         if [ "${path}" != "/" ]; then
143                 echo "-$(systemd-escape --path "${path}")"
144         else
145                 echo "-"
146         fi
147 }
148
149 # Scrub any mounted fs on lvm by creating a snapshot and fscking that.
150 stdin="$(realpath /dev/stdin)"
151 ls_targets | while read tgt; do
152         # If we're not reaping and systemd is present, try invoking the
153         # systemd service.
154         if [ "${reap}" -ne 1 ] && type systemctl > /dev/null 2>&1; then
155                 tgt_esc="$(escape_path_for_systemd "${tgt}")"
156                 ${DBG} systemctl start "e2scrub@${tgt_esc}" 2> /dev/null < "${stdin}"
157                 res=$?
158                 if [ "${res}" -eq 0 ] || [ "${res}" -eq 1 ]; then
159                         continue;
160                 fi
161         fi
162
163         # Otherwise use direct invocation
164         ${DBG} "@root_sbindir@/e2scrub" ${scrub_args} "${tgt}" < "${stdin}"
165 done
166
167 exitcode 0