badblocks is a utility that checks for bad blocks on a storage device.

This is useful for testing new or questionable media including fake USB drives that show as a larger drive than it can actually store. It's also useful for testing new hard drives (before the warranty expires) or checking if a older drive is reliable. Be forewarned - larger disks takes a very long time to test.

Basic Usage[edit]

The destructive test can be done by running:

## Warning! This will wipe your data!
# badblocks -wsv -o /root/badblocks.txt /dev/sda

Use the -n flag for a non-destructive test. A non-destructive test will store the sector value, test the sector, then restore the original sector value.

There are 3 numbers that will be returned as 1/2/3 in this order:

  1. number of read errors
  2. number of write errors
  3. number of corruption errors

The badblocks.txt file that gets created can then be used by mkfs so that the filesystem can skip over known bad sectors like so:

# mkfs.ext4 -l /tmp/badblocks.txt /dev/sda

Creating a filesystem around bad blocks[edit]

Most filesystems can take a file containing a list of all bad blocks and work around it. However, unless you know what you're doing, it's suggested that you use the -c option in mkfs.whatever instead because different filesystems may use different block sizes. The benefit of using badblocks is the fact testing is done with multiple passes rather than the one built into mkfs.


# fdisk /dev/sdb
## Make your desired partitions

# badblocks -wsv -o /tmp/badblocks.txt /dev/sdb1
# mkfs.vfat -l /tmp/badblocks /dev/sdb1

In some cases, the bad blocks may be positioned such that the filesystem cannot be created. Depending on where these bad blocks are located, this can be worked around by resizing down the partition or moving the starting location of the partition.

For instance, if every block past 40000 are bad, you will need to create a partition that is (40,000/1024) megabytes or smaller in order to avoid these bad blocks. We divide by 1024 because that's the default block size used by badblocks, unless otherwise specified by the -b option.

Other Information[edit]

Test Patterns[edit]

The test that is done by default is to write to the disk in 4 passes, each with the following pattern:

  1. 0xAA - 10101010
  2. 0x55 - 01010101
  3. 0xff - 11111111
  4. 0x00 - 00000000

You may override this using the -t flag.

See Also[edit]