Manage ext4 Bad Sectors in Linux with badblocks
This article provides an overview of how the Linux operating system
identifies and handles damaged storage drive areas on ext4 filesystems
using the badblocks utility. You will learn the mechanics
of sector deterioration, the process of scanning partitions for physical
errors, and the methods used to integrate detected bad blocks into the
ext4 filesystem's internal allocation tables to prevent data
corruption.
Understanding Bad Sectors and ext4
A bad sector is a storage block on a hard drive or solid-state drive that cannot be reliably read from or written to due to physical damage or magnetic degradation. While modern drives feature internal firmware that transparently remaps failing sectors using spare sectors, severe damage exhausts this pool. When hardware-level remapping fails, the operating system must intervene at the filesystem level.
The ext4 filesystem manages storage via block groups and tracks unusable blocks using a dedicated, reserved data structure known as the bad block inode (inode 1). Once an ext4 filesystem marks a block as bad, the kernel's block allocator avoids assigning that specific block to any file, permanently isolating the damaged area from active use.
Scanning Disks with
badblocks
The badblocks command searches a device for damaged or
unreadable blocks. It operates in two primary modes:
- Read-Only Mode: Performs a non-destructive scan that reads every block on the designated device.
- Read-Write / Non-Destructive Write Mode: Writes
patterns to each block and reads them back to verify data integrity. The
non-destructive write mode (
-n) preserves existing data, while the destructive write mode (-w) overwrites everything and should only be used on unformatted or backed-up drives.
A typical read-only scan to output bad sectors to a text file is executed as follows:
badblocks -v /dev/sdX > bad_blocks.txtPassing Bad Blocks to ext4
Once badblocks identifies defective sectors, that list
must be registered with the ext4 filesystem so the OS ceases allocating
them. There are two primary workflows for doing this depending on
whether the filesystem is new or existing.
Method 1:
Scanning an Existing Filesystem via e2fsck
For an existing filesystem that is currently unmounted, the
e2fsck utility integrates directly with
badblocks.
To automatically run a read-only scan and append any discovered bad blocks to the ext4 bad block list:
e2fsck -v -c /dev/sdX1Using -c triggers a read-only test. Using
-cc runs a non-destructive read-write test, which takes
significantly longer but provides a more thorough assessment.
Alternatively, if you already generated a list using the standalone
badblocks command, apply it directly using the
-l flag:
e2fsck -l bad_blocks.txt /dev/sdX1Method 2: Marking Bad Blocks During Filesystem Creation
If you are preparing a drive and want to check for surface errors
before writing the ext4 filesystem, use the -c flag with
mke2fs or mkfs.ext4:
mkfs.ext4 -c /dev/sdX1This instruction commands mkfs.ext4 to invoke
badblocks in read-only mode before initializing the
filesystem. Any blocks reported defective are pre-allocated to the bad
block inode during filesystem creation. Passing -c -c
executes a non-destructive read-write scan instead.
Verifying Bad Block Allocations
To verify that the ext4 filesystem has successfully mapped the
unusable blocks, use dumpe2fs to inspect the filesystem
superblock:
dumpe2fs -b /dev/sdX1This prints the list of block numbers currently registered in the bad block inode. By actively tracking and bypassing these addresses, Linux ensures that the ext4 filesystem remains stable and protects incoming data from being written to physically unstable media.