fbpx LVM on Linux: pvcreate, vgcreate, lvcreate with Examples | Certificação Linux

LVM on Linux: pvcreate, vgcreate, lvcreate and Volume Management

📅 01/01/2019 ⏱ 6 min ✍️ Uira Ribeiro
Linuxcommands
LVM on Linux: pvcreate, vgcreate, lvcreate and Volume Management
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

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

At best it is possible to move the data to the beginning of the disk and repartition it "hot". At worst, a backup is needed, recreate the partitions and restore the backup to the new layout.

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

LVM in Linux is a Disk Manager integrated into the Linux Kernel. It allows:

  • Disks to be swapped without service interruption (hotswap);
  • Resize volumes;
  • Create snapshots of volumes;
  • Create backup of volume snapshots;
  • Create a single volume from multiple disks (similar to RAID 0);
  • Create mirrored volumes on more than one disk (similar to RAID 1).

This way, LVM was created to extend the traditional file system which is seen as a set of physical disks and partitions. Its goal is to allow great flexibility for the administrator in disk management.

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

 Mount Point PartitionSize
/boot/dev/sda1500 Megabytes
 Swap/dev/sda21 Gigabyte
 //dev/sda36 Gigabytes
/home/dev/sda44 Gigabytes

In this example, if the user wants to increase the root partition, they would have to reformat their disk, or even move part of the data to another disk and mount the new partition as a directory of root.

Whereas, if the user uses LVM, they could simply decrease the size of /home and increase root, or even add another disk and increase root, without needing to backup data, format the partition and copy the data back. Notice the same example using volumes:

 Mount Point PartitionSize
/boot/dev/sda1500 Megabytes
 Swap/dev/vg00/swap1 Gigabyte
 //dev/vg00/root6 Gigabytes
/home/dev/vg00/home4 Gigabytes

In large systems with many disks, it is practically unfeasible to manage disks without using LVM.

It is important that you know 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 application of some patches.

Kernel 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 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 a 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 to 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 finally mount it.

Briefly, you need to do the following steps:

  1. If you are going to use only one partition of a disk, you need to create it with fdisk or parted and change its type to 8e (LVM), otherwise, just use the whole disk with pvcreate;
  2. Initialize the physical volumes (partitions or disks) with the pvcreate command;
  3. 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 mount.

Creating LVM partitions

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

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

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

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

Initializing physical volumes

To initialize physical volumes of whole 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 meuvolume /dev/sdb /dev/sdc
  Volume group “meuvolume” successfully created

Activating a volume group

After creating the volume group, you need to activate it with the vgchange command:

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

After a system reboot it is necessary to activate the volume group again. Therefore, it is necessary to include this command in the system boot scripts.

Creating logical volumes

The lvcreate command creates logical volumes. In the example, a 1GB logical volume called logico1 will be created in the meuvolume volume:

# lvcreate -L 1000 -n logico1 meuvolume
Logical volume “logico1” created

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

# lvcreate -L 4000 -n logico1 meuvolume
  Logical volume “logico1” created

Activating the logical volume

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

To ACTIVATE:

# lvchange -a y /dev/meuvolume/logico1

To DEACTIVATE:

# lvchange -a n /dev/meuvolume/logico1

Formatting the logical volume

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

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

After formatting the logical volume, it is necessary to mount it:

# mount /dev/meuvolume/logico1 /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 the volume size with a new disk

First it is necessary to create the physical volume:

# pvcreate /dev/sdd

Assign it to the group:

# vgextend meugrupo /dev/sdd

Unmount the logical volume:

# umount /dev/meuvolume/logico1

Increase the logical volume group:

# lvextend -L +13090M /dev/meuvolume/logico1

Check for errors and repair them:

# e2fsck -f /dev/meuvolume/logico1

Finally, we resize:

# resize2fs /dev/meuvolume/logico1

Now just mount again:

# mount /dev/meuvolume/logico1 /mnt

Learn much more about Linux in our online course. You can enroll 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

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp