Managing partitions with LVM

LVM – Logical Volume Manager

LVM is a logical volume manager for Linux that was first written back in 1998. The goal of LVM is to provide an abstraction layer between the filesystem (Ext2/3/4, XFS, ReiserFS, FAT, NTFS…) and the physical disk it lies on. This howto will give you a quick tour of the LVM architecture, as well as the most common use cases for it.

The LVM Architecture

LVM incorporates three types of objects:

  • PV: Physical Volume, an on-disk partition formatted for LVM
  • VG: Volume Group, a group of PVs in a RAID-0/JBOD fashion
  • LV: Logical Volume, a pseudo block device located in a VG, on which you create a filesystem.

The LVM Architecture

Physical Volumes, or PV, are created on top of regular disk partitions (like /dev/sdb2, for example). Then, those PVs are used to make one or more Volume Groups, or VG. A VG is basically a pool of physical volumes merged together, like in a RAID-0 (or “JBOD“) setup. You can add new PVs to a VG later, to make for additional space.

Once your physical volumes are aggregated into a volume group, you can create Logical Volumes, or LV, in the VG. The LV will reserve space in the VG. You can of course create multiple LV in a single VG. Once a LV has been created, it will expose a new virtual block device under /dev/mapper. This is the block device that will be used to create the filesystem (using mkfs), which is then mounted like a regular filesystem.


Initial setup

We will go through the steps required to mount a filesystem based on an LVM volume. This procedure has been performed on a Debian GNU/Linux 9 (stretch) system, but should work on any Linux 3.2+-based system.

0 – Install the LVM tools

If not already done, you need to install the LVM tools.

Debian users:

% sudo apt-get install lvm2

RHEL-based system users:

% sudo yum install lvm2-cluster

1 – Prepare a physical partition

In this case, we have a virtual machine running with two emulated physical disks: one where the system is installed (/dev/vda), and one that we will use for LVM : /dev/vdb.

We initialize the empty disk with a new primary partition that will be used as a LVM Physical Volume (PV).

The kernel re-reads the partition table and populates the new block device /dev/vdb1, which is our new primary partition.

2 – Creating the LVM resources

We initialize it for use by LVM with the pvcreate command:

We can check the attributes of the PV with the pvdisplay command:

Then, we initialize a new Volume Group (VG) with this PV:

We can also check its attributes with the vgdisplay command:

Two shorter commands (pvs and vgs) are also available to view the status of PVs and VGs:

As we can see, all the space is still available in the VG, since no Logical Volume (LV) has been created yet. Let’s fix that and create a 10 GiB volume using the lvcreate command:

Here we first specify the VG on which to create our LV, then its size using the -L option, and finally its name using -n. As previously, we can get quick informations about our volumes using the lvs command:

A new block device has been created: /dev/dm-0. A symbolic link also exists in /dev/mapper for easier reference by the system administrator; the naming convention is <vgname>-<lvname>:

As we can see, in the VG, there’s now only 15 GiB left, since we allocated 10 GiB to the new LV.

All the steps required to create a logical volume are now done, it’s time to format with a filesystem using mkfs.

3 – Creating and mounting the filesystem

Here at Jaguar Network, we are big fans of XFS, a filesystem developed by SGI in 1993. Most Linux kernels, including the Debian’s one, come with XFS support, but you might need to install userland programs to format it:

Next, we create the filesystem on the LV block device:

We can now mount it into any directory:

Let’s check the available space:

All is good! In the next tutorial, we’ll show you how to create additional logical volumes, how to extend volume groups with new physical volumes, and lots of interesting things with LVM.

Managing partitions with LVM
Vote

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.