How to Use the Command 'badblocks' (with Examples)
The badblocks
command is a utility in Unix-like operating systems used to search disks for bad blocks. A bad block (or sector) is a space on a disk storage device that is unusable due to a physical or logical damage. Identifying bad blocks can help prevent data loss by alerting users to failing hardware. The badblocks
command can execute both non-destructive checks, which do not affect data, and destructive tests, which may involve erasing data.
Search a Disk for Bad Blocks by Using a Non-Destructive Read-Only Test
Code:
sudo badblocks /dev/sdX
Motivation:
This non-destructive read-only test is often the first step for users who suspect their disk might be failing. When you want to check the integrity of a disk without risking any data loss, this command is your go-to option. It allows you to safely verify the health of your disk without making any changes.
Explanation:
sudo
: This command needs superuser privileges to access the device hardware, hence the use ofsudo
.badblocks
: The primary command used to search for bad blocks on disk storage./dev/sdX
: Replace/dev/sdX
with your specific device identifier. This specifies the device you are testing.
Example Output:
Checking for bad blocks in read-only mode
From block 0 to 976773167
Testing with random pattern: done
Testing with random pattern: Pass completed, 0 bad blocks found.
Search an Unmounted Disk for Bad Blocks with a Non-Destructive Read-Write Test
Code:
sudo badblocks -n /dev/sdX
Motivation:
This non-destructive test goes a step further than read-only tests by writing data to verify the disk blocks but without overwriting existing data. It is useful when you suspect issues that might not be picked up with a simple read or if you need more thorough testing without data loss.
Explanation:
-n
: Non-destructive read-write mode, which tests each block by writing a pattern, and then reads it back to ensure data integrity.
Example Output:
Checking for bad blocks in non-destructive read-write mode
From block 0 to 976773167
Testing with random pattern: done
Testing with random pattern: Pass completed, 0 bad blocks found.
Search an Unmounted Disk for Bad Blocks with a Destructive Write Test
Code:
sudo badblocks -w /dev/sdX
Motivation:
This option is sometimes necessary when disks are nearing end-of-life or when robustness of testing takes priority over data preservation. By writing patterns to every block, you can thoroughly test the disk’s physical media. It should be used when data on the disk is not needed or has been backed up.
Explanation:
-w
: Destructive write mode, which writes patterns to every block, erasing existing data but thoroughly testing disk sectors.
Example Output:
Checking for bad blocks in destructive read-write mode
From block 0 to 976773167
Testing with random pattern: Pass completed, 4 bad blocks found.
Use the Destructive Write Test and Show Verbose Progress
Code:
sudo badblocks -svw /dev/sdX
Motivation:
Adding verbosity with -sv
gives detailed progress updates, making it ideal for monitoring extensive testing processes. This is important for administrators who need real-time feedback on long-running operations, especially in servers or large storage arrays.
Explanation:
-s
: Displays progress during the test, providing a percentage completion update.-v
: Verbose mode, allows detailed logging of operations performed by the command, useful for diagnostic purposes.
Combined with the -w
flag, this mode is both destructive and detailed.
Example Output:
Checking for bad blocks in destructive read-write mode
Testing with pattern 0xaa: done 20.00% (07/35/16/29) done
Testing with pattern 0x55: done 50.00% (17/12/34/56) done
Testing with pattern 0xff: done 75.00% (24/67/89/12) done
Testing with pattern 0x00: done 100.00% (16/29/68/41) done
0 bad blocks found.
In Destructive Mode, Output Found Blocks to a File
Code:
sudo badblocks -o path/to/file -w /dev/sdX
Motivation:
Recording bad blocks to an output file can be critical for later analysis or notification purposes. This usage is beneficial in large environments where logs need to be kept for audits or for insights into ongoing hardware health trends.
Explanation:
-o path/to/file
: Directsbadblocks
to output a list of bad blocks found to the specified file.-w
: Destructive write test as explained previously.
Example Output:
If bad blocks are found, entries will be logged to the specified file:
Output file:
100000
100010
100020
Use the Destructive Mode with Improved Speed Using 4K Block Size and 64K Block Count
Code:
sudo badblocks -w -b 4096 -c 65536 /dev/sdX
Motivation:
Speed can be a critical factor when dealing with very large storage volumes. By increasing both block size (-b
) and block count (-c
), this command optimizes how data is read and written, thereby accelerating the testing process.
Explanation:
-b 4096
: Sets the block size to 4096 bytes (4K), which matches common sector sizes for modern drives and can speed up the process.-c 65536
: Sets the block count to 65536, thus testing more blocks in each single I/O operation, further enhancing speed.-w
: Destructive write test mode.
Example Output:
Checking for bad blocks (0-65536) ... done
0 bad blocks found.
Conclusion:
The badblocks
command is a versatile tool for investigating and diagnosing disk issues. Whether you need to perform quick checks, detailed audits, or thorough destructive tests, badblocks
offers multiple options to suit different diagnostic needs. Remember, some operations can be destructive, so using the correct command for your scenario is paramount.