LVM 101

Courtesy: (The Benefits of Using Linux Logical Volume Manager) and excerpts below (from Linux Magazine).

LVM comes with virtually every distribution of Linux and allows you to abstract various pieces of physical storage into virtual groups that can be carved into chunks that form the basis of file systems (virtual partitions if you like). It also allows you to combine physical partitions into groups, resize these groups (grow or shrink), and manage these groups. To effectively use LVM there are some fundamental concepts in LVM that you need to understand (and master). Figure 1 below illustrates these concepts.

At the bottom are the physical drives – in this case there are two drives, /dev/sdb and /dev/sdc. Each of these drives has two partitions. These two pieces form the “physical” basis of LVM. LVM itself sits above the physical devices and is shown in the light yellow area.

The physical partitions map to Physical Volumes (PV’s). So in Figure 1 the physical volumes are /dev/sdb1, /dev/sdb2, /dev/sdc1, and /dev/sdc2. From the Physical Volumes (PV’s), a Volume Group (VG) is created. A VG can use all or just one of the PV’s. In the example in Figure 1, the four PV’s are used in a single VG, primary_vg (note: the VG name actually ends in “_vg” to make it more noticeable). After creating one or more VG’s (Volume Groups), a Logical Volume (LV) is created. You have to have at least one Logical Volume (LV) per Volume Group (VG). These LV’s are used for creating file systems. In Figure 1, the VG is broken into two LV’s: /dev/primary_vg/home_lv and /dev/primary_vg/data_lv (again note the use of the “_lv” at the end of the name to better indicate a LV). Then on top of these these two PV’s are the file systems. For /dev/primary_vg/home_lv an ext3 file system is created and mounted as /home. For /dev/primary_vg/data_lv an xfs file system is created and mounted as /data.

In a shorter form, Physical Volumes (PV’s) are on the bottom of the stack and are really partitions on a disk (hence the use of the term “physical”). Above the PV’s is one or more Volume Groups (VG). Then you partition the VG into Logical Volumes (LV’s). These LV’s are used for creating file systems. If you like here are the steps or layers to LVM:

Disk Partition -> Physical Volumes (PV) -> Volume Group (VG) -> Logical Volume (LV) -> File System

If you’ve never used LVM before you may ask what have taken a two step process “Partition -> File System” and added three steps or layers. These additional steps or abstractions allow you to manipulate the storage (more on that in a bit).

Perhaps not the best way to think about LVM but one concept that can be useful is to think of the Volume Group (VG) as the “virtual hard drive”. It gathers real partitions (PV’s) into a virtual drive (the VG). Then you partition the “virtual drive” into Logical Volumes (LV’s). You can think of the LV’s as “virtual partitions.” Then you can create file systems or whatever else you need to do with the LV’s as though they are real physical devices.

Some advantages of LVM are:

  • You can “aggregate” physical volumes (PV’s) into a single volume
  • You can add or even remove PV’s from a Volume Group (VG) allowing you to resize it
  • You can resize Logical Volumes (LV’s) if there is enough storage in the underlying VG
  • If you increase an LV, you can possibly increase the size of a file system (if the file system allows this). This can typically be done without the loss of data.

Using the old approach of “Disk Partition -> File System” doesn’t give you this flexibility or control you need over your storage. When you are dealing with a large number of users with a large number of disks in a server, or even having to add storage to your desktop, having control and flexibility over your storage is very, very convenient.

If you read the previous article it walks through an example illustrating how to create PV’s, VG’s, and LV’s, and ultimately a file system. It is important to know the LVM command line commands in a general sense in case you need to use without a GUI (perhaps using only a remote console). Memorizing the various options is debatable (I’ve got a funny story about memorizing the myriad options for esoteric commands) but you should at least have some knowledge of the commands – perhaps even a cheat sheet or a log of the commands you used to arrive at the current configuration.

However, with LVM it is easy to make a mistake and lose data. So having a graphical representation of the steps in LVM can make things a lot easier. When I searched for GUI tools for LVM, the one that quickly rose to the top was system-config-lvm from Redhat.