LVM Partition, 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 more 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 have 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.
Similar: What is Swap Partition and how to create it on Linux
In this guide, we will briefly discuss how LVM works and then demonstrate the basic commands needed to get up and running quickly.
LVM Architecture and Terminology
Before we dive into the actual LVM administrative commands, it is important to have a basic understanding of how LVM organizes storage devices and some of the terminology it employs.
LVM Storage Management Structures
LVM functions by layering abstractions on top of physical storage devices. The basic layers that LVM uses, starting with the most primitive, are.
- Physical Volumes:
- LVM utility prefix:
pv...
- Description: Physical block devices or other disk-like devices (for example, other devices created by device-mapper, like RAID arrays) are used by LVM as the raw building material for higher levels of abstraction. Physical volumes are regular storage devices. LVM writes a header to the device to allocate it for management.
- LVM utility prefix:
- Volume Groups:
- LVM utility prefix:
vg...
- Description: 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 with a combined storage capacity of the component physical volumes.
- LVM utility prefix:
- Logical Volumes:
- LVM utility prefix:
lv...
(generic LVM utilities might begin withlvm...
) - Description: 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. Logical volumes are the primary component that users and applications will interact with.
- LVM utility prefix:
In summary, LVM can be used to combine physical volumes into volume groups to unify the storage space available on a system. Afterward, administrators can segment the volume group into arbitrary logical volumes, which act as flexible partitions.
Similar: What is Partition, Type, and how to create a normal partition
What are the 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).
The extents on a physical volume are called physical extents, while the 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.
Extents are behind much of the flexibility and power of 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.
►LV(Logical Volume): Logical volumes are functionally equivalent to partitions on a physical disk, but with much more flexibility.
Advantages of LVM?
►Use any number of disks as one big disk.
►Have logical volumes stretched over several disks?
►Create small logical volumes and resize them “dynamically” as they get filled up.
►Resize logical volumes regardless of their order on disk. It does not depend on the position of the LV within VG, there is no need to ensure surrounding available space.
►Resize/create/delete logical and physical volumes online. File systems on them still need to be resized, but some (such as ext4) support online resizing.
►Online/live migration of LV being used by services to different disks without having to restart services.
►Snapshots allow you to backup a frozen copy of the file system while keeping service downtime to a minimum.
Creating an LVM Partition:
root@localhost:~# lsblk root@localhost:~# fdisk /dev/vdb2 :n ## new partition ## :e ## extended ## :n ## new ## :l ## for logical partition ## :+1G ## size of partition in GB ## :t ## type of partition ## :5 ## partition number ## :8e ## Hex code for LVM partition ## :w ## for save ##
root@localhost:~# partprobe root@localhost:~# pvcreate /dev/vdb2 ## create PV for LVM ## root@localhost:~# pvdisplay root@localhost:~# vgcreate myvol /dev/vdb2 ## myvol is VG name ## root@localhost:~# vgdisplay root@localhost:~# lvcreate -n data -L +500M myvol ## "-n" name, "data" LV name, "-L" length of LV partition, "+500M" partition size for LV, "myvol" VG name root@localhost:~# lvdisplay
root@localhost:~# mkfs.ext4 /dev/myvol/data root@localhost:~# mkdir /data root@localhost:~# blkid /dev/myvol/data root@localhost:~# echo "UUID ---- /data ext4 defaults 0 0" >> /etc/fstab root@localhost:~# df -h
How to extend LVM?
PE: Physical extent is the smallest contiguous extent (default 4 MiB) in the PV that can be assigned to an LV. Think of PEs as parts of PVs that can be allocated to any LV.
There are 460MB of free PE available = 1GB of Free space available. So we can expand our logical volume up to 450MB more. Let us use the PE size to extend.
root@localhosr:~# lvextend -r -L +450M /dev/myvol/data
Note: “-r” option means resize if you don’t put this option then you need to run one more command which is:
root@localhost:~# resize2fs -f /dev/myvol/data
Extend XFS partition:
The same command works here but the “-r” option doesn’t work here so we need to run two commands which are:
root@localhost:~# lvextend -L +400M /dev/myvol/data root@localhost:~# xfs-growfs /data
How to reduce the partition:
Note: Reducing a partition is not that good because you lose some data in the reducing process but if you want to reduce then here’s the command:
root@localhost:~# lvreduce -r -L -200M /dev/myvol/data