#!/bin/bash if [ $# -ne 1 ] || [ ! -b "$1" ]; then >&2 echo 'run:' >&2 echo ' kamikaze-fsck /dev/$device' exit 1 fi if ! [ -w "$1" ]; then >&2 printf 'Cannot write to "%s" - you are not root, are you?\n' "$1" exit 1 fi >&2 printf 'Warning: running e2fsck in kamikaze-mode - this WILL destroy data on "%s".\n' "$1" >&2 printf 'Enter uppercase yes to continue.\n' read -r s if [ "$s" != 'YES' ]; then >&2 echo 'aborted.' exit 1 fi >&2 echo 'ok, lets do it' export LC_ALL="C" tmp_dir=$(mktemp -d) trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT touch "${tmp_dir}/faulty-inode" while [ -f "${tmp_dir}/faulty-inode" ]; do if [ -s "${tmp_dir}/faulty-inode" ]; then >&2 date uniq \ <"${tmp_dir}/faulty-inode" \ | while read -r s; do debugfs -w "$1" -R "${s}" done fi >&2 date rm "${tmp_dir}/faulty-inode" last_faulty_inode='INVALID' e2fsck -f -y "$1" -E no_optimize_extents,fixes_only -C 0 \ | tee /dev/stderr \ | sed ' s/^Inode \([0-9]\+\) block [0-9]\+ conflicts with critical metadata, skipping block checks\.$/\1/ t d ' \ | while read -r faulty_inode; do if [ "${last_faulty_inode}" != "${faulty_inode}" ]; then last_faulty_inode="${faulty_inode}" faulty_inode_count=0 fi faulty_inode_count=$((faulty_inode_count+1)) if [ ${faulty_inode_count} -ge 5 ]; then printf 'clri <%s>\n' "${faulty_inode}" \ >> "${tmp_dir}/faulty-inode" start=$(date +%s) pkill -xf "e2fsck -f -y $1 -E no_optimize_extents,fixes_only -C 0" while pgrep -xf "e2fsck -f -y $1 -E no_optimize_extents,fixes_only -C 0" >/dev/null; do if [ $(($(date +%s)-start)) -gt 10 ]; then pkill -9 -xf "e2fsck -f -y $1 -E no_optimize_extents,fixes_only -C 0" fi sleep 1 >&2 echo "running for $(($(date +%s)-start))" pkill -xf "e2fsck -f -y $1 -E no_optimize_extents,fixes_only -C 0" done break fi done done