How to repair a hard drive

The hard disk is the main information storage medium.

With a previous article we talked about how to fix BIOS related issues. In this we will focus on another component that, although it is not essential, is necessary to get the most out of our computer. Now we see how to repair a hard drive using free software and open source utilities.

The function of the hard disk is to allow the storage and retrieval of large amounts of information. In most cases, that storage is permanent (at least until we voluntarily decide to delete it). Additionally, it collaborates with the RAM when it must store more information than it can store, acting as a temporary storage place.

How to repair a hard drive

To know how to solve problems, we must first understand their nature and, for this, we need to know the composition and operation of a hard disk drive.

Inside the hermetic box we find two well differentiated components:

  • A set of electronic and mechanical components Responsible for procedures for storing and retrieving data.
  • A stack of discs known as platters. Platters store information in magnetic form on both the top and bottom sides in small elements that can be magnetized or demagnetized representing 1 or 0 in one bit of information.

Hard drive operation

Partitions allow you to install different operating systems on the same hard drive.

Partitions are divisions established by software that allow one drive to effectively function as if it were several different drives.

There is a read and write head for each surface, that is, the number of heads will be twice the number of plates. Using a mechanical arm heads move linearly from outside to inside. At the same time, the stack of plates rotates at a constant speed while information is being written or read. When something is going to be read or written, the heads get into position and wait for the disk to rotate until the corresponding head lines up either with the location of the data sought or with the place assigned to save it.

Each of the surfaces of the faces are divided into concentric circles called tracks.. Tracks that occupy the same position on all disks in the stack are called cylinders. Tracks are subdivided into sectors that are the smallest unit of information that can be written to a disk.

To identify head, sector and cylinder we must bear in mind that heads and cylinders begin to be numbered from zero and sectors from one. That is to say that the first sector of a hard disk will be the one corresponding to head 0, cylinder 0 and sector 1.

However, Linux (And the other operating systems) they do not work with physical partitions, but instead use software-based partitions known as partitions. In practice, the partitions work as if they were separate storage units.

Within each partition, the content is organized into hierarchical structures known as directories.. While partitions are fixed in size and occupy contiguous cylinders, directories can change this and be scattered anywhere on the partition. Within the same disk unit there can be a file system for each partition which will be mandatory for the content within each one of them.

Differences between GPT and MBR

To be able to write or read a specific data, the drive must have at least one partition and a place to find information about all available partitions, where they begin and end and which of them is the one that starts the operating system when turning on the computer.

Two ways to store that information: Master Boot Record (MBR) and GUID Partition Table (GPT)

MBR is the oldest method. Consists in a special boot sector located at the beginning of a drive. In addition to information about partitions the drive contains the bootloader that, in case of having more than one operating system installed, choose which one to start with.

MBR can only work with drives up to 2TB and four primary partitions or three primary and one extended partition that can be further subdivided into logical partitions.

With GPT, each partition is assigned a "globally unique identifier". GPT does not have the limitations of drive capacity or number of partitions of MBRIn any case, the limitations that exist will be those imposed by the operating system.

Another advantage of GPT is that unlike MBR, which stores partitioning and boot data at the beginning of the drive, it saves it in multiple copies throughout the drive. In addition, it detects data integrity problems by looking at the cyclic redundancy check values. In case of finding damage, try to recover them from another location on the disk.

Common problems and how to fix them in Linux

In general we can find 4 types of problems:

  • Key data deletion: This can happen due to a user error that deletes what should not be deleted or due to bugs in the software used.
  • Virus action: Although Linux has a permissions system that makes it less vulnerable than other operating systems, no security mechanism can survive a user's incompetence. All it takes is visiting a compromised website for the malware to access any of the connected drives and tamper with the data.
  • Bad sectors on the hard drive: In this case it may be due to manufacturing faults or physical damage produced during handling.

Some ways we can detect drive problems before it's too late are:

The dd command

With this command we can measure typing speed. For this we open the terminal and write:

dd if=/dev/zero of=/tmp/test1.img bs=1G count=1 oflag=dsync

It is possible to measure latency with the command:

dd if=/dev/zero of=/tmp/test2.img bs=512 count=1000 oflag=dsync

The fsck Command

The fdisk -l command lists the partitions of all drives with their identifier.

To execute the commands we must indicate the identifier of the unit and of the partition on which we need to work.

This command launches a utility that allows you to scan the file database looking for and trying to fix errors. In addition, it generates a report of the results. In case the system shuts down unexpectedly, fsck runs automatically.

To use this command we must identify the partition we want to analyze. We do it with the command:

sudo fdisk -l

Once we identify the searched partition, we must take note of its identifier. This takes the form /dev/sdx* where x is a letter starting with a for the first drive and * a number starting with 1 for the first partition.
To do the verification, we first unmount the partition with the command
umount /dev/sdX*
and then we launch the command with:
fsck /dev/sdX*

If we want to check a complete unit, we write the same commands, but without indicating the partition number.

In order to scan the current partition, you must do so from installation media or from the bootloader's rescue mode.

The badlocks command

This command find bad sectors and save the information in a text file.

The instruction is as follows:

sudo badblocks -v /dev/sdX*> ~/sectores_dañados.txt

The e2fsck Command

This is a error detection and correction command specific to Ext file systems. The syntax is:

sudo e2fsck -cfpv /dev/sdX*

The cfpv instruction states:

  • c So the program should find the bad blocks and add them to a list.
  • f which should also be done a file system check.
  • p that you should try to repair the bad blocks.
  • v which should be displayed in the finish procedure results.

e2fsck and badlocks can be combined by having the former read the list of errors detected by the latter.

sudo e2fsck -l bad_sectors.txt /dev/sdX*

The test disk command

Within the partitions the information is stored in hierarchical directories.

The TestDisk application tries to recover the deleted files and partitions. It works with both Linux and Windows, although the results are not always optimal.

TestDisk is a deleted data recovery tool intentionally, accidentally or maliciously. The result is not always perfect and the files will not recover their original names, so we must review one by one to find what we are looking for.

Before starting to use it, we must install it using the package manager of our distribution. Once installed we launch the program with the command

testdisk

When we do this we will see three options:

  1. Create a log file
  2. Add Additional information to that collected in previous sessions.
  3. do not register information.

Next, we select the unit that we want to analyze with the cursor and then move to the instruction to proceed and press Enter. In the following screens we indicate the type of partition table and the recovery mode. Finally, we choose the partition.

To complete we go to the directory where the deleted file was, we mark it, we press C to start the recovery and then the place where it will be saved.

Many of these commands can be used with a graphical interface. For example, in GNOME we have the application Gparted found in the repositories and as a Linux distribution that can be used in Live mode. The KDE desktop also has its own partition editing tool.

On the other hand, there are some payment recovery solutions. However, neither guarantees perfect results so it is best to have multiple copies of important files both locally and in the cloud.


The content of the article adheres to our principles of editorial ethics. To report an error click here.

2 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Gregory ros said

    Thank you for the compilation, fortunately they are all going well for me, but when the time comes it is very good to have an article as practical as this one in bookmarks.

    1.    Diego German Gonzalez said

      Thank you