7

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?

1
  • If you already have a list of bad blocks, you could feed it to hdparm Sep 21, 2021 at 11:38

4 Answers 4

11

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 (rm tempfile).
  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.

8
  • I think No.3 is my case otherwise i need new HDD.. Dec 2, 2011 at 16:04
  • after filling the free space, how can i remove the tempfile now? Dec 2, 2011 at 17:01
  • @AzizAl-ghannam: using rm, like rm tempfile.
    – derobert
    Dec 2, 2011 at 17:02
  • 0 Bad sectors, You are the best!! Dec 2, 2011 at 17:07
  • people, how can cat /dev/zero > tempfile fix bad blocks???
    – Dims
    Mar 26, 2016 at 17:45
2

e2fsck -c -c seems to do the same thing, with block size insurance.

1
  • why the negatives here? man e2fsck says that -c does the same thing
    – arivero
    Jan 23, 2020 at 14:26
0

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

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

1
  • 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, 2011 at 15:32
0

I am not sure if you can do this with a single command because I have seen some script solution on internet.

The script below finds bad sectors, puts bad blocks results into a text file, then if text file size is different than zero, e2fsck will mark bad sectors (so these marked sectors will not be used by operating system).

#!/bin/sh
target=/tmp/bad-blocks.txt
for disc in `fdisk -l | grep '^/' | awk '{ print $1 }'`; do
   badblocks -v $disc > $target
   if [ -s $target ]; then
       echo "badblock(s) found on $disc"
       e2fsck -l $target $disc
   else
       echo "no badblocks on $disc"
   fi
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .