Skip to content

LVM

Warning

LVM requires root permissions. Either log into the root account or use sudo for commands.

Terminology

  • Physical Volume (PV): A physical storage device (e.g., /dev/sda1) that has been initialized for LVM use
  • Volume Group (VG): A pool of storage created by combining one or more physical volumes
  • Logical Volume (LV): A virtual disk created from a volume group, which can be formatted with a filesystem
  • Extent: The smallest unit of space allocation in LVM (default size is 4 MiB)
  • Snapshot: A point-in-time copy of a logical volume that allows you to revert changes

Inspect storage

Show block devices and filesystems

lsblk --fs

List physical volumes

Display information about physical volumes. TLDR

pvs

Display various attributes of physical volume(s). TLDR

pvdisplay

List volume groups

Display information about volume groups. TLDR

vgs

Display volume group information. TLDR

vgdisplay

List logical volumes, devices, and tags

Display information about logical volumes. TLDR

lvs

Display information about logical volumes, including devices and tags.

lvs -o +devices,tags

Display information about a logical volume. TLDR

lvdisplay

Physical volumes

Create a physical volume

Warning

The device must not contain data that needs to be retained.

pvcreate {{/dev/sdX}}
  • {{/dev/sdX}} - Path to block device

Volume groups

Create a volume group

vgcreate {{volume_group_name}} {{/dev/sdX}}
  • {{volume_group_name}} - Name of the volume group to create
  • {{/dev/sdX}} - Path to the physical volume. Multiple can be specified by using a space between them.

Add a physical volume to a volume group

vgextend {{volume_group_name}} {{/dev/sdX}}
  • {{volume_group_name}} - Name of the volume group to extend
  • {{/dev/sdX}} - Path to the physical volume. Multiple can be specified by using a space between them.

Rename a volume group

vgrename {{current_volume_group_name}} {{new_volume_group_name}}
  • {{current_volume_group_name}} - Name of the volume group
  • {{new_volume_group_name}} - New name of the volume group

Logical volumes

Create a logical volume with a fixed size

lvcreate --size {{size}} --name {{logical_volume_name}} {{volume_group_name}}
  • {{size}} - Size of the logical volume to create. For example, use a size such as 20G or 500M.
  • {{logical_volume_name}} - Name of the logical volume to create
  • {{volume_group_name}} - Volume group to create the logical volume in

Create a logical volume using all free space

lvcreate --extents 100%FREE --name {{logical_volume_name}} {{volume_group_name}}
  • {{logical_volume_name}} - Name of the logical volume to create
  • {{volume_group_name}} - Volume group to create the logical volume in

Create a striped logical volume

Specify one physical volume for each stripe.

lvcreate --stripes {{number_of_stripes}} --size {{size}} --name {{logical_volume_name}} {{volume_group_name}} {{path/to/physical_volume_1}} {{path/to/physical_volume_2}}
  • {{number_of_stripes}} - Number of physical volumes across which to stripe data
  • {{size}} - Total size of the logical volume to create. For example, use a size such as 20G.
  • {{logical_volume_name}} - Name of the logical volume to create
  • {{volume_group_name}} - Volume group to create the logical volume in
  • {{path/to/physical_volume_1}} and {{path/to/physical_volume_2}} - Physical volumes to use for the stripes

Rename a logical volume

lvrename {{volume_group_name}} {{current_logical_volume_name}} {{new_logical_volume_name}}
  • {{volume_group_name}} - Name of the volume group containing the logical volume
  • {{current_logical_volume_name}} - Current name of the logical volume
  • {{new_logical_volume_name}} - New name of the logical volume

Add or remove a tag

lvchange --addtag {{tag_name}} {{volume_group_name}}/{{logical_volume_name}}
lvchange --deltag {{tag_name}} {{volume_group_name}}/{{logical_volume_name}}
  • {{tag_name}} - Tag to add or remove
  • {{volume_group_name}} - Name of the volume group containing the logical volume
  • {{logical_volume_name}} - Name of the logical volume to update

Activate or deactivate a logical volume

Unmount the filesystem before deactivating its logical volume.

lvchange --activate y {{volume_group_name}}/{{logical_volume_name}}
lvchange --activate n {{volume_group_name}}/{{logical_volume_name}}
  • {{volume_group_name}} - Name of the volume group containing the logical volume
  • {{logical_volume_name}} - Name of the logical volume to activate or deactivate

Filesystems

Create and mount an ext4 filesystem

mkfs.ext4 /dev/{{volume_group_name}}/{{logical_volume_name}}
mkdir -p {{path/to/mount_point}}
mount /dev/{{volume_group_name}}/{{logical_volume_name}} {{path/to/mount_point}}
  • {{volume_group_name}} - Name of the volume group containing the logical volume
  • {{logical_volume_name}} - Name of the logical volume on which to create the filesystem
  • {{path/to/mount_point}} - Directory at which to mount the filesystem

Extend a logical volume and its filesystem

Add a fixed amount:

lvextend --resizefs --size +{{amount_to_add}} /dev/{{volume_group_name}}/{{logical_volume_name}}
  • {{amount_to_add}} - Amount by which to increase the logical volume and filesystem. For example, use an amount such as 10G.
  • {{volume_group_name}} - Name of the volume group containing the logical volume
  • {{logical_volume_name}} - Name of the logical volume to extend

Use all remaining free space in the volume group:

lvextend --resizefs --extents +100%FREE /dev/{{volume_group_name}}/{{logical_volume_name}}
  • {{volume_group_name}} - Name of the volume group containing the logical volume
  • {{logical_volume_name}} - Name of the logical volume to extend

Reduce an ext4 logical volume and filesystem

Back up the filesystem first. Reducing a logical volume incorrectly can destroy data. XFS filesystems cannot be shrunk.

umount /dev/{{volume_group_name}}/{{logical_volume_name}}
e2fsck -f /dev/{{volume_group_name}}/{{logical_volume_name}}
resize2fs /dev/{{volume_group_name}}/{{logical_volume_name}} {{new_size}}
lvreduce --size {{new_size}} /dev/{{volume_group_name}}/{{logical_volume_name}}
mount /dev/{{volume_group_name}}/{{logical_volume_name}} {{path/to/mount_point}}
  • {{volume_group_name}} - Name of the volume group containing the logical volume
  • {{logical_volume_name}} - Name of the logical volume to reduce
  • {{new_size}} - New size for both the filesystem and logical volume. For example, use a size such as 20G.
  • {{path/to/mount_point}} - Directory at which to remount the filesystem

Move data between physical volumes

Move all allocated extents

Both physical volumes must belong to the same volume group.

pvmove {{path/to/source_physical_volume}} {{path/to/destination_physical_volume}}
  • {{path/to/source_physical_volume}} - Physical volume from which to move allocated extents
  • {{path/to/destination_physical_volume}} - Physical volume to which to move allocated extents

Move one logical volume

pvmove --name {{volume_group_name}}/{{logical_volume_name}} {{path/to/source_physical_volume}} {{path/to/destination_physical_volume}}
  • {{volume_group_name}} - Name of the volume group containing the logical volume
  • {{logical_volume_name}} - Name of the logical volume to move
  • {{path/to/source_physical_volume}} - Physical volume from which to move the logical volume's extents
  • {{path/to/destination_physical_volume}} - Physical volume to which to move the logical volume's extents

Remove an unused physical volume from a volume group

Confirm that pvs reports no allocated extents on the device before removing it.

vgreduce {{volume_group_name}} {{path/to/physical_volume}}
pvremove {{path/to/physical_volume}}
  • {{volume_group_name}} - Name of the volume group from which to remove the physical volume
  • {{path/to/physical_volume}} - Physical volume to remove from the volume group and LVM use

Snapshots

Create a snapshot

Snapshot space must accommodate changes made to the origin while the snapshot exists.

lvcreate --snapshot --size {{snapshot_size}} --name {{snapshot_name}} /dev/{{volume_group_name}}/{{origin_logical_volume_name}}
  • {{snapshot_size}} - Space reserved to track changes while the snapshot exists. For example, use a size such as 10G.
  • {{snapshot_name}} - Name of the snapshot logical volume to create
  • {{volume_group_name}} - Name of the volume group containing the origin logical volume
  • {{origin_logical_volume_name}} - Name of the logical volume to snapshot

Restore a snapshot

Merging discards changes made to the origin after the snapshot was created. If the origin is active, the merge completes on its next activation.

lvconvert --merge /dev/{{volume_group_name}}/{{snapshot_name}}
  • {{volume_group_name}} - Name of the volume group containing the snapshot
  • {{snapshot_name}} - Name of the snapshot to merge into its origin logical volume

Remove a snapshot

lvremove /dev/{{volume_group_name}}/{{snapshot_name}}
  • {{volume_group_name}} - Name of the volume group containing the snapshot
  • {{snapshot_name}} - Name of the snapshot to remove

RAID

Create a RAID 1 logical volume

lvcreate --type raid1 --mirrors 1 --size {{size}} --name {{logical_volume_name}} {{volume_group_name}} {{path/to/physical_volume_1}} {{path/to/physical_volume_2}}
  • {{size}} - Size of the RAID 1 logical volume to create. For example, use a size such as 20G.
  • {{logical_volume_name}} - Name of the logical volume to create
  • {{volume_group_name}} - Volume group to create the logical volume in
  • {{path/to/physical_volume_1}} and {{path/to/physical_volume_2}} - Physical volumes on which to store the mirrored copies

Convert an existing logical volume to RAID 1

lvconvert --type raid1 --mirrors 1 /dev/{{volume_group_name}}/{{logical_volume_name}}
  • {{volume_group_name}} - Name of the volume group containing the logical volume
  • {{logical_volume_name}} - Name of the logical volume to convert

Check RAID health

lvs -a -o name,segtype,devices,health_status

Remove LVM storage

These commands permanently destroy the logical volume and its data.

umount /dev/{{volume_group_name}}/{{logical_volume_name}}
lvremove /dev/{{volume_group_name}}/{{logical_volume_name}}
vgremove {{volume_group_name}}
pvremove {{path/to/physical_volume}}
  • {{volume_group_name}} - Name of the volume group to remove
  • {{logical_volume_name}} - Name of the logical volume to unmount and remove
  • {{path/to/physical_volume}} - Physical volume to remove from LVM use