Do you know which blocks are bad? Is there already a filesystem?
If there's no filesystem yet, and you don't know which blocks are bad:
mkfs -c # rest of normal options, like -t ext3 /dev/sda4
If you have a filesystem and you don't know which blocks are bad:
e2fsck -c /dev/sda4
If you know which blocks are bad, you can list them in a file and run -- however, this option is tricky since you have to refer to the blocks exactly right:
It's generally recommended to go with the -c option which scans for badblocks.
And if you mean a totally unused hdd, you could possibly just avoid the bad sectors when partitioning with fdisk (and use LVM to preserve flexibility about the size of your various filesystems).
If you mean flag bad blocks as such, and not actually fix them, check out the badblocks utility. It supports both destructive and nondestructive tests. It's available in most recent distos.
If you mean repair a bad block, then I second SpinRite.
If you're seeing any bad blocks on a hard drive this is normally not a good sign. When a hard drive runs into a bad block it remaps that block to one of it's spare blocks that it has just for this reason. So the number of bad blocks exposed at the level that your OS accesses the drive should always be 0. Any number higher than this means that the drive has remapped all it's spare blocks and it can no longer handle bad blocks gracefully.
Linux's tool badblock for identifying and working around these problems were created and used in the time when hard drives were expensive. Similar to the kernels ability to pre-allocate bad memory addresses in ram so that applications don't use them. But these tools have really been made obsolete due to the low cost of hard drives and ram.
Spinrite run on level 5 will double check all blocks on a hard drive. It will unmap any blocks previously marked as bad that were not really bad. Freeing up spare blocks for blocks that are truly bad.
So I would either use Spinrite to get the drive back to a healthy state with spare blocks or replace the drive. Remember that Spinrite is not going to repair all drives and if your drive just happens to have a lot of truly bad blocks there's nothing that Spinrite can do. But Spinrite is well worth the small cost.
Do you know which blocks are bad? Is there already a filesystem?
If there's no filesystem yet, and you don't know which blocks are bad:
If you have a filesystem and you don't know which blocks are bad:
If you know which blocks are bad, you can list them in a file and run -- however, this option is tricky since you have to refer to the blocks exactly right:
It's generally recommended to go with the -c option which scans for badblocks.
And if you mean a totally unused hdd, you could possibly just avoid the bad sectors when partitioning with fdisk (and use LVM to preserve flexibility about the size of your various filesystems).
SpinRite, but you will need a Windows machine to make the boot disk. It is OS agnostic and Steve will give you your money back if it doesn't work.
http://www.grc.com/cs/prepurch.htm
You could replace your drive under the manufacturer's warranty.
If you mean flag bad blocks as such, and not actually fix them, check out the badblocks utility. It supports both destructive and nondestructive tests. It's available in most recent distos.
If you mean repair a bad block, then I second SpinRite.
If you're seeing any bad blocks on a hard drive this is normally not a good sign. When a hard drive runs into a bad block it remaps that block to one of it's spare blocks that it has just for this reason. So the number of bad blocks exposed at the level that your OS accesses the drive should always be 0. Any number higher than this means that the drive has remapped all it's spare blocks and it can no longer handle bad blocks gracefully.
Linux's tool badblock for identifying and working around these problems were created and used in the time when hard drives were expensive. Similar to the kernels ability to pre-allocate bad memory addresses in ram so that applications don't use them. But these tools have really been made obsolete due to the low cost of hard drives and ram.
Spinrite run on level 5 will double check all blocks on a hard drive. It will unmap any blocks previously marked as bad that were not really bad. Freeing up spare blocks for blocks that are truly bad.
So I would either use Spinrite to get the drive back to a healthy state with spare blocks or replace the drive. Remember that Spinrite is not going to repair all drives and if your drive just happens to have a lot of truly bad blocks there's nothing that Spinrite can do. But Spinrite is well worth the small cost.