Logical Volume Management basics

This post is over 3 years old, so please keep in mind that some of its content might not be relevant anymore.

I've explained how to activate a LVM Logical Volume before but never talked too much about LVM itself. Here is a brief introduction.

I've been playing with LVM for about a couple of years now and, I must say, I've found it very practical, even for managing "simple home desktops/servers".

The main feature I use is the resize online but it's useful for other things too (LVM features).
Resizing online an LVM partition means you can extend and/or shrink a logical partition without turning the computer off.
To extend, you don't even need to un-mount the partition (unlike shrink).
So, since managing the partition is so easy, I normally create little partitions and enlarge them little by little every time I need more space.
In case one gets too big and I don't need that much space anymore, I simply need to un-mount it, shrink it and re-mount it.
If one day I decide I need a new partition, it can all be done while the computer is operating normally...

Now, let's see how to implement a simple LVM partition (to do this, you need to have at least one partition with no data, otherwise it will get lost).
First we need to install LVM. In Ubuntu it's as easy as:

apt-get install lvm2

To make LVM to recognise a partition, we need to create a LVM Physical Volume (PV) which creates the abstraction between the real partition and the logical one.

pvcreate /dev/sdxy

(eg: "/dev/sdxy" could be "/dev/sdb1", the first partition of the disk "b").
We can check the metadata with:

pvs

or

pvdisplay

for more details.

It's now time to create the first Volume Group (VG) with:

vgcreate group_name /dev/sdxy

I've created it with only one partition but it can be done with as many as you want, even from different hard drives (that's why LVM is so cool).
VGs can be seen as the "logical hard drives".

NB: If you join two partitions from two different hard drives you increase the risk of losing your data. If one hard drive fails, all data on the Volume Group will be unreadable.
To overcome this problem I can build everything on top of mdadm (Linux software raid).
As we have done for the PVs, we can check the metadata for the VGs with:

vgs

or

vgdisplay

for more details.

Once you have the VG up and running, it is now time to create the real Logical Volume (LV) with:

lvcreate -L 10G -n volume_name group_name

This will create a LV 10GB big named "volume_name" inside of the VG "group_name".
LVs can be seen as our "logical partitions".
Once again, you can check the metadata for LVs with:

lvs

or

lvdisplay

for more details.

From now on, this newly created LV, will be our partition and will be treated as any other (logical or not).
In fact, to format it (for instance to ext4), the procedure is always the same:

mkfs.ext4 -m 0 /dev/group_name/volume_name

(the option "-m" is only to avoid blocks reservation since it's not needed on a partition for data only).
If you want to find out some more commands here you find a very nice LVM tutorial I've consulted before.
Enjoy!

Leave a Reply

Your email address will not be published.