Checking disks for errors using the badblocks command
For better or worse I’ve been needing to check drives for errors quite a bit recently and have been using the badblocks command to do it. It’s definitely a command that people should become familiar with. Where fsck checks the file system for problems, badblocks attempts to ascertain the status of physical media which can include hard disks, usb drives, and other usb devices such as media players. I’ve compiled a detailed list of commands I’ve been using.
Read-only Test
This is a non-destructive read-only test which can be run on disk even if it contains a mounted filesystem. It simply verifies that each block can be read; it does not test for write errors.
sudo badblocks -s -v -c 10240 /dev/sdx- -s = show progress
- -v = verbose mode
- -c 10240 - check 10K blocks at a time
Read-write Test
This test is non-destructive read-write test which reads each block, writes it, then verifies it. It should not be used on block devices with mounted filesystems as it can lead to filesystem corruption.
sudo badblocks -n -s -v -c 10240 /dev/sdx- -n = non-destructive read-write mode
- -s = show progress
- -v = verbose mode
- -c 10240 - check 10K blocks at a time
Write-mode Test
Using this command will erase all data on the device so only use it if that is what you want. This will write a few patterns to each block, verifying that each one is written and read correctly.
sudo badblocks -w -s -v -c 10240 /dev/sdx- -w = destructive write-mode test
- -s = show progress
- -v = verbose mode
- -c 10240 - check 10K blocks at a time
Prepare a disk for encryption
The command will completely erase the data on a disk and replace it with random data. This is often a preferred way to prepare a disk for encryption as it is faster than other methods of filling a disk with random data and serves the purpose of checking the disk for errors before the encryption process.
sudo badblocks -w -t random -s -v -c 10240 /dev/sdx- -w = destructive write-mode test
- -t random = write random data onto the disk
- -s = show progress
- -v = verbose mode
- -c 10240 - check 10K blocks at a time




What's the difference
What's the difference between checking 10K blocks at a time and checking 64 blocks at a time (which is the default).
Interesting article. Thanks!
"-t random" is not random enough for encryption purposes
Hi, thanks for the useful tutorial - I completely missed the "-c" option on the man page, but seeing it here made me think about it and I can see how it reduces overhead (you can handle the data in bigger bursts).
Then, I was curious how random the data would be with "-t random", and looked up the source code (I found it at Sourceforge in the e2fsprogs project, the file is misc/badblocks.c and the function is called test_rw). It turns out, the random pattern that is generated only has a length "blocks_at_once * block_size" where the former is what you specify with "-c" and the latter is what you specify with "-b".
That means that for your settings above the pattern is repeated on disk every 10MB. I don't think this is good for an encrypted partition - it would be easy to see which parts have been used.
(I left out the link to Sourceforge to not get this stuck in some comment filter but it's easy to find)