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¶
List physical volumes¶
Display information about physical volumes. TLDR
Display various attributes of physical volume(s). TLDR
List volume groups¶
Display information about volume groups. TLDR
Display volume group information. TLDR
List logical volumes, devices, and tags¶
Display information about logical volumes. TLDR
Display information about logical volumes, including devices and tags.
Display information about a logical volume. TLDR
Physical volumes¶
Create a physical volume¶
Warning
The device must not contain data that needs to be retained.
{{/dev/sdX}}- Path to block device
Volume groups¶
Create a volume group¶
{{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¶
{{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¶
{{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¶
{{size}}- Size of the logical volume to create. For example, use a size such as20Gor500M.{{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¶
{{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 as20G.{{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¶
{{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:
{{amount_to_add}}- Amount by which to increase the logical volume and filesystem. For example, use an amount such as10G.{{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:
{{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 as20G.{{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.
{{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.
{{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 as10G.{{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.
{{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¶
{{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 as20G.{{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¶
{{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¶
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