-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixall.sh
executable file
·91 lines (79 loc) · 2.55 KB
/
fixall.sh
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# Copyright (C) 2017 bsmelo.io
#
# This file is subject to the terms and conditions of the GNU General
# Public License v3.0. See the file LICENSE in the top level directory
# for more details.
# Dependencies: smartctl, grep, dd, ifind, ffind, debugfs, shred, awk
FS_TYPE="ext4" # "NTFS" or "ext4"
STARTING_BLOCK=16001024 # From `sudo fdisk -lu /dev/sda`
DEVICE="/dev/sda" # Target Physical Device
FS_DEVICE="/dev/sda2" # Target File System
AUTO_INODE="NOOOONE" # Known bad inode (useful for big files spanning multiple bad sectors)
# We assume a Block Size of 4086 bytes and a Sector of 512 bytes
run_dd_ntfs ()
{
(set -x; sudo dd seek="$1" count=1 oflag=direct if=/dev/zero of="${DEVICE}")
sudo sync
echo
echo
}
run_dd_ext3 ()
{
(set -x; sudo dd if=/dev/zero of="${FS_DEVICE}" bs=4096 count=1 seek="$1")
sudo sync
echo
echo
}
b=$((($1-${STARTING_BLOCK})/8))
echo "Block is ${b}"
if [ "${FS_TYPE}" == "NTFS" ]; then
inode=$(sudo ifind -d $b "${FS_DEVICE}")
echo "Inode is ${inode}"
if [[ $inode == "${AUTO_INODE}" ]]; then
echo "Auto"
run_dd_ntfs $1
else
fname=$(sudo ffind "${FS_DEVICE}" $inode)
echo "File is ${fname}"
read -r -p "Should we delete this block? " response
if [[ $response == "y" ]]
then
run_dd_ntfs $1
fi
fi
elif [ "${FS_TYPE}" == "ext4" ]; then
output=$(set -x; sudo debugfs -R "testb ${b}" "${FS_DEVICE}")
if [[ $output == *"not in use"* ]]; then
echo "Block is not in use"
read -r -p "Should we delete this block? " response
if [[ $response == "y" ]]
then
run_dd_ext3 $b
fi
exit 0
fi
inode=$(set -x; sudo debugfs -R "icheck ${b}" "${FS_DEVICE}" | awk 'NR == 2 {print $2}')
echo "Inode is ${inode}"
if [[ $inode == "${AUTO_INODE}" ]]; then
echo "Auto"
run_dd_ext3 $b
else
fname=$(set -x; sudo debugfs -R "ncheck ${inode}" "${FS_DEVICE}" | awk 'NR == 2 { s = ""; for (i = 2; i <= NF; i++) s = s $i " "; print s }')
echo "File is ${fname}"
read -r -p "Should we delete this block? " response
if [[ $response == "y" ]]
then
run_dd_ext3 $b
else
read -r -p "Should we shred this file? " response
if [[ $response == "y" ]]
then
(set -x; shred --iterations=2 --remove "${fname}")
fi
fi
fi
else
echo "Unsupported File System type"
exit 1
fi