Child pages
  • Managing LVM Storage
Skip to end of metadata
Go to start of metadata

LVM (Logical Volume Manager) is a fantastically flexible system for maintaining and organizing your storage. It provides a few handy imaging tools such as snapshot creation, but most importantly it brings the ability to shrink and grow a single filesystem across multiple physical drives. If you are using RieserFS or XFS for your filesystem you will have the added benefit of being able to perform resizes while the filesystem is online. At first glance LVM may seem complicated, but it is well worth learning as it will likely save you from some pretty nasty headaches down the road.


Creating an LVM Volume

Grow an LVM Volume

Shrink an LVM Volume

Remove an LVM entity

Prepare a new disk to replace an old one

A summary of LVM

LVM volumes are organizationally stacked on top of the existing standard partitioning system. From LVM’s point of view, each standard partition is recognized as if it were a physical drive. The basic procedure for deploying LVM is as follows:

  1. Create standard partition: the standard partition on the physical drive which will be formatted as a virtual physical drive for LVM instead of with a filesystem

  2. Convert partition to PV (physical volume): an individual disk in the eyes of LVM

  3. Assign PV to VG (volume group a.k.a. span): Designates partitionable space that can be spread across multiple drives if desired.

  4. Create LVs (logical volume): logical partitions that can be formatted individually

This output of lsblk illustrates how nesting of logical volumes within standard partitions looks:

[root@moriarty ~]# lsblk
sda            8:0    0 931.3G  0 disk
├─sda1         8:1    0  1000M  0 part /boot
├─sda2         8:2    0 922.5G  0 part /
└─sda3         8:3    0   7.8G  0 part [SWAP]
sdb            8:16   0   7.3T  0 disk
└─sdb1         8:17   0   7.3T  0 part
└─vga-home 253:0    0   7.3T  0 lvm  /home

sdb – disk

sdb1 – standard partition

vga – volume group

home – logical volume


In order to make use of LVM partitioning, you will need to ensure that LVM is installed…

CentOS/RedHatyum install lvm2

Ubuntu/Debianapt-get install lvm2

Creating an LVM Volume

In this example we will create a logical volume inside of disk /dev/sdb

Remember to replace /dev/sdb with the empty drive you are working with! The below commands can, and will, destroy any existing data.

Create Standard Partition

Using the appropriate partition manager utility on the system (fdisk or parted)…

Create disk partitioning table

parted /dev/sdb mklabel gpt

Create a standard partition

parted /dev/sdb ‘mkpart primary 0 -0’

Create Logical Volume

Once a standard partition (/dev/sdb1) is created, we will then begin setting up LVM…

Designate standard partition as an LVM physical volume

pvcreate /dev/sdb1

Create volume group (span) and add however many physical volumes you would like to use in this partitionable space. In this example, only the physical volume /dev/sdb1 will be used and we will name the span ‘vga’.

vgcreate vga /dev/sdb1

Create logical volume ‘home’ inside of the volume group using all of the available free space. We are calling this volume ‘home’ simply because we will mount it to the home directory of the filesystem.

lvcreate vga -n home -l 100%free

You can also specify a size in bytes. We could create a partition of 100GB with this:

lvcreate vga -n home -L 100G

Mount logical volume to the filesystem

First we will display the existing logical volumes on the system with lvdisplay

[root@moriarty ~]# lvdisplay
--- Logical volume ---
LV Path                /dev/vga/home
LV Name                home
VG Name                vga
LV UUID                Pyfnjd-2c3b-Eg5t-R80j-5HUJ-0RPj-GrUo1g
LV Write Access        read/write
LV Creation host, time, 2015-02-13 16:40:44 -0700
LV Status              available
# open                 1
LV Size                7.28 TiB
Current LE             1907343
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:0

We will then format the logical volume with the filesystem of our choice, substituting the LV Path value from your results of lvdisplay as the partition identifier. We will format with ext4 in this example:

mkfs.ext4 /dev/vga/home

To finish up by making the logical volume partition usable, we’ll mount the LV to the filesystem on the /home directory

mount /dev/vga/home /home

Don’t forget to configure this mount in /etc/fstab so that the server knows to mount the partition on each boot!


Grow an LVM Volume

Add a new disk to volume group

A new physical volume can be added to an existing volume group with just a single command:

vgextend vga /dev/sdc1

Grow logical volume across volume group

To begin, we will print a list of our existing logical volume disk usage with lvs:

[root@moriarty ~]# lvs
LV   VG   Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync
lva  vga  -wi-a----- 745.05g
lvb  vga  -wi-a----- 745.05g
lvc  vga  -wi-a-----   1.46t

There are 3 logical volumes on this volume group ‘vga’. Let’s say that a second physical volume has been added to this volume group and that we now have up to 7.28TB of space in total:

[root@moriarty ~]# vgdisplay
--- Volume group ---
VG Name               vga
System ID
Format                lvm2
Metadata Areas        1
Metadata Sequence No  6
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                3
Open LV               0
Max PV                0
Cur PV                1
Act PV                1
VG Size               7.28 TiB
PE Size               4.00 MiB
Total PE              1907343
Alloc PE / Size       762936 / 2.91 TiB
Free  PE / Size       1144407 / 4.37 TiB
VG UUID               Ajrsef-PBnl-t0J6-d025-KT5n-eswF-q6XPMq

Here we’ll add 1000GB to logical volume ‘lva’ and expand ‘lvc’ to use the rest of the remaining space:

lvextend -L +1000G /dev/vga/lva

lvextend -l 100%free /dev/vga/lvc

We’ve now assigned all of the available space on vga:

[root@moriarty ~]# lvs
LV   VG   Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync
lva  vga  -wi-a-----   1.70t
lvb  vga  -wi-a----- 745.05g
lvc  vga  -wi-a-----   3.39t

So we have resized the logical volume, however the filesystem inside will also need to be resized in order to make use of the newly available space. We will use the resize utility specific to the filesystem installed – in this case resize2fs (ext2-4 filesystems) for the filesystem on lva:

resize2fs /dev/vga/lva

[root@moriarty ~]# resize2fs /dev/vga/lva
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vga/lva to 457455616 (4k) blocks.
The filesystem on /dev/vga/lva is now 457455616 blocks long.


Shrink an LVM Volume

Before shrinking a logical volume you must first shrink the filesystem inside. If this is not done first, the LV shrink will irreparably destroy the filesystem.

Shrink filesystem

We will use the filesystem’s shrink utility to adjust the size. Be aware that not all filesystems support shrinking.

Here we will shrink lva from its current 1.7TB to 100GB:

[root@moriarty ~]# resize2fs /dev/vga/lva 100G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vga/lva to 26214400 (4k) blocks.
The filesystem on /dev/vga/lva is now 26214400 blocks long.

Shrink logical volume

Now that the filesystem has been resized, we will adjust the logical volume to match using lvreduce. This is a dangerous step, make sure that you specify the exact same size to which the filesystem has been configured at:

[root@moriarty /]# lvreduce /dev/vga/lva -L 100G
WARNING: Reducing active logical volume to 100.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lva? [y/n]: y
Reducing logical volume lva to 100.00 GiB
Logical volume lva successfully resized

As you can see, the files are still intact so the resize was a success:

[root@moriarty ~]# mount /dev/vga/lva /mnt/lva; ls /mnt/lva
lost+found  testfile  testtesttest


Remove an LVM entity

Delete a logical volume

This can be done with the simple command ‘lvremove’. Based on our previous examples, we would use the following command to delete the volume ‘home’ in volume group ‘vga’:

lvremove /dev/vga/home

Delete a volume group

Likewise, there is another simple command for this when we want to remove ‘vga’:

vgremove vga

Delete a physical volume

We can then remove the physical volume designation from the standard partition:

pvremove /dev/sdb1


Prepare a new disk to replace an old one

The process of migrating data from a failing disk to a new one is fairly straightforward. First, add the new disk to the existing volume group. Here we will prepare a fresh disk /dev/sdd to replace a failing disk /dev/sdc:

parted /dev/sdd mkpart primary 0 -0

pvcreate /dev/sdd

Once that is done, add the new physical volume to the existing volume group:

vgextend vga /dev/sdd1

We will then use the pvmove command to specify the failing disk that we wish to remove, and LVM will automatically migrate its data to the empty space on the new physical volume:

pvmove /dev/sdc1

After the migration is complete, the old physical volume can be removed from the volume group:

vgreduce /dev/sdc1