Get LVM size using AWK?

6,054

Solution 1

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.

Solution 2

With

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

you get the size of your LV in useful form.

Solution 3

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:
6,054
Devator
Author by

Devator

Updated on September 18, 2022

Comments

  • Devator
    Devator over 1 year

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

  • Devator
    Devator about 12 years
    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
    Devator about 12 years
    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
    Devator about 12 years
    Thanks for your edit, I now know exactly how it works. Cheers!
  • DNolc
    DNolc about 12 years
    Ah. I'd have done it the way @kev suggested as well.
  • DNolc
    DNolc about 12 years
    @Devator, @kev, just my 2 cents, you might want to protect the /vm001/ from false-postitives (like vm0012) with say, /vm001$/ or something.
  • Ozan
    Ozan about 7 years
    Your answer is better to get the size without parsing and I think using --units g gives what user wants.