Back

How to work with LVM

Introduction

LVM (logical volume manager) is a special Linux OS subsystem that provides additional options for managing partitions on hard disks. Unlike the classical scheme, when partitions that are used for file systems are created on every disk, LVM allows for combining the space on different disks into a common logical space, which can then be used to create or expand partitions. Thus, with help of LVM, one can place one partition on multiple disks or place multiple partitions on one disk.

In this manual, we will consider the simplest example of basic LVM setup and use. As an example, we will use a system where there are 3 disks of 8 GB – /dev/sdc, /dev/sdd and /dev/sde. The space on those disks will be used to create two 10 GB partitions that will be formatted to the ext4 file system. In addition, we will consider the process of increasing the existing logical volume and its file system by adding disks.

In this manual, we use the commands the misuse of which can easily lead to complete loss of all data. Therefore, prior to performing each of them, one should fully understand the meaning of all the actions performed. In case of lack of understanding, please refer to the documentation for the relevant command.

Installation

All necessary packages are already installed by default on most Linux operating systems.

Configuration steps

Generally, LVM configuration process can be divided into the following steps:

  • Allocating disk space (creating partitions) for the LVM subsystem and its formatting. In LVM terminology, these partitions are referred to as physical volumes (PV).

  • Combining previously allocated space into one logical space by adding physical volumes to a volume group (VG).

  • Using available space in the group to create or expand logical volumes (LV).

  • Formatting and mounting logical volumes using standard file systems.

When using LVM, consider the following features and limitations:

  • Do not place the "/ boot" partition on LVM.

  • Each physical volume can exist only in a single volume group.

  • Each logical volume can use space from a single volume group.

  • A volume group can consist of one or more physical volumes.

  • One group of volumes is sufficient for full operation, however, several of them can be created for one’s convenience.

  • Logical volumes are available as block devices with the names / dev / <VGName> / <LVName>, where <VGName> and <LVName>. Those are the names of the volume group and logical volume.

  • The names of utilities for working with physical volumes begin with pv (pvcreate, pvchange, etc.), with volume groups – with vg (vgcreate, vgchange, etc.), with logical volumes – with lv (lvcreate, lvchange, etc.).

  • Configuration commands are to be run as root or by using sudo.

Creating physical volumes

To allow the LVM subsystem to use disk space, it must be partitioned and formatted in a special way. For this purpose, "Linux LVM" type partitions (type 8e in MBR, or 8e00 in GPT) are created on the disks, which are then formatted using the pvcreate command. If one wants to use the entire disk, one can create a single partition that will occupy the entire space.

Let’s set up the disk/dev/sdc to use as a physical volume.

Before performing the partitioning, make sure that there are no partitions on the disk:

# parted /dev/sdc print
Error: /dev/sdc: unrecognised disk label

Let’s create a MBR type partition table and a type 8e (Linux LVM) partition that occupies the entire free space:

# parted /dev/sdc mklabel msdos
# parted /dev/sdc mkpart primary ext2 0% 100%
# parted /dev/sdc set 1 lvm on

Check the partition table again:

# parted /dev/sdc print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type     File system  Flags
1      1049kB  8590MB  8589MB  primary               lvm

Let’s re-read the partition table for the system to record the changes:

# sfdisk -R /dev/sdc

Let’s format the /de/ sdc1 partition as a physical volume using the pvcreate command:

# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created

To get information about the existing physical volumes, run the pvdisplay command:

# pvdisplay
"/dev/sdc1" is a new physical volume of "8.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sdc1
VG Name
PV Size               8.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               MckLVU-GOJk-0BIc-EwNz-tjOT-lSfq-f7CX1I

Perform the same actions for the disks /dev/sdd и /dev/sde, which will lead us to creating three physical volumes:

# pvdisplay
"/dev/sdc1" is a new physical volume of "8.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sdc1
VG Name
PV Size               8.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               MckLVU-GOJk-0BIc-EwNz-tjOT-lSfq-f7CX1I

"/dev/sdd1" is a new physical volume of "8.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sdd1
VG Name
PV Size               8.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               5SsjpD-uqto-HgBS-Bzfw-onYY-NzPV-BMjRTa

"/dev/sde1" is a new physical volume of "8.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sde1
VG Name
PV Size               8.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               uncKHR-AJ1x-olGh-xA77-YJ35-cSxe-TgeNAo

Creating volume groups

After creating physical volumes, they need to be grouped together.

Create a volume group named "storage" from the previously created physical volumes with the vgcreate command:

# vgcreate storage /dev/sdc1 /dev/sdd1 /dev/sde1
Volume group "storage" successfully created

To get information about the existing volume groups, run the vgdisplay command:

# vgdisplay
--- Volume group ---
VG Name               storage
System ID
Format                lvm2
Metadata Areas        3
Metadata Sequence No  1
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                0
Open LV               0
Max PV                0
Cur PV                3
Act PV                3
VG Size               23.99 GiB
PE Size               4.00 MiB
Total PE              6141
Alloc PE / Size       0 / 0
Free  PE / Size       6141 / 23.99 GiB
VG UUID               XpVvbI-fEHM-hvg2-Is2K-pl5s-Ug1t-7xfXhv

The disk space in LVM is divided into blocks that called physical extent (PE). If we run the vgdisplay command, we will see the number of blocks used and unused by the logical volumes and the corresponding amount of allocated and available for allocation disk space in the group.

Creating logical volumes

Let’s create two logical volumes named "files" and "db" in the "storage" volume group using the following command:

# lvcreate -L 10G -n files storage
Logical volume "files" created.

# lvcreate -L 10G -n db storage
Logical volume "db" created.

To get information about the existing logical volumes, run the lvdisplay command:

# lvdisplay
--- Logical volume ---
LV Path                /dev/storage/files
LV Name                files
VG Name                storage
LV UUID                nZc0NN-GqIL-3Tse-A0ae-zXJf-tfqC-VqOfxm
LV Write Access        read/write
LV Creation host, time example.net, 2017-03-21 17:57:49 +0100
LV Status              available
# open                 0
LV Size                10.00 GiB
Current LE             2560
Segments               2
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:0

--- Logical volume ---
LV Path                /dev/storage/db
LV Name                db
VG Name                storage
LV UUID                kcw4M1-5JMF-TxL0-cmds-ZycI-TK6n-FaswG7
LV Write Access        read/write
LV Creation host, time example.net, 2017-03-21 17:57:55 +0100
LV Status              available
# open                 0
LV Size                10.00 GiB
Current LE             2560
Segments               2
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:1

Using logical volumes

From an OS perspective, a logical volume is a block device. Let’s consider working with the logical volume /dev/storage/files. It can be formatted using the standard tools:

# mkfs.ext4 /dev/storage/files
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

You can mount it using the standard procedure as well:

# mkdir /mnt/files
# mount /dev/storage/files /mnt/files/
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sdb1             7.8G  874M  6.5G  12% /
tmpfs                 372M     0  372M   0% /dev/shm
/dev/mapper/storage-files
9.8G   23M  9.2G   1% /mnt/files

For automated mounting, one can add a record to /etc/fstab:

/dev/storage/files     /mnt/files             ext4    defaults       0 0

At this stage, basic LVM configuration can be considered completed.

Increasing logical volumes

LVM allows for resizing existing logical volumes at the expense of the free space available in a volume group.

For demonstration, an additional disk /dev/sdf, 16 GB in volume, is added to the system. It must be used to increase the logical volume /dev/storage/files.

First of all, one needs to create a partition on the disk and format it as a physical volume, similarly to the previously created physical volumes:

# parted /dev/sdf mklabel msdos
# parted /dev/sdf mkpart primary ext2 0% 100%
# parted /dev/sdf set 1 lvm on
# sfdisk -R /dev/sdf
# pvcreate /dev/sdf1

To increase the size of an existing volume group using the new physical volume, one can run the vgextend command:

# vgextend storage /dev/sdf1
Volume group "storage" successfully extended

As we see after running the vgdisplay command, the storage volume group has now more than 20Gb of available space:

# vgdisplay
--- Volume group ---
VG Name               storage
System ID
Format                lvm2
Metadata Areas        4
Metadata Sequence No  6
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                2
Open LV               1
Max PV                0
Cur PV                4
Act PV                4
VG Size               39.98 GiB
PE Size               4.00 MiB
Total PE              10236
Alloc PE / Size       5120 / 20.00 GiB
Free  PE / Size       5116 / 19.98 GiB
VG UUID               XpVvbI-fEHM-hvg2-Is2K-pl5s-Ug1t-7xfXhv

Prior to any manipulations with the logical volume, it is necessary to unmount the file system placed on it to avoid data corruption:

# umount /mnt/files

When creating a file system, its metadata records its exact size. Increasing the size of a block device where it is located will not increase the available free space. Therefore, to increase the size of the ext4 file system on LVM, it is necessary to first increase the logical volume where it is located, and only then increase the size of the file system itself.

Increase the size of the logical volume /dev/storage/files by 18 gigabytes:

# lvextend -L +18G /dev/storage/files
Size of logical volume storage/files changed from 10.00 GiB (2560 extents) to 28.00 GiB (7168 extents).
Logical volume files successfully resized.

Before increasing the FS, it is necessary to check it:

# fsck -f /dev/storage/files

Should there be no errors after the check, one can increase the FS up to the maximum size:

# resize2fs /dev/storage/files
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/storage/files to 7340032 (4k) blocks.
The filesystem on /dev/storage/files is now 7340032 blocks long

Prior to using the increased FS, it is recommended to run a second check:

# fsck -f /dev/storage/files

Afterwards, the file system can be mounted and used as usual.

References

To obtain relevant reference information, it is recommended to use the system help (the man command).

Here is the list of the most common commands used for working with LVM:

Commands to manage physical volumes:

  • pvcreate – creating physical volumes.

  • pvdisplay – retrieving information about physical volumes.

  • pvchange – changing the settings of physical volumes.

  • pvresize – changing the size of physical volumes.

  • pvremove – deleting physical volumes.

  • pvscan – detecting physical volumes.

  • pvmove – moving extents (PE) between physical volumes.

Commands to manage group volumes: 

  • vgcreate – creating volume groups.

  • vgdisplay – retrieving information about volume groups.

  • vgchange – changing the settings of volume groups.

  • vgextend – increasing the size of volume groups.   

  • vgreduce – decreasing the size of volume groups.  

  • vgrename – renaming volume groups.

  • vgremove – deleting volume groups.

  • vgscan – detecting volume groups.

Commands to manage logical volumes:

  • lvcreate – creating logical volumes.

  • lvdisplay – retrieving information about logical volumes.

  • lvchange – changing the settings of logical volumes.

  • lvextend – increasing the size of logical volumes.

  • lvreduce – decreasing the size of logical volumes.

  • lvrename – renaming logical volumes.

  • lvremove – deleting logical volumes.  

  • lvscan – detecting logical volumes.

You can use lsblk command to get information about all available block devices or any specific devices.

Suggested Articles

  • Linux administration

    How to install nano text editor in Linux

  • Linux administration

    How to install LAMP on CentOS 6