HDD Badblock testing using smartctl

From Dawg Wiki

What you should know before doing anything

Using Badblock testing will PERMANENTLY delete all data on the testing Hard Drive! Before running a badblock test, make sure you're about to run the test on the correct drive.

Requirements

Configured postfix or a different mail daemon, screen and smartmontools installed

Usage

Run as root screen ./test.sh sd<DriveLetter> <NumberOfPasses>

test.sh

Make sure to use chmod +x test.sh before using

#!/bin/bash

if [ "$#" -lt 1 ];then
    echo "Usage: $0 <drive> [pass count]"
    echo "e.g. $0 sdc"
    exit 1
fi

passes=3

if [ "$#" -eq 2 ];then
    passes=$2
fi
 
DSK=$1
DISK=/dev/$1
smartctl -i $DISK

echo "Going to perform destructive read/write tests on $DISK"
echo "Break now with Ctrl-C to quit. To continue press Enter"

read

SERIAL=`smartctl -i $DISK | grep "Serial " | awk '{print $3;}'`

BASE=${DSK}_${SERIAL}

smartctl -a $DISK > $BASE-01-smart-before

echo "Starting badblocks test"

time badblocks -vw -p $passes -b 4194304 -f -o $BASE-02-bblocks $DISK 2> $BASE-03-bblocks

echo "Starting short smart test"
smartctl -t short $DISK

while smartctl -a $DISK | grep 'of test remaining';
do
    sleep 1
done

echo "Starting long smart test"
smartctl -t long $DISK

while smartctl -a $DISK | grep 'of test remaining';
do
    sleep 1
done

smartctl -a $DISK > $BASE-04-smart-after

cat $BASE* | mail -s "Disk test results for $SERIAL" YourMail@Here