examples: non-destructive

badblocks -nvs /dev/xxx

This would check the drive “sdb” in non-destructive read-write mode and display progress by writing out the block numbers as they are checked.

WARNING! DATA LOSS! destructive (-w = write test)

update: 2024: want to resell harddisk? shred it many many times for example with the help of this shred_harddisk_with_logs.sh “secure & fast wipe and refurbishment harddisk script v1.1 by dwaves.org GPLv2” script! 🙂

if the user wants the data on the harddisk ssd shredded…

badblocks -wvs /dev/xxx
# with specific block size
badblocks -wvsb 4096 /dev/xxx

This would check the harddisk in destructive (!WARNING ALL DATA WILL BE LOST!) read-write mode (-w = write-mode), which writes 4 different patterns on the whole partition and verifies each by reading back. It displays progress by writing out the block numbers as they are checked (-s = show, -v = verbose). All data on the partition will be overwritten at the block level.

DEPENDING ON HARDDISK SPEED & CONNECTION (USB/SATA) THIS WILL TAKE LOOOOONG! (because badblocks will test multiple write patterns)

sample output:

Checking for bad blocks in read-write mode
From block 0 to 62514773
Testing with pattern 0xaa: done 
Reading and comparing: done 
Testing with pattern 0x55: 34.85% done, 5h:38min:09 elapsed. (0/0/0 errors) <- that was only one-run-pattern on 256GB SSD via USB 2.0

-w Use write-mode test. With this option, badblocks scans for bad blocks by writing some patterns (0xaa, 0x55, 0xff, 0x00) on every block of the device, reading every block and comparing the contents.

-s Show the progress of the scan by writing out rough percentage completion of the current badblocks pass over the disk. Note that badblocks may do multiple test passes over the disk, in particular if the -p or -w option is requested by the user.

-b block_size Specify the size of blocks in bytes. The default is 1024

creditz: https://en.wikipedia.org/wiki/Badblocks

here is a comprehensive (no-write-non-destructive) harddisk test script:

it does:

  • detailed info about the harddisk
  • report about filesystem
  • smart short and long test
  • test for Media_Wearout_Indicator (not all harddisks ssds have that)
  • badblocks (non-destructive, read only)
vim /scripts/harddisk_info.sh
#!/bin/bash

HARDDISK=$1

echo "==== detailed info about the harddisk ===="
# get infos about the harddisk

# apt install lshw
# overview over all hardisks
# lshw -class tape -class disk -class storage -short
echo "==== enabling smart on the device (if not already) ===="
smartctl -s on -a $HARDDISK

echo "==== overall smart info ==== (WARNING! THIS IS A VERY ROUGH ESTIMATE ABOUT THE HARDDISK HEALTH THAT COULD CHANGE ANY MINUTE)"
smartctl -H $HARDDISK

echo "==== filesystem ===="

# how many blocks does this partition sdc1 have?
# (use sdc to see blocks of the whole harddisk)
blockdev --report $HARDDISK

echo "==== detailed hardware info ===="

hdparm -I $HARDDISK

tune2fs -l $HARDDISK

smartctl -i $HARDDISK

smartctl -a $HARDDISK

echo "=== testing for Media_Wearout_Indicator (not all harddisks have it) ==="
STATUS=$(smartctl -a $HARDDISK | grep Media_Wearout_Indicator)
if [ -n "$STATUS" ]; then
	echo $STATUS
else
	echo "does not have that indicator..."
fi
sleep 3;

read -p "==== run tests? (Y/N) ====" -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi

echo "=== running short test ==="
smartctl -t short -a $HARDDISK

echo "short - [ATA] runs SMART Short Self Test (usually under ten minutes).  This command can be given during normal system operation (unless run in captive mode - see the  '-C'
option  below).  This is a test in a different category than the immediate or automatic offline tests.

The "Self" tests check the electrical and mechanical performance as well as the read performance of the disk.

Their results are reported in the Self Test Error Log, readable with the '-l selftest' option.  Note  that  on  some  disks  the progress of the self-test can be monitored by watching this log during the self-test; with other disks use the '-c' option to monitor progress"

STATUS=""
while :
do
	STATUS=$(smartctl -c $HARDDISK|grep remaining)
	if [ -n "$STATUS" ]; then
		echo $STATUS
	else
		echo "finished..."
		break;
	fi
	sleep 1;
done

smartctl -l selftest $HARDDISK
sleep 10;

echo "=== running long test ==="
echo "long - [ATA] runs SMART Extended Self Test (tens of minutes to several hours).
This is a longer and more thorough version of the Short Self Test described above. Note that this command can be given during normal system operation (unless run in captive mode - see the '-C' option below)."

smartctl -t long -a $HARDDISK

STATUS=""
while :
do
	STATUS=$(smartctl -c $HARDDISK|grep remaining)
	if [ -n "$STATUS" ]; then
		echo $STATUS
	else
		echo "finished..."
		break;
	fi
	sleep 1;
done

smartctl -l selftest $HARDDISK
sleep 10;

read -p "==== scan for bad blocks? (non destructive) (Y/N) ====" -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi

badblocks -nvs $HARDDISK

usage:

/scripts/harddisk_info.sh /dev/sxx

man:

badblocks.man.txt

smartctl.man.txt

hdparm.man.txt

gui based tools:

https://www.geeksforgeeks.org/gsmartcontrol-tool-to-check-ssd-hdd-health-on-linux/

Links:

https://opensource.com/article/21/9/nvme-cli

GNU Linux bash script – iterate over all harddisks in the system and check their smart status – also for QNAP NAS

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin