Originally Posted By: Boelle
who can make such a script? i have both smartctl and hdparm in /tmp


Here you go, tested/fixed version is inline/attached here:
Code:
#!/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



Attachments
fix_bad_sectors.sh (234 downloads)
Description: fixed/tested version.




Edited by mlord (31/05/2008 12:59)