Badblocks
Another tips/reminder post for when I’ll need it again in the future.
When I get a new hard drive, or I’m getting rid of one, I like to make sure the drive is properly wiped-out and that it’s in a good working state.
It turns out that there’s a software doing both of that: badblocks.
Note that wiping out an SSD is different and this doesn’t really apply.
Checking hard drive’s block size
This is an optional part, but seems useful in some cases to specify the disk’s block size to badblocks.
Install blockdev first if necessary, on Debian: sudo apt install util-linux
Get the block size: sudo blockdev --getbsz /dev/sdX (replace sdX by the hard drive you want to check)
Installing badblocks
On Debian: sudo apt install e2fsprogs
Using badblocks
Note: the data on the drive will be lost as testing it requires writing on the whole disk.
I usually run badblocks this way:
sudo badblocks -s -v -w -b 4096 -c 65536 -t 0xaa -t 0x55 -t 0xff -t 0x00 -t random -o /tmp/badblocks.txt /dev/sdX
Here is a detail of the used options:
- -s: show progress.
- -v: verbose, display the number of errors.
- -w: write mode, this is DESTRUCTIVE, all content on the hard drive will be lost.
- -b: specify the block size.
- -c: specify the number of blocks to test at a time, I usually use a high value to increase speed, adjust to your own needs.
- -t: specify the patterns to write on the drive, if not specified- badblockswill by default use- 0xaa,- 0x55,- 0xffand- 0x00but since I want to add the- randompattern too, I have to put all of them again. Basically,- badblockswrite the pattern on the whole drive, then read it again, the read pattern should match the written one. Trying different patterns to cover all the combinations to make sure all bits of the drive work:- 0xaabeing- 10101010;
- 0x55being- 01010101;
- 0xffbeing- 11111111;
- 0x00being- 00000000;
- randombeing a random pattern chosen when starting- badblocks.
 
- -o: writes the bad blocks found in a file.
- /dev/sdx: the drive to test.
Again, this is a destructive operation, make sure you run it on the right drive.