Wednesday, December 5, 2018

Badblocks - Conclusively Diagnosing a Harddrive with Linux

I was presented with a computer hard drive that "died". 

Since it was a SSD and useful size of 500 GB, I really did not want to throw it out.

Step 1 was to use FSCK to check the device.  Similar to CHKDSK on windows.  The drive was clean.  fsck -y /dev/sdc1

Step 2 was to clone my existing drive to it to test whether DD would detect problems.  The drive was clean. dd if=/dev/sda of=/dev/sdc conv=noerror,sync status=progress

Step 3 was to realize there had to be a better way.

Badblock. 

It came on my Debian distribution.  Oh and "free" since I am using Linux for everything here.  If you are running Windows or Mac, download a "live" disk of Linux, find instructions on how to boot from a live computer and give it a go. 

What it will do is conclusively, and exhaustively test a hard drive by writing a pattern to it, byte by byte.  Then badblock will compare to see if it actually wrote onto the hard drive.

The end of my story is that drive was perfect, and I recommended to the client a replacement of the computer.  They did not want the drive back, and since the computer was "past the warranty" it was recycled.

Badblock is not generally recommended to be used to do a read only test.  Everywhere I have seen suggests using badblock to destructively test a hard drive.


Warning: This will destroy any data on the drive if you use the "-w" flag. 


This is also a way to securely wipe the data since you can tell badblock to do a number of passes using the -p flag.  For 1 Pass include -p 1 in the entry.  Five passes is -p 5 .

Set aside a block of time to do this.  My drive was on USB 3.0 and it took 29 Hours to complete.  YMMV!  Use USB 3 if you can, or plug it directly into the computer.

Step 1 - Connect the offending drive into a USB drive container, plug it into the testing computer, and power it on.

Step 2 - As Root, run "dmesg" in Terminal.  It will tell you at the end that you plugged a drive into your computer, what the device name is such as /dev/sdc,  and the block size of the drive in bytes.

Step 3 - Run the command.  Running this with the "-w" flag will destroy any data on the drive.

Final Warning - RUNNING THIS COMMAND WILL DESTROY ANY DATA ON THE DRIVE.   Badblock will refuse to run if the drive is mounted, so you may have to turn the drive off, and back on, to make sure the drive is unmounted. 

The Command that will wipe all data and conclusively test the drive for me was:

badblock -w -p 1 -b 512 -c 1024 -s /dev/sdc

First, if you see the picture at the top, I should have used 512 and not 4096.  It simply means that Badblock was a bit less efficient.  See the line in the picture that starts "[224902.607064]".

Second the -c 1024 seems unnecessary since it is testing 1024 blocks at a time.  64 is default.

Third since I did not specify the "-e" flag, it was to run to finish or report an error and terminate.  That was intentional.  If you have a lot of errors already on the drive, you can estimate the errors and list them in a text file to be used in badblocks to skip those. 

If you wanted to get fancy.

Mine ran to end of job and finished with a simple "Reading and comparing: done" message.

I now have a new-to-me 500GB SSD.  Yay for me, right?

Oh and that weird "^[[15~" stuff was from when I dropped my keyboard about 6 plus hours into the whole thing.  It spewed garbage into the terminal session but kept running.

No comments:

Post a Comment