3

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

4 Answers 4

2

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.
3
  • 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, 2012 at 12:38
  • Thanks for your edit, I now know exactly how it works. Cheers!
    – Devator
    Mar 12, 2012 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, 2012 at 14:44
6

With

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

you get the size of your LV in useful form.

1
  • Your answer is better to get the size without parsing and I think using --units g gives what user wants.
    – Ozan
    Mar 10, 2017 at 8:01
1

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

awk '/LV Size/ { print $3 }'
2
  • 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, 2012 at 10:34
  • Ah. I'd have done it the way @kev suggested as well.
    – Anil
    Mar 12, 2012 at 14:38
0

You can also ask lvs for the information directly:

lvs vm001 --nosuffix --units g -o size --no-headings

or more specifically,

lvs VGgroup/vm001 --nosuffix --units g -o size --no-headings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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