Why you should check out LVM

LVM (currently released as LVM2), the "logical volume manager", is a flexible storage manager for the Linux kernel. It allows you to add, remove and resize partitions to suit your needs. Instead of having to predict how your disk space is going to be use when your install a new server, you dedicate a good amount of disk space to LVM and then can make changes to how that storage is allocated when you need.

The easiest way to think of LVM is to picture a set of partitions or disks (one or many). These become your "physical volumes". For a personal system, your physical volumes may comprise just one large partition on your drive. For a critical server at work, they may be a rack of disks, possibly configured as a RAID array. Using LVM, you can concatenate your physical volumes into what LVM calls a "volume group". This volume group represents the disk space that is available to you for carving up into "logical volumes". You might think of these logical volumes as virtual partitions that are created on your overall disk space, but without any particular need to resemble the geometry of those disks.

Your logical volumes are where you will build partitions and create your file systems. Unlike the file systems that we've been using for years, however, these file systems will be resizable. If /home fills up and you have storage space in reserve (a good idea if you have enough disk space), you can make it bigger. If /home fills up and you don't have space in reserve, you can steal it from another partition. You can also create new partitions as you need them or add more physical volumes to increase your overall disk space.

The commands available for working with LVM are named after the various components of LVM. When you see "pv", think "physical volume". Commands for setting up physical volumes might look like these that initialize partitions as physical volumes.

# pvcreate /dev/sda3
# pvcreate /dev/sdb
# pvcreate /dev/sdc

When you see "vg", think "volume group". This is the overall storage that you have to work with. You create your volume group using the vgcreate command.

# vgcreate VolGroup00 /dev/sda3 /dev/sdb1 /dev/sdc1

Once you volume group is set up, you can verify it using the command "vgscan" -- as shown here:

# vgscan
 Reading all physical volumes.  This may take a while...
 Found volume group "VolGroup00" using metadata type lvm2

When you see "lv", think "logical volume". A logical volume is a portion of the overall disk space available in your volume group that you have designated to be treated as a separate volume. Once you have your volume group set up, you can slice off pieces of it to create your logical volumes. In this example, we are creating a 130 GB partition and a 16 GB partition.

# lvcreate -n LogVol00 --size 130G VolGroup00
Logical volume "LogVol00" created
# lvcreate -n LogVol01 --size 16G VolGroup00

You would then use the lvdisplay command to examine your logical volumes.

# lvdisplay
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                NP2Hzj-M895-y524-ExiG-w7Se-loWj-uwB44q
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                130 GB
  Current LE             4175
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                Z3MPGc-4EB8-bFSl-VL3S-huj8-MJ8v-c3FoDK
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                16.00 GB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

Once you get this far, you're ready to build file systems on your logical volumes. At this point, you will be using familiar commands -- like mkfs and mount.

# mkfs.ext4 /dev/ VolGroup00/LogVol00
# mkdir /apps
# mount /dev/ VolGroup00/LogVol00 /apps

Of course, you have to remember to update your /etc/fstab file with your new file system information.

/dev/VolGroup00/apps      /apps      ext4   noatime   0  2

To enlarge a logical volume, you should unmount it and then use the lvextend command. In the example below, we unmount the file system, check it with the e2fsck command, enlarge it with lvextend then check it again.

# umount /apps
# e2fsck -f /dev/VolGroup00/LogVol00
# lvextend -L+70g /dev/ VolGroup00/LogVol00
Extending logical volume homes to 200 GB
Logical volume homes successfully resized
# e2fsck -f /dev/ VolGroup00/LogVol00

We then need to then run the resize2fs commands to resize the file system to use the space we have just added to the logical volume. We then run e2fsck again for good measure.

# resize2fs -p /dev/VolGroup00/LogVol00
# e2fsck -f /dev/VolGroup00/LogVol00

We can then remount our file system and take a break.

The file system should still be in good shape, just roomier!

There are even more features to LVM than I've mentioned here. A good resource for additional information is:


This article is published as part of the IDG Contributor Network. Want to Join?

Computerworld's IT Salary Survey 2017 results
Shop Tech Products at Amazon