How to Use Logical Volume Management (LVM)

LVM - Logical Volume Manager - tools used to create and manage logical volumes.

When you attach a new hard drive to your Linux system, you'll likely need to create new particions and volumes. To do that, we're going to work with LVM; a storage layer that came about to solve many of the problems of traditional partitioning which is too rigid by comparison. Being able to quickly create, increase or decrease, and copy a volume is not something available to us with the old particioning method.

Using LVM, we can on-the-fly change volume sizes, merge multiple physical or virtual disks into single usable volume, and create snapshot copies. Sounds real nice. Even better though, is that LVM is pretty much absolutely stable, having gone through multiple iterations coming nearly to the point of perfection. Even though it adds another layer of abstraction, it simplifies common operations without compromising your disk systems' performance.

I would say that using LVM is an easy recommendation to use in modern Linux installations and for any new disk you add to them.

Some might recommend using ZFS, which is a hybrid file system and volume manager.  I prefer applying UnixWay, that a program needs to do one thing and do it well and, to me, LVM fits the criteria. On top of that LVM is compatible with any file system you want - ext3, ext4, xfs, raiserfs - within its volumes, which is something ZFS can't do.

Ok, well... maybe it's not completely perfect. The deficit in write performance after making a snapshot leaves much to be desired, so I advise to only create one temporarily - to make a directory backup for instance - and delete it immediately after. 

Let's go over some basic terminology first, and then look at some examples of what working with LVM is like.

Physical Volume

This is the basis of LVM. A physical volume is any drive space for use by the system. A hard drive, partition, virtual machine, or raid array are all considered physical volumes and can be used with LVM as long as a conflicting file system isn't set up on them.. If a drive already has a traditionally partitioned volume, a LVM Physical Volumes can be allocated next to it.

Logical Volume

Logical volumes is what this whole system is about. Logical volumes can be created by allowing you to allocate any amount of storage space you have to discrete volumes to serve the functions you want. I often set up 3 logical volumes on my servers: //var, and /back. If you have a LAMP server, and you don't know ahead of time whether your files or your database will take up more space, you can assign volumes /var/www and /var/lib/myseql. 

Volume Group

Improves the organization of volumes, by grouping together physical and logical volumes. Pretty simple.

 

So, how can you tell that a system is using LVM? Even if your system has been setup on LVM, there's really not much the system can tell you about itself without installing a lvm2 package. That's easy enough to do:


yum install lvm2

Now you can check your system's physical volumes:


pvdisplay

Or logical volumes:


lvdisplay

And, of course, volume groups:


vgdisplay

 

Let's play around with LVM a bit, shall we. For our first trick, let's take an empty, unpartitioned disk and create a physical volume, a volume group, and a logical volume. 

In this example, "vdb" is a 400gb drive. We'll designate it in its entirety as a physical volume:


pvcreate /dev/vdb

Physical volume "/dev/vdb" successfully created

You can use the command "pvcreate --help" to look through other useful parameters, but generally you won't need them. Just like before, you can use "pvdisplay."

Now let's create a volume group over this physical volume. You can name volume groups, and generally you want to use a name that's functional. I will use a backup group and will name it as such
 


vgcreate backupVG /dev/vdb

Similarly to "pvcreate" you can use "vgcreate --help" to your advantage.

Finally, let's make a logical volume. When making it, you need to specify the size of the volume, its name and the volume group it will belong to.


lvcreate -1 100% free -n backupLV backupVG

Notice how the size is labeled as a % of the total space in the group. We could specify the space in bytes but... well, let's see what happens:


lvcreate -L400G -n backupLV backupVG

Volume group "backupVG" has insufficient free space (102399 extents): 102400 required.

Oh, well of course, not only is our original drive probably not exactly 400 gigs, but LVM itself takes up some small amount of space itself using percent will allocate the correct amount automatically.

Let's allocate an amount much smaller than the maximum.. let's say just 100 gigs:


lvreduce -L100G /dev/backupVG/backupLV

  WARNING: Reducing active logical volume to 100,00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce backupLV? [y/n]:

Let's just hit "y" for our exercise.


Do you really want to reduce backupLV? [y/n]: y
  Size of logical volume backupVG/backupLV changed from 400,00 GiB (102399 extents) to 100,00 GiB (25600 extents).

Nice. We can add another volume next to it at any point:


lvcreate -L100G -n YetAnotherLV /dev/backupVG

Logical volume "YetAnotherLV" created

Great! And just remember you can delete these volumes using "lvremove."

When you manipulate logical volumes that contain actual data, you obviously need to make sure you have a backup saved somewhere else. To do this, you can actually create a volume snapshot and send it to another server through SSH.

Create a logical volume on your remote server of a equal or greater value:


lvcreate -L100G –n VolumeForDump /dev/volumegroup00

Now we make a snapshot on the original server using the "-s" flag:


lvcreate -L100G -s -n SnapshotCopy /dev/backupVG/backupLV

  Logical volume "SnapshotCopy" created

Now just start the dump over SSH:


dd if=/dev/backupVG/SnapshotCopy | ssh backup@192.168.10.15 -p 22 
dd of=/dev/volumegroup00/VolumeForDump

Obviously, the time it'll take depends on the size of the volume.

This about covers the basics of LVM and how to use it. Make sure you test all these examples on empty servers first, before messing with any sensitive data.

In our next article, we'll get into some advanced LVM territory. Keep up with us on Twitter and Facebook so you know when we drop that one.

Until next time!

 

This article was originally published June 2016

July 20 2020

Add or review comments

Please leave your comment

Existing comments

Comments 70


Lbgsesse
yourstabs viagra <a href="http://genqpviag.com/#">buy generic viagra by phone in australia</a> viagra onine [url=http://genqpviag.com/]viagra 100 mg generic[/url] ’
Jdbxsesse
cocaine and viagra <a href="http://llviabest.com/#">generic viagra canadian pharmacy</a> cheep viagra [url=http://llviabest.com/]viagra 300mg[/url] ’
Kbcxhifs
canadian pharmacy <a href="http://xuypharmacyonline.com/">medicine online order</a> canadia online pharmacy
JbnbUript
drugs online <a href="http://online21rxon.com/ ">Viagra Super Active</a> pharmacy northwest canada
Lbsxsesse
prescription without a doctor's prescription <a href="https://ssviagriia.com/ ">online viagra</a> best canadian online pharmacies
Ahkdsesse
the canadian pharmacy <a href="http://storerxpharmcanada.com/ ">cialis super active</a> antibiotics
FvfcExole
viagra for sale philippines <a href="http://hopeviagrin.com/ ">gold viagra x1</a> where can i find generic viagra
NncsExole
viagra dosage amounts <a href="http://lightvigra.com/ ">how can i buy viagra online</a> viagra canada fast shipping
Kuikinend
buy viagra brisbane <a href="http://viagerkr.com/ ">viagra online with american express</a> sildenafil citrate (generic viagra)
AqcfHemyinend
buy cialis at walmart <a href="http://cialisvja.com/ ">cialis no consultation</a> buy cialis online free shipping
Kvaxhifs
fast money loans bad credit <a href="http://rirocash.com/ ">chase bank hard money loan</a> tennessee quick cash flex loans
Labxsesse
cialis compresse 5 mg prezzo <a href="https://jecialisbn.com/ ">metformina e cialis</a> preço caixa cialis
Ahbzsesse
payday loans in temple tx <a href="http://daymoneygo.com/ ">7 year old payday loan</a> payday loans bham
Jbbnsesse
cialis retinopathy <a href="http://ljcialishe.com/ ">cialis no ejaculation</a> mecanismo de accion cialis
NbnhExole
viagra for sale fast shipping <a href="http://vigedon.com/ ">http://vigedon.com/ </a> online pharmacy viagra 100mg
Kbbfinend
viagra no perscription <a href="https://kloviagrli.com/ ">http://kloviagrli.com/ </a> viagra product
AbcfHemyinend
online pharmacy viagra <a href="https://viagriyvik.com/ ">buy viagra online without a prescription with overnight delivery </a> whats better cialis or viagra
Kndnhifs
buying viagra online legal <a href="http://viagratx.com/ ">india pharmacy viagra</a> indian viagra
FjjuExole
viagra generic cost <a href="https://viagraonlinejc.com/ ">buy viagra in mexico </a> generic viagra price
NbmoExole
order generic cialis <a href="https://buycialisxz.com/ ">cialis 20 mg tablets </a> cialis canada generic
Abgcsesse
[url=https://thesiswritingtob.com/]help writing thesis[/url] writing a phd thesis <a href="http://thesiswritingtob.com/ ">write a thesis</a> example thesis
Lmoppsesse
[url=http://essaywriteris.com/]buy essays cheap[/url] buy essay cheap <a href="http://essaywriteris.com/ ">cheap essay buy</a> your essay writer
JvbdUript
[url=https://essaywritera.com/]essay on service[/url] writing essays services <a href="http://essaywritera.com/ ">reviews for essay writing services</a> essay services reviews
Brfghifs
[url=https://ljcialishe.com/]how to get cialis prescription online[/url] [url=https://cialisvja.com/]cialis for bph[/url] [url=https://viagraonlinejc.com/]how much viagra should i take the first time?[/url] [url=https://viagratx.com/]free sample of viagra[/url] [url=https://buycialisxz.com/]cvs cialis over the counter[/url]
GrvUript
[url=https://kloviagrli.com/]viagra melanoma[/url] [url=https://vigedon.com/]viagra boys band[/url] [url=https://llecialisjaw.com/]non prescription cialis[/url] [url=https://jwcialislrt.com/]cialis generico[/url] [url=https://jecialisbn.com/]buy cialis online[/url]
Jvqqsesse
[url=https://dissertationahelp.com/]dissertation paper[/url] best dissertation service <a href="http://dissertationahelp.com/ ">college dissertation</a> help with dissertation writing
Abgcsesse
[url=https://thesiswritingtob.com/]education thesis[/url] buy a thesis <a href="http://thesiswritingtob.com/ ">best thesis writing</a> writing a phd thesis
Fbshsesse
[url=https://thesisacloud.com/]how do i write a thesis[/url] thesis writing assistance <a href="http://thesisacloud.com/ ">editing thesis</a> what is thesis writing
RfvbExole
cialis paypal pay with [url=https://cileve.com/ ]order generic cialis by phone[/url] buy cialis with american express
Fbsfsesse
tadalafil https://uspharmus.com/ Mobic
Lrbssesse
levitra manufacturer https://uslevitraanna.com/ levitra dose
GvdbUript
tadalafil generic vs brand https://boxtadafil.com/ tadalafil tab 5mg
Bbdfhifs
viagra contraindications https://gensitecil.com/ buying viagra online legally
Jbsdsesse
Azulfidine https://onlinecanda21.com/ Accupril
RfvbExole
cialis and douextine http://cileve.com/ cialis (generic)
Fbsfsesse
Tadapox https://uspharmus.com/ canadian pharmaceuticals
Lrbssesse
when to take levitra for best results https://uslevitraanna.com/ levitra doage
Abdgsesse
muscle relaxant http://canadianeve21.com/ walmart pharmacy online
RebfExole
cialis cheap https://rcialisgl.com/ cialis black wirkung
LbsoExole
cialis no perscrption https://ucialisdas.com/ cialis with dapoxetine or viagra with dapoxetine better
Fmrfsesse
cialis without a perscription http://cialisee.com/ buy 36 hour cialis without prescription
Bbshhifs
side effects of viagra http://loxviagra.com/ viagra no prescription
Anoosesse
where to buy viagra in paris https://llviagra.com/ viagra remboursГ©
Jebgsesse
ED Trial Pack [url=https://pharmacylo.com/ ]Biltricide[/url] Brand Viagra
RebfExole
brand cialis 20 http://rcialisgl.com/ cialis supplier uk
LbsoExole
cialis dosage [url=http://ucialisdas.com/ ]how to order cialis[/url] cialis paypal accepted australia
Fmrfsesse
cheap cialis generic http://cialisee.com/ order cialis online pharmacy
Anoosesse
viagra prank dangerous [url=https://llviagra.com/ ]generic viagra pharmacy online[/url] what happens when a boy takes viagra
Lebnsesse
pharmacy tech http://xlnpharmacy.com/ us online pharmacy
Bbshhifs
viagra 100mg price https://loxviagra.com/ viagra overnight delivery usa
Jebgsesse
canada pharmacy not requiring prescription https://pharmacylo.com/ drugs from canada online
RebfExole
cialis paypal http://rcialisgl.com/ cialis pills overnight
LbsoExole
cialis with daproxene http://ucialisdas.com/ generic cialis for daily use
Anoosesse
viagra prima del rapporto https://llviagra.com/ tiagra vs viagra
GtnbUript
what is the active ingredient in viagra [url=http://jokviagra.com/ ]viagra connect walgreens[/url] online viagra prescription
Jebgsesse
best online pharmacy for viagra http://pharmacylo.com/ Methocarbamol
Fmrfsesse
domestic cialis online cheap [url=http://cialisee.com/ ]cialis fastest shipping[/url] cialis 20mg shopping
Bbshhifs
does viagra make you horny https://loxviagra.com/ best time to take viagra
RebfExole
cheap cialis [url=http://rcialisgl.com/ ]cialis shop[/url] i need cialis
Anoosesse
viagra available in kerala https://llviagra.com/ viagra eilbestellung
Lebnsesse
walgreens pharmacy online https://xlnpharmacy.com/ prescription drugs from canada
Exoleloh
europe cialis [url=https://rcialisgl.com/ ]online cialis prescription[/url] how soon does cialis work
ExoleVed
viagra cialis levitra [url=https://krocialis.com/ ]order cialis with dapoxtine[/url] tadalafil 20 mg
sesseDev
list of prescription drugs [url=https://cjepharmacy.com/ ]drugs without prescription[/url] publix pharmacy online
sesseAni
canadian pharmacies for viagra [url=https://pharmacyken.com/ ]most addictive prescription drugs[/url] Voveran
hifsSwa
how much does cialis cost in canada [url=https://ckacialis.com/ ]cialis for sell[/url] cialis without prescription
sesseGtv
canadian pharmacies that are safe [url=https://pharmacyhrn.com/ ]order prescription drugs online[/url] pharmacy course online
sesseHtf
cialis controlled substance [url=https://cialishav.com/ ]cialis canada[/url] 40 mg cialis
sesseAni
cvs pharmacy store near me [url=https://pharmacyken.com/ ]buy viagra canadian pharmacy[/url] can you buy prescription drugs online legally
sesseHtf
cialis without prescription [url=https://cialishav.com/ ]can i take cialis with daxpoteine[/url] cialis free trial 2018