Logical Volume Management (LVM)¶
What is LVM?¶
Logical Volume Management (LVM) allows flexible management of disk storage by abstracting the physical hardware and creating an easier-to-manage virtual storage layer.
How does LVM work?¶
In a nutshell:
- Raw disks are loaded into LVM as physical volumes.
- Those physical volumes are then aggregated into a volume group.
- That volume group is then used to create logical volumes.
- The logical volume is formatted with
mkfs
and mounted.
From there, you can use the storage space of any of the physical volumes in the volume group(s) to add to a logical volume.
Where Logical Volumes are Stored¶
When a logical volume is created, it's stored in /dev/mapper
as /dev/mapper/myvg-mylv
.
A symlink to the LV is also created for convenience when it's created with lvcreate
.
The symlinks are stored in /dev/
as /dev/myvg/mylv
.
LVM Tools Cheatsheet¶
Tools for managing Physical Volumes, Volume Groups, and Logical Volumes (PV, VG, LV): For more indepth info on these, see this section.
pvcreate
: Creates a PV from 8e type partitionvgcreate
: Creates VG using PVslvmdiskscan
: Displays all storage devicesvgscan
: Scans all physical devices, searches for VGspvdata
: Displays debugging information about PV, reads VGDApvscan
: Scans PVs and displays activepvmove
: Moves data from one PV to another inside one VGvgreduce
: Removes PV from VGpvdisplay
: Displays information about physical volumesvgdisplay
: Displays information about volume groupslvdisplay
: Displays information about logical volumesvgchange
: Activates or deactivates VGvgexport
: Makes VGs unknown to the system, used prior to importing them on a different systemvgimport
: Imports VG from a different systemvgsplit
: Splits PV from existing VG into new onevgmerge
: Merges two VGs-
lvcreate
: Creates LV inside VGlvcreate vg1 -n space -L 5G # Create a logical volume called space, with 5GB of storage space lvcreate vg1 -n storage -l +100%FREE # Create a logical volume called storage, with all free space inside the VG.
-n
: Name of the LV-L
: Size of the LV- Use
-l +100%FREE
to use all available space in the VG.man://lvcreate 558
- The
-L
(--size
) and-l
(--extents
) options are alternate methods of specifying size. -
lvcreate --snapshot
: Creates a snapshot of a LV -
This creates only a 1 gig snapshot of the LV.
- For a snapshot, the size can be expressed with
-l
as a percentage of the total size of the origin LV with the suffix%ORIGIN
(100%ORIGIN
provides space for the whole origin).
-
lvextend
: Increases the size of LV lvreduce
: Decreases the size of LV
LVM, Step by Step¶
LVM starts with turning raw disks into physical volumes.
Then the physical volumes are aggregated into a volume group.
The storage space of all the disks in the volume group can then be used by a logical volume.
Raw Disks and Physical Volumes¶
-
Raw disks are unformatted, unpartitioned disks that are available for use in the LVM setup.
- MBR (BIOS-based operating systems) uses the partition type code
8e
for LVM, while GPT (UEFI-based operating systems) uses thelvm
flag to indicate an LVM partition.
- MBR (BIOS-based operating systems) uses the partition type code
-
Before raw disks can be used by LVM, they are initialized as physical volumes (PV) using
pvcreate
. pvcreate
gives permission to LVM to use these raw disks as storage devices. This initializes/dev/sdb
and/dev/sdc
as physical volumes.
Checking the Type of a Disk¶
You can check whether a disk is using GPT or MBR by using lsblk -f
PTTYPE
(Partition Type). It will show gpt
for GPT, or dos
for MBR.
You can also use the blkid
command to check the partitioning scheme:
-p
: Low-level probing mode.
The output should indicate eitherPTTYPE=gpt
for GPT orPTTYPE=dos
for MBR.
To check if the disk partitions are the correct type for LVM (8e
or lvm
):
-
For either MBR or GPT, you can use
lsblk
orblkid
to see if partitions are marked for LVM -
To check on MBR (Master Boot Record, used by BIOS-based operating systems):
There will be aType
column that will show8e
for LVM. -
To check on GPT (GUID Partition Tables, used by UEFI-based operating systems): To check if the disks are the correct type for LVM (
There will be a8e
), you can usefdisk -l
Flags
field that will showlvm
for LVM.
Physical Volumes and Volume Groups¶
-
Once physical volumes are created, they are aggregated (grouped) together into a volume group (VG) using
vgcreate
.- A volume group is essentially a pool of storage made up of the physical volumes created with
pvcreate
. - You can allocate space from this group when creating logical volumes.
- E.g.:
This creates a volume group named
my_volume_group
that includes/dev/sdb
and/dev/sdc
.
- A volume group is essentially a pool of storage made up of the physical volumes created with
Volume Groups and Logical Volumes¶
- The next step is to create logical volumes (LV) from the volume group using
lvcreate
.- Logical volumes act like partitions inside a volume group, but they are much more flexible because you can resize them on the fly and span them across multiple physical volumes.
- E.g.:
This creates a 50GB logical volume named
my_logical_volume
from themy_volume_group
volume group.
Formatting and Mounting Logical Volumes¶
-
Once the logical volume is created, you need to format it with a filesystem so it can be used to store data. This is done with
mkfs
.- The most common filesystems are
ext4
andxfs
, though there are others likebtrfs
. - E.g.:
This formats the logical volume with the
ext4
filesystem.
- The most common filesystems are
-
After formatting, the logical volume is ready to be mounted and used like any other filesystem.
- E.g.:
This mounts the logical volume to
/mnt/my_mount_point
.
- E.g.:
This mounts the logical volume to
Implementing RAID on Logical Volumes with mdadm¶
If redundancy or performance is a concern, you can configure RAID (Redundant Array of
Independent Disks) using a tool like mdadm
(Multiple Disk Admin).
RAID and LVM are separate technologies.
- RAID provides redundancy and performance benefits, while LVM provides flexibility in managing storage. You can use them together, but they serve different purposes.
- Example (creating RAID 1 for redundancy):
This creates a RAID 1 array (mirrored) with
/dev/sdb
and/dev/sdc
. Then you can usemd0
as a physical volume in your LVM setup.
Creating a Logical Volume from Raw Disks¶
fdisk -l | grep -i xvd # See all xvd-type raw disks
vgcreate vg1 /dev/xvdb /dev/xvdc # Make a volume group called vg1, with the two physical volumes xvdb and xvdc
pvs # Show all physical volumes
vgextend vg1 /dev/xvde # Add the 3rd physical volume to the volume group
lvcreate vg1 -n space -L 5G # Create a logical volume called space, with 5GB of storage space
lvs
# The logical volume will be stored in /dev/mapper/vg1-space
mkfs.ext4 /dev/mapper/vg1-space
mount /dev/mapper/vg1-space /space
pvdisplay # Show more information than pvs
vgdisplay
lvdisplay
Resizing Logical Volumes¶
One of LVM's best features is the ability to resize logical volumes dynamically.
You can both extend or reduce the size of a logical volume.
- To extend a logical volume, use
lvextend
andresize2fs
:
Tools for Managing Physical Volumes, Volume Groups, and Logical Volumes (PV, VG, LV):¶
Physical Volume Management¶
-
This creates a physical volume on partitionpvcreate
: Initializes a physical volume for use by LVM./dev/sdb1
. The partition should be of the type8e
on MBR (BIOS-based OSs), orlvm
on GPT (UEFI-based OSs). -
pvdisplay
: Displays detailed information about physical volumes. -
pvscan
: Scans all devices for physical volumes and displays active ones. -
pvmove
: Moves physical extents (data) from one physical volume to another within the same volume group.- This is useful when you need to migrate data off a failing disk or redistribute storage.
-
vgreduce
: Removes a physical volume from a volume group, but only after its data has been moved or there are no logical volumes on it.
Volume Group Management¶
-
vgcreate
: Creates a volume group using one or more physical volumes. -
vgdisplay
: Displays information about volume groups. -
vgscan
: Scans all devices for volume groups and displays found VGs. -
vgextend
: Adds one or more physical volumes to an existing volume group, increasing its capacity. -
vgreduce
: Removes a physical volume from a volume group (already covered above). -
vgchange
: Activates or deactivates volume groups.- Activating makes the logical volumes in the VG available for use, while deactivating disables access to the logical volumes.
-
vgexport
: Marks a volume group as inactive and exports it so it can be moved or imported on another system. -
vgimport
: Imports a volume group that has been exported from another system. -
vgsplit
: Splits a volume group into two separate volume groups.- One or more physical volumes from the original VG are moved to a new VG.
-
vgmerge
: Merges two volume groups into one.- The second VG is absorbed into the first.
Logical Volume Management¶
-
lvcreate
: Creates a logical volume from a volume group. You can specify the size and the name for the logical volume.- Example:
-
lvextend
: Increases the size of a logical volume. You must resize the filesystem afterwards.- Example:
-
lvreduce
: Decreases the size of a logical volume. Be careful when reducing the size of an LV to avoid data loss. Ensure that the filesystem is resized before reducing the LV.- Example:
-
lvresize
: Resizes a logical volume, which can be used for both increasing and decreasing the size.- Example:
-
lvdisplay
: Displays information about logical volumes.- Example:
Other Useful LVM Commands¶
-
lvmdiskscan
: Scans for all storage devices and shows their suitability for use as physical volumes in LVM.- Example:
-
pvdata
: Deprecated. This has been replaced by thepvdisplay
command in most modern distributions of Linux.* It used to provide detailed debugging information about physical volumes and Volume Group Descriptor Areas (VGDA).
-
lvremove
: Deletes a logical volume.- Example:
-
vgremove
: Deletes a volume group. You must remove all logical volumes from the VG first.- Example:
-
pvremove
: Removes a physical volume from LVM control, effectively undoing thepvcreate
command.- Example:
More LVM Actions¶
Check available free space in a volume group¶
- You can use the
vgs
command to quickly see the available free space in a volume group.
Filesystem resizing¶
lvextend
: Increases the size of LV
When extending an LV, ensure that the underlying filesystem is resized accordingly.
-
For
ext4
orext3
filesystems: -
For
xfs
filesystems:
LVM snapshots¶
LVM allows you to create snapshots of Logical Volumes for backup/testing.
sudo lvcreate --size 5G --snapshot --name mysnapshot /dev/mapper/myvg-mylv
# Or, use the symlink if you want (and if it's there)
sudo lvcreate --size 5G --snapshot --name mysnapshot /dev/myvg/mylv
LVM Thin Provisioning¶
Thin provisioning allows you to over-allocate storage to logical volumes, and space is allocated only when data is written.
- This is useful for maximizing storage utilization.
Reverting a Logical Volume back to Raw Disks¶
-
Backup data. Removing a logical volume is destructive and irreversible.
-
Identify the LV, VG, and PGs
-
Unmount the LV
Also remove the entry in/etc/fstab
-
Remove the LV
-
Remove the VG
-
Remove the PVs
-
(Optional) Wipe the disk to return it to a raw state.
wipefs -a
will return the disk to a raw, unpartitioned state.
dd
can also be used to do this.