linux poison RSS
linux poison Email

Howto check disk drive for errors and badblocks

badblocks is a Linux utility to check for bad sectors on a disk drive (A bad sector is a sector on a computer's disk drive or flash memory that cannot be used due to permanent damage or an OS inability to successfully access it.). It creates a list of these sectors that can be used with other programs, like mkfs, so that they are not used in the future and thus do not cause corruption of data. It is part of the e2fsprogs project.

It can be a good idea to periodically check for bad blocks. This is done with the badblocks command. It outputs a list of the numbers of all bad blocks it can find. This list can be fed to fsck to be recorded in the filesystem data structures so that the operating system won’t try to use the bad blocks for storing data. The following example will show how this could be done.

From the terminal, type following command:
$ sudo badblocks -v /dev/hda1 > bad-blocks
The above command will generate the file bad-blocks in the current directory from where you are running this command.

Now, you can pass this file to the fsck command to record these bad blocks
$ sudo fsck -t ext3 -l bad-blocks /dev/hda1
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Check reference counts.
Pass 5: Checking group summary information.

/dev/hda1: ***** FILE SYSTEM WAS MODIFIED *****

/dev/hda1: 11/360 files, 63/1440 blocks
If badblocks reports a block that was already used, e2fsck will try to move the block to another place. If the block was really bad, not just marginal, the contents of the file may be corrupted.

Looks at badblocks man pages for more command line options.


11 comments:

Anonymous said...

can I run this command without unmounting the partition? Cause I have problems unmounting /var. Lots of services running that is using this partition.

Anonymous said...

Moreover, the badblocks man page states that it must know the correct block size, otherwise it will go bad; and it is recommended to rather use fsck -c [mount point] then the fsck will pass correct block size to badblocks.

Eddy said...

abubin, i suggest you load a livecd to run badblocks on '/var'

Puppo Michaelson said...

Do you mean e2fsck -l bad-blocks-list?

fsck -l command is used for locking threads, not for feeding lists and I don't think if it will understand this as a command to pass over e2fsck.

Thanks for the tip anyway. Combining e2fs utilities and badblocks is very useful for me now that my 640Gb portable disk is dying on me ;)

DevOps said...

its for ext3 filesystem

-l filename
Add the block numbers listed in the file specified by filename to the list of bad blocks. The
format of this file is the same as the one generated by the badblocks(8) program. Note that
the block numbers are based on the blocksize of the filesystem. Hence, badblocks(8) must be
given the blocksize of the filesystem in order to obtain correct results. As a result, it is
much simpler and safer to use the -c option to e2fsck, since it will assure that the correct
parameters are passed to the badblocks program.

Javin @ grep command in unix said...

pretty interesting tips. thanks for sharing information.

Anonymous said...

For e2fsck (yes you can use e2fsck on ext3)
-c
This option causes e2fsck to use badblocks(8) program to do a read-only scan of the device in order to find any bad blocks. If any bad blocks are found, they are added to the bad block inode to prevent them from being allocated to a file or directory. If this option is specified twice, then the bad block scan will be done using a non-destructive read-write test.

Anonymous said...

Why does everything in Linux have to be so complicated?!

Anonymous said...

Okay, here's the much simpler Windows-style version.

Your drive is bad. Buy a new one.

That's the option you'd have on Windows. Actually, it's the option you should take, really. But if you have no choice but to use the drive for now, and want to make your situation slightly safer, you can do this.

Or you can buy a new drive.

Or you can whine.

Preferably somewhere else.

Anonymous said...

@Anonymous

So in this article in recommends:

$ sudo badblocks -v /dev/hda1 > bad-blocks
$ sudo fsck -t ext3 -l bad-blocks /dev/hda1

But you are recommending:

$ sudo badblocks -v /dev/hda1 > bad-blocks
$ sudo fsck -t ext3 -c bad-blocks /dev/hda1

?

thanks.

Anonymous said...

No, he's recommending:

$ sudo fsck -t ext3 -c /dev/hda1

The '-c' flag doesn't take a file name argument, from the man page:

"This option causes e2fsck to use badblocks(8) program to do a read-only scan of the device in order to find any bad blocks.
If any bad blocks are found, they are added to the bad block inode to prevent them from being allocated to a file or directory. If this option is specified twice, then the bad block scan will be done using a non-destructive read-write test."

Post a Comment

Related Posts with Thumbnails