Logical Volume Management (LVM) in Linux
Introduction
Section titled “Introduction”On this page, you will learn how to use Logical Volume Management in Linux. LVM provides a flexible way to manage storage by creating an abstraction layer between physical devices and filesystems. Instead of being limited by fixed partitions, LVM allows you to combine multiple disks or partitions into a single storage pool called a Volume Group. From this pool, you can create Logical Volumes that act like virtual partitions. These volumes can be resized, extended, or reduced without repartitioning, making storage management dynamic and efficient. LVM also supports RAID techniques within a Volume Group, allowing you to implement mirroring or striping for redundancy or performance.
LVM Hierarchy
Section titled “LVM Hierarchy”- Physical Volume (PV): Actual storage device or partition (e.g.,
/dev/sda1). - Volume Group (VG): Pool of storage made up of one or more PVs (e.g.,
vg_data). - Logical Volume (LV): Virtual partition created from a VG. Acts like a “normal partition” (e.g.,
lv_webcontent).
Hierarchy Example
Section titled “Hierarchy Example”The following diagram shows the typical LVM structure from the physical disk to the mount point.
Disk: /dev/sda└── Partition: /dev/sda1 └── Physical Volume (PV): /dev/sda1 └── Volume Group (VG): vg_data └── Logical Volume (LV): lv_webcontent └── Filesystem: ext4 or xfs └── Mount Point: /var/wwwNote: Keep in mind that a PV acts as a claim for a physical disk or partition. This way you don’t need a partition and you can just claim the whole disk by using
pvcreate /dev/sda. This will utillize the whole disk without partitioning. This will result in not being able to use the disk for anything else then LVM as long as its in use.
Key LVM Commands
Section titled “Key LVM Commands”The following commands show how to create and manage LVM components step by step.
Create a Physical Volume
Section titled “Create a Physical Volume”This initializes a disk or partition for LVM use.
pvcreate /dev/sda1Create a Volume Group
Section titled “Create a Volume Group”This combines one or more physical volumes into a storage pool.
vgcreate vg_data /dev/sda1 /dev/sdb1Note: This combines PV’s, no matter if they are built on disks or partitions, nor where they are. So this can span across disk, and use full disks and only patritions on other disks.
Create a Logical Volume
Section titled “Create a Logical Volume”Logical volumes are created from the volume group and act like partitions.
lvcreate -n lv_webcontent -L 64M vg_data-n: Name of the logical volume (here:lv_webcontent).-L: Size of the logical volume (here: 64 MB).- Last argument: VG name (
vg_data).
Note: Each LV can be formatted with it’s own (different) fileystem, without affecting other LV’s, despite might being on the same physical disk.
View LVM Information
Section titled “View LVM Information”You can use these commands to display details about PVs, VGs, and LVs.
pvdisplayvgdisplaylvdisplayExtend an LV
Section titled “Extend an LV”To increase the size of a logical volume and then resize the filesystem, you use these commands.
lvextend -L +500M /dev/vg_data/lv_webcontentresize2fs /dev/vg_data/lv_webcontent # For ext4 filesystemReduce an LV
Section titled “Reduce an LV ”To shrink logical volumes, you can use these commands to unmount the disk, shrink the filesystem and shrink the volume.
umount /mnt/webcontente2fsck -f /dev/vg_data/lv_webcontentresize2fs /dev/vg_data/lv_webcontent 100Mlvreduce -L 100M /dev/vg_data/lv_webcontentWarning: Shrinking a logical volume is risky and should always be done offline.
RAID with LVM
Section titled “RAID with LVM”LVM supports RAID techniques inside a volume group for redundancy or performance. You can use mirror (RAID1 ) for redundancy or striping (RAID0) for performance. The commands below will configure that.
lvcreate --type raid1 -m1 -L 1G -n lv_mirror vg_datalvcreate --type raid0 -i2 -L 2G -n lv_striped vg_data--type: RAID level (raid0, raid1, raid5, raid6, raid10).-m: number of mirrors (RAID1).-i: number of stripes (RAID0).
Filesystems and Mounts
Section titled “Filesystems and Mounts”After creating a logical volume, you can format it with a filesystem and mount it, so it can be used.
Format LV with Filesystem
Section titled “Format LV with Filesystem”mkfs.ext4 /dev/vg_data/lv_webcontentMount Filesystem
Section titled “Mount Filesystem”To be able to realy use the volume, now with filesystem, as a folder you can put stuff in, you have to mount it. This can be done by a single mount command like shown below. Here is de www folder in the path /var/www the mountpoint. Simply put, all contents of that folder, are actualy stored on the LV lv-webcontent.
mount /dev/vg_data/lv_webcontent /var/wwwPersistent Mount (at boot)
Section titled “Persistent Mount (at boot)”To make a mount persistant and automatically mounted at every boot, you can add the mount to /etc/fstab like shown below.
/dev/vg_data/lv_webcontent /var/www ext4 defaults 0 2| Field | Description |
|---|---|
/dev/vg_data/lv_webcontent | The device or logical volume to mount. |
/var/www | The directory where the filesystem will be mounted. |
ext4 | The type of filesystem used on the volume. |
defaults | Mount options (e.g. read/write, exec, auto). Uses system defaults. |
0 | Dump flag. Usually 0 to disable backup with dump. |
2 | fsck order. 1 is for root filesystem, 2 is for others, 0 disables check. |
After mounting, you can manage the folder and it’s contents with the Linux permissions. You can see Filesystem permissions in Linux for more information about the different permissions you can assign in Linux to files and folders.
Check configuration
Section titled “Check configuration”With the commands below, you can check the current configuration of your machine.
- Check free space in VG:
vgs - List LVs:
lvs - List VGs:
vgs - List PVs:
pvs - Mount overview:
lsblk,df -h