I want to use partial SMART tests and need to give an LBA size for each span.
How can I determine the maximum LBA of my disk? (I would then divide this by 5.)
With smartctl selective self-tests, you can use the max keyword to determine the maximum LBA for use within selective tests:
# 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.
So in this case it is 537234767 (inclusive so 537234768 total).
This is identical to the number of sectors printed by parted et al. but just to avoid any doubt, it's good to use smartctl to grab the value so there can be no possibility of disagreement regarding logical/physical sector sizes.
It also happens to start a self-test, which you can abort with smartctl -X.
Assuming you're on Linux (since you used tune2fs), the disk and partition sizes as visible to the OS can be seen in /proc/partitions. E.g.
# head -3 /proc/partitions
major minor #blocks name
8 0 976762584 sda
The #blocks field is the number of 1024-byte blocks, so the number of 512-byte blocks is double that.
# awk '$4 == "sda" { print $3 * 2; } ' < /proc/partitions
1953525168
fdisk and hdparm -i can also show the number of sectors:
# 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
Yet another way would be to seek to the end of the block device, and query the offset, with e.g. Perl:
# perl -le '$a = sysseek(STDIN, 0, 2); print $a / 512' < /dev/sda
1953525168
Or, since we're about to do SMART tests anyway, ask 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
tune2fs gave 3.418.087.163 blocks. Your fdisk gave me 27.344.699.392 sectors. Why the difference in my case (you have LBA = sectors)? Also, I get HDIO_GET_IDENTITY failed: Invalid argument when using hdparm. Maybe because it is connected via USB? And smartctl with awk gives 0.0273438. I thought this might be because I get a European notation of capacity with . instead of ,. But substituting those in the command did not work, it gave 0 as result.
awk '$4 == "sda" { print $3 * 2; } ' < /proc/partitions also prints 2.73447e+10 which seem to be the sector count, not LBA.
cat /proc/partitions is interesting. It delivers blocks per disk. But I have 13.672.348.655 which is about half of the sector count and about four times the LBA. Since my LBA size seems to be 4096 there might be a relation.
/proc/partitions gives blocks of 1024 bytes, Yes, I converted them all to 512-byte sectors. I don't think I have a disk at hand with 4096-byte sectors, so I don't know what e.g. fdisk shows for one but that's just a factor of 8 away to convert. I think tune2fs also gives the number of filesystem blocks, and they can be of different sizes too... Usually it'd be 4096 bytes per block for large partitions (see the Block size: row in the output). So yeah, 27.344.699.392 is pretty much 8 times 3.418.087.163.
sudo tune2fs -l /dev/sdX1 prints a lot of information, including the 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
Also interesting is /proc/partitions which seems to print LBA count times 4. Maybe there is a relation to the block size (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
As ilkkachu noted in his comment, the LBA count of a partition is not spanning the correct space of the whole hard disk. But if you need just an indication of the size to use it with smartctl -t select,0-$size this is perfectly fine. smartctl will start at 0 and end at the end of the disk anyway.
/dev/sdx1 is only a single partition of the disk, usually the first. There may be other partitions after it, and even if not, it doesn't necessarily span the whole disk. In any case, the partition won't start at the very beginning of the drive, there must be some space for the partition table, and usually more. 2) tune2fs gives information about the file system, which, regardless of the partition size, does not need to span even the whole partition!
/dev/sdx1 I wanted to express that tune2fs needs a partition as argument. /dev/sdx will not work.