Learn How to Configure LVM in Linux Step by Step

LVM (Logical Volume Management) is a modern storage device management system. It allows us to create flexible partitions. We can resize these partitions at any time as per our requirements. It uses a simple concept to provide a flexible partitioning scheme. It creates a pool of all storage units we allocate. This pool works as a logical drive. We make partitions on this drive. These partitions are called logical volumes. We can adjust their size as required.

LVM structure/layers

LVM works in three layers: Physical Volumes (PV), Volume Groups (VG), and Logical Volumes (LV).

Physical Volumes

Physical Volumes are the actual disk drives or partitions that LVM can use. These are the underlying physical storage units.

Volume Groups

Volume Groups are the pools of storage created by combining multiple physical volumes. We use these to make logical volumes.

Logical Volumes

Logical Volumes are the virtual partitions created from the volume groups. These are similar to the standard disk partitions.

LAB Setup for practice

I used a virtual system with three additional hard disks to demonstrate and explain all the exercises in this tutorial. You can check previous parts of this tutorial series to learn more about this setup.

lab setup

Listing disk drive configurations

The lsblk command lists all attached hard disks and their partitions.

the lsblk command

As the above output shows, this system has four hard disks: sda, sdb, sdc, and nvme0n1. The sda, sdb, and sdc are the virtual disks I attached for this tutorial. The nvme0n1 disk contains the operating system. I will not touch this disk to mitigate any chance of data loss or corrupting the operating system. You can follow a similar approach on your system to practice the LVM configuration without messing up the existing system.

LVM configuration

LVM configuration starts with the creation of physical volumes. We can use any storage unit to create physical volumes, such as a partition or a disk. However, to use a partition as the physical volume, we must change its type to LVM. On this system, we have three disks for practice. We will use the first disk as it is. We will create partitions for LVM on the second and third disks.

MBR and GPT

MBR and GPT are the methods to save partition-related information. A BIOS-based system uses MBR, while a UEFI-based system uses GPT. MBR is a classical approach. It has several limitations. For example, it allows us to create a maximum of fourteen partitions. It also limits the partition size. It supports a maximum of 2 TB partition size. GPT removes these limitations. It allows us to create a maximum of 128 partitions. Each partition can span up to 18 exabytes. LVM supports both partition types. You can check the following tutorials to learn more about these partition types.

Master Boot Record (MBR) Explained
GUID Partition Table (GPT) Explained

Creating LVM partitions

Linux offers three native tools for disk partitioning: fdisk, gdisk, and parted. The fdisk creates only MBR partitions. The gdisk makes only GPT partitions. The parted creates both partition types. We can use any one of these to create LVM partitions. We will use the fdisk and parted to create LVM partitions on the second disk. We will use the gdisk to create LVM partitions on the third disk.

Using the fdisk to create an LVM partition

The fdisk requires the disk path as an argument. Start it by specifying the second disk's path.

#fdisk /dev/sdb
  • On the fdisk command prompt, use the n command to create a new partition.
  • Keep default or type p to create a primary partition.
  • Keep the default starting point.
  • Type +1G to create a 1GB partition.
  • Use the t command to change the partition type.
  • Select the newly created partition.
  • Use the l command to list the hexacode of all partition types.
  • The hexacode of LVM is 8E. Type 8E to set the partition type to LVM.
  • Use the p command to print and verify the new partition.
  • Use the w command to save the change.
the fdisk command

You can check the previous tutorials in this series to learn more about the fdisk command.

Using the parted to create an LVM partition

Start the parted by specifying the second disk's path.

#parted /dev/sdb
  • Use the print command to list existing partitions.
  • Use the mkpart command to create a new partition.
  • Type primary to create the primary partition.
  • Keep the default file system.
  • Start the new partition's size from where the first partition's size ends.
  • Specify the desired partition size.
  • Use the set command to change the partition type.
  • Specify the partition number of the new partition.
  • Type lvm to change the partition type to LVM.
  • Change state to on.
  • Use the print command to verify the new partition and its type.
  • Use the quit command to exit the parted.

the parted command

Using the gdisk to create an LVM partition

We have created two MBR partitions using the fdisk and parted commands. Let us create a GPT partition. Start the gdisk by specifying the third disk's path.

#gdisk /dev/sdc
  • Use the n command to create a new partition.
  • Keep the default partition number.
  • Use the default first and last sectors to use all available space for this partition.
  • Set the partition type to LVM. The hexacode of LVM is 8e00. You can use the l command to list all hexacodes.
  • Use the p command to print and verify the new partition.
  • Use the w command to save the partition information.

the gdisk command

Verifying standard partitions

Use the lsblk command to verify the partitions.

the lsblk command

Underlying for LVM

We will use the following underlying for LVM practice.

TypePathDescription
Disk/dev/sdaIt is a blank disk. We will use it as it is.
Partition/dev/sdb1It is a primary partition on the MBR disk with the LVM flag.
Partition/dev/sdb2It is another primary partition on the MBR disk with the LVM flag.
Partition/dev/sdc1It is a GPT partition on the GPT disk with the LVM flag.

Creating / Listing physical volumes

The pvcreate command creates a physical volume. It requires the path of the device unit we want to use to add to the physical volume. We can specify multiple underlying using a space separator. The v option displays real-time updates. The following command creates a physical volume using the underlying listed in the above table.

#pvcreate -v /dev/sda /dev/sdb1  /dev/sdb2  /dev/sdc1

the pvcreate command

The pvdisplay command lists all physical volumes and their information.

#pvdisplay

the pvdisplay command

To view information about a specific storage unit, specify its path as the argument with the pvdisplay command. For example, the following command displays information only about the /dev/sda disk.

#pvdisplay /dev/sda

the pvdisplay command

Creating / Managing volume groups

Volume groups are the second layer of LVM. We use physical volumes to create volume groups. We can add physical volumes to a volume group as per requirement. For example, we can keep only one physical volume in a volume group or all physical volumes in a single volume group. Volume groups are flexible. We can add or remove physical volumes to them anytime.

The vgcreate command creates a volume group. It accepts the path of physical volumes as arguments we want to add. The following command creates a volume group named vg00 and adds physical volumes: /dev/sdb1, /dev/sdb2, and /dev/sdc1.

#vgcreate vg00 /dev/sdb1 /dev/sdb2  /dev/sdc1

the vgcreate command

The vgdisplay command lists all volume groups.

#vgdisplay

the vgdisplay command

By default, it lists all volume groups. To view information about a particular volume group, specify its name as an argument. For example, the following command lists information about the volume group vg00.

#vgdisplay vg00

Modifying / Extending volume groups

The vgextend command extends a volume group. It allows us to add a new physical volume to an existing volume group. For example, the following command adds the physical volume /dev/sda to the volume group vg00.

#vgextend vg00 /dev/sda

the vgextend command

We can use the vgdisplay command to verify the change.

the vgdisplay command

Creating logical volumes

Logical volumes are the last and topmost layer of LVM. We create logical volumes in the volume groups. Logical volumes provide striping, mirroring, and contiguous allocation features.

The lvcreate command creates a logical volume. It uses the following syntax.

#lvcreate --name [name of logical volume] --size [size] [volume group name]

The following command creates a logical volume named lv00 of 1GB in the volume group vg00.

#lvcreate --name lv00 --size 1G vg00

The lvdisplay command lists all logical volumes. To view information about a particular logical volume, specify its path. For example, the following command displays information about the logical volume created above.

#lvdisplay vg00/lv00

the lvcreate command

Formatting and mounting logical volumes

Logical volumes work similarly to standard partitions. Like standard partitions, they also need a file system to save data. The mkfs command creates a file system. The following command creates an xfs file system on the logical volume lv00.

#mkfs.xfs /dev/vg00/lv00

The above command requires the full path of the logical volume. We can use the lvdisplay command to view the full path. After formatting the logical volume, we mount it to the Linux file system. Create a directory. We will use it as the mount point.

#mkdir /lvdata

the mkfs command

Linux uses the /etc/fstab file to save information about partitions and their properties. We need to add an entry for it in this file. Take a backup of the current file.

#cp /etc/fstab /etc/fstab.backup

back up of the fstab file

A fstab entry has the following six fields.

Device name Device's name, full path, or UUID. It is the partition or device we want to mount to the Linux file system.
Mount point The directory where we want to mount the partition.
File system The file system is on the partition.
Options Control options.
Dump support Use value 0 to disable and 1 to enable this feature.
File system check at boot time Use value 0 to disable and 1 to enable this feature.

The following entry mounts our logical volume to the Linux file system.

/dev/vg00/lv00	/lvdata	xfs		default 	0	0

Add the above entry to the /etc/fstab file and save the file.

the fstab file entry

Restart the system and use the lsblk command to check and verify the partition.

the lsblk command

Expanding / Resizing the logical volume

Expanding or resizing a logical volume is a two-step process. First, we resize the logical volume itself, and second, we resize the filesystem. The lvextend command with the --resizefs option performs both operations simultaneously. For example, the following command expands the logical volume lv00 and updates the file system to add the expanded space.

#lvextend --size +2G --resizefs /dev/vg00/lv00
resize a lvm partition

Removing logical volume, volume group and physical volume

The lvremove command removes a logical volume. We cannot remove a mounted logical volume. If we remove a mounted partition, it returns the following error.

The logical volume contains a file system in use.

To remove a logical volume, unmount it first. After unmounting it, use the lvremove command to remove it.

#lvremove /dev/vg00/lv00

the lvremove command

Removing a volume group

The vgremove command removes a volume group. We cannot remove an active volume group. To remove it, we first have to deactivate it. The vgchange command changes the volume group state. The following command deactivates the volume group vg00.

#vgchange -a n /dev/vg00

After deactivating the volume group, we can use the vgremove command to remove it.

#vgremove /dev/vg00

the vgremove command

Removing physical volumes

The pvremove command removes a physical volume. The following command removes all the physical volumes we created in this exercise.

#pvremove /dev/sda /dev/sdb1 /dev/sdb2 /dev/sdc

the pvremove command

After removing the physical volumes, restart the system and use the lsblk, lvdisplay, vgdisplay, and pvdisplay commands to verify the remove operation.

the lsblk command

LVM commands cheat sheet
Command Description
lvm Start an interactive shell for executing LVM commands
lvmdiskscan Scan for LVM physical partitions on all disks
lvdisplay Display information about all or specified logical volumes
lvcreate Create a logical volume
lvrename Rename a logical volume
lvchange Modify a logical volume
lvextend Extend the size of a logical volume
lvreduce Reduce the size of a logical volume
lvremove Remove logical volumes
lvresize Change the size of a logical volume
lvscan Scan for logical volumes
lvs List logical volumes with detailed information
pvdisplay Display detailed information about physical volumes
pvchange Modify a physical volume
pvcreate Create a physical volume
pvmove Move data of a physical volume to another
pvremove Delete physical volumes
pvs List physical volumes with detailed information
pvresize Resize a physical volume
pvscan Scan for physical volumes
vgdisplay Display detailed information about volume groups
vgexport Activate a volume group
vgmerge Combine volume groups
vgreduce Remove physical volumes from a volume group
vgremove Remove a volume group
vgs List volume groups with detailed information
vgslit Split a volume group
vgscan Scan for volume groups
vgck Check volume groups
vgrename Rename a volume group
vgcfgbackup Backup volume group configuration
vgcfgrestore Restore volume group configuration

Traditional/standard partitions V/s LVM partitions

Unlike LVM partitions, traditional disk partitions use a fixed disk size. We cannot modify their size. For example, we have two partitions: one runs out of space while another has plenty of free space. If these are the traditional disk partitions, we cannot reallocate free space from the partition to the first partition. However, if these are LVM partitions, we can dynamically reallocate space from the greedy partition to the needy partition.

Advantages of LVM

LVM has the following advantages.

  • It creates flexible disk partitions. These partitions are called logical volumes. We can resize them as per our requirements.
  • It allows us to create backups or recover states of logical volumes. These backups are called snapshots. We can use snapshots to restore the logical volumes.
  • It allows us to relocate logical volumes to different physical devices.
  • It allows us to replace on-line drives without interrupting service.
  • It allows us to incorporate mirroring or striping in our logical volumes.

Disadvantages of LVM

LVM has the following disadvantages.

  • It is complex. It requires a lot of setup to work.
  • Managing and maintaining LVM is also challenging.
  • It requires system administration skills.

Conclusion

LVM (Logical Volume Management) is a powerful and flexible solution for managing disk storage, offering numerous advantages over traditional partitioning methods. Its ability to dynamically resize partitions, create snapshots for backups, and rearrange logical volumes makes it an ideal choice for environments requiring adaptability and efficient resource allocation.

ComputerNetworkingNotes Linux Tutorials Learn How to Configure LVM in Linux Step by Step

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com