1

If we want to get the byte size of a block device or one of its partitions without calling any binary, and in a way that will always work, is it correct to do:

/sys/block/sda/sda1/size
or /sys/block/sda/size

Multiplied by :

/sys/block/sda/queue/physical_block_size

Or is it logical_block_size or hw_sector_size ?

Is there a better approach?

0

After more research I finally found the answer:

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

According to the source: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/types.h?id=v4.4-rc6#n121

I was led there by this comment: Determine the size of a block device

So as silly as it may seem the [size] should simply be multiplied by 512.

I just hope it will still be a correct assumption 15 years from now, because I don't want my programs to be broken. physical_block_size seems wrong because it can sometimes be 4096 as I found with some Googling, but maybe it is still worth reading logical_block_size or hw_sector_size even if it will always be 512? Any comments on this?

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.