How to quickly format a HDD with bad sectors (in linux)?

Solution 1:

No, there is no quick way. Generally, when you have a disk with some bad sectors, you should overwrite the total contents of your disk, with a command similar to this:

dd bs=512k if=/dev/zero of=/dev/...

It'll take some time (2-3 hours normally). Doing this will give your disk a chance to handle bad sectors. A modern disk (made in the last ~15 years) handles bad sectors internally, transparently remapping those sectors from a reserved set of sectors during writes. So in the end, you should have a disk with all sectors usable. If the disk cannot do this remapping, it usually means that there are so many bad sectors that it ran out of reserved sectors. This is a clear indication that the disk has reached end of life.

Solution 2:

A bad sector (a sector is an old term, nowadays 'block' is more commonly used) on a HDD means one or more bad blocks are of out of spec on the disk magnetic surface area. The only way to locate those blocks is to try to read every single block on a hard drive. HDDs are slow so that will take a lot of time.

For example, a modern HDD has real world read throughput around 130 MB/s, so a modern 4TB disk will take around 4000000MB / 130MB/s = 8.5h to even read the whole disk once without bad blocks. A drive with one or more bad blocks will cause drive to try re-read those blocks repeatedly so the throughput will suffer a lot. That will allow you to locate the bad blocks that cause read errors. Getting any modern HDD to remap a bad block requires writing something to that bad block.

The only way to really test if a block of HDD surface works is to read that block, then write it back, and then see if the drive still says everything is okay for that block. Blocks used to be 512 bytes long but modern drives use 4KB blocks internally. A modern 4TB drive requires testing around a billion (1e9) 4KB blocks. And did I mention that HDDs are slow?

If you have bad blocks and any data, you really want fsck.ext4 -cc. And that one will take around a day or two at minimum. Doing fsck.ext4 -c will allow ext4 to avoid the bad blocks but it cannot fix the drive. The -c option of fsck.ext4 makes it to try read every block on the partition. The -cc option will make it to try read every block and the write the same contents back to the drive and check for errors. And even disks where -cc fails can be repaired sometimes... However, there is not a magic bullet to locate the bad blocks. The only way is to scan the whole drive and it will be slow with huge drives.

Since you don't need any data, doing sudo dd if=/dev/zero of=/dev/sdX bs=4M will allow skipping the "read the data" part and going directly to "fix the blocks" part. And that will still take somewhere between 8-20 hours to complete.

Note that the drive firmware will fix the bad block automatically once you overwrite the bad block. It really does not matter which OS or tool you use to overwrite a bad block on a HDD. And the data does not matter either. If the drive cannot fix any given bad block, that drive is done. This is because modern hard drives have some extra physical blocks in reserve that are used to fill bad blocks in the logical address space. If a bad block cannot be fixed, it's only because the all the blocks in the reserve space have already been used! The drive has been failing for a long time already in that case.

Note that you have to use correct blocksize to fix the block. For example, if your drive uses 4KB physical blocks and still allows 512B logical blocks, you cannot overwrite a bad block with a 512B logical write, because the drive will technically try to read the real 4KB block, alter the 512B you wrote and the re-write the 4KB block back to the magnetic plattern. Obviously the "read the real 4KB" will fail if the physical 4KB block is bad. The way to avoid that is to write the whole 4KB block in a single HDD command so nothing needs to be read from the physical disk. In the end, you have to write a correctly aligned 4KB block to fix given bad block. The dd command above will always write 4MB blocks, which will be correctly aligned if you use whole device as of target (you can use any multiple of 4 KB for writing, above example uses 4 MB to improve performance). If you use a single partition as the target and that partition is not correctly aligned, that command may still fail to fix bad blocks if logical and physical block sizes do not match for the drive. As far as I know, this is true for most modern HDDs. Executing smartctl -x /dev/sdX will give info about device sdX.

As always, do man fsck.ext4, man dd and man smartctl before messing with this stuff.

Solution 3:

Al depending on the drive make. You can download from the drive manufacturer website, tools that can to a certain extend repair the drive.

Seagate drives

Western Digital