A bad sector or bad block on a Linux hard drive is a part of the disk drive that cannot be read or written because the disk is marked with fixed physical damage or flash transistor failure. As the number of bad sectors increases, it can cause adverse or destructive effects on the disk drive or flash capacity or even hardware failure. The presence of bad blocks prompts you to consider purchasing a new disk drive or simply marking the bad block as unavailable. So, what are the ways to scan a disk to determine if a disk drive or flash memory has bad sectors?
Through the badblocks program can check the bad sectors of the disk in the device, the device can be a hard disk or external disk drive, by /dev/sdc file, need to have super user permissions with the fdisk command to display information about the disk drive or flash and its partitions:
$ sudo fdisk -l
Then enter the following command to scan the Linux disk drive for bad sectors/blocks:
$ sudo badblocks -v /dev/sda10 > badsectors.txt
In the command above, badblocks is scanning the device /dev/sda10 (remember to specify your actual device) and -v enables it to display details of the operation. In addition, the results of the operation are stored in the file badsections.txt via output redirection. If you find any bad sectors on the disk, you need to uninstall the disk and follow instructions to tell the operating system not to write the reported sectors.
You need to use the e2fsck (for ext2/ext3/ext4 file systems) or fsck command along with the badsections.txt file and the device file, as shown in the following commands.
The -l option tells the command to add the block number listed in the file specified by filename (badsections.txt) to the bad block list.
------------ Specifically for ext2/ext3/ext4 file-systems ------------
$ sudo e2fsck -l badsectors.txt /dev/sda10
OR
------------ For other file-systems ------------
$ sudo fsck -l badsectors.txt /dev/sda10
Modern hard drives (HDDS and SSDS) are equipped with SMART technology to more reliably and efficiently detect and report health conditions and predict hardware failures. Run the following command to install smartmontools:
------------ ------------ on Debian/ Ubuntu-based systems
$ sudo apt-get install smartmontools
------------ ------------ on RHEL/CentOS based systems
$ sudo yum install smartmontools
After installation, use smartctl to control the SMART system integrated into the disk. You can view its man page or help page as follows:
$ man smartctl
$ smartctl -h
Now execute the smartctrl command and name your specific device as the parameter, as shown in the following command, with the flag -H or --health included to display the SMART Overall Health Self-assessment test results.
$ sudo smartctl -H /dev/sda10
For an overview of disk information, use the -a or --all options to print out all smart-X information about the disk, or --xall to display all SMART and non-smart information about the disk.