]> 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 23d122d258dbe5c3a36a86517b4f1462b67d3560..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