Logical Volume Management (LVM) creates a layer of abstraction over physical storage, allowing you to create logical storage volumes. With LVM in place, you are not bothered with physical disk sizes because the hardware storage is hidden from the software so it can be resized and moved without stopping applications or unmounting file systems. You can think of LVM as dynamic partitions.
For example, if you are running out of disk space on your server, you can just add another disk and extend the logical volume on the fly.
Below are some advantages of using Logical volumes over using physical storage directly:
- Resize storage pools: You can extend the logical space as well as reduce it without reformatting the disks.
- Flexible storage capacity: You can add more space by adding more disks and adding them to the pool of physical storage, thus you have a flexible storage capacity.
- Use of striped, mirrored and snapshot volumes: Striped logical volume that stripes data across two or more disks can dramatically increase throughput. Mirrored Logical volumes provide a convenient way to configure a mirror for your data. And you can take device snapshots for backups or to test the effect of changes without affecting the real data.
LVM have 3 concepts
- Physical Volume (PV): it is a whole disk or a partition of a disk
- Volume Group (VG): corresponds to one or more PV
- Logical Volume (LV): represents a portion of a VG. A LV can only belong to one VG. It’s on a LV that we can create a file system.
1) Create Physical Volume
Physical volume is the actual storage device that will be used in the LVM configuration. It can be an entire disk, a partition on disk or a LUN on the SAN. You can use pvcreate to create the physical volume. In this example I have added two disks
/dev/sdc of 1 GB each. I will be using these for examples.
pvcreate command initialize these disks so that they can be a part in forming volume groups.
Display the physical volumes:
You can also use pvs command that will display the output in a configurable form.
2) Create Volume Group
Physical volumes are combined into volume groups (VGs). It creates a pool of disk space out of which logical volumes can be allocated. The disk space available for allocation in Volume Group is divided into units of a fixed-size called extents. An extent is the smallest unit of storage that can be allocated. Within a physical volume, extents are referred to as physical extents.
A logical volume is allocated into logical extents of the same size as the physical extents. The extent size is thus the same for all logical volumes in the volume group. The volume group maps the logical extents to physical extents.
# vgcreate vg-01 /dev/sdb /dev/sdc Volume group "vg-01" successfully created
Display information about VG's
You can also use vgs command that will display the output in a configurable form.
3) Create Logical Volume
A volume group is divided up into logical volumes. So if you have created vg-01 earlier then you can create logical volumes from that VG. The amount of space you want to allocate depends on your requirement. You might want to create LV of 200MB, 1GB etc. In this example, I will discuss three type of logical volumes.
- Linear Volume
- Striped Volume
- Mirrored Volume
a. Linear Logical Volumes
A linear volume aggregates space from one or more physical volumes into one logical volume. For example, if you have two 2GB disks, you can create a 4GB logical volume. The physical storage is concatenated. The application will see one device that is 4 GB in size. By default, we create linear volumes in LVM. Consider the following examples:
# lvcreate -L 1G -n lv_linear vg-01 Logical volume "lv_linear" created
Display Information about Logical Volumes.
You can also use lvs command that will display the output in a configurable form.
b. Striped Logical Volume
When you write data to a logical volume, the file system lays the data out across the underlying physical volumes. You can control the way the data is written to the physical volumes by creating a striped logical volume. For large sequential reads and writes, this can improve the efficiency of the data I/O.
With striping, I/O can be done in parallel. Below command is used to create striped volume:
# lvcreate -L 1G -i2 -I64 -n lv_stripe vg-01 Logical volume "lv_stripe" created
Now we can check with lvdisplay command
-i denotes the number of stripes. This determines over how many physical volumes the logical volume will be striped. The number of stripes cannot be greater than the number of physical volumes in the volume group. The
-I denotes the strip size. The above command creates a striped logical volume across 2 physical volumes with a stripe of 64kB.
c. Mirrored Logical Volume
A mirror maintains identical copies of data on different devices. When data is written to one device, it is written to a second device as well. This provides protection for device failures. When one leg of a mirror fails, the logical volume becomes a linear volume and can still be accessed.
An LVM mirror divides the device being copied into regions that are typically 512KB in size. LVM maintains a small log which it uses to keep track of which regions are in sync with the mirror. This log can be kept on disk, which will keep it persistent across reboots, or it can be maintained in memory. The following command will create a mirrored logical volume.
# lvcreate -L 200M -m1 -n lv_mirror vg-01 Logical volume "lv_mirror" created
Now display the mirrored logical volume created
4) Activating LVM
The Logical Volumes can be made known to the kernel using the command lvchange. After rebooting the system or running vgchange -an, you will not be able to access your VGs and LVs. To reactivate the volume group, run The
-a option is used to activate or deactivate the Logical Volume. Using y with
-a option will make it known to kernel and n will make it unavailable.
Note: When you create a volume group/logical volume it is, by default, activated. So only run this command based on specific requirement. Indicate the LV to activate.
The similar command, vgchange (with -ay option) is used for activating the Volume Group.
5) Create LVM on a new hard disk
More physical volumes can be added to an existing volume group thus increasing its size. In general, using LVM, a partition can span more than one disk. The size of logical volumes can also be extended and reduced without any loss of data on that volume.
First using fdisk command make a partition and toggle that partition to LINUX LVM (8e) label.
Then create a physical volume using pvcreate command.
# pvcreate /dev/sdb1
Display the size of the physical volume
# pvdisplay /dev/sdb1
Create volume group whose name test
# vgcreate test /dev/sdb1
Display the details of volume group created
# vgdisplay test
Create logical volume of size 100 MB with name as data , /etc/test/data */
# lvcreate -L 100M -n data test
Display the information about logical volume
# lvdisplay /dev/test/data
Convert/format logical partition to ext3 filesystem
# mke2fs -j /dev/test/data
Mount the volume to any directory
# mount /dev/test/data /mnt
If you have device mapper
# mount /dev/mapper/test-data /oracle
6) Commands to Scan PVs, LVs and VGs
The lvscan command scans for all logical volumes on the host
The vgscan command scans all the disks for volume groups and rebuilds the LVM cache file.
The pvscan command scans all lvm block devices for physical volumes.
Note: All these commands, work properly on Redhat/Centos/Ubuntu systems
LVM partitioning is very useful if you have more than one hard disk, logical volumes can extend over more than one disk which means that they are not limited by the size of one single disk, rather by the total aggregate size. You can create a (read-only) snapshot of any LV. You can revert the original LV to the snapshot at a later time, or delete the snapshot if you no longer need it.