LVM or Logical Volume Manager is a key tool for system administrators managing storage on Linux servers. LVM introduces three layers of abstraction b/n the underlying storage devices on a Linux server and the file system which is accessed by the users.

This example has 3 hard drive devices – sdc1, sdd1 and sde. We will use LVM to aggregate these 3 hard drives to provide storage, extend 3 formatted file system and mount it under a directory called ‘/lvmdata’. This additional abstraction between the underlying storage devices and the user accessible file system enables us to manage storage more effectively and with greater flexibility.
Let’s discuss each LVM layer in sequence and take a look at some specific command used to configure each layer.
The 1st layer is know as LVM Physical Volumes (PV’s). A storage device must be formatted as a Physical Volume (PV) before it can participate in the LVM infrastructure that we will be building.
pvcreate /dev/sdc1 /dev/sdd1
You can check the newly created PV using the command ‘pvdisplay’. This command shows the 2 physcal volumes (sdc1 and sdd1) that we used right now.
Next we need to aggregate the PV into a single contiguous pool of storage known as Volume Group (VG).
vgcreate exampeVG /dev/sdc1 /dev/sdd1
You can check the newly created VG using ‘vgdisplay’ command. VGdisplay tells you the volume group name, how much size the volume has and so on.
Finally, we need to carve out a section of VG that we can then place our file system on. This carved out piece of storage is known as Logical Volume (LV).
lvcreate -n exampleLV -L 1G exampleVG
You can check the newly created LV using the command ‘lvdisplay’.
Now, create a file system, create a mount point (/lvmdata/) and then finally mount the new LVM file system back to the mount point.
mkfs.ext3 /dev/exampleVG/exampleLV
mkdir /lvmdata
mount /dev/exampleVG/exampleLV /lvmdata/
We can now expand our /lvmdata filesystem in real time while all the data remains online and fully accessible.
You can look the current storage utilization using df command:
df -h
You can see the new LVM (/lvmdata/) partition which is of 1 gig in size.
Now to extend the /lvmdata partition, you can use the command ‘lvextend’ as follows:
lvextend -L +500M /dev/exampleVG/exampleLV
Here we are adding additional 500 megs of space to the existing exampleLV logical volume. Note the output of df command now. You will see the additional 500 megs is not reflected on the /lvmdata although we have already added 500 megs to it. This is because we have extended the underlying logical volume, but not the file system itself. The command ‘resize2fs’ will do the trick.
resize2fs /dev/exampleVG/exampleLV
Now, check the ‘df -h’ command and see the difference. You will see the additional 500 megs added to the /lvmdata/ partition.
Remember, all this was done without unmounting the filesystem and all the data remained intact and accessible to users.
Migrating data to a SAN storage device:
Imagine you want to migrate all your data from your servers local hard drive to a SAN (Storage Area Network).
In our case the SAN device is /dev/sde
So, our job is to migrate all of our data from local storage (sdc1 and sdd1) to our SAN device (sde)
Using LVM you can easily accomplish this task. First we need to initialize our SAN storage using pvcreate.
pvcreate /dev/sde
Then we can extend our existing Volume Group (VG) on to our new SAN device using ‘vgextend’.
vgextend exampleVG /dev/sde
Now we need to migrate all the data from our local physical volume to our SAN PVs using the command ‘pvmove’
pvmove /dev/sdc1 /dev/sde
pvmove /dev/sdd1 /dev/sde
Finally, we can remove the local storage devices from our VG using the command ‘vgreduce’
vgreduce exampleVG /dev/sdc1 /dev/sdd1
Again, this was done in real time when the file system data remained online and fully accessible to the users.