Skip to main content

Managing LVM Volumes

1. Understanding LVM Concepts

Concept Description Details
Volume Group (VG) Logical storage container Combines multiple physical volumes into a single logical unit. Acts as a pool for logical volumes.
Logical Volume (LV) Virtual disk partition Allocated from a volume group, behaves like a traditional partition but can be resized dynamically.
Physical Volume (PV) Actual storage device Represents a physical disk or partition used to build volume groups.

Example: Creating a basic LVM setup

# Create a physical volume from /dev/sdb
pvcreate /dev/sdb

# Create a volume group named "my_vg"
vgcreate my_vg /dev/sdb

# Create a logical volume named "my_lv" with 5GB size
lvcreate -L 5G -n my_lv my_vg

# Format and mount the logical volume
mkfs.ext4 /dev/my_vg/my_lv
mount /dev/my_vg/my_lv /mnt/lvm

Key Benefits of LVM

  • Dynamic resizing of volumes without downtime
  • Ability to combine multiple disks into one logical unit
  • Snapshot support for backups and testing
  • Easy management of storage allocation

2. Creating Physical Volumes

Command Description Example
pvcreate Initializes a disk or partition as a physical volume pvcreate /dev/sdb
pvcreate --uuid Assigns a specific UUID to the physical volume pvcreate --uuid 12345678-1234-1234-1234-123456789012 /dev/sdc

Example: Creating a Physical Volume

pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
Note: The device must not be in use and should not contain a filesystem.
Warning: Data on the device will be lost when creating a physical volume.

3. Viewing Physical Volumes

Command Description Example Output
pvs Display information about physical volumes /dev/sda1 100% 10G 0 100% 0
pvs -v Verbose output with additional details /dev/sda1 100% 10G 0 100% 0 0 0
pvs --segments Show segment information for physical volumes /dev/sda1 100% 10G 0 100% 0

Example: Using pvs command

pvs

Example: Using pvs with verbose flag

pvs -v

4. Creating Volume Groups

Command Description Example
vgcreate Creates a new volume group vgcreate vg0 /dev/sdb1
vgcreate -s Specify extent size (default is 4MB) vgcreate -s 128M vg0 /dev/sdb1
vgcreate --physicalextentsize Set physical extent size in MB or KB vgcreate --physicalextentsize 16M vg0 /dev/sdb1

Example: Creating a Volume Group with Two Physical Volumes

vgcreate vg0 /dev/sdb1 /dev/sdc1
Note: The physical volumes must be initialized with pvcreate before they can be used in a volume group.
Warning: Ensure that the physical volumes are not already part of another volume group or used in a filesystem.

5. Viewing Volume Groups

Command Description Example
vgdisplay Displays detailed information about volume groups vgdisplay
vgscan Scans for volume groups and updates the volume group cache vgscan
vgs Lists volume groups with basic information vgs
vgchange Enables or disables volume groups vgchange -an <vg_name>

Example: Displaying Volume Group Details

vgs -o vg_name,pv_count,lv_count,vg_attr
Note: The vgs command provides a quick overview of volume groups, while vgdisplay offers more detailed configuration information.

6. Extending Volume Groups

Command Description Example
vgextend Extends a volume group by adding physical volumes to it vgextend /dev/vg0 /dev/sdb
Note: The physical volume must be already initialized with pvcreate.

Example: Extending Volume Group with New Disk

# Create physical volume on new disk
pvcreate /dev/sdb

# Extend the volume group to include the new physical volume
vgextend vg0 /dev/sdb

7. Creating Logical Volumes

Property Description Example
lvcreate Command to create a new logical volume lvcreate -L 10G -n mylv vg0
-L size Size of the logical volume in MB, GB, etc. -L 5G
-l extents Number of logical extents (default 100% free) -l 100
-n name Name of the logical volume to create -n mylv
vg_name Volume group to create the logical volume in vg0

Example: Create a 5GB Logical Volume

lvcreate -L 5G -n mylv vg0
Note: The logical volume will be created in the specified volume group and can be formatted with a filesystem.

8. Viewing Logical Volumes

Command Description Example Output
lvs Lists all logical volumes with basic information
LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Copy%  Sync%  Open
root root   -wi-ao----  10.00g
lvs -a Lists all logical volumes including hidden/system ones
LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Copy%  Sync%  Open
root root   -wi-ao----  10.00g
lvs -o +lv_attr Shows detailed attributes of logical volumes
LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Copy%  Sync%  Open
root root   -wi-ao----  10.00g
lvs -o lv_name,vg_name,lv_size,lv_attr Displays specific fields for logical volumes
LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Copy%  Sync%  Open
root root   -wi-ao----  10.00g

Example: View all logical volumes with full details

lvs -a -o +lv_attr,vg_name,lv_size,lv_permissions,lv_layout
Note: The lvs command is part of the LVM tools suite and requires root privileges to view all volumes.

9. Extending Logical Volumes

Command Description Example
lvextend Extends a logical volume to use additional space lvextend -L +5G /dev/vg0/lv_data
lvextend Extends a logical volume to use all available space in the volume group lvextend -l +100%FREE /dev/vg0/lv_data

Example: Extend Logical Volume by Size

lvextend -L +10G /dev/vg0/lv_home

Example: Extend Logical Volume to Use All Free Space

lvextend -l +100%FREE /dev/vg0/lv_data
Note: After extending the logical volume, you must also extend the filesystem to use the new space.

Example: Extend XFS Filesystem After Extending LV

xfs_growfs /home

Example: Extend ext4 Filesystem After Extending LV

resize2fs /dev/vg0/lv_data

10. Resizing File Systems

File System Resize Command Notes
ext2/ext3/ext4 resize2fs Can resize online or offline
XFS xfs_growfs Only supports online resizing
Btrfs btrfs filesystem resize Supports online and offline resizing

Example: Resizing ext4 File System

resize2fs /dev/sdb1 5G

Example: Resizing XFS File System

xfs_growfs /mount/point
Note: Before resizing, ensure that the underlying LVM volume is already resized to accommodate the new filesystem size.

11. Creating LVM Snapshots

LVM snapshots provide a point-in-time copy of a logical volume, useful for backup and testing purposes. Snapshots are read-only by default but can be made writable with additional steps.

Command Description Notes
lvcreate -s Creates a snapshot logical volume Must be created from an existing LV

Example: Creating a Snapshot

lvcreate -L 1G -s -n snapshot_name /dev/vg_name/original_lv
Note: The snapshot size should be sufficient to accommodate changes made after the snapshot is created. If the original volume grows beyond the snapshot size, it can lead to data loss or system instability.
Warning: Snapshots are not a replacement for backups. They should be used as part of a broader data protection strategy.

Snapshot Creation Steps

  • Create a new logical volume with the same size as the original
  • Set the snapshot to be read-only (default)
  • Mount or use the snapshot as needed

12. Removing LVM Components

Example: Remove Logical Volume

lvremove /dev/vg0/lv_data
Component Action Notes
Logical Volume lvremove <path> Must be unmounted before removal
Volume Group vgremove <vg_name> Must be inactive before removal
Physical Volume pvremove <pv_path> Must be removed from VG before deletion
Note: Always backup data before removing LVM components.
Warning: Removing a logical volume will permanently delete all data.

Workflow: Remove LVM Components Safely

  1. Unmount the logical volume
  2. Deactivate the logical volume: lvchange -an <lv_path>
  3. Remove the logical volume: lvremove <lv_path>
  4. Remove the volume group: vgremove <vg_name>
  5. Remove the physical volume: pvremove <pv_path>

Was this article helpful?