Skip to content

LVM on Linux (volume manager)

The disk partitioning scheme you just saw is useful for dividing the data into an organizational structure that meets and makes some sense. However, once the partition is created, making changes is complicated without involving a possible loss of data.

At best, it is possible to move the data to the beginning of the disk and repartition it “hot”. Worst case scenario, you need a backup, recreate the partitions, and restore the backup to the new layout.

To avoid all this hassle and possible data loss, the staff decided to create a logical volume manager, called LVM - Logical Volume Manager.

LVM on Linux is a Disk Manager integrated with the Linux Kernel. It allows you to:

  • Disks are exchanged without service interruption (hotswap);
  • Change volume size;
  • Create volume images (snapshots);
  • Back up volume images;
  • Create a single volume from multiple disks (similar to RAID 0);
  • Create mirrored volumes on more than one disk (similar to RAID 1).

In this way, LVM was created to expand the file system that is traditionally viewed as a set of physical disks and partitions. Its purpose is to allow great flexibility for the administrator in managing the disks.

Imagine that the user has the following partition scheme without LVM:

**Mount Point** **Partition** **Size**
/boot /dev/sda1 500 Megabytes
Swap /dev/sda2 1 Gigabyte
//dev/sda3 6 Gigabytes
/home /dev/sda4 4 Gigabytes
In this example, if the user wanted to increase the root partition, he would have to reformat his disk, or even move part of the data to another disk and mount the new partition as a root directory.

Whereas, if the user uses LVM, he could simply decrease the size of /home and increase the root, or even add another disk and increase the root, without having to back up the data, format the partition, and copy the data back. Look at the same example using volumes:

**Mount Point** **Partition** **Size**
/boot /dev/sda1 500 Megabytes
Swap /dev/vg00/swap 1 Gigabyte
//dev/vg00/root 6 Gigabytes
/home /dev/vg00/home 4 Gigabytes
In large systems with many disks, it is almost impossible to manage the disks without the use of LVM.

It’s important that you know that there are two versions of LVM: 1 and 2. Version 2 is supported by Kernel 2.6 and 3.x, and by Kernel 2.4 with the application of some patches.

Kernels versions 4.x and 5.x already support LVM version 2.

The only partition that cannot be used with the volume manager is /boot.

LVM terminology

Before showing the LVM commands, you need to understand the terminology that LVM uses:

  • Physical Volume: It is a disk or some hardware that behaves like a disk (such as storage that uses RAID);
  • Volume Group: It is an abstraction of LVM that brings together logical volumes and physical volumes in the same administrative unit;
  • Logical Volume: It is the equivalent of a partition in a non-LVM system.

Step by step to work with LVM

To work with LVM, you need to initialize the disks for LVM, then create at least one Volume group, create at least one logical volume in the group you just created, format the volume with the desired file system, and then mount it.

Briefly, it is necessary to take the following steps:

  1. If you are going to use only one disk partition, you must create it with fdisk or parted and change its type to 8e (LVM), otherwise, simply use the entire disk with pvcreate;
  2. Initialize physical volumes (partitions or disks) with the command
  3. pvcreate; Create a volume group with the vgcreate command;
  4. Activate a volume group with the vgchange command;
  5. Create a logical volume with the lvcreate command;
  6. Format the logical volume with the desired file system with mkfs .
  7. Mount the logical volume with the mount.

Creating LVM partitions

If you don’t want to use the entire disk as an LVM, you can use fdisk to create one or more LVM-like partitions. To create an LVM partition with fdisk, you create the partition normally and change the partition type to 8e.

For example, let’s imagine that the system was installed on the /dev/sda disk.

And for LVM, two more disks will be used: /dev/sdb and /dev/sdc that are not partitioned.

Before adding a disk or partition as a physical LVM volume, you must initialize it with the pvcreate command.

Initializing physical volumes

To initialize physical volumes from entire disks, the command is: pvcreate and the full path of the partition or disk:

# pvcreate /dev/sdb
Physical volume “/dev/sdb” successfully created
# pvcreate /dev/sdc
Physical volume “/dev/sdc” successfully created

Creating a volume group

After initializing the disks, you need to create a volume group with the disks with the vgcreate command:

# vgcreate my volume /dev/sdb /dev/sdc
Volume group “myvolume” successfully created

Activating a volume group

After creating the volume group, it is necessary to activate it with the vgchange command:

# vgchange -a and myvolume
0 logical volume (s) in volume group “myvolume” now active

After the system is rebooted it is necessary to activate the volume group again. Therefore, it is necessary to include this command in the system load scripts.

Creating logical volumes

The lvcreate command creates logical volumes. In the example, a 1GB logical volume called logic1 will be created on the volume myvolume:

# lvcreate -L 1000 -n logic1 my volume
Logical volume 'logic1' created

Since the /dev/sdb and /dev/sdc disks each have 2GB in our example, it is possible to create up to 4 volumes of 1GB each, or a single logical volume of 4GB, as in the example below:

# lvcreate -L 4000 -n logic1 my volume
Logical volume 'logic1' created

Activating logical volume

The lvchange command activates/deactivates the logical volume for use:

To ACTIVATE:

# lvchange -a and /dev/myvolume/logic1

To DISABLE:

# lvchange -a n /dev/myvolume/logic1

Formatting the logical volume

Any file system can be used to format the logical volume:

# mkfs.ext4 /dev/myvolume/logic1
mke2fs 1.41.14 (22-Dec-2010)
Filesystem label=
OS type: Linux
(...)

Once you have formatted the logical volume, you need to mount it:

# mount /dev/myvolume/logic1 /mnt

After these steps, the logical volume will be ready for use.

You can also use LVM to increase or decrease the size of a volume.

Increasing volume size with a new disk

First you need to create the physical volume:

# pvcreate /dev/sdd

Assign it to the group:

# vgextend my /dev/sdd group

Disassemble the logical volume:

# umount /dev/myvolume/logic1

Increase the logical volume group:

# lvextend -L +13090M /dev/myvolume/logic1

Search for error and repair:

# e2fsck -f /dev/myvolume/logic1

Finally, we resized:

# resize2fs /dev/myvolume/logic1

Now just put it back together:

# mount /dev/myvolume/logic1 /mnt

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share