The Logical Volume Manager, LVM, on Linux is a powerful and useful tool for creating and managing volumes for a variety of uses. LVM comes installed as part of the base installation on Centos, Red Hat, and Ubuntu and can be setup during the install process or after your install is complete. Some of the more popular uses for LVM include:
- Creating large volumes that span multiple physical drives
- Creating smaller volumes from a large physical drives
- Creating volumes that can be dynamically resized
LVM can also be used to simulate striping and mirroring raid volumes however this is best done using the Linux Multiple Device Administrator, mdadm.
The basic principals for using LVM are the same regardless of how you are going to use it. The easiest way to remember it is to think of LVM as the following 3 level structure:
- Physical Volumes: These are your actual storage drives. The physical volume can either be a whole physical drive such as /dev/sda or it can be a partition on the physical drive such as /dev/sda1. All of the commands for this level start with the letters “pv”.
- Volume Groups: Your Physical Volumes are taken and stored together as part of a volume group. Groups can be named anything you like and you can have multiple groups of PV’s on a system. All of the commands for this level start with the letters “vg”.
- Logical Volumes: The usable devices you create from your Volume Groups. You specify the size of each device when you create it and you can re-size it later if needed. All of the commands for this level start with the letters… “lv”. (really, who would have guessed?)
So… Let’s get started on using the LVM. I’ll be using a Red Hat 6 clone for all of the examples below.
Partitioning Your Drives
If you’re going to use full empty drives for your physical volumes then you can skip this setup and jump right to “Setting Up Physical Volumes”. If your drives are not empty or you simply want to use a portion of them then your going to need to create a LVM partition. If your performing a fresh install of Linux using Centos or Red Hat then you can simply create a partition using the file system type “Physical Volume (LVM)”.
On a already installed system, this becomes slightly more difficult. We will use the “fdisk” utility to partition the drive and change the partition type to “8e” which is labeled “Linux LVM”. In the example below I will use /dev/sda as my physical disk, this may be a different drive for you so be sure to specify the correct one.
[root@storage ~]# fdisk /dev/sda
Command (m for help): pDisk /dev/sda: 1000.0 GB, 999989182464 bytes 255 heads, 63 sectors/track, 121575 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x673a61fd Device Boot Start End Blocks Id System Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-121575, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-121575, default 121575): +100GB Command (m for help): p Disk /dev/sda: 1000.0 GB, 999989182464 bytes 255 heads, 63 sectors/track, 121575 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x673a61fd Device Boot Start End Blocks Id System /dev/sda1 1 12159 97667136 83 Linux Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
As you can see we ran fdisk on /dev/sda and checked the partition table with the “p” command. There were no partitions so I created a new partition using the “n” command, specified a primary partition and a size of 100GB. Once the partition was created I printed the partition table again using the “p” command and we can see that the new partition has a ID of 83 which is the type “Linux”. I used the “t” command to change the partition type to “8e” which is the type “Linux LVM”. After writing the partition table to the disk using the “w” command the system re-read the partition table to loaded the new partitions using the kernel.
Our new Linux LVM partition is ready to be used as a physical volume.
Setting Up Physical Volumes
Setting up your physical volumes is one of the easiest steps in the entire setup. A single command “pvcreate” needs to be run on each drive or partition you want to use with LVM. Simply change the /dev/ reference below and repeat for as many drives or partitions as you will be using with LVM.
[root@storage ~]# pvcreate /dev/sda1 Physical volume "/dev/sda1" successfully created
Creating A Volume Group
The volume group takes multiple physical volumes and groups them together to be used when creating the logical volumes. To create a volume group use the “vgcreate” command and specify the name and physical volumes you want to include in the group. Don’t worry if you forget some of your physical volumes as you can add more PV’s to the group after it has been created. Here we will create a new volume group called “vg_test” and include our /dev/sda1 physical volume.
[root@storage ~]# vgcreate vg_test /dev/sda1 Volume group "vg_test" successfully created
Creating a Logical Volume
The logical volume is the actual virtual device you are going to use for storage in your system. Each LV is created in a volume group and the group decides how to place them on the physical volumes. For basic volumes all that needs to be specified is the size and name of the logical volume and which volume group you want to place it in. Here I created a simple 20GB logical volume in the volume group “vg_test” that we created about. The logical volume group size is specified using the “-L” flag and the name 0f the new volume is specified using the “-n” flag.
[root@storage ~]# lvcreate -n lv_testing -L 20G vg_test Logical volume "lv_testing" created
This logical volume is now setup and can be used if we formatted and mounted it. The block device can be found at /dev/vg_test/lv_testing on the file system for use with mkfs and mount.
Checking Logical Volume Information
We can check on the logical volumes information by running the “lvdisplay” command with the volume group and logical volume names. This gives you the volumes UUID and other status information.
[root@storage ~]# lvdisplay vg_test/lv_testing --- Logical volume --- LV Name /dev/vg_test/lv_testing VG Name vg_test LV UUID FrW4Qr-p9S2-u2zx-UDmx-c5VD-wvzN-KsTVOb LV Write Access read/write LV Status available # open 0 LV Size 20.00 GiB Current LE 5120 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
Checking the Volume Group Information
We can check on our volume group by running the “vgdisplay” command. This displays the number of logical volumes in the group, total amount of space used, the amount of space available in the group, and the number of physical volumes in the group.
[root@storage ~]# vgdisplay vg_test --- Volume group --- VG Name vg_test System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 1 Act PV 1 VG Size 93.14 GiB PE Size 4.00 MiB Total PE 23844 Alloc PE / Size 5120 / 20.00 GiB Free PE / Size 18724 / 73.14 GiB VG UUID qBTRtr-18qN-M2rg-ZjPN-YHyN-aydj-QMRc2M
Adding More Physical Volumes
Using the “vgextend” command we can more physical volumes to our volume group making more space available for LV’s. Here I added a 2nd physical volume to the volume group and expanded the size by another 200GB.
[root@storage ~]# vgextend vg_test /dev/sda2 Volume group "vg_test" successfully extended [root@storage ~]# vgdisplay vg_test --- Volume group --- VG Name vg_test System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 293.14 GiB PE Size 4.00 MiB Total PE 75045 Alloc PE / Size 5120 / 20.00 GiB Free PE / Size 69925 / 273.14 GiB VG UUID qBTRtr-18qN-M2rg-ZjPN-YHyN-aydj-QMRc2M
Removing Physical Volumes
Before removing a physical volume from a volume group you should check to see if the physical volume has been used by the group using the “pvdisplay” command. If it has been used then you will want to migrate the data off the drive using the “pvmove” command before removing the physical volume. Once you are sure the physical volume is no longer used by the group you can remove it using the “vgreduce” command.
[root@storage ~]# vgreduce vg_test /dev/sda2 Removed "/dev/sda2" from volume group "vg_test"
Expanding and Reducing Logical Volume Sizes
Expanding a logical volume is very straight forward. Using the “lvextend” command we can can add space to the logical volume is there is space available in the volume group. This is done by specifying the new size of the logical volume and the logical volume name.
[root@storage ~]# lvextend -L 40G vg_test/lv_testing Extending logical volume lv_testing to 40.00 GiB Logical volume lv_testing successfully resized
Reducing the size of the logical volume can be a bit more tricky. Before reducing the size of the logical volume you will want to be sure that the partitions created on the LV have also been reduced before removing the space from the logical volume virtual block device. This can be done using tools like gParted and is outside the scope of this how to. Once the LV has enough free space to remove you can reduce the space using the “lvreduce” command.
[root@storage ~]# lvreduce -L 10G vg_test/lv_testing WARNING: Reducing active logical volume to 10.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv_testing? [y/n]: Y Reducing logical volume lv_testing to 10.00 GiB Logical volume lv_testing successfully resized
Removing Logical Volumes
Once you are done using your logical volume you can remove it using the “lvremove” command. This is done by specifying the path to the block device for the logical volume. Removing our “lv_testing” logical volume would look like the following:
[root@storage ~]# lvremove /dev/vg_test/lv_testing Do you really want to remove active logical volume lv_testing? [y/n]: y Logical volume "lv_testing" successfully removed
Removing Volume Groups
Once all of the logical volumes in a group have been removed, you can also remove the volume group using the “vgremove” command. Simply specifying the name of the volume group is all that is needed to remove the group from existence.
[root@storage ~]# vgremove vg_test Volume group "vg_test" successfully removed
Hopefully this will give you a basic understanding of how to use the logical volume manager, LVM, on Linux and what you can use it for. The LVM system was originally created and used because disk sizes were much smaller than they are now and combining disks was necessary to provide enough space for storage. Today’s disk drives are much more affordable in larger sizes and so LVM is not used as often. A excellent use for LVM in today’s computing world would be exporting logical volumes as iSCSI targets for use on other machines. Thus making LVM a great storage management solution for a dedicated storage server.

[No Site Links]