LVM (Logical Volume Management) is a very important tool to have in the toolkit of a MySQL DBA. It allows you to create and extend logical volumes on the fly. This allows me to, say, add another disk and extend a partition effortlessly. The other very important feature is the ability to take snapshots, that you can then use for backups. All in all its a must have tool. Hence, this guide will allow you to understand various terminologies associated with LVM, together with setting up LVM volumes and in a later part will also show you how to extend the volumes.

But first up let’s understand various terminologies associated with LVM.

Terminologies

There are three terminologies that you need to be familiar with before you start working with LVM.

  • Physical Volume: Physical Volumes are actual hard-disks or partitions, that you normally mount or unmount on a directory.
  • Volume Group: Volume Group is a group of multiple hard-disks or partitions that act as one. Volume Group is what is actually divided into logical volumes. In layman terms Volume Group is a logical hard-disk, which abstracts the fact that it is built from combining multiple harddisks or partitions.
  • Logical Volume: Logical Volumes are actual partitions created on top of the Volume Group. These are what will be mounted on your system.

The beauty of this whole system is that volume groups and logical volumes can created, resized, destroyed on the fly. Say, for example, you are nearly filled up your hard-disk and you want to extend the storage space by adding another hard-disk, then you can simply extend the volume group to include that hard-disk, and then extend the logical volume as well. Examples of these scenarios will be shown soon.

Now let’s get our hand dirty, setting up LVM groups and volumes.

Creating Volume Group and Logical Volumes

Before we start making any changes let’s have a look at the partition table of our hard-disk.

$ fdisk -l

The output will be similar to something like the following:

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00095aaf

Device      Boot      Start     End     Blocks    Id    System
/dev/sda1             1         63      498688    82    Linux swap / Solaris
/dev/sda2    *        63        125     499712    83    Linux
/dev/sda3             125       611     3906560   83    Linux
/dev/sda4             611       1305    5576428+  83    Linux

Now, lets use the partition /dev/sda4 for setting up the Volume Group.

But before proceeding further you need to have the package lvm2 installed. On debian/ubuntu systems do the following

$ apt-get install lvm2

Now following are the steps to creating a volume group and logical volumes:

Initializing the partition

The first thing to do is to initialize the partition to be able to work with LVM, for that do the following

$ pvcreate /dev/sda4

The output will be similar to the following:

Physical volume "/dev/sda4" successfully created

Creating a Volume Group

Now that we have the partition ready to be used by LVM, lets create a volume group named system

$ vgcreate system /dev/sda4

The output will be similar to the following:

Volume group "system" successfully created

Execute the following command to see the volume group created and its details:

$ vgs

The output will be similar to the following:

  VG     #PV #LV #SN Attr   VSize VFree
  system   1   0   0 wz--n- 5.32g 5.32g

Creating Logical Volumes

Now lets create two logical volumes named logs and mysql, both having sizes of 2GB each.

$ lvcreate -n logs -L 2g system
$ lvcreate -n mysql -L 2g system

Now lets see the details of the logical volumes:

$ lvs

The output will be similar to the following:

  LV    VG     Attr   LSize Origin Snap%  Move Log Copy%  Convert
  logs  system -wi-a- 2.00g
  mysql system -wi-a- 2.00g

Making the logical volumes usable

Now lets make these volumes usable, by creating filesystems and mounting them on appropriate directories.

Let’s first create filesystems.

$ mkfs.ext4 /dev/system/logs
$ mkfs.ext4 /dev/system/mysql

Note that I am creating ext4 filesystems. The other important thing to note in the commands executed is that the path to the volumes are actually:

/dev/volume_group_name/logical_volume_name

Since, we named the volume group system and the logical volumes logs and mysql, hence the above paths.

Now lets mount the logical volumes on appropriate partitions.

$ mkdir /var/logs
$ mkdir /var/lib/mysql
$ mount /dev/system/logs /var/logs/
$ mount /dev/system/mysql /var/lib/mysql/

Now execute the following command to see the logical volumes actually mounted,

$ df -h

The output is going to be something similar to the following,

Filesystem                Size  Used Avail Use% Mounted on
/dev/sda3                 3.7G  695M  2.9G  20% /
none                      117M  216K  117M   1% /dev
none                      122M     0  122M   0% /dev/shm
none                      122M   36K  122M   1% /var/run
none                      122M     0  122M   0% /var/lock
/dev/sda2                 473M   30M  419M   7% /boot
/dev/mapper/system-logs   2.0G   67M  1.9G   4% /var/logs
/dev/mapper/system-mysql  2.0G   67M  1.9G   4% /var/lib/mysql

Although we have successfully mounted the volumes, but we have not yet told the OS to mount the volumes on system startup. So in the final step, its just what we are going to do.

Mounting the volumes on system startups

The final step is now to configure the system to remount the logical volumes on system reboots, for the you will need to edit /etc/fstab

I will use my favourite editor vim,

$ vim /etc/fstab

and append the following entries to the end of the file:


/dev/system/logs /var/logs ext4 noatime 0 2
/dev/system/mysql /var/lib/mysql ext4 noatime 0 2

And we are done setting up LVM. See how easy that was! In the next part of this tutorial we will have a look at how to extend existing Volume Groups and Logical Volumes. See you then!