External Hard Drive bad sectors recover/mark utility in linux

36,169

Solution 1

If the OS itself is showing bad sectors, then your drive is going downhill. The hardware should be removing bad sectors from service transparently, and it may have run out of spare sectors to remap to. If this is the case, your data is very much at risk.

I'd take David's suggestion to heart and migrate your data ASAP. You may want to install "smartmontools" and run smartctl to see what the device itself reports.

You can try using the "badblocks" utility on the raw device which houses your NTFS drive, since it works on the device and is not filesystem dependent. Make sure you read the man pages CAREFULLY and use the non-destructive mode. It should be able to read each sector, then re-write each sector. Doing this should force the drive itself to remap any problematic sectors. Test this procedure on a USB stick or some other media first.

I recommend you tell badblocks to process a large number of sectors at a time, since a 1TB drive will take a very long time to scan.

Again... make sure you know what you are doing. One slip and your data is toast.

If the data is valuable to you and you're not confident in your abilities, you may wish to use a commercial solution such as SpinRite (though, it's pricey and will require a Windows machinie). Note, I've never used SpinRite, but I've heard good things about it online.

Solution 2

It depends on the filesystem you are using. If you are using ext2, etx3 or ext4 (IIRC Ubuntu 9.10 defaults to ext4 for new installs, earlier versions default to ext3) then you can run

e2fsck -c -c -k -C 0 /dev/device

i.e.

e2fsck -c -c -k -C 0 /dev/sdc1

for the filesystem on the first partition of drive sdc. The filesystem should not be mounted while this takes place, so if it is needed for your normal running system you will need to run e2fsck while booted from a "live CD" or similar.

The -c option is what causes the surface scan to be added to what the checker does, then second -c makes it do a non-destructive write+read test, -k tells it to keep any existing list of badblocks instead of retesting them and -C 0 just makes e2fsck output more progress information as it does its work. See man e2fsck for more detail.

The above will mark the bad blocks so that are not used in future. You will need to rerun it if you ever reformat the drive or that individual partition. You should also rerun it occasionally anyway, in case there are any "almost bad" sectors that degrade further. There is no way of "recovering" bad sectors.

I recommend you consider migrating your data onto another drive instead though, unless there is nothing of any real importance on there (i.e. it wouldn't inconvenience you overly if the drive suddenly got much worse and the data became practically unreadable or corrupt). If you don't replace the drive, make sure you have good backups in place for anything you do care about (though you should have a good backup regime in place anyway).

Solution 3

fsck.ext4 -cDfty -C 0 /dev/sdxx

source: http://www.commandlinefu.com/commands/view/8474/run-a-ext4-file-system-check-and-badblocks-scan-with-progress-info

Share:
36,169

Related videos on Youtube

quack quixote
Author by

quack quixote

Updated on September 17, 2022

Comments

  • quack quixote
    quack quixote over 1 year

    I've just installed the new version of Ubuntu, Karmic Koala, in my laptop. Everything is fine, except for the fact the system identified about 300 bad sectors in my 1TB external hard drive. I'd like to recover or just mark then. Is there any free utility, that I can run on Linux, to accomplish this?

    Update:

    The partition is formatted in NTFS.

    Thanks in advance

  • themaestro
    themaestro over 14 years
    Thanks but I'm using the NTFS filesystem in my external hard drive. Does this command apply to this situation, too?
  • David Spillett
    David Spillett over 14 years
    To my knowledge there is no standard Linux command/utility to check/repair NTFS volumes, with or without a surface scan. You'll need to try from a Windows machine.
  • Graham Perrin
    Graham Perrin almost 12 years
    For link purposes, as this answer may be of equal interest to Mac users who wish to check blocks of a volume that uses NTFS: Fix bad blocks on Mac hard disk and in Ask Different, What free or open source software can I use with Mac hardware to verify integrity of every block of a disk where Core Storage is used?
  • Tamara Wijsman
    Tamara Wijsman over 11 years
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
  • JoshP
    JoshP over 11 years
    It would probably be useful to give a little context to the code block you've posted. Those that know will I'm sure, have no trouble understanding. But if your answer is to be useful for others as well, you should describe here what the commands actually do.
  • Taryn
    Taryn over 5 years
    Using badblocks with -n raises the possibility of further corrupting the drive, particularly if there are many bad blocks, causing the firmware to exhaust the 'spare' blocks.