12

I'm looking for a way I can get the free space on an LVM physical volume without having to use a calculator.

I know the pvdisplay command will show me the size of a PE size as well as the free PE, and thus by multiplying the PE size by the free PE I know the free space in KB. But I'd like a command which tells me the free space in megabytes, gigabytes, etc.

The output of pvdisplay is:

[root@df02 mysql]# pvdisplay
  /dev/cdrom: open failed: No medium found
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               MMB
  PV Size               29.71 GB / not usable 19.77 MB
  Allocatable           yes 
  PE Size (KByte)       32768
  Total PE              950
  Free PE               221
  Allocated PE          729
  PV UUID               QfZGfn-a3VV-IRkw-bV9g-6iqm-zXjN-y5e6gr

So the free space in this case is 32768 KByte * 221 Free PE = 7241728 KiB, or 6.90625 GiB. But that's a lot of math to do without a calculator ;-)

Is there a command which can give me the free space on an LVM physical volume in megabytes/gigabytes?

Josh
  • 8,449
  • I'm sure a perl master could whip up a oneliner. I'm working on a ruby one but my ruby oneline skills are a bit rusty... – Josh Dec 03 '13 at 17:36
  • The math could be done in Bash as well, though you're probably looking for a switch to one of the 1/2 dozen LVM related tools. – slm Dec 03 '13 at 17:39
  • 1
    @terdon - added some output – slm Dec 03 '13 at 17:45
  • Thanks @sim! I edited it to show my actual output plus the formula, just because my PV actually has ~7 GiB free. – Josh Dec 03 '13 at 17:48

3 Answers3

17

The tool pvs shows the output in whatever units you like.

$ pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sda2  MMB  lvm2 a--  29.69G 6.91G

I noticed this mention in the man page:

--units hHbBsSkKmMgGtTpPeE
       All  sizes  are  output in these units: (h)uman-readable, (b)ytes, 
       (s)ectors, (k)ilobytes, (m)egabytes, (g)igabytes,(t)erabytes, 
       (p)etabytes, (e)xabytes.  Capitalise to use multiples of 1000 (S.I.) 
       instead of 1024.  Can also specify custom units e.g. --units 3M

Example

You can override the units like so:

$ pvs --units m
  PV         VG             Fmt  Attr PSize     PFree
  /dev/sda2  vg_switchboard lvm2 a--  37664.00m    0m
slm
  • 369,824
3

Well, I said I'd give you a one liner so here it is but it really is not very good. @slm's answer is obviously the way to go. Anyway, the one-liner below assumes that the PV size is expressed in KBytes (which is not always the case) and prints GiB by default.

$ pvdisplay | perl -plne '$f=$1 if /Free PE\s*(\d+)/; 
                        $s=$1 if /PE Size.*?(\d+)/; 
                        print "  Free Space\t\t",($s*$f)/1048576," GiB" if /UUID/'
  /dev/cdrom: open failed: No medium found
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               MMB
  PV Size               29.71 GB / not usable 19.77 MB
  Allocatable           yes 
  PE Size (KByte)       32768
  Total PE              950
  Free PE               221
  Allocated PE          729
  Free Space            6.90625 GiB
  PV UUID               QfZGfn-a3VV-IRkw-bV9g-6iqm-zXjN-y5e6gr
terdon
  • 242,166
2

i know i'm a bit late to answer but i think the easiest way is just : (Same options work for vgs also),

$ pvs -o name,free --units g --noheadings

/dev/sda2      0g
/dev/sdb   18.40g
/dev/sdc    5.00g
/dev/sdd   14.00g
walx4r
  • 21