Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm in need to find out the LVM size of image vm001. Let's say I have a LVM volume called /dev/VGgroup/vm001. Now using lvdisplay I can find out the size:

--- Logical volume ---
  LV Name                /dev/VGgroup/vm001
  VG Name                VGgroup
  LV UUID                i0aYKs-Hpfv-q64V-9Rqu-6Wrq-eV3C-pZzo0D
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                25.00 GB
  Current LE             6400
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:41

How can I find out the LV size using a script/command which will output just 25? I know with awk you can find strings horizontally, but not vertically (as far as I know).

Edit There are more Logical Volumes, using lvdisplay | awk '/LV Size/ { print $3 }' will output all sizes (obviously), how do I only get the size of the volume I want to? (in this case vm001).

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

You can set a variable(found) in awk, and exit immediately after printing out LV Size.

$ lvdisplay | awk '/vm001/{found=1}; /LV Size/ && found{print $3; exit}'
25.00
  • if vm001 is found, then set found to 1 (because we known LV Size is following this line)
  • if LV Size and found!=0, then print column#3, and exit immediately.
share|improve this answer
    
Thank you, this works like a charm! However could you eleborate your command a bit? I don't understand exactly what it does, trying to learn a bit aswell ;-) –  Devator Mar 12 '12 at 12:38
    
Thanks for your edit, I now know exactly how it works. Cheers! –  Devator Mar 12 '12 at 13:15
1  
@Devator, @kev, just my 2 cents, you might want to protect the /vm001/ from false-postitives (like vm0012) with say, /vm001$/ or something. –  Anil Mar 12 '12 at 14:44
add comment

You can use regex to select lines using '//' prefix for a block. For e.g on your lvdisplay output.

awk '/LV Size/ { print $3 }'
share|improve this answer
    
Awesome, thanks. However there are more Logical Volumes, using lvdisplay | awk '/LV Size/ { print $3 }' will output all sizes (obviously), how do I only get the size of the volume I want to? (in this case vm001. Please refer to the table in my original post. –  Devator Mar 12 '12 at 10:34
    
Ah. I'd have done it the way @kev suggested as well. –  Anil Mar 12 '12 at 14:38
add comment

With

lvs /dev/vg/lvname -o LV_SIZE --noheadings --units G --nosuffix

you get the size of your LV in useful form.

share|improve this answer
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.