20

I am using tune2fs, but it gives data in blocks, and I can't get the exact value of total size of the partition.

I have also used fdisk -l /dev/mmcblk0p1, but the size am getting from here is also a different value.

How can I find the exact partition size?

peterh
  • 9,731
  • 8
    Note that tune2fs does not print the size of the partition, it prints the size of the filesystem. A filesystem may be smaller than a partition (if there is unused space on the partition), it may be larger than a partition (in case the partition was somehow truncated; obviously the filesystem is corrupted in that case, but still it is larger than the partition). It also may span multiple partitions, or even be "virtual", i.e. don't even have a backing device (e.g. sysfs, procfs, tmpfs). – Jörg W Mittag Sep 28 '17 at 19:22

4 Answers4

30

The command is:

blockdev --getsize64 /dev/mmcblk0p1

It gives the result in bytes, as a 64-bit integer. It queries the byte size of a block device, as the kernel see its size.

The reason, why fdisk -l /dev/mmcblk0p1 didn't work, was that fdisk does some total different thing: it reads in the partition table (= first sector) of the block device, and prints what it found. It doesn't check anything, only says what is in the partition table.

It doesn't even bother if the partition table is damaged, or the block device doesn't have one: it will print a warning that the checksum is not okay, but it still prints what is finds, even if the values are clearly non-sense.

This is what happened in your case: /dev/mmcblk0p1 does not have a partition table. As the name of the device shows, it is already the first partition of the physical disk /dev/mmcblk0. This disk contains a partition table, had you queried it with fdisk -l /dev/mmcblk0, it had worked (assuming it had an msdos partition table).

MrPaulch
  • 105
peterh
  • 9,731
  • My partition space is exactly 2gb but blockdev and fdisk both give values lesser than that where is the remaining space and please also tell command to retrieve its value – Sarthak_Bhutani Sep 29 '17 at 05:45
  • @Sarthak_Bhutani Which information you need isn't printed by fdisk -l /dev/mmcblk0? It should tell you the size of one block, the total numbers of blocks on the device, the size of the partitions and also the start and end block of each partition. You can also figure out block ranges that are not assigned to any partition from that info. – BlackJack Sep 29 '17 at 13:36
17

Try lsblk, it doesn't even require root:

$ lsblk -b
NAME    MAJ:MIN RM        SIZE RO TYPE MOUNTPOINT
xvda    202:0    0 34359738368  0 disk 
├─xvda1 202:1    0  1676673024  0 part [SWAP]
└─xvda2 202:2    0 32682016768  0 part /var/spool

The -b parameter tells it to output the size in bytes.

8

To get the exact value of total size of the partition run:

awk '{print $1*512}' /sys/class/block/mmcblk0p1/size
GAD3R
  • 66,769
  • 2
    I think it depends also on if the sector size is 512 (which is the overwhelming majority of the cases today). – peterh Sep 28 '17 at 12:56
  • 1
    @peterh It is actually unusual for a block device to still have a physical sector size of 512 these days, but many things (I can't find definite information on /sys/class/block) are designed to report in units of 512 bytes regardless of the actual block size. – Random832 Sep 28 '17 at 13:27
1

My solution:

part_dev='/dev/hda2'

total_block=`tune2fs -l $part_dev | awk '/Block count/ {print $3}'`
block_size=`tune2fs -l $part_dev | awk '/Block size/ {print $3}'`

total_size=$(($total_block* $block_size))

echo $total_size

where /dev/hda2 is the partition that is not necessarily mounted. it is tested with tune2fs ver 1.42.4

AdminBee
  • 22,803