Logical volume management in GNU/Linux

Written by
Shubhra Prakash Nandi's picture

Logical volume management is a very handy tool to take care of your growing need for disk space as more and more data fills your disk. Traditionally hard disk partitions were created during operating system install to statically allocate space to take care of present and future need of disk space. This turned out to be quite a constrained situation for a system administrator as adding more space to a partition involved moving data from one place to another, extend an already existing partition by either allocating free space available next to it, or by adding a new disk and moving all the data to it and then mounting the partition back. This could mean hours of downtime for your server if the data size involved is in tens of gigabytes or more.

Logical volume management or LVM is the answer to the problem of dynamically allocating more space to a logical volume without having to bother about moving data as the logical volume is grown. Many popular GNU/Linux distributions now provide LVM as an option to partition the disk during installation itself, though logical volumes can be setup any time after it.

Now let’s understand how logical volumes work. The basic disk area which a LVM uses is a physical volume (PV) which is nothing more than a partition on the disk or the complete disk itself. The partition type needs to of physical volume for LVM for it to be considered by LVM as a PV. Once a few PVs have been created you can allocate them to volume group (VG). Volume groups are a repository of PVs and can be extended or reduced dynamically by adding or removing PVs from it respectively. Many logical volumes (LV) can then be carved out of a VG. Size of a LV cannot be greater than the size of the VG it resides in.

Let’s say you have 3 PVs, PV1, PV2 and PVof size 20GB, 10GB and 5GB respectively. Now you created a VG, VG1 using PVand PV2, so now you have VG1 of size 30GB. You can now create a LV of size 30GB or less to begin with which can be dynamically grown. Let’s say you created a LV, LV1 of size 30GB and started using it to fill up data. After consuming 90% of the space in LV1 you started to feel the need to add more space to the LV. You can do so by first allocating PV3, which was lying unallocated to VG1. Once you have done so you can increase LV1 to the size of 35GB which makes 5GB of extra free space available to LV1 in a transparent manner without affecting any data already residing on LV1. You also need to resize the filesystem of the LV for the operating system make use of the extra space allocated to the LV.

The steps to achieve the above in GNU/Linux is as follows. You need to have lvm2 package installed in your system for the below commands to be available.

 

Creating a LV from scratch

# Create PVs from disk partitions
pvcreate /dev/sdb1 /dev/sdb2 /dev/sdc1 /dev/sdc2

# Create VG from PVs
vgcreate primary_vg /dev/sdb1 /dev/sdb2 /dev/sdc1 /dev/sdc2

# Create LV of a defined size from VG
lvcreate -n home_lv -L 10g primary_vg

# Create a filesystem on the LV
mkfs.ext4 /dev/primary_vg/home_lv

# Mount the LV using fstab file
/dev/mapper/primary_vg-home_lv    /home ext4    defaults    0    0

 

Increasing LV by extending VG

# Add new PV
pvcreate /dev/sdd1

# Extend VG
vgextend primary_vg /dev/sdd1

# Extend LV to a fixed size
lvextend -L 15g /dev/primary_vg/home_lv
# Or to extend by a fixed size
lvextend -L '+5g' /dev/primary_vg/home_lv

# Grow the filesystem of the LV also to 
# make the filesystem make use of the extended space of the LV
resize2fs /dev/primary_vg/home_lv

 

Increasing LV to make use of free space in VG

# Extend LV to a fixed size
lvextend -L 15g /dev/primary_vg/home_lv
# Or to extend by a fixed size
lvextend -L '+5g' /dev/primary_vg/home_lv

# Grow the filesystem of the LV also to 
# make the filesystem make use of the extended space of the LV
resize2fs /dev/primary_vg/home_lv

 

Important note regarding choice of filesystem for LV

If it is important for you to both grow and shrink your LV then you should pay prior attention to the choice of filesystem which you plan to use on the LV. Not all filesystems in the GNU/Linux system can be grown or shrunk. Also some filesystems allow growing or shrinking the filesystem while mounted, others do not. Make use of the below table for choosing the filesystem as per your needs.

Feature

Btrfs

XFS

Ext4

ReiserFS*

Online extend

Yes

Yes

Yes

Yes

Online shrink

Yes

No

No

No

Offline extend

Yes

No

Yes

Yes

Offline shrink

Yes

No

Yes

No

* ReiserFS is not actively maintained in the Linux kernel now, so consider it carefully before choosing it as your LVM filesystem.

Zircon - This is a contributing Drupal Theme
Design by WeebPal.