深入理解badblocks

发布时间:2024年01月24日


??团队博客: 汽车电子社区


一、概述

??badblocks命令是一个用于查找与标记磁盘坏块的工具,可以扫描和诊断磁盘上的坏块并将其从可使用中排除。

二、安装

2.1、源码编译安装

??源码编译安装请参考如下命令:

// 用web浏览器打开如下链接进行下载:
https://sourceforge.net/projects/e2fsprogs/postdownload

??请使用如下命令进行解压:

tar zxvf e2fsprogs-1.47.0.tar.gz

请使用如下命令进行编译安装:

// 编译之前配置命令
./configure --host=arm-hisiv400-linux

// 编译
make

// 安装
sudo make install

在这里插入图片描述

2.2、命令行安装

??Ubuntu下执行如下命令进行安装:

sudo apt-get  install badblocks

在这里插入图片描述

2.3、安装确认

??执行如下命令来确认badblocks是否安装成功:

badblocks -version

在这里插入图片描述

三、重要参数详解

3.1、查询支持的参数

??执行如下命令可以确认badblocks支持的参数:

badblocks -help
BADBLOCKS(8)                                                                            System Manager's Manual                                                                           BADBLOCKS(8)

NAME
       badblocks - search a device for bad blocks

SYNOPSIS
       badblocks [ -svwnfBX ] [ -b block_size ] [ -c blocks_at_once ] [ -d read_delay_factor ] [ -e max_bad_blocks ] [ -i input_file ] [ -o output_file ] [ -p num_passes ] [ -t test_pattern ] device
       [ last_block ] [ first_block ]

DESCRIPTION
       badblocks is used to search for bad blocks on a device (usually a disk partition).  device is the special file corresponding to the device (e.g /dev/hdc1).  last_block is the last block to be
       checked;  if  it is not specified, the last block on the device is used as a default.  first_block is an optional parameter specifying the starting block number for the test, which allows the
       testing to start in the middle of the disk.  If it is not specified the first block on the disk is used as a default.

       Important note: If the output of badblocks is going to be fed to the e2fsck or mke2fs programs, it is important that the block size is properly specified, since the block  numbers  which  are
       generated  are  very dependent on the block size in use by the file system.  For this reason, it is strongly recommended that users not run badblocks directly, but rather use the -c option of
       the e2fsck and mke2fs programs.

OPTIONS
       -b block_size
              Specify the size of blocks in bytes.  The default is 1024.

       -c number of blocks
              is the number of blocks which are tested at a time.  The default is 64.

       -d read delay factor
              This parameter, if passed and non-zero, will cause bad blocks to sleep between reads if there were no errors encountered in the read operation; the delay will be calculated as  a  per‐
              centage  of the time it took for the read operation to be performed. In other words, a value of 100 will cause each read to be delayed by the amount the previous read took, and a value
              of 200 by twice the amount.

       -e max bad block count
              Specify a maximum number of bad blocks before aborting the test.  The default is 0, meaning the test will continue until the end of the test range is reached.

       -f     Normally, badblocks will refuse to do a read/write or a non-destructive test on a device which is mounted, since either can cause the system to potentially crash and/or damage the file
              system  even  if  it  is  mounted  read-only.  This can be overridden using the -f flag, but should almost never be used --- if you think you're smarter than the badblocks program, you
              almost certainly aren't.  The only time when this option might be safe to use is if the /etc/mtab file is incorrect, and the device really isn't mounted.

       -i input_file
              Read a list of already existing known bad blocks.  Badblocks will skip testing these blocks since they are known to be bad.  If input_file is specified as "-", the list  will  be  read
              from  the  standard  input.   Blocks  listed  in  this  list  will  be  omitted from the list of new bad blocks produced on the standard output or in the output file.  The -b option of
              dumpe2fs(8) can be used to retrieve the list of blocks currently marked bad on an existing file system, in a format suitable for use with this option.

       -n     Use non-destructive read-write mode.  By default only a non-destructive read-only test is done.  This option must not be combined with the -w option, as they are mutually exclusive.

       -o output_file
              Write the list of bad blocks to the specified file.  Without this option, badblocks displays the list on its standard output.  The format of this file is suitable for  use  by  the  -l

3.2、参数说明

	-s:输出扫描的进度。
	-v:输出详细信息。
	-w:使用写模式检查坏块。
	-n:使用非破坏性的读写测试来识别坏块。这个选项只在Linux Ext2fs和Linux Ext3fs上有效。
	-f:强制扫描即使在磁盘可能有故障的情况下。
	-B:指定块大小,默认值是4096。
	-c:指定每批次测试块数。
	-e:指定最大坏块数。
	-o:指定输出文件。
	-p:指定扫描的次数。
	-t:指定测试模式,可选值为l、o、s、a、p、0,默认值为l。

四、实例

4.1、全面扫描

??使用如下命令进行磁盘全面扫描:

sudo badblocks /dev/loop9 -v

在这里插入图片描述

4.2、破坏性写入并修复

??使用-w参数,该命令将对硬盘进行破坏性写入测试,以检测坏块并尝试修复。

sudo badblocks -w /dev/loop7 -v

??不安全的操作会有如下提示:
在这里插入图片描述

4.3、非破坏性写入测试

??使用-n参数,该命令将对硬盘/dev/loop7进行非破坏性写入测试,以检测坏块。

sudo badblocks -n /dev/loop7 -v

??不安全的操作会有如下提示:
在这里插入图片描述

五、实现原理

??badblocks命令底层是通过与硬盘进行直接交互来实现的。它使用了底层的读写操作,以及硬盘的块设备接口来进行检测和识别坏块。
??具体来说,badblocks命令通过以下步骤实现:
????1、打开设备:badblocks命令首先会打开指定的设备,例如/dev/sda。这样就可以通过设备文件来与硬盘进行交互。
????2、分配内存:badblocks命令会分配一定数量的内存作为缓冲区,用于读取和写入数据。这样可以提高读写操作的效率。
????3、写入测试:如果使用了破坏性写入测试(-w选项),badblocks命令会将特定的模式数据写入硬盘的每个块中。这样可以检测硬盘中是否存在无法正常写入的块。
????4、读取测试:无论是非破坏性写入测试(-n选项)还是破坏性写入测试,badblocks命令都会尝试读取每个块中的数据。如果读取失败,说明该块可能是坏块。
????5、标记坏块:如果在读取测试中发现了坏块,badblocks命令会将其标记为坏块,并将其位置记录下来。这样可以帮助用户识别和处理坏块。
????6、输出结果:badblocks命令会将扫描结果输出到终端或指定的输出文件中。用户可以根据输出结果来查看坏块的位置和数量。

六、注意事项

??输入任何命令前一定要仔细核对设备名:输入错误的设备名可能导致数据丢失。
??不要在正在运行的操作系统上测试它自己的硬盘:这会导致数据丢失和系统崩溃。
??在修复坏道之前备份数据:某些坏道可能无法修复,导致数据丢失。

文章来源:https://blog.csdn.net/code_lyb/article/details/135828306
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。