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