#!/bin/bash
export PATH="$PATH:/:/tmp"
## Before running this script, you must first have done these commands once:
##
## smartctl -S on /dev/hdX
## smartctl -t long /dev/hdX
## ## wait for it to finish, according to the instructions it gives you
dev=$1
if [ ! -b "$dev" ]; then
echo "$0: bad/missing device path parameter (eg. /dev/hda)"
exit 1
fi
function mend_sector(){
dev=$1
lba=$2
echo -n "$dev: Examining LBA $lba: "
hdparm --read-sector $lba $dev &>/dev/null
if [ $? -ne 5 ]; then
echo "not bad."
else
echo -n "needs repair: "
echo hdparm --yes-i-know-what-i-am-doing --repair-sector $lba $dev
hdparm --yes-i-know-what-i-am-doing --repair-sector $lba $dev
fi
}
function mend_bad_sectors(){
dev=$1
while [ 1 ]; do
read lba
[ "$lba" == "" ] && break
[ $((lba)) -gt 0 ] && mend_sector $dev $lba
done
}
## Fix currently known errors:
smartctl -l error $dev | grep 'Error:.*LBA = 0x[^ ]* = [0-9][0-9]*$' | sed -e"s/^.* //" | mend_bad_sectors $dev
smartctl -l selftest $dev | grep 'read failure ' | sed -e"s/^.* //" | mend_bad_sectors $dev