Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 remembere to use the getsize64)

time blockdev --getsize64 /dev/system/home

share|improve this question
add comment

4 Answers

up vote 8 down vote accepted

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
share|improve this answer
    
Wow, that's fast! Let me try in my environment and I'll tell about it. –  Aseques Aug 9 '10 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 '10 at 16:16
add comment

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.

share|improve this answer
    
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 '10 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 '10 at 14:35
add comment

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

share|improve this answer
add comment

/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)

share|improve this answer
    
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 '10 at 14:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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