]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - scrub/e2scrub_all.in
e2scrub_all: make sure there's enough free space for a snapshot
[thirdparty/e2fsprogs.git] / scrub / e2scrub_all.in
index 9581dc2c4d0491a4f6fb6b79749b0608eccc0e18..4cb90a0de4765f76821f0a89ac3ae00f85a021f8 100644 (file)
 #  along with this program; if not, write the Free Software Foundation,
 #  Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
 
+PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
+
 scrub_all=0
+snap_size_mb=256
 conffile="@root_sysconfdir@/e2scrub.conf"
 
 test -f "${conffile}" && . "${conffile}"
@@ -27,8 +30,9 @@ scrub_args=""
 
 print_help() {
        echo "Usage: $0 [OPTIONS]"
-       echo " -A: Scrub all ext[234] filesystems even if not mounted."
+       echo " -n: Show what commands e2scrub_all would execute."
        echo " -r: Remove e2scrub snapshots."
+       echo " -A: Scrub all ext[234] filesystems even if not mounted."
        echo " -V: Print version information and exit."
 }
 
@@ -58,16 +62,31 @@ exitcode() {
        exit "${ret}"
 }
 
-while getopts "ArV" opt; do
+while getopts "nrAV" opt; do
        case "${opt}" in
-       "A") scrub_all=1;;
+       "n") DBG="echo Would execute: " ;;
        "r") scrub_args="${scrub_args} -r";;
+       "A") scrub_all=1;;
        "V") print_version; exitcode 0;;
        *) print_help; exitcode 2;;
        esac
 done
 shift "$((OPTIND - 1))"
 
+# If some prerequisite packages are not installed, exit with a code
+# indicating success to avoid spamming the sysadmin with fail messages
+# when e2scrub_all is run out of cron or a systemd timer.
+
+if ! type lsblk >& /dev/null ; then
+    echo "e2scrub_all: can't find lsblk --- is util-linux installed?"
+    exitcode 0
+fi
+
+if ! type lvcreate >& /dev/null ; then
+    echo "e2scrub_all: can't find lvcreate --- is lvm2 installed?"
+    exitcode 0
+fi
+
 # Find scrub targets, make sure we only do this once.
 ls_scrub_targets() {
        lsblk -o NAME,FSTYPE,MOUNTPOINT -p -P -n | while read vars; do
@@ -90,6 +109,9 @@ ls_scrub_targets() {
                eval "${lvm_vars}"
                echo "${LVM2_LV_ROLE}" | grep -q "snapshot" && continue
 
+               free_space="$(vgs -o vg_free --units m --noheadings --no-suffix "${LVM2_VG_NAME}" 2> /dev/null | sed -e 's/\..*//')"
+               test "${snap_size_mb}" -gt "${free_space}" && continue
+
                if [ -n "${MOUNTPOINT}" ]; then
                        echo "${MOUNTPOINT}"
                else
@@ -101,13 +123,18 @@ ls_scrub_targets() {
 # systemd doesn't know to do path escaping on the instance variable we pass
 # to the e2scrub service, which breaks things if there is a dash in the path
 # name.  Therefore, do the path escaping ourselves if needed.
+#
+# systemd path escaping also drops the initial slash so we add that back in so
+# that log messages from the service units preserve the full path and users can
+# look up log messages using full paths.  However, for "/" the escaping rules
+# do /not/ drop the initial slash, so we have to special-case that here.
 escape_path_for_systemd() {
        local path="$1"
 
-       if echo "${path}" | grep -q -- "-"; then
+       if [ "${path}" != "/" ]; then
                echo "-$(systemd-escape --path "${path}")"
        else
-               echo "${path}"
+               echo "-"
        fi
 }