LVM, Demystified

Step 2: the Volume Group

You don't currently have any volume groups, so create one using the two physical volumes you just made:

vgcreate my_volume_group /dev/sdb /dev/sdc

Hopefully the command is clear. You've created a volume group named my_volume_group using the physical volumes /dev/sdb and /dev/sdc. As with the physical volumes, if you want to check the current state of LVM Volume Groups on your system, type vgdisplay to get a listing:

$ sudo vgdisplay
  --- Volume group ---
  VG Name               my_volume_group
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               20.8 GiB
  PE Size               4.00 MiB
  Total PE              9988
  Alloc PE / Size       0 / 0 GiB
  Free  PE / Size       9988 / 20.8 GiB
  VG UUID               oVYiY6-bQp9-4CVO-QgrN-LGgB-1umR-ebJQo4

As you can see in the output, you've combined the available space of the two physical volumes (10.4GB each) into a total pool of 20.8GB. You could add more drives to the volume group or mix and match entire drives with partitions from other drives. LVM is very flexible. The large pool of available data does no good, however, until you create Logical Volumes to act as your usable disks.

Step 3: Logical Volumes

When you add a hard drive to your system, you don't really get to pick its name. You get /dev/sda, /dev/sdb and so on. When you create logical volumes, however, you decide what you want the devices to be called. You also get to decide how large each "drive" is as you carve it out of the larger volume group. It's good to note here that if you make your logical volumes too small, it's very easy to expand them later, so don't worry too much about planning for long-term potential needs. If you need more space later, you can just add it. To create your logical volumes, type:

$ sudo lvcreate -L 5G -n 5gig my_volume_group
  Logical volume "5gig" created

Then to see what happened behind the scenes, type:

$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/my_volume_group/5gig
  LV Name                5gig
  VG Name                my_volume_group
  LV UUID                3MxOB0-ce5o-yvBD-YORT-52qV-j8HJ-oDru2G
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                5.0 GiB
  Current LE             5753
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

Notice how nice and clean the device-naming system is with LVM. It's important to run the lvdisplay command, however, to make sure you know the mapped device name. Many systems use symbolic linking in an attempt to make the device's virtual locations easier to find, but I think that adds a layer of confusion for folks trying to understand what's really going on.

Look, a New (Virtual) Hard Drive!

Once you've successfully created your logical volumes, it's just a matter of using them as block devices. If you need a filesystem to mount as your /home directory, just do this:

$ sudo mkfs.ext4 /dev/my_volume_group/5gig
$ sudo mount -t ext4 /dev/my_volume_group/5gig /home

And, your /home directory will be a whopping 5GB in size, but fully expandable, thanks to LVM. (Obviously, if you really want to mount your logical volume as your home directory, you should add an entry to /etc/fstab so it mounts on boot.) From the standpoint of your Linux system, however, /dev/my_volume/5gig is a block device similar to any hard drive you might plug in. You can use it as swap, format it like you did above, or even encrypt it and mount it somewhere as an encrypted partition.

That Was a Lot of Work, Why Again?

I know, in this little example, you've done nothing but create a JBOD (Just a Bunch Of Disks) type system, which will completely fail if you lose even one drive. The power of LVM isn't fully realized until down the road when you want to expand your logical volumes without migrating data. Or, when you want to take an LVM snapshot of your drive so you can roll back to an instantaneous backup when an upgrade fails. Or, when you replace a small drive with a fast RAID array and want to migrate the data quietly to your new PV.

The Logical Volume Manager is a system that abstracts storage devices. It does add a layer of complexity to your system, I won't lie, but the trade-off is significant. It may complicate your system a bit more, but it also simplifies your work a great deal when you have to deal with storage in the future.

You Keep Talking about the Future...

Hopefully at this point, you see LVM isn't a complete waste of time. When the time comes, what sorts of advantages will LVM provide? Here's a quick off-the-top-of-my-head list you might want to check out:

  • Move Logical Volumes from old, slow PVs to new, fast PVs, on the fly.

  • Resize Logical Volumes, filling more space in the Volume Group.

  • Stripe data across PVs in a VG for increased performance.

  • Resize Volume Groups by adding or subtracting physical volumes.

  • Take a snapshot of any Logical Volume, which can be restored later.

One of my favorite uses for LVM in production is to take an LVM snapshot before an upgrade. If something goes wrong, I can just revert back to the snapshot. Once you start thinking about all the possibilities LVM offers, you'll wonder why you waited so long!


Shawn Powers is a Linux Journal Associate Editor. You might find him on IRC, Twitter, or training IT pros at CBT Nuggets.