Tuesday, March 19, 2013

LVM to the rescue


Recently I was reminded of the magic of LVM (Logical Volume Manager) when a friend called to say that a virtual server I had built for him on Citrix XenServer was running out of disk space. The server was built with Debian GNU/Linux 6.0 and luckily I elected to use LVM partitioning during the install.

There is surprisingly little written about the history of LVM. The concept of Logical Volumes to manage storage has been around a long time and existed in Unix systems under various names. LVM version 1 became part of the Linux kernel in January 2001 with the release of version 2.4. The 2.6 kernel integrated LVM2 and this became the default for many distros. It replaced version 1 in the 2005 release of Red Hat Enterprise Linux RHEL 4.

While championed by Red Hat in the early days, most distributions now offer an option to select LVM during installation and you would be wise to do so. Old concerns over I/O latency have pretty much been overcome by faster hard drives and modern file systems.

Let's get back to the situation I spoke of above. I was asked to create a VM to replace an aging physical machine. The original server had a 100GB drive and was using about 30 percent of this so I figured a 100GB disk image in XenServer would be adequate. Of course the usage pattern changed and this quickly became too small for the server usage.

In the days before LVM there would have been a couple of ways to deal with this. If the disk usage was for a none root directory such as /var that could live on its own parition we could easily add another drive and mount it at /var. We would of course have to copy all the stuff already in /var over to the new partition and if these were active log files this would mean stopping services while we do this. If the root partition itself was taking up the space we could grow the disk image in XenServer but would then have to use something like PartitionMagic to grow the file system. This would result in significant down time.

With LVM things become much more simple. On a physical machine we could simply add another drive and then let perform the LVM magic illustrated below to make use of it within the existing disk configuration. On a VM this gets even better because we can add a disk image without physical access to the server.

I won't go into adding a disk image in XenServer as it is simple enough through the Windows app XenCenter. In my case when I added an additional 200GB image it showed up as /dev/xvdb. I could then log into the server and begin integrating this into LVM.

The first thing is to create a partition on the new drive:

$> sudo fdisk /dev/xvdb

Next we make a file system:

$> sudo mkfs -t ext3 /dev/xvdb1

We get the usual warning about this new filesystem being checked after 33 reboots and I don't want that so I use tune2fs to fix this:

$> sudo tune2fs -c 0 /dev/xvdb1

Next we create the physical volume:

$> sudo pvcreate /dev/xvdb1

Before we go any further we want to our current disk layout.

$> sudo lvdisplay

$> sudo lvdisplay 
  --- Logical volume ---
  LV Name                /dev/www6/root
  VG Name                www6
  LV UUID                2lhl2U-h1L7-jgeO-2weG-LVQJ-EZvF-fIFLUw
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                285.67 GiB
  Current LE             73132
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0
   
  --- Logical volume ---
  LV Name                /dev/www6/swap_1
  VG Name                www6
  LV UUID                gqWkKS-q9t8-MxEK-iFOu-zBp5-E9Qp-ulwBid
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                4.09 GiB
  Current LE             1046
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

So now we need to expand the size of the volume group to accomdate the new disk:

$> sudo vgextend www6 /dev/xvdb1

Now we expand the Logical Volume:

$> sudo lvextend -L+190G /dev/www6/root

In Debian we use resize2fs to make the file system fit the volume:

$> sudo resize2fs /dev/www6/root

To see how cool LVM is, open up another shell and run df to see the file system growing live without interrupting running services on the system. All this with zero downtime!