Badblocks: Difference between revisions

From Leo's Notes
This page was last edited on 2 October 2022, at 20:06.
m recategorize
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 12: Line 12:
Use the {{code|-n}} flag for a non-destructive test. A non-destructive test will store the sector value, test the sector, then restore the original sector value.
Use the {{code|-n}} flag for a non-destructive test. A non-destructive test will store the sector value, test the sector, then restore the original sector value.


There are 3 numbers that will be returned as 1/2/3 in this order:
Use the <code>-b</code> option to specify the block sizes. This defaults to 1024 bytes. If you intend to use this on EXT filesystem creation, ensure this matches the filesystem block size by checking with <code>dumpe2fs /dev/sdX | grep 'Block size'</code>.
 
=== Understanding the output ===
{{Highlight
| code = Checking for bad blocks in read-write mode
From block 0 to 15630677
Testing with pattern 0xaa:  20.32% done, 1:55 elapsed. (0/0/0 errors)
| lang = text
}}
There are 3 numbers that will be returned as <code>(1/2/3 errors)</code> in the last line. Each number represents:
# number of read errors
# number of read errors
# number of write errors
# number of write errors
# number of corruption errors
# number of corruption errors


The badblocks.txt file that gets created can then be used by {{code|mkfs}} so that the filesystem can skip over known bad sectors like so:
=== Creating a filesystem around bad blocks ===
{{highlight|lang=terminal|code=
Most filesystems can take a file containing a list of all bad blocks and create a filesystem that skips over these known bad locations.  
# mkfs.ext4 -l /tmp/badblocks.txt /dev/sda
}}


=== Creating a filesystem around bad blocks ===
For ext4, you can pass in the list of bad blocks with <code>-l</code>. You must however, make sure that the block size you used with <code>badblocks</code> matches what <code>mkfs</code> uses. As a result, unless you're sure what you're doing, you should just use the <code>-c</code> or <code>-cc</code> options with <code>mkfs.ext4</code>.{{highlight
Most filesystems can take a file containing a list of all bad blocks and work around it. However, unless you know what you're doing, it's suggested that you use the {{code|-c}} option in {{code|mkfs.whatever}} instead because different filesystems may use different block sizes. The benefit of using badblocks is the fact testing is done with multiple passes rather than the one built into mkfs.
| lang = terminal
| code = # mkfs.ext4 -l /tmp/badblocks.txt /dev/sda


Eg:
## Alternatively, just have mkfs.ext4 check for badblocks for you. c=read only test, cc=read/write test
{{highlight|lang=terminal|code=
# mkfs.ext4 -cc /dev/sda
}}Other filesystems that take a list of bad blocks using <code>-l</code> include EXT2, EXT3, FAT. Here's another full example:{{highlight|lang=terminal|code=
# fdisk /dev/sdb
# fdisk /dev/sdb
## Make your desired partitions
## Make your desired partitions
Line 51: Line 60:
* https://wiki.archlinux.org/index.php/Badblocks
* https://wiki.archlinux.org/index.php/Badblocks


 
{{Navbox Linux}}[[Category:Linux]]
[[Category:Linux]]{{Navbox Linux}}
[[Category:LinuxUtilities]]

Latest revision as of 20:06, 2 October 2022

badblocks is a utility that checks for bad blocks on a storage device.

This is useful for testing new or questionable media including fake USB drives that show as a larger drive than it can actually store. It's also useful for testing new hard drives (before the warranty expires) or checking if a older drive is reliable. Be forewarned - larger disks takes a very long time to test.

Basic Usage

The destructive test can be done by running:

## Warning! This will wipe your data!
# badblocks -wsv -o /root/badblocks.txt /dev/sda

Use the -n flag for a non-destructive test. A non-destructive test will store the sector value, test the sector, then restore the original sector value.

Use the -b option to specify the block sizes. This defaults to 1024 bytes. If you intend to use this on EXT filesystem creation, ensure this matches the filesystem block size by checking with dumpe2fs /dev/sdX | grep 'Block size'.

Understanding the output

Checking for bad blocks in read-write mode
From block 0 to 15630677
Testing with pattern 0xaa:  20.32% done, 1:55 elapsed. (0/0/0 errors)

There are 3 numbers that will be returned as (1/2/3 errors) in the last line. Each number represents:

  1. number of read errors
  2. number of write errors
  3. number of corruption errors

Creating a filesystem around bad blocks

Most filesystems can take a file containing a list of all bad blocks and create a filesystem that skips over these known bad locations.

For ext4, you can pass in the list of bad blocks with -l. You must however, make sure that the block size you used with badblocks matches what mkfs uses. As a result, unless you're sure what you're doing, you should just use the -c or -cc options with mkfs.ext4.

# mkfs.ext4 -l /tmp/badblocks.txt /dev/sda

## Alternatively, just have mkfs.ext4 check for badblocks for you. c=read only test, cc=read/write test
# mkfs.ext4 -cc /dev/sda

Other filesystems that take a list of bad blocks using -l include EXT2, EXT3, FAT. Here's another full example:

# fdisk /dev/sdb
## Make your desired partitions

# badblocks -wsv -o /tmp/badblocks.txt /dev/sdb1
# mkfs.vfat -l /tmp/badblocks /dev/sdb1

In some cases, the bad blocks may be positioned such that the filesystem cannot be created. Depending on where these bad blocks are located, this can be worked around by resizing down the partition or moving the starting location of the partition.

For instance, if every block past 40000 are bad, you will need to create a partition that is (40,000/1024) megabytes or smaller in order to avoid these bad blocks. We divide by 1024 because that's the default block size used by badblocks, unless otherwise specified by the -b option.

Other Information

Test Patterns

The test that is done by default is to write to the disk in 4 passes, each with the following pattern:

  1. 0xAA - 10101010
  2. 0x55 - 01010101
  3. 0xff - 11111111
  4. 0x00 - 00000000

You may override this using the -t flag.

See Also