4

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

4 Answers 4

10

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
2
  • Wow, that's fast! Let me try in my environment and I'll tell about it.
    – aseques
    Aug 9, 2010 at 15:53
  • 1
    It's also answering the more generic version of the question that you were really asking - "How do I determine the size of a block device?"
    – MikeyB
    Aug 9, 2010 at 16:16
2

Here are some things you could try. I don't know lvm internals, so I don't know if they'll make any difference.

  • Try lvs instead of lvdisplay. Maybe it's faster since it looks for less information.

  • Try running lvdisplay or lvs only once for all the volumes you're interested in. Since some information is stored per volume group, there will be less duplication of reading effort that way.

2
  • lvs on my system (about 60 logical volumes): 0.15 seconds. lvdisplay: 4 seconds. lvdisplay, one volume at a time: 20 seconds.
    – pehrs
    Aug 9, 2010 at 12:56
  • I just tried both, there's not many difference, but at least I see some improvement. > 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 The pity is that lvs doesn't seem to be capable of showing only the size
    – aseques
    Aug 9, 2010 at 14:35
0

there's also lvs but they all rely on the same codebase I think

0

/proc/partitions also lists the LVM-managed volumes and should be far faster. the biggest downside is that it lists them as 'dm-0', 'dm-1', etc. you'll need the name-number mapping from somewhere else. (hint, check the minor device number of device files)

1
  • That was my first shot, the problem is that the information is very sparse. There is the /proc/partitions with the size + dm information There is the /dev/mapper/ with all the lvm volumes as devices There is also dmsetup ls that links mapper and partition entries. To sum up, there are three places that put together can give me the information I need, but with a bash script this is quite complicated.
    – aseques
    Aug 9, 2010 at 14:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .