What it is
LVM (Logical Volume Management) provides a flexible layer of abstraction over physical storage, allowing you to create and manage logical volumes that can span multiple physical disks or partitions, offering features like resizing, snapshots, and striping.
Installation
Linux
LVM is typically installed by default on most Linux distributions. If not, you can install it using your package manager:
Debian/Ubuntu:
sudo apt update
sudo apt install lvm2
Fedora/CentOS/RHEL:
sudo yum install lvm2
# or
sudo dnf install lvm2
Mac
LVM is not natively supported on macOS. You would typically use Disk Utility or third-party tools for volume management on macOS.
Windows
LVM is not natively supported on Windows. Windows uses its own volume management system (Disk Management, Storage Spaces).
Core Concepts
- Physical Volume (PV): A raw disk partition or whole disk that has been initialized for use by LVM.
- Volume Group (VG): A pool of storage created by combining one or more Physical Volumes. Logical Volumes are carved out of Volume Groups.
- Logical Volume (LV): A "virtual" partition that is created from a Volume Group. These are what you format and mount like regular partitions.
- Physical Extent (PE): The smallest unit of storage that LVM allocates from a Physical Volume. All PEs within a VG are the same size.
- Logical Extent (LE): The smallest unit of storage that LVM allocates for a Logical Volume. LEs map to PEs. The size of LEs can differ from PEs, but is usually the same.
- Snapshot: A point-in-time copy of a Logical Volume. Snapshots can be used for backups or testing.
Commands / Usage
Initializing and Managing Physical Volumes (PVs)
Initialize a partition as a Physical Volume:
sudo pvcreate /dev/sdb1
Initializes the partition /dev/sdb1 to be used as an LVM Physical Volume.
Initialize a whole disk as a Physical Volume:
sudo pvcreate /dev/sdc
Initializes the entire disk /dev/sdc to be used as an LVM Physical Volume.
List all Physical Volumes:
sudo pvdisplay
Displays detailed information about all Physical Volumes on the system.
List Physical Volumes in a more compact format:
sudo pvs
Shows a summary of Physical Volumes, including their size and VG association.
Remove a Physical Volume:
sudo pvremove /dev/sdb1
Removes the LVM metadata from a Physical Volume, making it available for other uses. Caution: This does not delete data on the disk if it was already part of a VG.
Creating and Managing Volume Groups (VGs)
Create a Volume Group using one or more PVs:
sudo vgcreate my_vg /dev/sdb1 /dev/sdc1
Creates a Volume Group named my_vg using the Physical Volumes /dev/sdb1 and /dev/sdc1.
Create a Volume Group using all available PVs:
sudo vgcreate data_vg /dev/sd[d-f]
Creates a Volume Group named data_vg using all partitions from /dev/sdd1 to /dev/sdf1.
List all Volume Groups:
sudo vgdisplay
Displays detailed information about all Volume Groups on the system.
List Volume Groups in a more compact format:
sudo vgs
Shows a summary of Volume Groups, including their size and the PVs they contain.
Extend a Volume Group with new Physical Volumes:
sudo vgextend my_vg /dev/sdd1
Adds the Physical Volume /dev/sdd1 to the existing Volume Group my_vg.
Reduce a Volume Group by removing Physical Volumes:
sudo vgreduce my_vg /dev/sdb1
Removes the Physical Volume /dev/sdb1 from the Volume Group my_vg. Caution: All Logical Volumes using space on /dev/sdb1 must be moved or removed first.
Remove a Volume Group:
sudo vgremove my_vg
Removes the Volume Group my_vg. Caution: All Logical Volumes within the VG must be removed first.
Creating and Managing Logical Volumes (LVs)
Create a Logical Volume with a specific size:
sudo lvcreate -L 10G -n my_lv my_vg
Creates a Logical Volume named my_lv of size 10 Gigabytes from the Volume Group my_vg.
Create a Logical Volume filling the remaining space in a VG:
sudo lvcreate -l 100%FREE -n big_data_lv my_vg
Creates a Logical Volume named big_data_lv that uses all the remaining free space in my_vg.
Create a Logical Volume as a stripe across multiple PVs for performance:
sudo lvcreate --stripes 2 --stripesize 64K -L 50G -n striped_lv my_vg /dev/sdb1 /dev/sdc1
Creates a 50GB Logical Volume named striped_lv striped across two PVs (/dev/sdb1, /dev/sdc1) with a stripe size of 64KB.
Create a Logical Volume as a mirror for redundancy:
sudo lvcreate --mirror y -L 20G -n mirrored_lv my_vg /dev/sdb1 /dev/sdc1
Creates a 20GB Logical Volume named mirrored_lv mirrored across /dev/sdb1 and /dev/sdc1.
List all Logical Volumes:
sudo lvdisplay
Displays detailed information about all Logical Volumes on the system.
List Logical Volumes in a more compact format:
sudo lvs
Shows a summary of Logical Volumes, including their size, VG, and device path.
Extend a Logical Volume:
sudo lvextend -L +5G /dev/my_vg/my_lv
Adds 5 Gigabytes to the Logical Volume /dev/my_vg/my_lv.
Shrink a Logical Volume:
sudo lvreduce -L 8G /dev/my_vg/my_lv
Shrinks the Logical Volume /dev/my_vg/my_lv to 8 Gigabytes. Caution: This requires unmounting the filesystem and can lead to data loss if not done carefully.
Remove a Logical Volume:
sudo lvremove /dev/my_vg/my_lv
Removes the Logical Volume /dev/my_vg/my_lv. Caution: This permanently deletes all data on the LV.
Resizing Filesystems After LV Resizing
After resizing a Logical Volume, you must also resize the filesystem on it.
Resize an ext4 filesystem:
sudo resize2fs /dev/my_vg/my_lv
Resizes the ext4 filesystem on /dev/my_vg/my_lv to match the current size of the Logical Volume.
Resize an XFS filesystem:
sudo xfs_growfs /mount/point/of/my_lv
Resizes the XFS filesystem mounted at /mount/point/of/my_lv to match the current size of the Logical Volume.
Snapshots
Create a snapshot of a Logical Volume:
sudo lvcreate --size 5G --snapshot --name my_lv_snap /dev/my_vg/my_lv
Creates a 5GB snapshot named my_lv_snap of the Logical Volume my_lv from my_vg.
List snapshots:
sudo lvdisplay /dev/my_vg/
Snapshots are listed alongside regular LVs.
Remove a snapshot:
sudo lvremove /dev/my_vg/my_lv_snap
Removes the snapshot my_lv_snap.
Restore a Logical Volume from a snapshot (requires unmounting):
sudo umount /dev/my_vg/my_lv
sudo lvconvert --merge /dev/my_vg/my_lv_snap
Merges the snapshot my_lv_snap back into the original Logical Volume my_lv. The snapshot is destroyed after the merge.
Common Patterns
Setting up a new LVM disk:
- Initialize the disk/partition as a PV:
sudo pvcreate /dev/sdX1 - Create a VG from the PV:
sudo vgcreate vg_new_data /dev/sdX1 - Create an LV of desired size:
sudo lvcreate -L 50G -n lv_data vg_new_data - Format the LV:
sudo mkfs.ext4 /dev/vg_new_data/lv_data - Mount the LV:
sudo mkdir /mnt/new_storage sudo mount /dev/vg_new_data/lv_data /mnt/new_storage - Add to
/etc/fstabfor persistence:echo "/dev/vg_new_data/lv_data /mnt/new_storage ext4 defaults 0 0" | sudo tee -a /etc/fstab
Resizing an existing LVM-based filesystem:
- Extend the Logical Volume:
sudo lvextend -L +10G /dev/my_vg/my_lv - Resize the filesystem (e.g., ext4):
(For XFS, usesudo resize2fs /dev/my_vg/my_lvsudo xfs_growfs /mount/point/of/my_lv)
Creating a backup using a snapshot:
- Create a snapshot:
sudo lvcreate --size 10G --snapshot --name my_lv_backup_snap /dev/my_vg/my_lv - Mount the snapshot and back up its contents:
sudo mkdir /mnt/snapshot_backup sudo mount /dev/my_vg/my_lv_backup_snap /mnt/snapshot_backup # Perform backup using rsync, tar, etc. sudo rsync -avz /mnt/snapshot_backup/ /path/to/backup/location/ - Unmount and remove the snapshot:
sudo umount /mnt/snapshot_backup sudo lvremove /dev/my_vg/my_lv_backup_snap
Gotchas
- Data Loss on
pvremoveandvgremove: These commands only remove LVM metadata. If the PV/VG contained data, that data is still there but inaccessible through LVM. It’s crucial to ensure no data is on the disk/VG before removal, or to move data off first. - Filesystem Resizing is Mandatory: Simply extending an LV does not make the space available to the OS. You must resize the filesystem on the LV afterwards using
resize2fs(for ext2/3/4) orxfs_growfs(for XFS). - Shrinking Filesystems: Shrinking LVs and filesystems is much riskier than growing. Always back up data before attempting to shrink. You typically need to unmount the filesystem and ensure the filesystem data is moved to the beginning of the LV before shrinking.
- Snapshot Space: Snapshots consume space in the original Volume Group. If the VG runs out of space, the snapshot becomes invalid and can no longer be used. You need to monitor VG free space when using snapshots.
- Snapshot Merge Direction: When merging a snapshot back into the original LV (
lvconvert --merge), the merge is always from the snapshot to the original. If you want to revert the original LV to the snapshot’s state, you must merge the snapshot into the original. - Device Naming: Logical Volumes are typically represented as
/dev/<volume_group_name>/<logical_volume_name>or/dev/<volume_group_name>/lvol<number>. The former is preferred for readability. - Permissions: Most LVM commands require root privileges (
sudo).