How to scan a hard disk partition using badblocks (or e2fsck)

e2fsck and badblocks come inside e2fsprogs package.

e2fsprogs is a set of tools used to create, check or maintain ext2, ext3 and ext4 based file systems.

To install badblocks and e2fsck tools in Debian:
$ sudo aptitude install e2fsprogs

badblocks tool

badblocks searches for bad blocks on a storage device (usually a disk partition).

badblocks man page:
$ man badblocks

IMPORTANT: If we are going to use badblocks output to feed e2fsck or mke2fs tools, we need to accurately specify the block size.
This happens because output block numbers depend on the block size.
So it is recommended to use e2fsck or mke2fs programs with -c option instead of directly calling badblocks.

Nevertheless we are going to run badblocks directly.

To list current devices in our system:
$ lsblk
sdb 8:16 0 3.7T 0 disk
└─sdb1 8:17 0 3.7T 0 part

I am going to check /dev/sdb1 partition.

Dump filesystem information (to find block size):
$ sudo dumpe2fs /dev/sdb1

In my /dev/sdb device block size equals 4096.

I use badblocks directly with right block size:

$ sudo badblocks -b 4096 -n -o out_sdb1 -s -v /dev/sdb1
Checking for bad blocks in non-destructive read-write mode
From block 0 to 976754384
Checking for bad blocks (non-destructive read-write test)
Testing with random pattern: 0.00% done, 0:09 elapsed. (0/0/0 errors)

-b option indicates block size.

-n read-write test.

-o output file with data for e2fsck or mke2fs programs.

-s show progress of the scan.

-v verbose mode

If I stop badblocks while running, I can restart it again later by indicating the block to start it with:


$ sudo badblocks -b 4096 -n -o out_sdb1 -s -v /dev/sdb1
Checking for bad blocks in non-destructive read-write mode
From block 0 to 976754384
Checking for bad blocks (non-destructive read-write test)
Testing with random pattern:

I interrupt it by using Ctrl-c signal.

1.18% done, 1:35:07 elapsed. (0/0/0 errors)

Interrupted at block 11481408

Interrupt caught, cleaning up

out_sdb1 file shows nothing:
$ cat out_sdb1

I rerun badblocks again starting a block 11481408:

$ sudo badblocks -b 4096 -n -o out_sdb1 -s -v /dev/sdb1 976754384 11481408
Checking for bad blocks in non-destructive read-write mode
From block 11481408 to 976754384
Checking for bad blocks (non-destructive read-write test)
Testing with random pattern: 1.18% done, 0:04 elapsed. (0/0/0 errors)

If we want to perform read only test (without -n option):
(it is quicker than read-write one, also read-write scans are not allowed on mounted devices):

$ sudo badblocks -b 4096 -o out_sdb1_002.txt -s -v /dev/sdb1 976754384
Checking blocks 0 to 976754384
Checking for bad blocks (read-only test): 0.01% done, 0:07 elapsed. (0/0/0 errors)

Checking for bad blocks (read-only test): 3.73% done, 1:01:53 elapsed. (0/0/0 errors)

After completed. E.g:

Checking for bad blocks (read-only test): done
Pass completed, 0 bad blocks found. (0/0/0 errors)

e2fsck tool

We can check ext2/ext3/ext4 type of file systemse2fsck with e2fsck program.

It is not safe to run e2fsck on mounted filesystems.

e2fsck man page:
$ man e2fsck

(From man page):
When running e2fsck in interactive mode (that it is none of -y, -n, or -p are specified),
the program will ask the user to fix each problem found in the filesystem. A response of
'y' will fix the error; 'n' will leave the error unfixed; and 'a' will fix
the problem and all subsequent problems; pressing Enter will proceed with the default
response, which is printed before the question mark. Pressing Control-C terminates
e2fsck immediately.

Interesting options (from man page):

-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.

-p Automatically repair ("preen") the file system. This option will cause e2fsck
to automatically fix any filesystem problems that can be safely fixed without
human intervention. If e2fsck discovers a problem which may require the system
administrator to take additional corrective action, e2fsck will print a
description of the problem and then exit with the value 4 logically or'ed into
the exit code. (See the EXIT CODE section.) This option is normally used by
the system's boot scripts. It may not be specified at the same time as the -n
or -y options.

-t Print timing statistics for e2fsck. If this option is used twice, additional
timing statistics are printed on a pass by pass basis.

-v Verbose mode.

I run e2fsck tool in /dev/sdb1 partition:

Only check the file system but do not scan for bad blocks:
$ sudo e2fsck -p -t -v /dev/sdb1
BACKUP_DISK: clean, 3485338/244195328 files, 815515700/976754385 blocks

Scan the file system in read-write mode:
$ sudo e2fsck -cc -p -t -v /dev/sdb1

Internaly e2fsck calls badblocks:
$ sudo iotop # shows badblocks at work:
16.00 M/s 15.76 M/s 0.00 % 97.13 % badblocks -b 4096 -X -n /dev/sdb1 976754384

If I use e2fcsk without read-write mode:

$ sudo e2fsck -c -p -t -v /dev/sdb1

38.30 M/s 0.00 B/s 0.00 % 98.03 % badblocks -b 4096 -X /dev/sdb1 976754384

Call e2fsck passing badblocks output file as argument

I am going to pass output file results from badblocks as input to e2fsck program to mark bad blocks.

From $man e2fsck

-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 sim‐
pler and safer to use the -c option to e2fsck, since it will assure that the
correct parameters are passed to the badblocks program.

-L filename
Set the bad blocks list to be the list of blocks specified by filename. (This
option is the same as the -l option, except the bad blocks list is cleared
before the blocks listed in the file are added to the bad blocks list.)

$ time sudo e2fsck -l out_sdb1_002.txt -t -v /dev/sdb1
e2fsck 1.43.6 (29-Aug-2017)
BACKUP_DISK: Updating bad block inode.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information


3485338 inodes used (1.43%, out of 244195328)
7109 non-contiguous files (0.2%)
1889 non-contiguous directories (0.1%)
# of inodes with ind/dind/tind blocks: 0/0/0
Extent depth histogram: 3448933/2825/6
815515700 blocks used (83.49%, out of 976754385)
0 bad blocks
37 large files

3061125 regular files
374601 directories
7 character device files
0 block device files
0 fifos
271 links
49595 symbolic links (33558 fast symbolic links)
1 socket
3485600 files
Memory used: 15984k/1868k (879k/15106k), time: 196.82/44.74/ 6.47
I/O read: 2753MB, write: 2MB, rate: 14.00MB/s

real 3m20.105s
user 0m44.778s
sys 0m6.615s


$ man badblocks

$ man e2fsck