Garry Conn

How To Repair Bad Sectors on a Hard Drive Using Linux

By Garry Conn - Mon, Oct 1 2012, 8:14am CDT

In my experience it seems that hard drives are taking a big dive in quality. I am not sure if its due to lack of craftsmanship, increased capacity or a combination of both. Regardless of what the reasons may be, fact alone is I witness as well as personally experience more and more problems with new hard drives compared to older ones.

The most common issue I face when dealing with hard drives are bad sectors. Now, bad sectors are not all that big of a deal. Hard drives are designed with a reserve tank. When main sectors on the disk become bad, reserve sectors are available to take over. This works great until all the reserve sectors are used. After that any new bad sectors can not be reallocated.

Using the badblocks command you can easily identify which sectors are bad on a hard drive. Using the fsck you can attempt to repair the bad sectors. Here is how that is done:

badblocks -v /dev/sdc1 > bad-blocks

This command checks for bad sectors on the sdc1 hard drive and if there are any found it reports which ones in a file called bad-blocks. So, before running the command from shell, you should change directory to a place where you would like to have the file created and written to. A safe place could be /home/username/Desktop. Allow an extended amout of time for the process to complete. In my experience, badblocks command usually takes around 1 minute per 2 gigs to check. So if you are checking a 30Gb hard drive, that will take about 15 minutes.

After the scan has completed, if bad sectors are found it will report this:

Pass completed, 10 bad blocks found.

In order to attempt to repair these bad sectors the fsck command is used. Here is how that is done:

fsck -t ext3 -l bad-blocks /dev/sdc1

This will attempt to repair the bad sectors. The report will look like this when completed:

fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/sdc1: Updating bad block inode.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information

/dev/sdc1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdc1: 11/835584 files (0.0% non-contiguous), 93395/3337495 blocks

Repeat the process to run another pass over the hard drive. Erase the bad-blocks file that was created during the first pass so that updated information can be written to the file during the second pass. If the second pass results in zero bad sectors, then you can sign off on your hard drive as being free of bad sectors.