How To Check Damaged Sectors on Linux Hard Drive
Today we are going to verify if the hard disks of our Linux operating systems have damaged sectors that affect their performance. For this case, we will use Fedora 25.
What is a sector of a hard drive and what is it used for?
Before delving into how to check these discs we must remember or know what a sector is and how it influences the behavior of the disc.
A sector is the surface of the disk which corresponds to the area enclosed between two radial lines of a disk track. Each sector is responsible for storing a fixed amount of information, therefore, when a sector is defective the information stored there will not be available for use.
We know that hard disks don’t work sequentially , since in the background they create groups of bits which are called sectors of a drive and when the hard disk tries to write or read the information of that sector and internally receives an error as a result It indicates that this sector is defective.
Types of failures in the sectors of the hard disk in Linux
Within the sectors of the hard disk we can find two types of errors, which are:
- Physical failure: This is due to a hardware error since part of the storage is lost, this can be caused by a hit or scratch on the hard disk.
- Logical failure: This type of failure is due to a software error and occurs when information is lost when trying to access the sector.
If we don’t periodically check the status of the sectors of the hard drive, they can continue accumulating one after the other until reaching the point of destroying the hard drive or cause hardware errors in the equipment which is something really serious that takes a lot of work administrative and, sometimes, economic.
Verify bad sectors in Fedora using the badblocks tool
This tool is quite practical because it allows us as administrators or support personnel to scan a hard disk to verify blocks or bad sectors and in this way take preventive or corrective measures on it.
First of all, we must execute the fdisk command as superusers to access all the information of the hard disks and their respective partitions, for this we enter the following command:
sudo fdisk -l
Scanning the Drive on Linux
Once we have this information we proceed to execute the individual scan to check the status of the sectors on the hard disk, for this we enter the following command:
sudo badblocks -v /dev/sda2> badsectors.txt
The parameters that we have used are the following:
-v: It allows us to see details of the operation.
badsectors.txt: It will enable us to store the result in a text file for further analysis.
In case of presenting any defective sector in the hard disk we must perform the following process to avoid that the hard disk continues to overwrite information in that sector.
We must use the e2fsck command for the file systems ext2/ext3 and ext4 or the fsck command followed by the name of the txt file in this way:
For ext2, ext3 or ext4 systems
sudo e2fsck -l badsectors.txt /dev/sda2
For other file systems
sudo fsck -l badsectors.txt /dev/sda2
The -l parameter indicates that the defective block numbers are displayed.
In this way, thanks to badblocks we can easily verify which sectors are defective and take preventive measures on them.
Verify bad sectors in Linux using the Smartmontools tool
This tool is developed with a focus on modern disks, ATA, SATA, SAS, etc., which are integrated in SMART systems which allows us to Sysadmins detect, report and repair sectors with some type of error.
In Fedora, CentOS and RedHa systems we can use the following command to install the tool:
sudo yum install smartmontools
In Debian and Ubuntu systems we can use the following command:
sudo apt-get install smartmontools
Once the tool is installed, we can use the following command to access the tool’s help :
To start the scanning process we will use the following command. We have added the -H or -health parameter so that the tool displays the final result of the analysis.
We can also use the -a or -al l parameters to see all the SMART information on a disk. As we observe the result is PASSED which indicates that the disk is correct.
sudo smartctl -H /dev/sda2