summaryrefslogtreecommitdiff
path: root/kamikaze-fsck
blob: 24640011d8fb3b099e5454f6ef7a7c2147a60486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/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