X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=scrub%2Fe2scrub.in;h=30ab7cbd11c63c31b9f3253bafd46fce46426765;hb=0b3208958eb63df6cd8b38ee63f3bc4266a683e7;hp=e1965db4ec8751c4e0f95cf25dc4820f4f62449c;hpb=a2df58945c232186c96ffa8958f555ed33965ff7;p=tools%2Fe2fsprogs.git diff --git a/scrub/e2scrub.in b/scrub/e2scrub.in index e1965db..30ab7cb 100644 --- a/scrub/e2scrub.in +++ b/scrub/e2scrub.in @@ -18,11 +18,18 @@ # along with this program; if not, write the Free Software Foundation, # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. -# Automatically check a LVM-managed filesystem online. +# Automatically check an LVM-managed filesystem online. # We use lvm snapshots to do this, which means that we can only # check filesystems in VGs that have at least 256MB (or so) of # free space. +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin + +if (( $EUID != 0 )); then + echo "e2scrub must be run as root" + exit 1 +fi + snap_size_mb=256 fstrim=0 reap=0 @@ -34,7 +41,8 @@ test -f "${conffile}" && . "${conffile}" print_help() { echo "Usage: $0 [OPTIONS] mountpoint | device" echo - echo "mountpoint must be on a LVM-managed block device" + echo "mountpoint must be on an LVM-managed block device" + echo "-n: Show what commands e2scrub would execute." echo "-r: Remove e2scrub snapshot and exit, do not check anything." echo "-t: Run fstrim if successful." echo "-V: Print version information and exit." @@ -58,7 +66,7 @@ exitcode() { # for capturing all the log messages if the scrub fails, because the # fail service uses the service name to gather log messages for the # error report. - if [ -n "${SERVICE_MODE}" ]; then + if [ -n "${SERVICE_MODE}" -a "${ret}" -ne 0 ]; then test "${ret}" -ne 0 && ret=1 sleep 2 fi @@ -66,8 +74,9 @@ exitcode() { exit "${ret}" } -while getopts "rtV" opt; do - case "${opt}" in +while getopts "nrtV" opt; do + case "${opt}" in + "n") DBG="echo Would execute: " ;; "r") reap=1;; "t") fstrim=1;; "V") print_version; exitcode 0;; @@ -82,6 +91,19 @@ if [ -z "${arg}" ]; then exitcode 1 fi +if ! type lsblk >& /dev/null ; then + echo "e2scrub: can't find lsblk --- is util-linux installed?" + exitcode 1 +fi + +if ! type lvcreate >& /dev/null ; then + echo "e2scrub: can't find lvcreate --- is lvm2 installed?" + exitcode 1 +fi + +# close file descriptor 3 (from cron) since it causes lvm to kvetch +exec 3<&- + # Find the device for a given mountpoint dev_from_mount() { local mountpt="$(realpath "$1")" @@ -142,7 +164,7 @@ lvm_vars="$(lvs --nameprefixes -o name,vgname,lv_role --noheadings "${dev}" 2> / eval "${lvm_vars}" if [ -z "${LVM2_VG_NAME}" ] || [ -z "${LVM2_LV_NAME}" ] || echo "${LVM2_LV_ROLE}" | grep -q "snapshot"; then - echo "${arg}: Not connnected to a LVM logical volume." + echo "${arg}: Not connnected to an LVM logical volume." print_help exitcode 16 fi @@ -152,10 +174,10 @@ snap_dev="/dev/${LVM2_VG_NAME}/${snap}" teardown() { # Remove and wait for removal to succeed. - ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" 3>&- + ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" while [ -e "${snap_dev}" ] && [ "$?" -eq "5" ]; do sleep 0.5 - ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" 3>&- + ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" done } @@ -179,19 +201,19 @@ mark_corrupt() { setup() { # Try to remove snapshot for 30s, bail out if we can't remove it. - lveremove_deadline="$(( $(date "+%s") + 30))" - ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" 3>&- 2>/dev/null + lvremove_deadline="$(( $(date "+%s") + 30))" + ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" 2>/dev/null while [ -e "${snap_dev}" ] && [ "$?" -eq "5" ] && [ "$(date "+%s")" -lt "${lvremove_deadline}" ]; do sleep 0.5 - ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" 3>&- + ${DBG} lvremove -f "${LVM2_VG_NAME}/${snap}" done if [ -e "${snap_dev}" ]; then echo "${arg}: e2scrub snapshot is in use, cannot check!" return 1 fi # Create the snapshot, wait for device to appear. - ${DBG} lvcreate -s -L "${snap_size_mb}m" -n "${snap}" "${LVM2_VG_NAME}/${LVM2_LV_NAME}" 3>&- + ${DBG} lvcreate -s -L "${snap_size_mb}m" -n "${snap}" "${LVM2_VG_NAME}/${LVM2_LV_NAME}" if [ $? -ne 0 ]; then echo "${arg}: e2scrub snapshot FAILED, will not check!" return 1