Monday, August 24, 2009

How to fix bad sectors on hard disk

Over time, due to errors during disk operations, the hard disk will begin to generate bad sectors. To thoroughly solve this, usually the last resort we have is  the disk utility from hard disk maker. This kind of utilities are often referred as low level format.
There are two types of bad sectors, the one which is caused by physical damage to the surface of the hard disk, or the one caused by the logical errors from I/O system of computers. When we use format utility from Windows, no matter what type of bad sectors we are facing, windows will always mark them as bad sector and makes no distinguishes between them. However, with disk utility from hardware maker, the bad sectors due to logical errors could be corrected and the utility will also start the S.M.A.R.T function of hard disk to remap the physically damaged sectors to some other place. For this purpose, hardware makers usually reserve even thousands of sectors on newly produced hard disks so that the S.M.A.R.T system could remap bad sectors.
However, with the huge disk volume of hard disk nowadays, using disk utility to reformat the hard disk is really slow and painful. Plus, it will totally erase the data on the disk. Is there a way to avoid all these hassles? the answer is yes.
First of all, we need a Linux operation system, forget about your old bloat windows, let's try something new and slim. Above it, we need install two software packages, namely smartctl and hdparm. These two packages can be easily found in any major Linux distro.
OK, time to connect our hard disk with bad sectors to our shiny Linux box. Now we are ready to fix the bad sectors on the hard disk. I will use an example to illustrate the process.
The Linux box I used is Ubuntu 904, the hard disk with bad sectors is connected to /dev/sdb, which means the 2nd hard drive.

run:
sudo smartctl -t short /dev/sdb

This will do a quick test on 2nd hard drive and report the location of the first error it encounters.

It says disk error at LBA 151.

run:

sudo hdparm --read-sector 151 /dev/sdb

to verify it, indeed, it says I/O error. Read the sector after 151, we found sector 152 is also a bad sector. So we need to fix sector 151 and 152.

run:

sudo hdparm --repair-sector 151 /dev/sdb --yes-i-know-what-i-am-doing

repeat for sector 152 then
rerun
sudo smartctl -t short /dev/sdb
it says completed without errors.

The above procedure is actually a localized low level format, with this we can save the trouble and pain to do the format with the disk utility from hardware vendors.