Disk size /sys/block/sda/size is always correct but the partition size /sys/block/sda/sda2/size is not correct if the partition type is extended, boot, etc, due to this I rely on reading MBR to get the correct partition size.

What does the value in /sys/block/sd*/sd*/size file signify? What is the correct way to get the partition size apart from reading MBR or using commands?


nyc@nyc:~/Documents$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x13ce1e72

Device     Boot      Start        End    Sectors  Size Id Type
/dev/sdb1  *          2048 1881196543 1881194496  897G 83 Linux
/dev/sdb2       1881196544 2147483647  266287104  127G  5 Extended
/dev/sdb5       1881198592 1983643647  102445056 48.9G 83 Linux
/dev/sdb6       1983645696 2076295167   92649472 44.2G 83 Linux
/dev/sdb7       2090141696 2139455487   49313792 23.5G 83 Linux
/dev/sdb8       2139457536 2147483647    8026112  3.8G  7 HPFS/NTFS/exFAT
nyc@nyc:~/Documents$ cat /sys/block/sdb/size 
nyc@nyc:~/Documents$ cat /sys/block/sdb/sdb2/size 

You are getting 2 because that partition is extended partition, which contains logical partitions. Here 2 denotes the number of 512 byte sectors, or we can say that it has 1 1K block. You can use the command lsblk which will give you output as:

sdb2   8:3    0     1K  0 part 

which shows that it is an extended partition.

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.