深入理解badblocks

文章目录


团队博客: 汽车电子社区


一、概述

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

二、安装

2.1、源码编译安装

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

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

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

bash 复制代码
tar zxvf e2fsprogs-1.47.0.tar.gz

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

bash 复制代码
// 编译之前配置命令
./configure --host=arm-hisiv400-linux

// 编译
make

// 安装
sudo make install

2.2、命令行安装

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

bash 复制代码
sudo apt-get  install badblocks

2.3、安装确认

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

bash 复制代码
badblocks -version

三、重要参数详解

3.1、查询支持的参数

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

bash 复制代码
badblocks -help
cpp 复制代码
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、参数说明

bash 复制代码
	-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、全面扫描

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

bash 复制代码
sudo badblocks /dev/loop9 -v

4.2、破坏性写入并修复

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

bash 复制代码
sudo badblocks -w /dev/loop7 -v

不安全的操作会有如下提示:

4.3、非破坏性写入测试

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

bash 复制代码
sudo badblocks -n /dev/loop7 -v

不安全的操作会有如下提示:

五、实现原理

badblocks命令底层是通过与硬盘进行直接交互来实现的。它使用了底层的读写操作,以及硬盘的块设备接口来进行检测和识别坏块。

具体来说,badblocks命令通过以下步骤实现:

1、打开设备 :badblocks命令首先会打开指定的设备,例如/dev/sda。这样就可以通过设备文件来与硬盘进行交互。

2、分配内存 :badblocks命令会分配一定数量的内存作为缓冲区,用于读取和写入数据。这样可以提高读写操作的效率。

3、写入测试 :如果使用了破坏性写入测试(-w选项),badblocks命令会将特定的模式数据写入硬盘的每个块中。这样可以检测硬盘中是否存在无法正常写入的块。

4、读取测试 :无论是非破坏性写入测试(-n选项)还是破坏性写入测试,badblocks命令都会尝试读取每个块中的数据。如果读取失败,说明该块可能是坏块。

5、标记坏块 :如果在读取测试中发现了坏块,badblocks命令会将其标记为坏块,并将其位置记录下来。这样可以帮助用户识别和处理坏块。

6、输出结果:badblocks命令会将扫描结果输出到终端或指定的输出文件中。用户可以根据输出结果来查看坏块的位置和数量。

六、注意事项

输入任何命令前一定要仔细核对设备名:输入错误的设备名可能导致数据丢失。

不要在正在运行的操作系统上测试它自己的硬盘:这会导致数据丢失和系统崩溃。

在修复坏道之前备份数据:某些坏道可能无法修复,导致数据丢失。

相关推荐
CSXB991 小时前
三十四、Python基础语法(文件操作-上)
开发语言·python·功能测试·测试工具
无尽的大道12 小时前
Java反射原理及其性能优化
jvm·性能优化
萌面小侠Plus16 小时前
Android笔记(三十三):封装设备性能级别判断工具——低端机还是高端机
android·性能优化·kotlin·工具类·低端机
测试杂货铺18 小时前
外包干了2年,快要废了。。
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
码农派大星。19 小时前
软件测试--BUG篇
功能测试·bug
测试界萧萧19 小时前
外包干了4年,技术退步太明显了。。。。。
自动化测试·软件测试·功能测试·程序人生·面试·职场和发展
人工智能培训咨询叶梓21 小时前
探索开放资源上指令微调语言模型的现状
人工智能·语言模型·自然语言处理·性能优化·调优·大模型微调·指令微调
CodeToGym1 天前
Webpack性能优化指南:从构建到部署的全方位策略
前端·webpack·性能优化
无尽的大道1 天前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化
superman超哥1 天前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba