0


Linux shell编程学习笔记42:hdparm命令

ChatGPT 和文心一言哪个更好用?

从智能回复、语言准确性、知识库丰富度等方面比较,两大AI助手哪个更胜一筹?快来和我们分享一下你的看法吧~

0 前言

获取硬盘序列号是信息资产管理和信息安全检测中经常要收集的信息,对于Linux来说,获取硬盘序列号的方法不止一种,比如,我们可以使用上节介绍的lsblk命令来获取硬盘序列号,详见:银河麒麟桌面操作系统中获取硬盘序列号

再如,我们今天学习的hdparm命令也可以用来获取硬盘序列号。

1 hdparm命令的功能、格式

我们可以使用 hdparm --help命令查看 该命令的帮助信息。

purpleEndurer @ bash $ hdparm --help

hdparm - get/set hard disk parameters - version v9.48, by Mark Lord.

Usage: hdparm [options] [device ...]

Options:
-a Get/set fs readahead
-A Get/set the drive look-ahead flag (0/1)
-b Get/set bus state (0 == off, 1 == on, 2 == tristate)
-B Set Advanced Power Management setting (1-255)
-c Get/set IDE 32-bit IO setting
-C Check drive power mode status
-d Get/set using_dma flag
-D Enable/disable drive defect management
-E Set cd/dvd drive speed
-f Flush buffer cache for device on exit
-F Flush drive write cache
-g Display drive geometry
-h Display terse usage information
-H Read temperature from drive (Hitachi only)
-i Display drive identification
-I Detailed/current information directly from drive
-J Get/set Western DIgital "Idle3" timeout for a WDC "Green" drive (DANGEROUS)
-k Get/set keep_settings_over_reset flag (0/1)
-K Set drive keep_features_over_reset flag (0/1)
-L Set drive doorlock (0/1) (removable harddisks only)
-m Get/set multiple sector count
-M Get/set acoustic management (0-254, 128: quiet, 254: fast)
-n Get/set ignore-write-errors flag (0/1)
-N Get/set max visible number of sectors (HPA) (VERY DANGEROUS)
-p Set PIO mode on IDE interface chipset (0,1,2,3,4,...)
-P Set drive prefetch count
-q Change next setting quietly
-Q Get/set DMA queue_depth (if supported)
-r Get/set device readonly flag (DANGEROUS to set)
-R Get/set device write-read-verify flag
-s Set power-up in standby flag (0/1) (DANGEROUS)
-S Set standby (spindown) timeout
-t Perform device read timings
-T Perform cache read timings
-u Get/set unmaskirq flag (0/1)
-U Obsolete
-v Use defaults; same as -acdgkmur for IDE drives
-V Display program version and exit immediately
-w Perform device reset (DANGEROUS)
-W Get/set drive write-caching flag (0/1)
-x Obsolete
-X Set IDE xfer mode (DANGEROUS)
-y Put drive in standby mode
-Y Put drive to sleep
-z Re-read partition table
-Z Disable Seagate auto-powersaving mode
--dco-freeze Freeze/lock current device configuration until next power cycle
--dco-identify Read/dump device configuration identify data
--dco-restore Reset device configuration back to factory defaults
--dco-setmax Use DCO to set maximum addressable sectors
--direct Use O_DIRECT to bypass page cache for timings
--drq-hsm-error Crash system with a "stuck DRQ" error (VERY DANGEROUS)
--fallocate Create a file without writing data to disk
--fibmap Show device extents (and fragmentation) for a file
--fwdownload Download firmware file to drive (EXTREMELY DANGEROUS)
--fwdownload-mode3 Download firmware using min-size segments (EXTREMELY DANGEROUS)
--fwdownload-mode3-max Download firmware using max-size segments (EXTREMELY DANGEROUS)
--fwdownload-mode7 Download firmware using a single segment (EXTREMELY DANGEROUS)
--fwdownload-modee Download firmware using mode E (min-size segments) (EXTREMELY DANGEROUS)
--fwdownload-modee-max Download firmware using mode E (max-size segments) (EXTREMELY DANGEROUS)
--idle-immediate Idle drive immediately
--idle-unload Idle immediately and unload heads
--Istdin Read identify data from stdin as ASCII hex
--Istdout Write identify data to stdout as ASCII hex
--make-bad-sector Deliberately corrupt a sector directly on the media (VERY DANGEROUS)
--offset use with -t, to begin timings at given offset (in GiB) from start of drive
--prefer-ata12 Use 12-byte (instead of 16-byte) SAT commands when possible
--read-sector Read and dump (in hex) a sector directly from the media
--repair-sector Alias for the --write-sector option (VERY DANGEROUS)
--security-help Display help for ATA security commands
--trim-sector-ranges Tell SSD firmware to discard unneeded data sectors: lba:count ..
--trim-sector-ranges-stdin Same as above, but reads lba:count pairs from stdin
--verbose Display extra diagnostics from some commands
--write-sector Repair/overwrite a (possibly bad) sector directly on the media (VERY DANGEROUS)

1.1 hdparm 命令功能

获取或设置硬盘参数。

hdparm 源自 hard disk parameter。

1.2 hdparm 命令格式

hdparm [选项] [设备……]

注意:选项在前,设备在后。

选项说明
选项描述-a [分区数]获取/设置读取文件时,预先存入块区的分区数-A禁用/启用驱动器读取文件时的快取功能(0/1)-b获取/设置总线状态(0==关闭,1==打开,2==三态)-B
设置高级电源管理APM设置(1-255)

要禁用APM,可以将值设置为255。
-c获取/设置IDE 32位 IO设置。32位可以通过PCI或VLB总线传输输入。-C检查驱动器电源模式状态,标志-S,-Y,-y和-Z用于以熟练的方式控制IDE电源技术。-d获取/设置设备的DMA标志using_dma状态-D启用/禁用驱动器缺陷管理-E设置cd/dvd驱动器速度-f退出时刷新设备的缓冲区缓存,该选项可以与*-t-T*的一同使用。-F刷新驱动器写缓存-g显示驱动器的配置,驱动器的扩展以及设备从驱动器起始点的起始偏移量。-h显示帮助信息并退出-H从驱动器读取温度(仅限Hitachi)-i显示开机时由设备本身所提供的信息(内核驱动的识别信息)-I
直接显示来自设备的详细信息

比-i反馈的信息更多
-J获取/设置WDC“绿色”驱动器的Western DIgital“Idle3”超时(危险)-k获取/设置keep_settings_over_reset标志(0/1)-K设置驱动器keep_features_over_reset标志(0/1)-L设置驱动器门锁(0/1)(仅限可移动硬盘)-m
获取/设置驱动器上多个扇区I / O的扇区数。

要禁用此功能,可以将值设置为零。
-M获取/设置声音管理(0-254,128:安静,254:快速)-n获取/设置ignore-write-errors标志(0/1)-N获取/设置最大可见扇区数(HPA)(非常危险)-p在IDE接口芯片组上设置PIO模式(0,1,2,3,4,...)-P设置驱动器内部快取的分区数-q悄悄地更改下一个设置(在执行后续的参数时,不在屏幕上显示任何信息)-Q获取/设置DMA queue_depth(如果支持)-r获取/设置设备只读标志(设置危险)-R获取/设置设定硬盘进入省电模式前的等待时间-s在待机标志中设置通电(0/1)(危险)-S设置驱动器进入省电模式前的等待时间。-t执行设备读取计时,从而获取硬盘的读取速度-T执行缓存读取计时,从而获取硬盘的缓存读取速度-u
获取/设置unmaskirq标志(0/1)

该标志设定了在硬盘存取时,是否允许其他中断要求同时执行

当值设置为1时,它用于获取/设置驱动器的中断 - 取消屏蔽标志,然后驱动程序可以取消屏蔽磁盘中断处理中发生的其他干预。

此选项可能导致巨大的文件系统损坏(非常危险)
-U已过时-v使用默认值;与IDE驱动器的-acdgkmur相同-V显示程序版本并立即退出-w执行设备重置(危险)-W
获取/设置驱动器写入缓存标志(0/1)

每个盘都有读取缓存和写入缓存,有时我们关闭磁盘写入缓存的效率高于开启磁盘写入缓存的效率,那当然选择前者了。
-x已过时-X设置IDExfer模式(危险)-y
将驱动器置于待机模式

它强制IDE驱动器立即进入低功耗待机模式,通常会导致它停止运转。可以使用-C标志检查当前的电源模式状态。
-Y
将驱动器置于睡眠状态

这会强制驱动器加压以中断进入低功耗睡眠模式,以便完全关闭它,这里可以在-C标志的帮助下检测当前的电源模式状态。
-z重新读取分区表-Z禁用希捷自动省电模式--dco-freeze冻结/锁定当前设备配置,直到下一次电源重启--dco-identify读取/转储设备配置识别数据--dco-restore将设备配置重置为出厂默认设置--dco-setmax使用DCO设置最大可寻址扇区--direct使用O_DIRECT绕过计时的页面缓存--drq-hsm-error系统崩溃并出现“卡住的DRQ”错误(非常危险)--fallocate在不写入数据的情况下创建文件到磁盘--fibmap显示文件的设备范围(和碎片)--fwdownload将固件文件下载到驱动器(极其危险)--fwdownload-mode3使用最小尺寸段下载固件(极其危险)--fwdownload-mode3-max使用max-size段下载固件(极其危险)--fwdownload-mode7使用单段下载固件(极其危险)--fwdownload-modee使用模式E下载固件(最小尺寸段)(极其危险)--fwdownload-modee-max使用模式E下载固件(最大大小段)(极其危险)--idle-immediate立即空闲驱动器--idle-unload立即空闲并卸载磁头--Istdin读取将stdin中的数据识别为ASCII十六进制--Istdout将标识数据写入stdout为ASCII十六进制--make-bad-sector故意直接在媒体上腐败一个部门(非常危险)--offset与-t一起使用,以从驱动器开始的给定偏移量(以GiB为单位)开始计时--prefer-ata12尽可能使用12字节(而不是16字节)SAT命令--read-sector直接从媒体读取和转储(十六进制)扇区--repair-sector--write-sector选项的别名(非常危险)--security-help显示ATA安全命令的帮助--trim-sector-ranges告诉SSD固件丢弃不需要的数据扇区:lba:count--trim-sector-ranges-stdin与上述相同,但从stdin读取--verbose显示某些命令的额外诊断--write-sector直接在介质上修复/覆盖(可能是坏的)扇区(非常危险)
如果使用命令时没用指定选项, 则默认选项为:acdgkmnru。

2 hdparm命令使用实例

2.1 hdparm 设备:显示设备的基础信息

例 查看 设备 /dev/sdb 的基础信息:

purpleEndurer @ bash ~ $ hdparm /dev/sdb
/dev/sdb: Permission denied
purpleEndurer @ bash ~ $ sudo hdparm /dev/sdb

/dev/sdb:
multcount = 0 (off)
IO_support = 1 (32-bit)
readonly = 0 (off)
readahead = 256 (on)
geometry = 121601/255/63, sectors = 1953525168, start = 0

当前帐户权限不够,不能直接执行hdparm命令。

只好使用sudo命令来执行。

命令返回的信息包括:

  • multcount = 0 (off) :一次读取的扇区数
  • IO_support = 1 (32-bit) :IO模式为32位
  • readonly = 0 (off) :只读标志为0,不处于只读状态
  • readahead = 256 (on) :读取文件时,预先存入块区的分区数为256
  • geometry = 121601/255/63, sectors = 1953525168, start = 0 : geometry = 柱面数/ 磁头数/扇区数, sectors = 总扇区数, start = 起始扇区数

2.2 hdparm -a 设备:只查看设备的预先存入块区信息

例:只查看 设备 /dev/sdb 的预先存入块区信息。

purpleEndurer @ bash ~ $ sudo hdparm -a /dev/sdb

/dev/sdb:
readahead = 256 (on)
purpleEndurer @ bash ~ $

如果我们输入的命令是 sudo hdparm /dev/sdb -a

purpleEndurer @ bash ~ $ sudo hdparm /dev/sdb -a

/dev/sdb:
multcount = 0 (off)
IO_support = 1 (32-bit)
readonly = 0 (off)
readahead = 256 (on)
geometry = 121601/255/63, sectors = 1953525168, start = 0

由于命令中 设备 /dev/sdb 和 选项-a的顺序不对,hdparm不采纳指定的参数,显示结果与命令hdparm /dev/sdb 相同。

2.3 hdparm -A 设备:查看设备读取文件时的快取功能

例 查看 设备 /dev/sdb 的读取文件时的快取功能信息:

purpleEndurer @ bash ~ $ sudo hdparm -A /dev/sdb

/dev/sdb:
look-ahead = 1 (on)
purpleEndurer @ bash ~ $

2.4 hdparm -B 设备:显示设备高级电源管理设置

例 查看 设备 /dev/sdb 的高级电源管理设置信息

purpleEndurer @ bash ~ $ sudo hdparm -B /dev/sdb

/dev/sdb:
APM_level = 254
purpleEndurer @ bash ~ $

2.4 hdparm -C 设备:显示设备的电源管理模式

purpleEndurer @ bash ~ $ sudo hdparm -C /dev/sdb

/dev/sdb:
drive state is: active/idle
purpleEndurer @ bash ~ $

2.5 hdparm -i 设备:显示设备的硬件规格信息

例: 显示 设备 /dev/sdb 的硬件规格信息。

purpleEndurer @ bash ~ $ sudo hdparm -i /dev/sdb

/dev/sdb:

Model=ST1000DM010-2EP102, FwRev=CC46, SerialNo=W9APF1CX
Config={ HardSect NotMFM HdSw>15uSec Fixed DTR>10Mbs RotSpdTol>.5% }
RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=0
BuffType=unknown, BuffSize=unknown, MaxMultSect=16, MultSect=off
CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=1953525168
IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
PIO modes: pio0 pio1 pio2 pio3 pio4
DMA modes: mdma0 mdma1 mdma2
UDMA modes: udma0 udma1 udma2 udma3 udma4 udma5 *udma6
AdvancedPM=yes: unknown setting WriteCache=enabled
Drive conforms to: unknown: ATA/ATAPI-4,5,6,7

  • signifies the current active mode

purpleEndurer @ bash ~ $

命令返回信息中的SerialNo 就是硬盘序列号。

2.6 hdparm -I 设备:直接显示来自设备的详细信息

例: 直接显示来自 设备 /dev/sdb 的详细信息。

purpleEndurer @ bash ~ $ sudo hdparm -I /dev/sdb

/dev/sdb:

ATA device, with non-removable media
Model Number: ST1000DM010-2EP102
Serial Number: W9APF1CX
Firmware Revision: CC46
Transport: Serial, SATA Rev 3.0
Standards:
Used: unknown (minor revision code 0x0029)
Supported: 8 7 6 5
Likely used: 8
Configuration:
Logical max current
cylinders 16383 16383
heads 16 16
sectors/track 63 63
--
CHS current addressable sectors: 16514064
LBA user addressable sectors: 268435455
LBA48 user addressable sectors: 1953525168
Logical Sector size: 512 bytes
Physical Sector size: 4096 bytes
Logical Sector-0 offset: 0 bytes
device size with M = 10241024: 953869 MBytes
device size with M = 1000
1000: 1000204 MBytes (1000 GB)
cache/buffer size = unknown
Form Factor: 3.5 inch
Nominal Media Rotation Rate: 7200
Capabilities:
LBA, IORDY(can be disabled)
Queue depth: 32
Standby timer values: spec'd by Standard, no device specific minimum
R/W multiple sector transfer: Max = 16 Current = ?
Advanced power management level: 254
Recommended acoustic management value: 208, current value: 208
DMA: mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 udma4 udma5 *udma6
Cycle time: min=120ns recommended=120ns
PIO: pio0 pio1 pio2 pio3 pio4
Cycle time: no flow control=120ns IORDY flow control=120ns
Commands/features:
Enabled Supported:
* SMART feature set
Security Mode feature set
* Power Management feature set
* Write cache
* Look-ahead
* Host Protected Area feature set
* WRITE_BUFFER command
* READ_BUFFER command
* DOWNLOAD_MICROCODE
* Advanced Power Management feature set
Power-Up In Standby feature set
* SET_FEATURES required to spinup after power up
SET_MAX security extension
* 48-bit Address feature set
* Device Configuration Overlay feature set
* Mandatory FLUSH_CACHE
* FLUSH_CACHE_EXT
* SMART error logging
* SMART self-test
* General Purpose Logging feature set
* WRITE_{DMA|MULTIPLE}_FUA_EXT
* 64-bit World wide name
Write-Read-Verify feature set
* WRITE_UNCORRECTABLE_EXT command
* {READ,WRITE}_DMA_EXT_GPL commands
* Segmented DOWNLOAD_MICROCODE
* Gen1 signaling speed (1.5Gb/s)
* Gen2 signaling speed (3.0Gb/s)
* Gen3 signaling speed (6.0Gb/s)
* Native Command Queueing (NCQ)
* Phy event counters
* READ_LOG_DMA_EXT equivalent to READ_LOG_EXT
* DMA Setup Auto-Activate optimization
Device-initiated interface power management
* Software settings preservation
unknown 78[7]
* SMART Command Transport (SCT) feature set
* SCT Write Same (AC2)
unknown 206[7]
unknown 206[12] (vendor specific)
Security:
Master password revision code = 65534
supported
not enabled
not locked
not frozen
not expired: security count
supported: enhanced erase
104min for SECURITY ERASE UNIT. 104min for ENHANCED SECURITY ERASE UNIT.
Logical Unit WWN Device Identifier: 5000c500d61665e6
NAA : 5
IEEE OUI : 000c50
Unique ID : 0d61665e6
Checksum: correct
purpleEndurer @ bash ~ $

命令返回信息中的Serial Number:就是硬盘序列号。

2.7 hdparm -t 设备:执行设备读取计时,从而获取设备的读取速度

例:获取设备 /dev/sdb 的读取速度

purpleEndurer @ bash ~ $ sudo hdparm -t /dev/sdb

/dev/sdb:
Timing buffered disk reads: 628 MB in 3.01 seconds = 208.75 MB/sec
purpleEndurer @ bash ~ $

2.8 hdparm -T 设备:执行设备缓存读取计时,从而获取设备的缓存读取速度

例:获取设备 /dev/sdb 的缓存读取速度。

purpleEndurer @ bash ~ $ sudo hdparm -T /dev/sdb

/dev/sdb:
Timing cached reads: 7340 MB in 2.00 seconds = 3674.62 MB/sec
purpleEndurer @ bash ~ $

有些选项比较危险,就不演示了。


本文转载自: https://blog.csdn.net/Purpleendurer/article/details/135768102
版权归原作者 紫郢剑侠 所有, 如有侵权,请联系我们删除。

“Linux shell编程学习笔记42:hdparm命令”的评论:

还没有评论