Saturday 22 April 2017

Intro To 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.

No comments:

Post a Comment