Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm working on fixing the errors in my Hard Disk, It's possible to do that manually but it might take hours.

After typing this command sudo badblocks -b 512 /dev/sda, I got hundreds of results and I want to avoid fixing them manually.

More details about my error can be found here

So is there any tool I can use to fix the errors? Or maybe you could help me write a shell script?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

Well, there are a couple of cases:

  1. This disk is part of a RAID array. Good. Just have md 'repair' the array like this: echo 'repair' > /sys/block/md0/md/sync_action. Problem solved without data loss. (I'm guessing this isn't the case for you, but you really ought to consider changing that.)
  2. You don't care about the data on the disk (or there isn't any). Just use dd to zero the whole disk.
  3. The bad blocks are part of the free space on the disk. Use e.g., cat /dev/zero > tempfile to fill the free space with zeros. Do this as root (there is space reserved for root only), probably in single-user mode (so nothing breaks from running out of space). After that runs out of space, remove the file.
  4. The bad blocks are part of the data (files) or metadata (filesystem structure) on the disk. You have lost data. fsck -fc (run with the filesystem unmounted, or worst case in readonly during early boot if its the root filesystem) will tell you which files. Replace them from backup.

Its also possible that badblocks -n, which must only be done on an unmounted filesystem, will force a remap. It shouldn't lose any data (other than what was in the bad blocks, which is already lost).

If you want to script it based on the badblocks output (which is not safe, it leaves you with silent corruption), that's fairly easy. Each line of badblocks output gives you a block number, based on your block size (512 in your example). Use the same block size for dd's bs. The block number is your seek for dd. Your count is 1 (or higher, if there are a few bad blocks in a row). of is the partition (or disk) you ran badblocks on. A good if is /dev/zero.

share|improve this answer
    
this was awesome explanation, thanks Robert –  Nikhil Mulley Dec 2 '11 at 15:57
    
I think No.3 is my case otherwise i need new HDD.. –  Aziz Al-ghannam Dec 2 '11 at 16:04
    
Ill send you feedback soon. Thanks. –  Aziz Al-ghannam Dec 2 '11 at 16:05
    
after filling the free space, how can i remove the tempfile now? –  Aziz Al-ghannam Dec 2 '11 at 17:01
    
@AzizAl-ghannam: using rm, like rm tempfile. –  derobert Dec 2 '11 at 17:02

go into single user mode and do fsck -yvf /dev/sda

It seems like, it does need an fsck after all.

share|improve this answer
    
I'd be cautious about running fsck -y personally. Also, this won't get the sectors remapped, or even noted in the filesystem as bad (you need -c for that). –  derobert Dec 2 '11 at 15:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.