Check Hard drive for bad sectors or bad blocks in linux

badblocks is the command or utility in linux like operating system which can scan or test our hard disk and external drive for bad sectors. Bad sectors or bad blocks is the space of the disk which can’t be used due to the permanent damage or OS is unable to access it.

Badblocks  command will detect all bad blocks(bad sectors)  on our hard disk and save them in a text file so that we can use it with e2fsck to configure  Operating System(OS) to not store our data on these damaged sectors.

Step:1 Use fdisk command to identify your hard drive info 

# sudo fdisk -l 

Step:2  Scan your hard drive for Bad Sectors or Bad Blocks 

# sudo badblocks -v /dev/sdb > /tmp/bad-blocks.txt

Just replace “/dev/sdb” with your own hard disk / partition. When we execute above command  a text file “bad-blocks” will be created under /tmp , which will contains all bad blocks.

Example :

badblocks

Step:3 Inform OS not to use bad blocks  for storing data

Once the scanning is completed , if the bad sectors are reported , then use file “bad-blocks.txt” with e2fsck command  and force OS not to use these bad blocks for storing data.

# sudo e2fsck -l /tmp/bad-blocks.txt  /dev/sdb

Note : Before running e2fsck command , you just make sure the drive is not mounted.

For any futher help on badblocks & e2fsck command , read their man pages 

# man badblocks
# man e2fsck 

6 Responses

  1. David Wood says:

    don’t you need to make sure the blocks are not allocated to a file or swap etc?

  2. Bernardo Verda says:

    Don’t modern harddrives already have all this already — as built in functionality?

    (access via smartctl)

    $ apropos smartctl
    smartctl (8) – Control and Monitor Utility for SMART Disks

  3. Виктор says:

    From time to time, it is a good practice to check your hard drive for errors using Windows CHKDSK or Check Disk.

  4. deco says:

    This has given me an idea of creating this script

    #!/bin/sh
    minsize=0
    target=”/tmp/bad-blocks.txt”
    for disc in `fdisk -l | grep ‘^/’ | awk ‘{ print $1 }’`; do
    badblocks -v $disc > “$target”
    tam=$(du -k “$target” | cut -f 1)
    if [ $tam -eq $minsize ]; then
    echo “no badblocks on $disc”
    else
    echo “badblock(s) found(s) on $disc”
    fi
    done

  5. cipher says:

    You don’t need to run badblocks first. That’s a waste of time. Just use the e2fsck executable to run it for you with the -c option…

    # sudo e2fsck -c /dev/sdb

    This lets you avoid creating an unnecessary file, especially when you’re booting to an optical drive and don’t want to write to the hard drive you’re testing.

  6. Stoat says:

    DO NOT DO THIS!

    The ONLY reason you should ever create a bad blocks table is to send commands to the drivbe to tell it to repair the sectors

    ATA and scsi drives are supposed to map out bad blocks. You can force the issue by wirting 0x00 to the sector (or use hdparm–repair-sector)

    Take note of smartctl -A /dev/drive returns – in particular the bad sector (mapped out sectors) and pending sectors (probably bad but not mapped out) values – if the bad sector normalised value (not the raw value) is below the threshold then you need to replace the drive – NOW.

    If you have large numbers of pending bad sectors then you’ll need to identify them and write zeros to them to allow the drive to fix or map them out (perfectly sectors can get marked as pending due to vibration issues, amongst other things).

    Badblocks -svn will do this (unmount the partition first!), or if the partition is trashable/part of a raidset, hdparm –repair-sector and then rescan/reformat afterwards.

    If bad sectors are not being mapped out then the drive needs to be replaced immediately. It’s probably out of spares (you’ll see this with smartctl -A) or about to fail.

Leave a Reply

Your email address will not be published. Required fields are marked *