Dark Developments Where Knowledge Meets Power

7Nov/120

The Joy of Logical Volumes.

Posted by dijit

Linux logical volumes sound scary, but I can get you going in three simple commands, and the benefits are huge.

I was presenting at a conference recently where I happened to mention the Linux logical volume manager (“LVM”). I was surprised at people’s reactions. Most people that manage Linux have heard of it, but it seems that a lot of folks have never used it. Let me tell you, you’ve got to try this. It’s easy, it’s quick, and it makes life so much easier. You can create and delete partitions (really, “logical volumes”) on the fly, have ones that are larger than your largest physical disk, and grow and shrink them at will. It’s really good stuff.

Three Simple Terms

There are three terms you need to understand, and they’re not very complicated. Here they are:

  • Physical Volume: While this sounds like a physical disk, it’s really just disk storage. It can be a whole disk, but typically it’s just a partition or an MD RAID device.
  • Volume Group: This just combines the space of one or more physical volumes into a single pool of space. I usually just have one of these, but you can have more if you want to maintain separate storage pools (i.e. fast disks vs. slow disks, RAIDed vs. non-RAIDed, etc.).
  • Logical Volume: This is the functional equivalent to a partition, and we use it wherever we would normally use a partition. It’s a bunch of space allocated from a volume group.

Three Simple Commands

I promised three commands to go from nothing to done, so let’s do it.

  1. Create the physical volume: This is a brief step that labels the physical disk storage for use with the logical volume manager. In the simplest case, this is an existing but unused partition. Let’s assume that /dev/sda6 is my unused partition. To turn it into a LVM physical volume, just enter: 
    pvcreate /dev/sda6

     

    Repeat this command for each physical volume you want to create.

  2. Create the volume group: Now that we’ve created our physical volume(s), we need to create a volume group. This creates our pool of disk space. Let’s assume I’m going to call my volume group “workspace”. To create the “workspace” volume group using the physical volume I just created, I use this command: 
    vgcreate workspace /dev/sda6

     

    If I had multiple physical volumes, I would just list them all on the command line. I can also add and remove physical volumes on the fly as my needs change.

  3. Create the logical volume: Now that I have my volume group, I’m ready to create my first logical volume. The logical volume can be any size I want up to the amount of space available in the volume group, even if that space spans multiple physical volumes. Let’s assume that I want my logical volume to be called “userdata”, and I want it to be 30G in size. Here’s my command: 
    lvcreate -n userdata -L 30G workspace

     

    Use another lvcreate command any time you need a new “partition”.

That’s it. I now have a new logical volume called /dev/workspace/userdata that I can use just like a partition. I can format it with any file system I like, mount it, add it to /etc/fstab, use it for swap space, whatever.

So Why Is This Good?

At first glance, it seems we haven’t done much more than we could have done with ordinary partitions. What are the benefits of these extra couple of steps? Well, here are a few:

  1. We didn’t take the system down to create our “userdata” logical volume. We can create as many logical volumes as space allows without downtime. This alone has probably reduced the late evenings I’ve had to work by 90% - and that is a very good thing in my book.
  2. We can delete logical volumes any time we want. That means we can recover wasted space and reallocate it elsewhere without downtime.
  3. We can change the size of logical volumes on the fly. Is /tmp getting too small for our needs? Just grow it with the lvresize. Or shrink /home if you allocated too much space.IMPORTANT CAVEAT: LVM doesn’t know anything about the data inside the logical volume. It’s up to you manage the file system properly. To increase a file system, you can extend the logical volume, but then you still need to use the appropriate file system tool in order for the file system to recognize that its “partition” just got larger. Even more important, when reducing space you need to reduce the file system size first, then reduce the logical volume size. I always make the file system a little smaller than intended, change the volume size, and then grow the file system to fit the available space. This assures that I don’t chop off the end of my file system.

    Reiser can shrink its file systems if they’re unmounted, but most file systems don’t have tools to reduce a file system in-place. In those cases you typically have to create a new, smaller logical volume, copy all the files over, and change the mount points. As always, take backups before doing anything risky, and shrinking a file system is definitely risky.

    None of this is different than it would be if you were managing real partitions. It’s just easier and quicker, and you don’t need to take the system down.

  4. As mentioned before, logical volumes can span physical volumes. You can get really big file systems this way, or put all those old, too-small disks back to work. Need a 10 terabyte file system? Done.
  5. We don’t have to worry about partition locations. With conventional partitions, I might have a partition /dev/sdb5 that is too small, and have available space at the end of the disk that I’d like to allocate to it, but I have /dev/sdb6 in between. That means I have to take the system down, move /dev/sdb6 down to the end of the disk, and then grow /dev/sdb5. With the logical volume manager, available space is available space. You don’t need to worry about where it is.
  6. You can move logical volumes between physical disks. If you suspect a disk is beginning to fail, you can create a new physical volume on a different disk, add it to the volume group, and migrate all the logical volumes off of the old disk while they’re still in use!
  7. Is your backup window too small? The logical volume manager supports snapshots. A snapshot gives you a static copy of your logical volume to backup. Just quiesce your applications if necessary, take your snapshot, and resume normal operations. You can backup the static snapshot at your leisure and delete it when you’re done. Your backup outage just went from six hours to 90 seconds.

Summary

The Linux Logical Volume Manager is easy to set up, easy to use, reduces downtime, and reduces off-hours work. If you’ve got a partition available, I encourage you to try it right now. It’ll take you 10 minutes, and you’ll never go back.

 

[1] http://www.pdxsys.com/articles/lvm/