LinuxCommandLibrary

badblocks

Scan a storage device for bad sectors

TLDR

Search a disk for bad blocks by using a non-destructive read-only test

$ sudo badblocks [/dev/sdX]
copy

Search an unmounted disk for bad blocks with a [n]on-destructive read-write test
$ sudo badblocks -n [/dev/sdX]
copy

Search an unmounted disk for bad blocks with a destructive [w]rite test
$ sudo badblocks -w [/dev/sdX]
copy

Use the destructive [w]rite test and [s]how [v]erbose progress
$ sudo badblocks -svw [/dev/sdX]
copy

In destructive mode, [o]utput found blocks to a file
$ sudo badblocks -o [path/to/file] -w [/dev/sdX]
copy

Use the destructive mode with improved speed using 4K [b]lock size and 64K block [c]ount
$ sudo badblocks -w -b [4096] -c [65536] [/dev/sdX]
copy

SYNOPSIS

badblocks [options] device [block_count [start_block]]

PARAMETERS

-b block_size
    Specify the block size in bytes for the scan. Default is 1024 bytes.

-c num_blocks
    Specify the number of blocks to test at a time during the scan.

-f
    Force the check, even if the device is mounted. Use with extreme caution.

-n
    Perform a non-destructive read-write test. This mode writes a test pattern, verifies, then restores the original data.

-o output_file
    Write the list of found bad blocks to the specified file, one block number per line.

-p num_passes
    Specify the number of passes over the device. Default is 1 pass.

-s
    Show the progress of the scan by printing percentages.

-t test_pattern
    Specify a test pattern. Can be a numeric pattern (e.g., 0xaa, 0x55, 0xff, 0x00) or 'random' for a random pattern.

-v
    Enable verbose mode, providing more detailed information during the scan.

-w
    Perform a destructive write-test mode. This writes test patterns to every block and verifies.
WARNING: This will destroy ALL data on the device.

device
    The block device or partition to be scanned (e.g., /dev/sda1, /dev/sdb).

block_count
    The number of blocks to check, starting from start_block. If omitted, checks to the end of the device.

start_block
    The starting block number from which to begin the scan. If omitted, starts from block 0.

DESCRIPTION

badblocks is a utility program for testing block devices (like hard drives, SSDs, or partitions) for bad blocks. It performs a comprehensive scan, identifying sectors that cannot be reliably read or written due to physical damage or corruption. The primary use case is to locate these faulty blocks before creating a filesystem with mkfs or after a filesystem has been created using e2fsck. By marking these bad blocks, the filesystem avoids writing data to them, thereby preventing data loss and ensuring the integrity of stored information. It supports both non-destructive read-only or read-write tests and a more thorough, but destructive, write-test mode. It is an integral part of the e2fsprogs suite of utilities, commonly used with ext2, ext3, and ext4 filesystems.

CAVEATS

  • Data Loss: The -w (write-test) option is destructive and will erase all data on the target device. Always double-check the device name before running this mode.
  • Unmounted Device: It is highly recommended to run badblocks on an unmounted device or partition to avoid data corruption and ensure accurate results. If the device is mounted, use the -f option with extreme caution.
  • Time Consuming: Scanning large drives, especially with multiple passes or in write-test mode, can take a very long time (hours to days) to complete.
  • Permissions: Running badblocks typically requires root privileges.

TYPICAL WORKFLOW

The most common workflow involves running badblocks on a new or problematic drive (preferably unmounted) and directing its output to a file using the -o option. For example:

sudo badblocks -svw -o bad_blocks.txt /dev/sdb

(destructive scan, output to file)
or
sudo badblocks -svno bad_blocks.txt /dev/sdb

(non-destructive scan, output to file).

This bad_blocks.txt file, containing the list of bad block numbers, can then be passed to mkfs when creating a filesystem, or to e2fsck to mark existing bad blocks within an already created filesystem. For instance:
sudo mkfs.ext4 -l bad_blocks.txt /dev/sdb1

or
sudo e2fsck -l bad_blocks.txt /dev/sdb1

This ensures that the filesystem avoids using the damaged sectors, preventing data corruption.

HISTORY

The badblocks utility has been a long-standing component of the e2fsprogs package, which provides essential utilities for the ext2, ext3, and ext4 filesystems in Linux. Its development has closely tracked the evolution of these filesystems, providing a foundational tool for ensuring the reliability and data integrity of storage media before or after filesystem creation. It has been a standard part of most Linux distributions for many years, serving as a critical diagnostic and preparatory tool for disk management.

SEE ALSO

e2fsck(8), fsck(8), mkfs(8), debugfs(8), tune2fs(8)

Copied to clipboard