Badblocks

From Leo's Notes
Last edited on 2 October 2022, at 20:06.

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

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.

Use the -b option to specify the block sizes. This defaults to 1024 bytes. If you intend to use this on EXT filesystem creation, ensure this matches the filesystem block size by checking with dumpe2fs /dev/sdX | grep 'Block size'.

Understanding the output

Checking for bad blocks in read-write mode
From block 0 to 15630677
Testing with pattern 0xaa:  20.32% done, 1:55 elapsed. (0/0/0 errors)

There are 3 numbers that will be returned as (1/2/3 errors) in the last line. Each number represents:

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

Creating a filesystem around bad blocks

Most filesystems can take a file containing a list of all bad blocks and create a filesystem that skips over these known bad locations.

For ext4, you can pass in the list of bad blocks with -l. You must however, make sure that the block size you used with badblocks matches what mkfs uses. As a result, unless you're sure what you're doing, you should just use the -c or -cc options with mkfs.ext4.

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

## Alternatively, just have mkfs.ext4 check for badblocks for you. c=read only test, cc=read/write test
# mkfs.ext4 -cc /dev/sda

Other filesystems that take a list of bad blocks using -l include EXT2, EXT3, FAT. Here's another full example:

# 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

Test Patterns

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