Creating, extending and shrinking logical volumes (LVM)

In earlier post I explained how to copy logical volumes over network, but I didn’t actually explain how to create LV’s and why exactly use them at all.  Few benefits of using LVM over old partitioning system:

  • Spanning logical volume over many physical volumes, even if they reside on different physical disks (which isn’t really good idea though)
  • Creating, shrinking and extending volumes according to our needs
  • We can remove old LV’s and create new ones without any hassle
  • No need to guess from the beginning, how large volumes should be for each virtual server
  • We can resize our volumes on the fly, without rebooting server
  • Naming logical volumes and groups makes managing more "human" and therefore better understood
  • Aditional benefit you get when you add more disks to your server and manage resources according to user’s needs

Here’s a quick review on LVM:

  • Physical volumes:  They are your physical disks or disk partitions, such as /dev/sda or /dev/sdb1
  • Volume groups:  You can add several physical volumes to single volume group on which you can create several logical volumes
  • Logical volumes: These volumes you mount to your system. They can be larger than largest physical volume, since you create them in volume group, which contains several physical volumes.

Visual example of LVM hierarchy:


Creating Logical Volume

To start creating logical volumes that you want to mount to your system, you have to first partition your disk. Basically you can have a single partition over whole disk and create single physical volume on it. Example for new blank disk:

cfdisk /dev/sdb

Select "New" after that "Type" type in "8e" and choose "Write".

You should have new device named /dev/sdb1 on which we will create physical volume:

pvcreate /dev/sdb1

To be able to create logical volumes we need volume group. To create "VG" volume group with only one physical volume /dev/sdb1:

vgcreate VG /dev/sdb1

Now that we have volume group, we want to create logical volume that we will mount to our server. To create such logical volume on our "VG" with name let’s say "LV_MYVM_01" and size of 20G enter:

lvcreate -L 20G -n LV_MYVM_01 VG

We want to have ext4 filesystem on this logical volume:

mkfs.ext4 /dev/VG/LV_MYVM_01

Resizing existing Logical Volume


Trickier part is resizing volumes. Easy one is extending the volume. We can take existing volume and simply extend it for 60GB with following command:

lvextend -L+60G /dev/VG/LV_MYVM_01

To extend volume to a certain size, you should just skip the + part:

lvextend -L60G /dev/VG/LV_MYVM_01

We would like to extend our ext4 filesystem that resides on the volume, without unmounting it:

partprobe /dev/VG/LV_MYVM_01
resize2fs /dev/VG/LV_MYVM_01

If you are resizing your root partition, it should return you something like this:

[[email protected] ]# resize2fs /dev/vg0/lv_root
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/vg0/lv_root is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 10
Performing an on-line resize of /dev/vg0/lv_root to 38427648 (4k) blocks.
The filesystem on /dev/vg0/lv_root is now 38427648 blocks long.
[[email protected] ]#

Shrinking partition should be used with care. First you need to shrink your unmounted filesystem. (shrinking volume should never be done online, like extending) Be sure to still have enough space left on the device for all your data. In this example we will resize LV from 100GB to 90GB. To make sure there is no gap or overlap of filesystem and actual LV, we should always resize filesystem to 90% of final volume we want it to be, and then aditionally resize it to final size. To do that we use following set of commands:

umount /dev/vg0/LV_MYVM_01
e2fsck -f /dev/vg0/LV_MYVM_01
resize2fs /dev/vg0/LV_MYVM_01 80G
lvreduce -L 90G /dev/vg0/LV_MYVM_01
resize2fs /dev/VG/LV_MYVM_01


  1. By Matej


Leave a Reply

Purpose of the commenting system is to share your experience. I encourage you to post feedback with your own suggestions, ideas or optimizations regarding the topic of a blog post. What commenting system isn't for, is asking questions about similar issues of yours and requesting support for it. Blog post is provided as is and I am not here to solve all your problems. Please bear that in mind and try to avoid posting such comments. I do take privilege to remove comment from my blog for any reason whatsoever. Usually I do it when I sense a comment was posted only for spam/seo reasons or is out of blog post's topic. Thank you for reading this, now you may continue :)

Your email address will not be published. Required fields are marked *