Setting up an LVM filesystem is quite easy assuming you have the right tools installed and a recent kernel. LVM has a lot of advantages, most notably the ability to take snapshots of the current filesystem – this is why LVM is often used in live database environments.

Assuming a Debian Lenny machine, get the relevant packages. Some may already be installed:  apt-get install lvm2 dmsetup mdadm

In this example, we will assuming that /dev/sda is your boot drive, and that you want to leave it out of your LVM array, but include /dev/sdb and /dev/sdc. Both /dev/sdb and /dev/sdc should be of equal sizes.

Firstly, using fdisk, remove any existing partitions with ‘d’, on /dev/sdb and /dev/sdc, and create one new partition to span the drive. Change the partition type to ‘8e’ which is the LVM type.

Now prepare your physical disk for LVM with the ‘pvcreate’ tool:

pvcreate /dev/sdb1 /dev/sdc1

Note that you can reverse this with pvremove. You can also use pvdisplay now to display information on all physical volumes.

Oh – you do realie that you can use /dev/mdX just as easily to create LVM on your RAID devices?

Now, we need to create a ‘volume group’: vgcreate myvg /dev/sdb1 /dev/sdc1

We can also use vgdestroy just the same as pvdestroy to remove the volume group. At this point, we can also use vgscan to scan for volume groups, and vgrename to rename a volume group. vgdisplay will display information about known volume groups, again, just the same as pvdisplay.

Now we have a volume group on top of our physical volume, we need to create a logical volume on top of the volume group.

lvcreate –name mylv1 –size 10G myvg willcreate a 10Gb sized logical volume called ‘mylv1’ on side the ‘myvg’ volume group. Again, same as with the above, we can also use lvdestroy, lvscan, lvrename and lvdisplay

To extend the size of the logical volume, we can use lvextend i.e. lvextend -L20G /dev/myvg/mylv1 and then back again with lvreduce i.e. lvreduce -L10G /dev/myvg/mylv1

*** Do take appropriate backups before editing and/or resizing your partitions. ***

Now, we can create a filesystem if we wanted to. We can use mkfs.ext3 /dev/myvg/mylv1 to do this, in the same way you would create any filesystem.

Now this is done, you can resize the partition as you would any other partition. If you wish to make it larger, unmount the partition, use lvexpand to increase the ‘physical’ size, then use resize2fs to increate the ext3 partition size. If you wish to reduce it, unmount it, use resize2fs to decrease the ext3 partition size and then lvreduce to reduce the physical space. Be very careful with your maths or you’ll suffer disasterous results.

Now, assume that you’ve just added a hard disk (/dev/sdd) to your machine and wish to add it to our array. After preparing the partition table for LVM, we need to create a physical volume under LVM and then extend our volume group to this disk – vgcreate /dev/sdd1; vgextend myvg /dev/sdd1. To remove a disk, first move the data from it to another, pvmove /dev/sde1 /dev/sdd1; vgreduce myvg /dev/sde1

Now the bit we’ve all been waiting for – how to take a snapshot.

First, you’ll need to create a logical volume the same size as the volume you wish to snapshot. We’ve created a logical volume earlier: lvcreate –name mylv1 –size 10G myvg willcreate a 10Gb

Now, to create a snapshot, we’ll use: lvcreate -L10G -s -n mysnapshot /dev/myvg/mvlv1

lvdisplay will confirm the snapshot has been created. Now, you can mount your /dev/myvg/mysnapshot as you would have done /dev/myvg/mylv1. You can back it up with ‘tar’ or rsync once mounted, or even just dd it and leave it unmounted. It’s a regular ‘block device’ for you to use. snapshots will be created ‘pseudo-instantly’ for you. If you’re using virtual machines, you may notice a slight pause.