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 setfound
to1
(because we knownLV Size
is following this line) - if
LV Size
andfound!=0
, then print column#3, andexit
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 }'
Author by
Devator
Updated on September 18, 2022Comments
-
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 usinglvdisplay
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 withawk
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 casevm001
). -
Devator about 12 yearsAwesome, 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 casevm001
. Please refer to the table in my original post. -
Devator about 12 yearsThank 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 about 12 yearsThanks for your edit, I now know exactly how it works. Cheers!
-
DNolc about 12 yearsAh. I'd have done it the way @kev suggested as well.
-
DNolc about 12 years@Devator, @kev, just my 2 cents, you might want to protect the
/vm001/
from false-postitives (likevm0012
) with say,/vm001$/
or something. -
Ozan about 7 yearsYour answer is better to get the size without parsing and I think using
--units g
gives what user wants.