How to use LVM to manage storage on Ubuntu & Debian
Logical Volume Management (LVM) is a powerful tool for managing storage volumes on Linux systems. It offers the flexibility to create logical volumes that can span multiple physical storage devices, providing a layer of abstraction that simplifies storage allocation and management. This is particularly useful in environments where storage requirements are dynamic and may change over time.
This guide will provide a comprehensive overview of setting up and managing LVM volumes on Debian and Ubuntu 18.04, 20.04, and 22.04. We’ll cover the essential concepts, installation process, and practical examples of common LVM operations.
Overview of LVM
Understanding the following key terms and concepts is crucial for working with LVM:
- Physical Volume (PV): A physical disk or partition that is initialized for use by LVM.
- Volume Group (VG): A container that groups together one or more PVs, creating a pool of storage space.
- Logical Volume (LV): A virtual partition created within a VG, which can be formatted and mounted like a regular partition.
- Physical Extent (PE): The smallest unit of storage allocation within a VG.
- Logical Extent (LE): The smallest unit of storage allocation within an LV, typically mapped to a PE.
The primary advantage of Logical Volume Management (LVM) lies in the separation of physical disks from logical volumes. This abstraction enables you to resize, snapshot, and move logical volumes without needing to directly modify the underlying physical storage.
Installing LVM
To begin using LVM, you first need to install the lvm2
package:
$ sudo apt install lvm2
This command installs all the necessary tools for managing LVM volumes.
Creating Physical Volumes
The first step is to initialize disks or partitions as physical volumes (PVs).
For example, to initialize the /dev/sdb1
partition as an LVM physical volume:
$ sudo pvcreate /dev/sdb1
Output:
Physical volume "/dev/sdb1" successfully created
You can verify available PVs using:
$ sudo pvs
Output:
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- <223.57G <223.57G
This will list all physical volumes that LVM can access.
Creating Volume Groups
Next, you need to combine one or more PVs into a volume group (VG).
For example, to create a volume group called data
using the /dev/sdb1
PV:
$ sudo vgcreate data /dev/sdb1
Output:
Volume group "data" successfully created
You can verify available VGs using:
$ sudo vgs
Output:
VG #PV #LV #SN Attr VSize VFree
data 1 0 0 wz--n- <223.57G <223.57G
List the details of a specific VG:
$ sudo vgdisplay data
Output:
--- Volume group ---
VG Name data
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size <223.57 GiB
PE Size 4.00 MiB
Total PE 57313
Alloc PE / Size 0 / 0
Free PE / Size 57313 / <223.57 GiB
Creating Logical Volumes
Once you have a volume group, you can create logical volumes (LVs) within that VG.
For example, to create a 10GB logical volume called logs
:
$ sudo lvcreate -L 10G -n logs data
Output:
Logical volume "logs" created.
This creates a 10GB LV called logs
in the VG data
.
To create an LV that uses all remaining free space in the VG:
$ sudo lvcreate -l 100%FREE -n apps data
Output:
Logical volume "apps" created.
Verify logical volumes:
$ sudo lvs
Output:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
apps data -wi-a----- <213.57G
logs data -wi-a----- 10.00G
You can also see the full path for the created LVs with:
$ sudo lvdisplay
Output:
--- Logical volume ---
LV Path /dev/data/apps
LV Name apps
VG Name data
LV UUID XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
LV Write Access read/write
LV Creation host, time ubuntu-server, 2023-07-26 15:37:11 +0200
LV Status available
# open 0
LV Size <213.57 GiB
Current LE 54698
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
The logical volumes will be mapped to /dev/VG/LV
devices that can be formatted and mounted, just like regular partitions.
Formatting and Mounting Logical Volumes
To use LVM logical volumes, they need to be formatted with a filesystem like any other block device.
For example, to format the logs
LV to ext4 and mount it at /var/log
:
$ sudo mkfs.ext4 /dev/data/logs
$ sudo mkdir /var/log
$ sudo mount /dev/data/logs /var/log
To mount this automatically on reboot, add this to /etc/fstab
:
/dev/data/logs /var/log ext4 defaults 0 0
Do the same for any other LVs you want to use permanently.
Extending a Logical Volume
One of the advantages of LVM is that you can easily extend logical volumes.
For example, to grow the logs
LV by 5GB, first extend the volume group by 5GB:
$ sudo lvextend -L +5G /dev/data/logs
Output:
Size of logical volume data/logs changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
Logical volume data/logs successfully resized.
Then resize the filesystem to match:
$ sudo resize2fs /dev/data/logs
Output:
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/data/logs is mounted on /var/log; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/data/logs is now 153600 (4k) blocks long.
The LV is now 5GB larger.
This is much easier than extending disk partitions, which requires painful steps like moving and resizing adjacent partitions.
Reducing a Logical Volume
To shrink an LV, resize the filesystem first:
$ sudo resize2fs /dev/data/logs 20G
Output:
resize2fs 1.45.5 (07-Jan-2020)
Resizing the filesystem on /dev/data/logs to 5242880 (4k) blocks.
The filesystem on /dev/data/logs is now 5242880 (4k) blocks long.
This shrinks the filesystem to 20GB.
Then shrink the LV size to match:
$ sudo lvreduce -L 20G /dev/data/logs
Output:
Size of logical volume data/logs changed from 15.00 GiB (3840 extents) to 20.00 GiB (5120 extents).
Logical volume data/logs successfully resized.
The logs
LV is now reduced to 20GB.
Creating Snapshots
LVM allows you to create snapshots of logical volumes.
For example, create a snapshot of the logs
LV:
$ sudo lvcreate --size 10G --snapshot --name logs-snap /dev/data/logs
Output:
Logical volume "logs-snap" created.
This will create a snapshot called logs-snap
that is a copy of logs
at the time the snapshot was taken.
Initially, the snapshot uses no space, but as the original LV changes, the snapshot will grow to store the old blocks as they are overwritten.
You can mount, back up, or restore data from the snapshot just like a regular LV.
Monitoring LVM Usage
It’s important to monitor your LVM volume groups and logical volumes to make sure you don’t run out of space.
To see allocated physical extents per VG:
$ sudo vgdisplay -v data
Output:
--- Volume group ---
VG Name data
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size <223.57 GiB
PE Size 4.00 MiB
Total PE 57313
Alloc PE / Size 15360 / 60.00 GiB
Free PE / Size 41953 / <163.57 GiB
To monitor LV usage:
$ sudo lvdisplay
Output:
--- Logical volume ---
LV Path /dev/data/logs
LV Name logs
VG Name data
LV UUID XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX
LV Write Access read/write
LV Creation host, time ubuntu-server, 2022-03-05 10:17:11 +0200
LV Status available
# open 0
LV Size 20.00 GiB
Current LE 5120
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/data/apps
LV Name apps
VG Name data
LV UUID XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX
LV Write Access read/write
LV Creation host, time ubuntu-server, 2023-07-26 15:37:11 +0200
LV Status available
# open 0
LV Size <193.57 GiB
Current LE 49408
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
You can also install an LVM monitoring utility like lvm2 snarf
to get notifications when usage crosses thresholds.
Advantages of LVM
Some of the key advantages of Logical Volume Management (LVM) include:
- Flexibility: LVM allows you to easily resize, move, and snapshot logical volumes without needing to modify the underlying physical disks.
- Aggregation: You can combine multiple physical disks into a single volume group, providing a larger pool of storage space.
- Snapshots: LVM allows you to create snapshots of logical volumes, which can be used for backups or testing purposes.
- Thin Provisioning: You can create logical volumes that are larger than the available physical storage, and the storage will be allocated on demand.
- Simplified Management: LVM simplifies storage management by providing a consistent interface for managing logical volumes, regardless of the underlying physical disks.
Overall, Logical Volume Management (LVM) provides powerful, flexible storage management for your Ubuntu servers.
Conclusion
Logical Volume Management (LVM) offers a robust solution for managing dynamic storage in Ubuntu and Debian environments. It allows for effective handling of changing storage needs and facilitates key functionalities such as snapshots and thin provisioning. While it necessitates learning new concepts and commands, LVM becomes an invaluable asset for deploying and managing Ubuntu storage with hands-on practice. Its flexibility in dynamically growing, shrinking, and migrating volumes makes LVM a worthwhile investment for long-term storage management.
Alternative Solutions for Storage Management on Ubuntu & Debian
While LVM is a powerful tool, other solutions exist for managing storage on Ubuntu and Debian systems. Here are two alternative approaches:
1. ZFS (Zettabyte File System)
Explanation:
ZFS is an advanced file system that integrates volume management capabilities. It provides features like data integrity protection, snapshots, copy-on-write functionality, and built-in RAID-like configurations. Unlike LVM, ZFS combines the functionalities of a traditional file system and a volume manager into a single layer. This integration simplifies storage management and enhances data reliability.
Advantages over LVM:
- Data Integrity: ZFS uses checksums to detect and correct data corruption, ensuring data integrity.
- Simplified Management: ZFS simplifies storage management by integrating volume management and file system functionalities.
- Built-in RAID: ZFS supports various RAID levels (RAID-Z1, RAID-Z2, RAID-Z3) for data redundancy and fault tolerance.
- Compression and Deduplication: ZFS supports data compression and deduplication, which can save storage space and improve performance.
Example:
To create a ZFS pool named tank
using the disks /dev/sdb
and /dev/sdc
in a RAID-Z1 configuration:
sudo apt install zfsutils-linux
sudo zpool create tank raidz1 /dev/sdb /dev/sdc
To create a ZFS filesystem called data
within the tank
pool:
sudo zfs create tank/data
To mount the data
filesystem:
sudo zfs set mountpoint=/mnt/data tank/data
ZFS filesystems are automatically mounted on boot, which eliminates the need to edit /etc/fstab
.
2. Btrfs (B-tree file system)
Explanation:
Btrfs is another modern file system that offers advanced storage management features. It supports copy-on-write, snapshots, subvolumes, and online resizing. Like ZFS, Btrfs aims to address some of the limitations of traditional file systems by integrating volume management features.
Advantages over LVM:
- Snapshots: Btrfs supports snapshots, allowing you to create point-in-time copies of your data.
- Subvolumes: Btrfs allows you to create subvolumes, which are like separate file systems within a larger Btrfs file system.
- Online Resizing: Btrfs allows you to resize file systems while they are mounted and in use.
- Checksumming: Btrfs uses checksums to detect and correct data corruption.
Example:
To create a Btrfs file system on /dev/sdb1
and /dev/sdc1
:
sudo mkfs.btrfs /dev/sdb1 /dev/sdc1
To mount the Btrfs file system:
sudo mount /dev/sdb1 /mnt/btrfs
To create a subvolume:
sudo btrfs subvolume create /mnt/btrfs/subvolume1
These alternative solutions offer different approaches to storage management compared to LVM, each with its own advantages and disadvantages. The choice of which solution to use depends on your specific needs and requirements.