Linux – Quickest way to determine size of a block device

block-devicelinuxlvm

I've a remote script that checks for some parameters from lvm volumes. One of the things I need to know is the size.
I am currently using this:

lvdisplay /dev/virtuals/volume_name -C -o lv_size

But the last comand can take about one second to be executed on my server and I have multiple volumes I want to check. I've also tried usin lvs instead of lvdisplay, but there's no much improvement in the speed.

time lvs -o lv_size /dev/virtuals/volume_name -C -o lv_size
real 0m0.809s
time lvdisplay /dev/virtuals/volume_name
real 0m0.982s

Does someone know a faster way to obtain that information, just the volume size, I am not interested on the ocupation.

Update:
Using blockdev gives a massive improvement. I really recomment using it instead of lvdisplay (just remember to use the getsize64)

time blockdev –getsize64 /dev/system/home

Best Answer

Use the blockdev command:

Bytes:

> time blockdev --getsize64 /dev/system/home
32212254720

real   0m0.004s
user   0m0.001s
sys    0m0.002s

Sectors:

> time blockdev --getsize /dev/system/home
62914560

real   0m0.006s
user   0m0.001s
sys    0m0.003s