Logical Volume Management (LVM)
Introduction
Section titled “Introduction”LVM, or Logical Volume Management, is a storage device management technology that gives users the power to pool and abstract the physical layout of component storage devices for easier and flexible administration. Utilizing the device mapper Linux kernel framework, the current iteration, LVM2, can be used to gather existing storage devices into groups and allocate logical units from the combined space as needed.
The main advantages of LVM are increased abstraction, flexibility, and control. Logical volumes can have meaningful names like “databases” or “root-backup”. Volumes can be resized dynamically as space requirements change and migrated between physical devices within the pool on a running system or exported easily. LVM also offers advanced features like snapshotting, striping, and mirroring.
LVM Architecture and Terminology
Section titled “LVM Architecture and Terminology”LVM Storage Management Structures
Section titled “LVM Storage Management Structures”LVM functions by layering abstractions on top of physical storage devices. The basic layers, starting with the most primitive, are:
| Layer | LVM Prefix | Description |
|---|---|---|
| Physical Volumes | pv... | Physical block devices or other disk-like devices used by LVM as the raw building material. LVM writes a header to the device to allocate it for management. |
| Volume Groups | vg... | LVM combines physical volumes into storage pools known as volume groups. Volume groups abstract the characteristics of the underlying devices and function as a unified logical device. |
| Logical Volumes | lv... | A volume group can be sliced up into any number of logical volumes. Logical volumes are functionally equivalent to partitions on a physical disk, but with much more flexibility. |
In summary, LVM can be used to combine physical volumes into volume groups to unify the storage space available on a system. Afterwards, administrators can segment the volume group into arbitrary logical volumes, which act as flexible partitions.
What are Extents?
Section titled “What are Extents?”Each volume within a volume group is segmented into small, fixed-size chunks called extents. The size of the extents is determined by the volume group (all volumes within the group conform to the same extent size).
- Extents on a physical volume are called physical extents
- Extents of a logical volume are called logical extents
- A logical volume is simply a mapping that LVM maintains between logical and physical extents
Because of this relationship, the extent size represents the smallest amount of space that can be allocated by LVM. The logical extents that are presented as a unified device by LVM do not have to map to continuous physical extents. LVM can copy and reorganize the physical extents that compose a logical volume without any interruption to users. Logical volumes can also be easily expanded or shrunk by simply adding extents to or removing extents from the volume.
Practical Guide
Section titled “Practical Guide”1. Mark the Physical Devices as Physical Volumes
Section titled “1. Mark the Physical Devices as Physical Volumes”Scan the system for block devices that LVM can see and manage:
sudo lvmdiskscan /dev/sda [ 200.00 GiB] /dev/sdb [ 100.00 GiB] 2 disks 0 LVM physical volume whole disks 0 LVM physical volumesWarning: Make sure that you double-check that the devices you intend to use with LVM do not have any important data already written to them. Using these devices within LVM will overwrite the current contents.
Mark devices as physical volumes:
sudo pvcreate /dev/sda /dev/sdb Physical volume "/dev/sda" successfully created Physical volume "/dev/sdb" successfully createdVerify that LVM has registered the physical volumes:
sudo pvs PV VG Fmt Attr PSize PFree /dev/sda lvm2 --- 200.00g 200.00g /dev/sdb lvm2 --- 100.00g 100.00g2. Add the Physical Volumes to a Volume Group
Section titled “2. Add the Physical Volumes to a Volume Group”Create a volume group and add both physical volumes to it:
sudo vgcreate LVMVolGroup /dev/sda /dev/sdb Volume group "LVMVolGroup" successfully createdSee a brief summary of the volume group:
sudo vgs VG #PV #LV #SN Attr VSize VFree LVMVolGroup 2 0 0 wz--n- 299.99g 299.99g3. Create Logical Volumes from the Volume Group Pool
Section titled “3. Create Logical Volumes from the Volume Group Pool”Unlike conventional partitioning, when working with logical volumes, you do not need to know the layout of the volume since LVM maps and handles this for you. You only need to supply the size of the volume and a name.
Create logical volumes with the -L option (for size) and -n option (for name):
sudo lvcreate -L 10G -n projects LVMVolGroupsudo lvcreate -L 5G -n www LVMVolGroupsudo lvcreate -L 20G -n db LVMVolGroup Logical volume "projects" created. Logical volume "www" created. Logical volume "db" created.Allocate remaining space to a “workspace” volume using percentages:
sudo lvcreate -l 100%FREE -n workspace LVMVolGroupView the result:
sudo vgs -o +lv_size,lv_name VG #PV #LV #SN Attr VSize VFree LSize LV LVMVolGroup 2 4 0 wz--n- 299.99g 0 10.00g projects LVMVolGroup 2 4 0 wz--n- 299.99g 0 5.00g www LVMVolGroup 2 4 0 wz--n- 299.99g 0 20.00g db LVMVolGroup 2 4 0 wz--n- 299.99g 0 264.99g workspace4. Format and Mount the Logical Volumes
Section titled “4. Format and Mount the Logical Volumes”The logical devices are available within the /dev directory in two locations:
/dev/volume_group_name/logical_volume_name/dev/mapper/volume_group_name-logical_volume_name
Format the logical volumes with the Ext4 filesystem:
sudo mkfs.ext4 /dev/LVMVolGroup/projectssudo mkfs.ext4 /dev/LVMVolGroup/wwwsudo mkfs.ext4 /dev/LVMVolGroup/dbsudo mkfs.ext4 /dev/LVMVolGroup/workspaceCreate mount points:
sudo mkdir -p /mnt/{projects,www,db,workspace}Mount the logical volumes:
sudo mount /dev/LVMVolGroup/projects /mnt/projectssudo mount /dev/LVMVolGroup/www /mnt/wwwsudo mount /dev/LVMVolGroup/db /mnt/dbsudo mount /dev/LVMVolGroup/workspace /mnt/workspace5. Make Mounts Persistent
Section titled “5. Make Mounts Persistent”Add them to /etc/fstab just like you would with normal block devices:
/dev/LVMVolGroup/projects /mnt/projects ext4 defaults,nofail 0 0/dev/LVMVolGroup/www /mnt/www ext4 defaults,nofail 0 0/dev/LVMVolGroup/db /mnt/db ext4 defaults,nofail 0 0/dev/LVMVolGroup/workspace /mnt/workspace ext4 defaults,nofail 0 0The operating system should now mount the LVM logical volumes automatically at boot.