Check a Drive For Bad Sectors with Badblocks

At home I have a pfSense firewall, the thing about my firewall is that I did a full installation on a Compact Flash card. Because, I want the benefits of a full installation like not loosing RRD graphs in case someone accidentally pulls the plug. The downside of using Compact Flash cards or Flash memory is the finite number of writes before wearing down and resulting in damaged sectors (unusable ones).

The advantages of Flash memory are: no moving parts, creates little heat, low power, and no noise. This is why Flash memory is ideal for my needs, and I know of other full installs on Compact Flash cards that have worked for up to three years and even longer.

You can take some precautions to reduce wear on a Compact Flash like:

  • Use the embedded version of pfSense
  • With enough RAM the system may not have to use the swap partition at all.
  • Remove the swap partition.

And it doens’t hurt to know that SLC Flash will last longer than MLC Flash.

Flash Cell Endurance: For Multi-Level Cell (MLC) Flash, up to 10,000 write cycles per physical sector. For Single-Level Cell (SLC) Flash, up to 100,000 write cycles per physical sector.

Source: Kingston Flash Memory Guide

Because, I have 512MB of RAM in my Firewall which is more than enough, I did not remove the swap partition.

Still, I worry about the finite number of writes on Flash memory, which prompted me to use badblocks. Badblocks is a tool capable of scanning a drive and report back on any damaged sectors it encounters. Modern drives can handle damaged sectors by seamlessly ignoring damaged sectors and only working with the remaining good sectors. This may sound great at first, but the truth is that the card is in the process of deteriorating(more to come) and the drive may need to be replaced.

Tools of the trade

For this tutorial I will be using Parted Magic a Live distribution which includes badblocks. However, you will find badblocks on any Linux distributions. Also, make sure the target drive is unmounted before running badblocks.

First you need the name of the drive you wish to scan, for this we can use lshw or Hardware Lister. The command is lshw -C disk.

root@PartedMagic:~# lshw -C disk
       description: ATA Disk
       product: ELITE PRO CF CAR
       physical id: 0.0.0
       bus info: scsi@2:0.0.0
       logical name: /dev/sda
       version: Ver3
       serial: 5B021A040
       size: 3639MiB (3816MB)
       capabilities: partitioned partitioned:dos
       configuration: ansiversion=5 signature=8f800000
       description: DVD reader
       physical id: 0.1.0
       bus info: scsi@3:0.1.0
       logical name: /dev/scd0
       logical name: /dev/sr0
       capabilities: audio dvd
       configuration: status=nodisc
       description: SCSI Disk
       physical id: 0.0.0
       bus info: scsi@4:0.0.0
       logical name: /dev/sdb
       size: 492MiB (515MB)
       capabilities: partitioned partitioned:dos
       configuration: signature=795b8ead

Fortunately, the Compact Flash card I am using provides more than enough information about its location /dev/sda. With that information in mind we can continue to the second step of this tutorial.

Let’s search for bad blocks

The command is very simple to understand badblocks -options /target_drive. The graphic below should help explain the meaning of the parameters.


From the output below you can see that badblocks checks every block from 0 all the way to 3727079.

root@PartedMagic:~# badblocks -nvs /dev/sda
Checking for bad blocks in non-destructive read-write mode
From block 0 to 3727079
Checking for bad blocks (non-destructive read-write test)
Testing with random pattern: done
Pass completed, 0 bad blocks found.

After three months of use my 4GB Kingston Compact Flash card is performing like expected with zero bad blocks.

If you plan on doing a full installation on a Compact Flash then you should use badblocks to monitor the health of the Compact Flash over time. Remember to unmount the drive you wish to scan with badblocks.


I also made a video of the process.


Parted Magic Home Page