Linux: Logical Volume Manager (LVM)
Managing Disk and Storage with LVM (Logical Volume Manager)
Logical Volume Management
Logical Volume Management is carried out by LVM the "Logical Volume Manager". LVM allows you to easily manage your disk space. LVM gives you the ability to add additional disk space to filesystems and move data from one partition to another.
Basically, LVM is split into 3 distinct layers. These are Physical Volumes (PV), Volume Groups (VG) and Logical Volumes (LV).
Physical Volumes (PV)
Physical Volumes are literally storage space. Generally these are partitions on either a local disk or a SAN attached device or even a loop back file. The storage space on a Physical Volume is divided into small units called Physical Extents (PE).
Volume Groups (VG)
Logical Volumes and Physical Volumes are grouped together into a Volume Group (VG)
Logical Volumes (LV)
Volume Groups are divided into Logical Volumes (LV).
LVM Basic Process
Each of the above three sections have specialised commands for managing/manipulating your storage requirements. The basic order for creating a LVM managed system is to assign disk, create a partition or partitions on this disk, create a volume group and then create your logical volumes. Once you have created your Logical Volumes, you then need to create a filesystem before you can use this storage area. One a filesystem has been created, you then need to create a mount point where you can then mount your logical volumes.
Physical Volume Commands:
pvchange: Change attributes of a Physical Volume (PV)
pvcreate: Initialise a disk or partition for use with LVM
pvmove: Move Physical Extents
pvresize: Resize a disk partition utilised by LVM
pvscan: Scan all disks for Physical Partitions
pvck: Check Physical Volume metadata
pvdisplay: Display Physical Volume Information
pvremove: Remove a Physical Volume
pvs: Report Physical Volume Information
Volume Group Commands:
vgcfgbackup: Backup a volume group descriptor area
vgconvert: Convert Volume Group metadata
vgextend: Add Physical Volumes to a Volume Group
vgmknodes: Recreate volume group directory and logical volume special files
vgs: Report information about volume groups
vgcfgrestore: Restore volume group descriptor area
vgcreate: Create a Volume Group
vgimport: Make exported volume groups known to the system
vgreduce: Reduce a volume group by removing one or more physical volumes
vgscan: Scan all disks for volume groups and rebuild cache
vgchange: Change attributes of a volume group
vgdisplay: Display Volume Group Information
vgimport: Make exported volume groups known to your system
vgimportclone: Import and rename duplicated volume group
vgremove: Remove a Volume Group
vgsplit: Split a volume group into two, moving any logical volumes from one volume group to another by moving entire physical volumes
vgck: Check volume Group metadata
vgexport: Make volume groups unknown to the system
vgmerge: Merge two Volume Groups
vgrename: Rename an existing Volume Group
Logical Volume Commands:
lvchange: Change attributes of a logical volume
lvconvert: Convert a Logical Volume from linear to mirror or snapshot
lvcreate: Create a logical volume in an existing volume group
lvdisplay: Display attributes of a logical volume
lvextend: Extend the size of a logical volume
lvmchange: Change attributes of the logical volume manager
lvmdiskscan: Scan for all devices visible to LVM2
lvmdump: Create lvm2 information dumps for diagnostic purposes
lvreduce: Reduce the size of a logical volume
lvremove: Remove a logical volume
lvrename: Rename a logical volume
lvresize: Resize a logical volume
lvs: Report information about logical volumes
lvscan: Scan (all disks) for logical volumes
Make filesystem commands:
mkfs: Make a filesystem of specified type
mkfs.ext2: Make an ext2 type filesystem
mkfs.ext3: Make an ext3 type filesystem
mkfs.ext4: Make an ext4 type filesystem
mkfs.xfs: Make an xfs type filesystem
The above list will vary depending on what support you have installed and also the Linux distribution you are using.
The easiest way to get an understanding of LVM is to create some basic examples using some of the commands from the lists above. In the following examples we shall use Virtual Box, fdisk and the CentOS Linux operating system. We will also provide an example of LVM using loop back devices.
LVM Example 01: In this example we will create a simple 2 disk scenario whereby we have a single volume group that spans two physical disks. Each disk will be 1GB in size.
LVM Example 02: In this example we will create a multi disk scenario. We will use commands to add and reduce space. Instead of using Virtual box, we will create loopback devices. This will enable you to try out your LVM commands.
LVM Example 03: In this example, we will initially add a single disk to our LVM environment. Later we then add a second larger disk. We will then use the "pvmove" commands to migrate data from the smaller disk to the larger disk.
LVM Command Examples: In this section we will cover some of the frequently used LVM commands.
LVM Snapshots: LVM snapshots are used to take a point in time snapshot of a Logical Volume. These snapshots can then be used to roll-back your Logical Volume to its point in time state. Snapshots are a very powerful feature of LVM. In this example we take a snapshot copy of a Logical Volume and then make some changes to the original copy. We then revert the changes back with the "lvconvert --merge" command.