Detailed explanation of badblocks instruction under CentOS

Time:2020-4-4

Hard disk is a loss of equipment, when used for a period of time may appear bad track and other physical failures. If the computer hard disk is not replaced in time or handled technically, more and more bad paths will occur, which will cause frequent crashes and data loss. The best way to deal with it is to replace the disk, but in a temporary situation, you should shield the sectors of the bad channel part in time and do not touch them. Badblocks is a tool to check the location of bad tracks.

1、 Command parameters

The format of badblocks is:

Quote

badblocks [ -svwnf ] [ -b block-size ] [ -c blocks_at_once ] [ -i

     input_file ] [ -o output_file ] [ -p num_passes ] [ -t test_pattern  ]

     device [ last-block ] [ start-block ]

Parameter meaning:

Quote

-b blocksize

Specifies the block size of the disk, in bytes. The default value is “block 4K” (4K / block)

-c blocksize

Number of checks per block, 16 by default

-f

Force a read-write or non-destructive write test operation on a mounted device

(we suggest umount device first, and then bad track detection. You can use this option only if / etc / mtab reports the device mount error in error.)

-i file

Skip bad traces that are already displayed in file files without detection (avoid duplicate detection)

-o file

Output test results to file file

-p number

Repeatedly search the device until no new bad block location is found within the specified number of passes. The default number is 0

-s

Show progress at check

-t pattern

Detects blocks by reading and writing in the specified mode. You can specify a decimal positive value from 0 to ulong Gu MAX-1, or use random.

If you specify multiple patterns, badblocks will use the first pattern to detect all the blocks, and then the next pattern to detect all the blocks.

Read only mode only accepts one mode, it cannot accept random mode.

-v

Show detailed information on execution

-w

Write to and read from each block

[device]

Specifies the disk device to check.

[last-block]

Specifies the total number of chunks for the disk device.

[start-block]

Specify which block to start the check from

Two, example

Badblocks uses a block of 4096 to check each block 16 times, and outputs the result to the “HDA badblocks list” file

# badblocks -b 4096 -c 16 /dev/hda1 -o hda-badblocks-list

HDA badblocks list “is a text file with the following contents:

Quote

# cat hda-badblocks-list

51249

51250

51251

51253

51254

……

61245

……

You can do more operations for suspicious blocks. Next, badblocks is a “block” with 4096 bytes. Each “block” is checked once, and the result is output to the “hda-badblocks-list. 1” file, starting from 51000 block and ending at 63000 block

# badblocks -b 4096 -c 1 /dev/hda1 -o hda-badblocks-list.1 63000 51000

The time spent this time is relatively short, and the hard disk will produce a “quack” sound in a short time under specified circumstances. Due to different inspection conditions, the output results are not exactly the same. Repeat the same operation several times, because the conditions are somewhat different, so the results are different. After many operations, until the final hda-badblock-list.final file is generated.

Three, others

1. Fsck uses badlocks information

Badblocks will only mark the bad track information in the log file, but if you want to skip these bad blocks and not detect them when detecting the disk, you can use the – L parameter of fsck:

# fsck.ext3 -l /tmp/hda-badblock-list.final /dev/hda1

2. Detect bad traces before creating a file system

Badblocks can run with the deletion of – C of e2fsck and mke2fs (the same is true for ext3 file system), and detect the bad path information before creating the file system:

# mkfs.ext3 -c /dev/hda1

The code indicates that – C is used to check the bad hard disk before creating the file system.

This operation has clearly told us that we can use the “mkfs. Ext3 – C” option to check the hard disk with “read only” mode. This command will check the hard disk when formatting the hard disk and mark the wrong hard disk “block”. Using this method to format the hard disk requires considerable patience, because after the command is run, the hard disk will be checked by reading one by one.