如何获得硬盘的最大LBA(用于部分SMART测试)?

如何获得硬盘的最大LBA(用于部分SMART测试)?

我想使用部分 SMART 测试,并且需要为每个跨度提供 LBA 大小。

如何确定磁盘的最大 LBA? (然后我会将其除以 5。)

答案1

smartctl 选择性自测,您可以使用max关键字来确定选择性测试中使用的最大 LBA:

# smartctl -t select,0-max /dev/sda
smartctl 7.1 2019-12-30 r5022 [x86_64-linux-5.9.11-arch2-1] (local build)
Copyright (C) 2002-19, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF OFFLINE IMMEDIATE AND SELF-TEST SECTION ===
Sending command: "Execute SMART Selective self-test routine immediately in off-line mode".
SPAN         STARTING_LBA           ENDING_LBA
   0                    0            537234767
Drive command "Execute SMART Selective self-test routine immediately in off-line mode" successful.
Testing has begun.

所以在本例中它是 537234767(包括总共 537234768)。

这与等人打印的扇区数相同parted。但为了避免任何疑问,最好使用smartctl来获取该值,这样就不会在逻辑/物理扇区大小方面出现分歧。

它还会启动自检,您可以使用 中止自检smartctl -X

答案2

假设您使用的是 Linux(因为您使用过tune2fs),操作系统可见的磁盘和分区大小可以在 中看到/proc/partitions。例如

# head -3 /proc/partitions
major minor  #blocks  name

   8        0  976762584 sda

#blocks字段是 1024 字节块的数量,因此 512 字节块的数量是它的两倍。

# awk '$4 == "sda" { print $3 * 2; } ' < /proc/partitions
1953525168

fdiskhdparm -i可以显示扇区数:

# fdisk -lu /dev/sda | head -1
Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
# hdparm -i /dev/sda |grep LBAsects
 CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=1953525168

另一种方法是寻找块设备的末尾,并查询偏移量,例如使用 Perl:

# perl -le '$a = sysseek(STDIN, 0, 2); print $a / 512' < /dev/sda
1953525168

或者,既然我们即将进行 SMART 测试,请询问smartctl

# smartctl -i /dev/sda |grep  '^User Capacity:'
User Capacity:    1,000,204,886,016 bytes [1.00 TB]
# smartctl -i /dev/sda |awk '/^User Capacity: / { gsub(",", "", $3); print $3 / 512; }'
1953525168

答案3

sudo tune2fs -l /dev/sdX1打印很多信息,包括block count

benraid@OMV:/srv$ sudo tune2fs -l /dev/sdk1
tune2fs 1.44.5 (15-Dec-2018)
Filesystem volume name:   Elements
Last mounted on:          <not available>
Filesystem UUID:          e58521dc-7ac6-4096-8339-42830ceee05d
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              427261952
Block count:              3418087163
Reserved block count:     0
Free blocks:              3390863340
Free inodes:              427261941
First block:              0
Block size:               4096
Fragment size:            4096
Group descriptor size:    64
Reserved GDT blocks:      418
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         4096
Inode blocks per group:   256
Flex block group size:    16
Filesystem created:       Wed Dec  2 20:57:48 2020
Last mount time:          n/a
Last write time:          Wed Dec  2 21:09:13 2020
Mount count:              0
Maximum mount count:      -1
Last checked:             Wed Dec  2 20:57:48 2020
Check interval:           0 (<none>)
Lifetime writes:          103 GB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:           256
Required extra isize:     32
Desired extra isize:      32
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      9bccaff2-62b7-45c9-876b-b011f5e8c6b9
Journal backup:           inode blocks
Checksum type:            crc32c
Checksum:                 0x526fb02e

另外有趣的是/proc/partitions,它似乎打印了 LBA 计数乘以 4。也许与块大小(4096)有关系:

benraid@OMV:/srv$ cat /proc/partitions
major minor  #blocks  name

   8        0 2930266584 sda
   8        1 2930265543 sda1
   8       48 2930266584 sdd
   8       49 2930265543 sdd1
   8       80 2930266584 sdf
   8       81 2930265543 sdf1
   8       32 2930266584 sdc
   8       33 2930265543 sdc1
   8       64   39082680 sde
   8       65   35221504 sde1
   8       66          1 sde2
   8       69    3858432 sde5
   8       16 2930266584 sdb
   8       17 2930265543 sdb1
   8       96 2930266584 sdg
   8       97 2930265543 sdg1
   8      112 11718885376 sdh
   8      113 11718884335 sdh1
   8      128 9766436864 sdi
   8      129 9766435823 sdi1
   8      144 11718885376 sdj
   8      145 11718884335 sdj1
   8      160 13672349696 sdk
   8      161 13672348655 sdk1

正如 ilkkachu 在他的评论中指出的那样,分区的 LBA 计数没有跨越整个硬盘的正确空间。但如果您只需要指示尺寸即可与smartctl -t select,0-$size它一起使用,这是完全可以的。smartctl无论如何都会从 0 开始并在磁盘末尾结束。

相关内容