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 21PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 22 23if (( $EUID != 0 )); then 24 echo "e2scrub_all must be run as root" 25 exit 1 26fi 27 28periodic_e2scrub=0 29scrub_all=0 30snap_size_mb=256 31reap=0 32conffile="@root_sysconfdir@/e2scrub.conf" 33 34test -f "${conffile}" && . "${conffile}" 35 36scrub_args="" 37 38print_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 46print_version() { 47 echo "e2scrub_all @E2FSPROGS_VERSION@ (@E2FSPROGS_DATE@)" 48} 49 50exitcode() { 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 if [ -n "${SERVICE_MODE}" -a "${ret}" -ne 0 ]; then 60 test "${ret}" -ne 0 && ret=1 61 fi 62 63 exit "${ret}" 64} 65 66while getopts "nrAV" opt; do 67 case "${opt}" in 68 "n") DBG="echo Would execute: " ;; 69 "r") scrub_args="${scrub_args} -r"; reap=1;; 70 "A") scrub_all=1;; 71 "V") print_version; exitcode 0;; 72 *) print_help; exitcode 2;; 73 esac 74done 75shift "$((OPTIND - 1))" 76 77# If we're in service mode and the service is not enabled via config file... 78if [ -n "${SERVICE_MODE}" -a "${periodic_e2scrub}" -ne 1 ]; then 79 # ...don't start e2scrub processes. 80 if [ "${reap}" -eq 0 ]; then 81 exitcode 0 82 fi 83 84 # ...and if we don't see any leftover e2scrub snapshots, don't 85 # run the reaping process either, because lvs can be slow. 86 if ! readlink -q -s -e /dev/mapper/*.e2scrub* > /dev/null; then 87 exitcode 0 88 fi 89fi 90 91# close file descriptor 3 (from cron) since it causes lvm to kvetch 92exec 3<&- 93 94# If some prerequisite packages are not installed, exit with a code 95# indicating success to avoid spamming the sysadmin with fail messages 96# when e2scrub_all is run out of cron or a systemd timer. 97 98if ! type mapfile >& /dev/null ; then 99 test -n "${SERVICE_MODE}" && exitcode 0 100 echo "e2scrub_all: can't find mapfile --- is bash 4.xx installed?" 101 exitcode 1 102fi 103 104if ! type lsblk >& /dev/null ; then 105 test -n "${SERVICE_MODE}" && exitcode 0 106 echo "e2scrub_all: can't find lsblk --- is util-linux installed?" 107 exitcode 1 108fi 109 110if ! type lvcreate >& /dev/null ; then 111 test -n "${SERVICE_MODE}" && exitcode 0 112 echo "e2scrub_all: can't find lvcreate --- is lvm2 installed?" 113 exitcode 1 114fi 115 116# Find scrub targets, make sure we only do this once. 117ls_scan_targets() { 118 local devices=$(lvs -o lv_path --noheadings -S "lv_active=active,lv_role=public,lv_role!=snapshot,vg_free>=${snap_size_mb}") 119 120 if [ -z "$devices" ]; then 121 return 0; 122 fi 123 lsblk -o NAME,MOUNTPOINT,FSTYPE,TYPE -P -n -p $devices | \ 124 grep FSTYPE=\"ext\[234\]\" | grep TYPE=\"lvm\" | \ 125 while read vars ; do 126 eval "${vars}" 127 128 if [ "${scrub_all}" -eq 1 ] || [ -n "${MOUNTPOINT}" ]; then 129 echo ${MOUNTPOINT:-${NAME}} 130 fi 131 done 132} 133 134# Find leftover scrub snapshots 135ls_reap_targets() { 136 lvs -o lv_path -S lv_role=snapshot -S lv_name=~\(e2scrub$\) \ 137 --noheadings | sed -e 's/.e2scrub$//' 138} 139 140# Figure out what we're targeting 141ls_targets() { 142 if [ "${reap}" -eq 1 ]; then 143 ls_reap_targets 144 else 145 ls_scan_targets 146 fi 147} 148 149# systemd doesn't know to do path escaping on the instance variable we pass 150# to the e2scrub service, which breaks things if there is a dash in the path 151# name. Therefore, do the path escaping ourselves if needed. 152# 153# systemd path escaping also drops the initial slash so we add that back in so 154# that log messages from the service units preserve the full path and users can 155# look up log messages using full paths. However, for "/" the escaping rules 156# do /not/ drop the initial slash, so we have to special-case that here. 157escape_path_for_systemd() { 158 local path="$1" 159 160 if [ "${path}" != "/" ]; then 161 echo "-$(systemd-escape --path "${path}")" 162 else 163 echo "-" 164 fi 165} 166 167# Scrub any mounted fs on lvm by creating a snapshot and fscking that. 168mapfile -t targets < <(ls_targets) 169for tgt in "${targets[@]}"; do 170 # If we're not reaping and systemd is present, try invoking the 171 # systemd service. 172 if [ "${reap}" -ne 1 ] && type systemctl > /dev/null 2>&1; then 173 tgt_esc="$(escape_path_for_systemd "${tgt}")" 174 ${DBG} systemctl start "e2scrub@${tgt_esc}" 2> /dev/null 175 res=$? 176 if [ "${res}" -eq 0 ] || [ "${res}" -eq 1 ]; then 177 continue; 178 fi 179 fi 180 181 # Otherwise use direct invocation 182 ${DBG} "@root_sbindir@/e2scrub" ${scrub_args} "${tgt}" 183done 184 185exitcode 0 186