badblocks is a program to test for bad blocks (commonly known as sectors) on a storage device. A sector is a subdivision of a track on storage device and sectors that have become bad cannot be used because they have become permanently damaged.
Storage Device Fidelity
Although there is no firm rule has been set, it is common thinking that a new drive should have zero bad sectors. Over time, bad sectors will develop on a storage device and although they are able to be defined to the file system so that they are avoided, continual use of the drive will usually result in additional bad sectors forming and are usually the harbinger of death of a hard drive. Replacement of the device is recommended.
Comparisons with Other Programs
Typical recommended practice for testing a storage device for bad sectors is to use the manufacturer's testing program. Most manufacturers have programs that do this. The main reasoning for this is that manufacturers usually have their standards built into the test programs that will tell you if the drive needs to be replaced or not. The caveat here being is that some manufacturers testing programs do not print full test results and allow a certain number of bad sectors saying only if they pass or not. Manufacturer programs, however, are generally quicker than badblocks sometimes a fair amount so.
Testing for Bad Sectors
To test for bad sectors the program badblocks is used in Linux. badblocks had several different modes to be able to detect bad sectors.
This test is primarily for testing new drives and is a read-write(only) test - it will destroy any data on the device. It is an extensive four-pass test that on some devices will take a couple days to complete.
badblocks -wsv /dev/<device>
-w option tells badblocks to do a write test; the
-s option tells badblocks to show its progress; and the
-v option tells badblocks to be verbose and output the bad sectors it has detected.
read-write Test (non-destructive)
This test is designed for devices with data already on them. A non-destructive test writes a random pattern to a sector, reads the sector to verify the write, and then writes what was on the original sector. This is a single pass test and is used for a general maintenance tests.
badblocks -nsv /dev/<device>
-n option signifies a non-destructive test.
Have File System Incorporate Bad Sectors
The easiest way would be to have the filesystem check utility (
fsck) use badblocks during its check. To do a read-write (non-destructive) test and have the bad sectors made known to the filesystem run:
fsck -vcc /dev/<device-PARTITION>
-cc option tells run
fsck in non-destructive test mode and
fsck to show its output.
To do a read-only test (not recommended):
fsck -vc /dev/<device-PARTITION>
Alternately, you can do this manually by first finding the file systems block size. For example for ext# filesystems:
dumpe2fs /dev/<device-PARTITION> | grep 'Block size'
feed this to badblocks and save its output:
badblocks -b <block size> -ns -o badblocks.txt /dev/<device-PARTITION>
Then re-create the file system with the information:
mkfs.<your-past-filesystem> -l badblocks.txt /dev/<device-PARTITION>