Scan and check USB drives
Bad sectors or bad blocks are damaged portion of your mechanical hard disk drive which can not be used at all for data storing purposes. However, the operating system still can write to those sectors unless you specifically mark them as ‘bad’ or unusable. Also if you have data stored in those areas of the hard drive, it is very difficult to recover those as well.
Although, the latest computer storage technology such as SSD, etc almost eliminates this problem. However, there is still a huge number of hard disk drives in use today which is aging and might slowly start having bad sectors.
Hence, you should periodically scan your hard drive (especially aging ones) for bad sectors if you feel your system is slowing down, or, disk IO is increasing. In Linux (Ubuntu, Fedora, and other distributions), you can easily do this via below terminal commands.
How to Manage Disk Bad Sectors in Linux
It is better to run below commands when your disk is not mounted with the operating system. Hence I would suggest, you try this using LIVE operating system boot from a USB stick. You can create a LIVE USB using this guide with any Linux operating system of your choice (recommended: Ubuntu).
However, you can still run these commands in your installed Linux distribution but you should not scan or mark the mounted “/” root filesystem.
Scan for Bad Sectors
As a first step, identify the disk partition which you want to scan for bad sectors. If you have GParted installed, it is easy to find out. Otherwise, you can run below command (lsblk – List block devices) to view your disk partitions.
sudo lsblk -o name,mountpoint,label,size,uuid
If you are running above command via LIVE USB, make sure you can identify your HDD and USB stick. Typically HDD should be defined as /dev/sda.
Then you can run badblocks command as below with the verbose (-v) switch. And save the output to a text file for further investigation. This is just a verification whether you have bad sectors in the hard drive or not.
From the big wiki...
badblocks is a Linux utility to check for bad sectors on a disk drive. It can create a text file with list of these sectors that can be used with other programs, like mkfs, so that they are not used in the future and thus do not cause corruption of data. It is part of the e2fsprogs project,<ref>E2fsprogs: Ext2/3/4 Filesystem Utilities – Release notes</ref> and a port is available for BSD operating systems.<ref>FreshPorts – sysutils/e2fsprogs</ref>
When run as a standalone program, badblocks gives a list of blocks with problems, if any. This makes it a good option for checking whether the disk drive is healthy, independent of SMART data and file system checks.<ref>Template:Man</ref>
e2fsck's "-c" option
A more common use case is the invocation of badblocks as part of e2fsck when passing the option "-c"
to scan for bad blocks and prevent data from being stored on these blocks. This is done by adding the list of found bad blocks to the bad block inode to prevent the affected sectors from being allocated to a file or directory. The test can be done using a read-only ("-c"
) or non-destructive read–write ("-cc"
) test method.<ref>Template:Man</ref>
dumpe2fs
Running dumpe2fs -b
will display the list of bad blocks recorded by e2fsck
or tune2fs
.
Examples
badblocks -nvs /dev/sdb
This would check the drive "sdb" in non-destructive read–write mode and display progress by writing out the block numbers as they are checked.
badblocks -wvs /dev/sdb6
This would check the sixth partition of the drive "sdb", in destructive read–write mode (-w
= write-mode), which writes 4 different patterns on the whole partition and verifies each by reading back. It displays progress by writing out the block numbers as they are checked (-s
= show, -v
= verbose). All data on the partition will be overwritten at the block level.
badblocks -wvsb 4096 /dev/sdb
This does the same as above, but to the entire drive, with a block size (-b
) of 4096. This destroys MBRs, partitions and data alike. Modern disk drives will probably not show any defective sectors because they silently remap bad sectors to spare tracks,<ref>Template:Cite web</ref> but running the program with a new drive for several days will test the whole surface, and when reading it afterwards S.M.A.R.T. data will eventually show reallocated sectors.
Using the -w
option on a device containing an existing file system erases the data on that device.
External links
- Smartmontools – Bad block HOWTO for smartmontools. A complex how to for badblocks; might be confusing for the casual user.
- Badblocks - ArchWIKI
sudo badblocks -v /dev/sda1 > ~/bad_sectors.txt
Repair Bad Sectors
For ext2, ext3, and ext4 file systems, you can use e2fsck utility to check and repair bad sectors. In the terminal run below command with admin privilege to check and repair.
sudo e2fsck -cfpv /dev/sda1
Make sure to replace sda1 with the proper device identifier. The parameters “c” searches for bad blocks and add it to a list, “f” does a check on the file system. The “p” parameter repairs anything if possible and “v” is the verbose mode which gives you the terminal output of the command progress.
You can also specify the bad_sectors.txt file created in the earlier steps as well to force e2fsck to repair those in the file only via the below command.
sudo e2fsck -l bad_sectors.txt /dev/sda1
For other file systems (such as FAT32), you can use fsck.
sudo fsck -l bad_sectors.txt /dev/sda1
However, the above command execution might take several hours to run depending on your disk partition size and health of your disk. So be ready before you start the command. Try not to terminate the command via CTRL+C or CTRL+Z while it is in progress.
I hope this tutorial helped you to identify issues in your hard drive and mark them as bad to prevent further data loss. Remember that even if you mark the sectors as ‘bad’, the disk is still physically damaged already. It is merely a software fix to mark those tracks as bad to tell the operating system not to access. If your hard disk started having bad sectors, in the long run, it would become worse. Hence is it recommended that you should start backing up your data and replace your hard drive with a new one or go for the latest SSD drives.
This writeup was taken from https://www.debugpoint.com/2020/07/scan-repair-bad-sector-disk-linux/