badblocks Command in Linux

Introduction

In this tutorial, we'll dive into the Linux badblocks command, a critical utility for system administrators to diagnose disk issues. The badblocks command is designed to scan a storage device for defective sectors, commonly known as bad blocks. Identifying and marking these bad sectors can significantly improve the reliability and longevity of your storage infrastructure. We will demonstrate how to execute a non-destructive read-only scan to identify any bad blocks, and then explore potential repair methods. Mastering the badblocks command is essential for any systemadmin responsible for maintaining healthy storage systems.

Introduction to badblocks Command

In this section, we'll provide an in-depth look at the badblocks command, a valuable Linux utility for scanning disks for bad blocks. This command helps system administrators identify and isolate failing sectors, preventing data corruption and enhancing storage device dependability. Using badblocks is a proactive approach to maintaining data integrity.

Let's begin by verifying the version of the badblocks command installed on your system:

badblocks --version

Example output:

badblocks 1.46.2 (11-Nov-2022)

The badblocks command offers a range of options for customizing the scanning process. Key options include:

  • -b: Specifies the block size in bytes (default is 1024 bytes)
  • -c: Specifies the number of blocks to check at a time (default is 64)
  • -s: Prints the status of the scan as it progresses
  • -t: Specifies the test type (non-destructive read-write, non-destructive read-only, or destructive)
  • -v: Enables verbose output

To execute a non-destructive read-only scan on a specific disk, use the following command:

sudo badblocks -v /dev/sdb

This command initiates a scan of the /dev/sdb disk, searching for bad blocks and displaying the scan's progress. The -v option provides verbose output, offering more detailed information throughout the scan.

Scanning a Disk for Bad Blocks

This section guides you through performing a comprehensive scan of a disk to identify any bad blocks. This process is crucial for maintaining data integrity and preventing data loss.

First, identify the target disk for scanning. The lsblk command lists all block devices on your system:

sudo lsblk

Example output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   100G  0 disk
└─sda1   8:1    0   100G  0 part /
sdb      8:16   0    20G  0 disk

In this example, /dev/sdb, a 20GB disk, is the target for scanning.

Execute a non-destructive read-only scan of the disk using the following command:

sudo badblocks -v /dev/sdb

This command scans /dev/sdb for bad blocks and displays the scan's progress. The -v option provides verbose output for detailed information.

Example output:

Checking blocks 0 to 41943039
Checking for bad blocks (read-only test)
Pass completed, 0 bad blocks found.

The output indicates that no bad blocks were found on the /dev/sdb disk.

If bad blocks are detected, use the -o option to save the list of bad blocks to a file:

sudo badblocks -v -o badblocks.txt /dev/sdb

This command saves the identified bad blocks to a file named badblocks.txt in the current directory. This file can be used later with other utilities like e2fsck.

Repairing Bad Blocks on a Disk

Having learned how to scan for bad blocks using badblocks, this section explores the process of repairing them. While true "repair" is often impossible (bad blocks indicate physical damage), we will cover methods to prevent their use and avoid data loss. Note that relying on software repair is not a long-term solution; drive replacement may be necessary.

The e2fsck command, a utility for checking and repairing ext2, ext3, and ext4 file systems, can be used to mark bad blocks as unusable. Here's how to use it:

  1. Determine the file system type of the disk. Use the lsblk command to list block devices and identify the file system type:

    sudo lsblk -f

    Example output:

    NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
    sda1   ext4         c4a1d8d1-c3d3-4d4f-a6b0-e0c8c6a6d8d7 /
    sdb

    In this example, /dev/sdb has no file system. If it did have an ext2/3/4 filesystem, we could use e2fsck. Note: Using e2fsck on a mounted filesystem is risky and may lead to data loss. Unmount the filesystem if possible, or run the command in read-only mode.

  2. If the disk has an ext2, ext3, or ext4 file system, use the e2fsck command to attempt to mark the bad blocks:

    sudo e2fsck -c /dev/sdb

    This command scans /dev/sdb for bad blocks and attempts to mark them as unusable, preventing their use for data storage. Important: This command assumes /dev/sdb contains a valid ext2/3/4 filesystem. Running it on a device without a filesystem can cause data corruption.

    Example output:

    e2fsck 1.46.2 (11-Nov-2022)
    Pass 1: Checking inodes, blocks, and sizes
    Checking for bad blocks (read-only test)
    Pass completed, 0 bad blocks found.

    The output indicates that e2fsck did not find any bad blocks on /dev/sdb. This may be because it does not contain a valid filesystem, or because the -c option performs a non-destructive read-only test and doesn't actually *add* the bad blocks to the filesystem's bad block list. To actually add the bad blocks, you need to provide the file generated by `badblocks` using the `-l` option.

If e2fsck identifies bad blocks, it attempts to mark them as unusable. However, remember that physical damage is usually progressive. After marking the bad blocks, you can use the disk, but monitoring it is crucial. If the number of bad blocks increases, consider replacing the disk as a more reliable solution.

Summary

In this tutorial, we explored the Linux badblocks command for scanning disks for bad blocks. We covered the various command options, including specifying block size, the number of blocks to check, and the test type. We demonstrated how to perform a non-destructive read-only scan to identify bad blocks and save them to a file. Finally, we discussed the role of e2fsck in managing filesystems with bad blocks, emphasizing the need for caution and the importance of replacing failing drives.

400+ Linux Commands