大网数据广告活动
服务器运维问答

Documented size of Linux kernel partition sector size in bytes

· 52CE 我爱测

My primary question is to authoritatively see if the block size used in sector counts: /sys/block/<disk>/<partition>/size is in fact 512 B, or if this can vary. I thought this would be fairly easy to find an answer to, but for ex…

问题原文

原帖链接 · Lizardx · CC BY-SA 4.0

My primary question is to authoritatively see if the block size used in sector counts:

/sys/block/<disk>/<partition>/size

is in fact 512 B, or if this can vary.

I thought this would be fairly easy to find an answer to, but for example here:

https://lore.kernel.org/lkml/[email omitted]/T/#u

the question was asked:

There are /sys/block/<device>/size and 
/sys/block/<device>/<device><partition>/size.

But these values are reported in sectors, not in bytes. As discs with 4K 
sectors are on the horizon now, I don't want to make wrong assumptions on the 
sector size.

So what is the correct sector size for /sys/block/<device>/size?

And a patch was offered in the documentation to apparently once and for all answer this question in documentation: https://lore.kernel.org/lkml/[email omitted]/T/

As the meaning has not changed for over 13 years, I would like to formalize these attributes such that users can rely on it[1][2]. The sector definition was inspired by the block/stat.txt documentation.

With the patch:

diff --git a/Documentation/ABI/testing/sysfs-block   b/Documentation/ABI/testing/sysfs-block
+What:      /sys/block/<disk>/<partition>/size
+Date:      October 2002
+Contact:   [email omitted]
+Kernel Version:    2.5.43
+Description:
+       Size of the partition in standard UNIX 512-byte sectors
+       (not a device-specific block size).

Which all seems straightforward, but when I look up the current existing doc: https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block

There is no presence of this patch. Which suggests that in fact this apparently simple question may not be simple?

Can someone point me to an actual authoritative answer to this question, ideally using kernel documentation or code with comments?

Currently I'm relying on the output of a tool, but if that is not in fact always 512B that output could be wrong, which would lead the code I'm working on to be wrong as well in some cases.

I'm surprised it's been so difficult to find a truly authoritative answer,

https://unix.stackexchange.com/questions/52215/determine-the-size-of-a-block-device

Units for size in /proc/partitions don't make sense

Some of these point to the patch here, but none seem to be aware that the patch does not seem to have been accepted, though it appears to be have been signed off, yet it's not in the documentation code as far as I can see.

It would be nice if we could once and for all determine what the internal kernel code really is doing, and if this behavior is documented, or merely assumed, which means it could in theory randomly change at any time.

已采纳解答

原帖链接 · shodanshok · CC BY-SA 4.0

From Linux source code comment:

Linux always considers sectors to be 512 bytes long independently of the devices real block size

That said, I think scanning /sys/block/<disk>/ is the worst (or at least harder) approach from the ones available to get disks and partitions size:

  • lsblk --bytes --list produce an informative, non-ambiguous and parseable output;
  • cat /proc/partitions give similar information in 1K-sized blocks
  • blockdev --getsize64 <dev> returns device size in bytes;
  • blockdev --getsz <dev> returns device size in 512-bytes sectors;
  • ... surely other methods which I don't remember now ...

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-09-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。