Linux badblocks Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux badblocks command, which is a utility used to scan a disk for bad blocks. The badblocks command can help identify and mark bad sectors on a disk, improving the reliability of your storage devices. We will learn how to perform a non-destructive read-only scan of a disk to detect any bad blocks, and how to repair them if necessary. The badblocks command is a powerful tool for maintaining the health of your storage systems.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/du("`File Space Estimating`") subgraph Lab Skills linux/sudo -.-> lab-422568{{"`Linux badblocks Command with Practical Examples`"}} linux/dd -.-> lab-422568{{"`Linux badblocks Command with Practical Examples`"}} linux/df -.-> lab-422568{{"`Linux badblocks Command with Practical Examples`"}} linux/du -.-> lab-422568{{"`Linux badblocks Command with Practical Examples`"}} end

Introduction to badblocks Command

In this step, we will explore the badblocks command, which is a Linux utility used to scan a disk for bad blocks. The badblocks command can be used to identify and mark bad sectors on a disk, which can help prevent data loss and improve the reliability of your storage devices.

First, let's check the version of the badblocks command installed on our system:

badblocks --version

Example output:

badblocks 1.46.2 (11-Nov-2022)

The badblocks command has several options that allow you to customize the scan process. Some of the most commonly used 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 perform a non-destructive read-only scan on a disk, you can use the following command:

sudo badblocks -v /dev/sdb

This command will scan the /dev/sdb disk for bad blocks and display the progress of the scan. The -v option enables verbose output, which provides more detailed information about the scan.

Scanning a Disk for Bad Blocks

In this step, we will learn how to perform a comprehensive scan of a disk to identify any bad blocks.

First, let's identify the disk we want to scan. You can use the lsblk command to list all the 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, the disk we want to scan is /dev/sdb, which is a 20GB disk.

To perform a non-destructive read-only scan of the disk, we can use the following command:

sudo badblocks -v /dev/sdb

This command will scan the /dev/sdb disk for bad blocks and display the progress of the scan. The -v option enables verbose output, which provides more detailed information about the scan.

Example output:

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

The output shows that the scan found no bad blocks on the /dev/sdb disk.

If the scan finds any bad blocks, you can use the -o option to save the list of bad blocks to a file:

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

This will save the list of bad blocks to a file named badblocks.txt in the current directory.

Repairing Bad Blocks on a Disk

In the previous step, we learned how to scan a disk for bad blocks using the badblocks command. In this step, we will learn how to repair the bad blocks on a disk.

To repair bad blocks, we can use the e2fsck command, which is a utility for checking and repairing ext2, ext3, and ext4 file systems. Here's how you can use it to repair bad blocks on a disk:

  1. Identify the file system type of the disk you want to repair. You can use the lsblk command to list the block devices on your system 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, the disk /dev/sdb does not have a file system, so we can't use e2fsck to repair it.

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

    sudo e2fsck -c /dev/sdb

    This command will scan the /dev/sdb disk for bad blocks and mark them as unusable, preventing them from being used for data storage.

    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 shows that the e2fsck command did not find any bad blocks on the /dev/sdb disk.

If the e2fsck command finds any bad blocks, it will mark them as unusable and prevent them from being used for data storage. You can then use the disk with the bad blocks marked, or consider replacing the disk if the number of bad blocks is too high.

Summary

In this lab, we learned about the Linux badblocks command, which is used to scan a disk for bad blocks. We started by exploring the different options available with the badblocks command, such as specifying the block size, the number of blocks to check at a time, and the type of test to perform. We then learned how to perform a non-destructive read-only scan of a disk to identify any bad blocks. Finally, we discussed how to repair bad blocks on a disk using the badblocks command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like