Friday 22 January 2010

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
o -s = show progress
o -v = verbose mode
o -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
o -n = non-destructive read-write mode
o -s = show progress
o -v = verbose mode
o -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
o -w = destructive write-mode test
o -s = show progress
o -v = verbose mode
o -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
o -w = destructive write-mode test
o -t random = write random data onto the disk
o -s = show progress
o -v = verbose mode
o -c 10240 – check 10K blocks at a time

1 comment: