Logical Volume Management

From OptionC

One of the most useful tools for working with Xen virtual machines is Logical Volume Manager or LVM.

LVM allows for more flexibility in disk storage than normal disk partitioning. Administrators can resize volumes, rearrange and move them, manage volumes in user-defined groups and label them with names that are easier for human consumption. In addition, volumes can be spread over multiple physical devices allowing for quicker, easier and less expensive growth of storage.

LVM has the following grouping:

 - Physical Volumes (PVs) - The physical media, either whole disks or partitions
 - Volume Groups (VGs) - Grouping of PVs
 - Logical Volumes (LVs) - The volumes created within the VGs

We will assume that you have LVM2 (and its dependencies) installed on your machine. The following is a basic overview on how to create PVs, VGs, and LVs, and how to resize an LV. (The following commands need root/administrative privileges).


In order to create a PV use the command pvcreate:

 pvcreate /dev/hdd1     (to create a PV out of a partition)
 pvcreate /dev/hdd      (to create a PV out of an entire drive)


To create a VG, use the vgcreate command. For example, to build a VG with disk hdb and partitions hdd1 and hdd2:

 vgcreate my_vg /dev/hdb /dev/hdd1 /dev/hdd2   

To add a newly create PV to an existing VG, use the vgextend command.


To create a new 2.5 GB logical volume named my_lv, on the my_vg volume group:

 lvcreate -L 2.5G -n my_lv my_vg


To actually make the filesystem (note: you can make other filesystem types; we'll demonstrate with ext2fs/ext3fs):

 mke2fs /dev/my_vg/my_lv


Then, to mount the new LV:

 mount /dev/my_vg/my_lv /mnt/lv


Increasing LV size (by 1500 MBytes):

 umount /mnt/lv
 lvextend -L +1500M /dev/my_vg/my_lv
 e2fsck -f /dev/my_vg/my_lv
 resize2fs /dev/my_vg/my_lv 
 mount /dev/my_vg/my_lv /mnt/lv


To decrease the LV size to a specific value (example, 1G):

 umount /mnt/lv
 e2fsck -f /dev/vmvg/blah
 resize2fs /dev/vmvg/blah 1G
 lvreduce -L 1G /dev/vmvg/blah

Note: the resize2fs and the lvreduce commands take size suffixes of K (kilobyte), M (megabyte) and G (gigabyte). It is important that the lvreduce command does not shrink the partition smaller than the value used by the resize2fs command.

We strongly recommend reading the man pages for any of the commands listed.


Other useful commands:

 pvscan -v      Scan all disks for physical volumes.  -v = verbose output
 vgscan -v      Scan all disks for volume groups.     -v = verbose output
 lvscan -v      Scan all disks for logical volumes.   -v = verbose output

Recommended Reading