Thursday, June 7, 2018

Logical Volume Manager LVM

To LVM or not to LVM?

The Linux Volume Manager aka LVM, at first view, is a simple storage management tool. However it differs from other disk management tools (eg:parted,fdisk) because it basically operates in a layer between the actual storage hardware and the filesystem. So the concept now becomes about Volume Management, rather than disk management.

  With LVM you can do magic, you can simply extend any partition by adding one or more hard disks without rebooting, neither interrupting any service on the running operating system. You can add more disk space literally "on the fly". Imagine you're facing a situation where you have a production server, or a VM, running out of space suddenly for some reason (logs,database data, fileserver storage etc). So you need to save the day by keeping the system running, then LVM can do the job. In addition to that LVM gives you the capability to take snapshots of the files which is very useful for backup, and can be used also to create Software RAID on your storage.

All of the above sounds amazing indeed but beware, there are some pitfalls you ought to consider before get involved in this sorcery.

LVM , as we mentioned before, is an extra layer between your physical partition and the actual filesystem. Well that extra layer apparently requires some extra kernel resources, and adds a small performance reduction. Now the tricky part is that LVM increases complexity and that can make the data recovery impossible. For example imagine that you lose a hard disk, mapping two different folders on different partitions on different filesystems. Yes that will cause a great mess.


LVM Anatomy






As we can see from the image below the whole architecture consists out of 3 layers

1. The Physical Volume layer.
 Simply the physical partition with added LVM metadata.

2. The Volume Group Layer.
 The pool of disks ,where they can be allocated to Logical Volume Layer.
 
3. The Logical Volume Layer.
 Here we have the logical partitions. A logical volume is perceived from the Linux operating system as a normal hard disk, but in fact is a virtual disk (not to be confused with Virtual Machine hard drive), which contains one or more hard drives in a lower layer.


LVM hands on

And now, after acquired a basic understanding of the architecture, we're ready to play with the spells (commands) to create LVM magic.

So taking as an example the above diagram lets assume we have a system with 2 hard drives and each disk is formatted in two partitions.

disk1: /dev/sda1 and /dev/sda2
disk2: /dev/sdb1 and /dev/sdb2

Lets start from bottom to top by creating the physical volumes, give :

# pvcreate /dev/sda1 /dev/sda2 /dev/sdb1 /dev/sdb2

You can confirm the new physical volumes by giving:

# pvdisplay for more detailed information


Proceeding to the upper layer enter the command:

# vgcreate volgroup /dev/sda1 /dev/sda2 /dev/sdb1 /dev/sdb2

to create the volume group.
And again to confirm the results enter:

# vgs  to examine the volume group created.

Now there's the main course ,where we create the Logical Volumes, lets assume we have a 500 GB hard disk:

# lvcreate -L 120G -n lvhome volgroup

creates a logical partition 120 GB from the volgroup pool.

# lvcreate -L 380G -n lvstorage volgroup

or

# lvcreate -l 100%FREE -n lvstorage volgroup

which is more accurate because it simply uses all the remaining free space of the volume group to create the logical volume.

again #lvs  to check the newly created logical volumes.


LVM layer is ready, the only thing left now is to create the filesystem on top.

# mkfs.ext4 /dev/volgroup/lvhome

and

# mkfs.ext4 /dev/volgroup/lvstorage

finally we need to mount those volumes to the desirable folders

#mount /dev/volgroup/lvhome /root
#mount /dev/volgroup/lvstorage /storage

and don't forget to put them on fstab to make the mounts work after the reboot.


LVM Magic

As it was mentioned before a very strong advantage of using LVM is to add more disk space without interrupting the system. This can be as follows:
Lest assume we need to add another 500GB (/dev/sdc) hard drive to expand the /storage folder, which is the lvstorage logical partition. 

After creating the partition /dev/sdc1 you need to create also the corresponding physical volume

# pvcreate /dev/sdc1

and then contain the physical volume in the volume group

# vgextend volgroup /dev/sdc1

and continue by expanding the storage volume group

#lvextend /dev/volgroup/lvstorage /dev/sdc1

Finally we need to extend the filesystem of the logical partition to acquire the new additional space

# resize2fs dev/volgroup/lvstorage

give

# df -h to confirm the canges


Now you're a bit wiser to decide whether you need LVM or not and if its finally worth the effort. I'm curious about your opinion on this, until then...

May the source be with you!