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.
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:
Identify the file system type of the disk you want to repair. You can use the
lsblkcommand to list the block devices on your system and identify the file system type:sudo lsblk -fExample output:
NAME FSTYPE LABEL UUID MOUNTPOINT sda1 ext4 c4a1d8d1-c3d3-4d4f-a6b0-e0c8c6a6d8d7 / sdbIn this example, the disk
/dev/sdbdoes not have a file system, so we can't usee2fsckto repair it.If the disk has an ext2, ext3, or ext4 file system, you can use the
e2fsckcommand to repair the bad blocks:sudo e2fsck -c /dev/sdbThis command will scan the
/dev/sdbdisk 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
e2fsckcommand did not find any bad blocks on the/dev/sdbdisk.
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.



