- How to fix the hard-drive Bad Sector
- How to Check Hard Drive for Bad Sectors or Blocks in Linux
- Step 1) Use fdisk command to identify hard drive info
- Step 2) Scan hard drive for Bad Sectors or Bad Blocks
- Step 3) Inform OS not to use bad blocks for storing data
- 8 thoughts on “How to Check Hard Drive for Bad Sectors or Blocks in Linux”
How to fix the hard-drive Bad Sector
A bad sector on a drive is a sign of permanent damage to the drive. Unless you have reason to believe that your drive marked these sectors as bad incorrectly, you cannot «fix» them.
It means that a part of your drive is damaged to the extent that it can no longer reliably be read from and/or written to.
Your system can continue to use the drive by marking that sector as unusable, but if you have enough bad sectors, or a SMART tool triggers a warning level, you might consider a drive replacement, as bad sectors can be a sign that more sectors, or the whole drive, might fail soon.
While there may be ways to force the drive to un-mark a sector as bad, allowing you to use it again, this is likely not a good idea. The sector may stay good, but it will just as likely become bad again. Some data may be lost or corrupted depending on how it fails.
Now, as for the error message you’ve pasted in your question (as of my writing this), that error has nothing to do with bad sectors. It means that you don’t have access to the drive. Being sudo can give you access, so:
However, this is still probably not what you want, because /dev/sdb refers to the entire drive, whereas fsck is designed to work on filesystems, which are usually (but not always, and you may have an exception here) placed in partitions. If the above didn’t work, you may instead have wanted to do this to the 1st partition on that drive:
You can get a list of partitions per drive with:
Its probably just a bad block. It can’t read the data, is all. After remapping the block the drive is likely good for another three years. So goes my experience.
A HDD with a bad sector is much, much more likely to fail at any moment than one which has never had a bad sector. How many drives is your experience based on? I would still call it a bigger gamble.
More likely? Sure. But it isn’t immediately doomed. So much depends on why that block went bad. Like I said, I’ve had drives that slowly remap a few bad blocks over their lifetime and just keep working.
it isn’t immediately doomed — you have no way of knowing or guaranteeing that. In some cases it might be. So much depends on why that block went bad — it would be impossible to reliably find out the why, without opening it up and effectively destroying it. As I said it’s always an increased risk. If you are absolutely fine with such a risk I have no problem with that. Most of the time, maybe even >90% of the time, the drive won’t quickly fail after a bad sector is found and in some cases it might even last for years after. Keep good backups and any future failure won’t hurt as much.
In short: Boot a rescue system and use badblocks:
to have the hard disk controller replace bad blocks by spare blocks.
I have answered the same question in detail here.
You probably want to use this indirectly with fsck’s -c option. AFAIK using badblocks standalone will never change the behaviour of anything, it will only report on where the bad blocks are. Of course, the drive firmware might transparently modify its behaviour when put under the usage patterns characteristic of badblocks , but surely this would be drive dependent?
As a new user you first need to gain «reputation» before being allowed to do certain activities on this site. So it was not possible to duplicate this. Still the question deserved an answer. As bad blocks are an issue of the physical disk (check out SMART values, the filesystem is not directly affected. It might suffer corruption due to data loss. So doing a fsck is a good idea as well. As original question was about how to «repair» bad blocks this was the answer.
@amoe the documentation supposes the behaviour will change, here: «Run sudo badblocks -n on a device name to run it by itself and report badblocks right in your terminal. You can use the -w option to use a write-mode test, but don’t use the -n and -w options together since they’re mutually exclusive. You should under no circumstance ever use the -w option on a volume that has data, since it will erase everything clean. Use the slower -n option since it will preserve your data in this case. The -w option is fine for volumes that you don’t mind erasing.»
In the olden days you used to have to take a note of the badblocks that were written on the drive and enter those in the defect list before formatting the drive, because hard disk surfaces were never «perfect», manufacturer’s got wise to this because people buying disks would look at the defect table printed on the disk and buy the ones with the least amount of defects.
Now scroll forward 20 or so years and hard disk manufacturers hide the fact a brand new disk has bad blocks with the firmware, when you buy a brand new disk it will have in all probability bad blocks already, the firmware will detect newly grown badblocks and maps them out from a set of spare cylinders it has, but this only happens when a write operation occurs on that sector and the ECC algorithm detects bit failure, only then will it map the block out. So getting back to the point you can force a drive to map out the badblocks by simply using DD, ie/ [edit: following example was edited to prevent accidental drive destruction: removed wildcard character, replaced with X].
WARNING: DO NOT BLINDLY USE THE dd COMMAND BELOW IF YOU DO NOT KNOW WHAT IT DOES.
dd if=/dev/zero of=/dev/sdX bs=1M
obviously alter the of=target to reflect the drive you want to zero this is the quickest way to re-map a drives defects, its also the quickest way to totally trash your Linux setup by getting the target drive wrong, so check, double check then check again, before you press the enter key. You can increase the size of the blocksize in the dd command to optimize read/writes and perhaps make things go quicker, but there are diminishing returns after a point. I find anything between 1M and 8M works best for me
You can get DD to just write one sector, the bad one. to get it remapped, so you don’t have to backup your drive, but that’s a whole different kettle of fish and Russian roulette if you don’t know exactly what you’re doing .
This command WILL destroy everything on your hard drive including any partitions. But it will force the drive to map out any bad sectors it may have.
It is perfectly ok for a disk to have bad sectors as long as they aren’t on the boot sector, if they are then the drive is useless, if you notice a few months later that your drive has developed more bad sectors then it’s time to start shopping for a replacement.
I’ve revived hundreds of drives like this and they’ve lived on for many years afterwards, a bad sector isn’t necessarily the death knell for the drive.
How to Check Hard Drive for Bad Sectors or Blocks in Linux
Badblocks is the command-line utility in Linux like operating systems which can scan or test the 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(or bad sectors) of a hard disk and save them in a text file. Later we can use it with e2fsck to instruct Operating System(OS) not to store any data on these damaged sectors or blocks.
In this article, we will learn how to check or scan hard drive for bad sectors using badblocks command.
Step 1) Use fdisk command to identify hard drive info
Run fdisk command to list all available hard disks to Linux operating system. Identify the disk which you want to scan for bad blocks.
Step 2) Scan hard drive for Bad Sectors or Bad Blocks
Once you identify the hard disk then run badblocks command. Example is shown below
$ 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.
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. Run the following e2fsck command
$ sudo e2fsck -l /tmp/bad-blocks.txt /dev/sdb or $ sudo e2fsck -c /dev/sdb
Note : Before running e2fsck command, you just make sure the drive is not mounted.
For any further help on badblocks & e2fsck command , read their respective man pages
$ man badblocks $ man e2fsck
8 thoughts on “How to Check Hard Drive for Bad Sectors or Blocks in Linux”
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 Reply
From time to time, it is a good practice to check your hard drive for errors using Windows CHKDSK or Check Disk. Reply
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 Reply
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. Reply
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. Reply
What command just to report sectors or drive health.
Using an old pata drive that has not been used in some time to run Linux
peppermint 64x.
From my experience with windows os’s and smart, one bad sector can trash
a program or even the entire drive. Reply
Most filesystems these days use 4096 byte blocks. Using the commands as directed here will make a hash of your filesystem. As advised in the current man page for badblocks
“Important note: If the output of badblocks is going to be fed to the
e2fsck or mke2fs programs, it is important that the block size is prop‐
erly specified, since the block numbers which are generated are very
dependent on the block size in use by the filesystem. For this reason,
it is strongly recommended that users not run badblocks directly, but
rather use the -c option of the e2fsck and mke2fs programs” Following the example on this page the command would be: es2fsck -c /dev/sdb Reply