Logical Volume Management (LVM)¶
Table of Contents¶
- What is LVM?
- How does LVM work?
- Where Logical Volumes are Stored
- LVM Tools Cheatsheet
- LVM, Step by Step
- Resizing Logical Volumes
- Tools for Managing Physical Volumes, Volume Groups, and Logical Volumes (PV, VG, LV)
- More LVM Actions
- Reverting a Logical Volume back to Raw Disks
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:
1. Raw disks are loaded into LVM as physical volumes.
2. Those physical volumes are then aggregated into a volume group.
3. That volume group is then used to create logical volumes.
4. 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 VGslvcreate
: 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 LVsudo lvcreate --size 1G --snapshot --name my_snapshot /dev/my_volume_group/my_logical_volume
- 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 initializessudo pvcreate /dev/sdb /dev/sdc
/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
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:
blkid -p /dev/sda
-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 LVMlsblk -o NAME,TYPE,FSTYPE,SIZE,UUID,MOUNTPOINT blkid /dev/sdX # Where X is the disk partition
-
To check on MBR (Master Boot Record, used by BIOS-based operating systems):
There will be afdisk -l /dev/sdX # Where X is the disk partition
Type
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 (
8e
), you can usefdisk -l
There will be aparted /dev/sdX # Where X is the disk partition
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
sudo vgcreate my_volume_group /dev/sdb /dev/sdc
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
sudo lvcreate -L 50G -n my_logical_volume my_volume_group
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
sudo mkfs.ext4 /dev/my_volume_group/my_logical_volume
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
sudo mount /dev/my_volume_group/my_logical_volume /mnt/my_mount_point
/mnt/my_mount_point
.
- E.g.:
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
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
/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
:sudo lvextend -L +20G /dev/my_volume_group/my_logical_volume sudo resize2fs /dev/my_volume_group/my_logical_volume
Tools for Managing Physical Volumes, Volume Groups, and Logical Volumes (PV, VG, LV):¶
Physical Volume Management¶
-
pvcreate
: Initializes a physical volume for use by LVM.This creates a physical volume on partitionsudo pvcreate /dev/sdb1
/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.sudo pvdisplay /dev/sdb1
-
pvscan
: Scans all devices for physical volumes and displays active ones.sudo pvscan
-
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.
sudo pvmove /dev/sdb1 /dev/sdc1
- 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.sudo vgreduce myvg /dev/sdb1
Volume Group Management¶
-
vgcreate
: Creates a volume group using one or more physical volumes.sudo vgcreate myvg /dev/sdb1 /dev/sdc1
-
vgdisplay
: Displays information about volume groups.sudo vgdisplay myvg
-
vgscan
: Scans all devices for volume groups and displays found VGs.sudo vgscan
-
vgextend
: Adds one or more physical volumes to an existing volume group, increasing its capacity.sudo vgextend myvg /dev/sdd1
-
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.
sudo vgchange -a y myvg # Activates VG sudo vgchange -a n myvg # Deactivates VG
- 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.sudo vgexport myvg
-
vgimport
: Imports a volume group that has been exported from another system.sudo vgimport myvg
-
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.
sudo vgsplit myvg mynewvg /dev/sdd1
- 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.
sudo vgmerge myvg1 myvg2
- 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:
sudo lvcreate -L 10G -n mylv myvg
- Example:
-
lvextend
: Increases the size of a logical volume. You must resize the filesystem afterwards.- Example:
sudo lvextend -L +5G /dev/mapper/myvg-mylv sudo resize2fs /dev/mapper/myvg-mylv # If ext4 filesystem
- 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:
sudo resize2fs /dev/mapper/myvg-mylv 5G # Resize filesystem first (e.g., ext4) sudo lvreduce -L 5G /dev/mapper/myvg-mylv
- Example:
-
lvresize
: Resizes a logical volume, which can be used for both increasing and decreasing the size.- Example:
sudo lvresize -L 15G /dev/mapper/myvg-mylv
- Example:
-
lvdisplay
: Displays information about logical volumes.- Example:
sudo lvdisplay /dev/mapper/myvg-mylv
- Example:
Other Useful LVM Commands¶
-
lvmdiskscan
: Scans for all storage devices and shows their suitability for use as physical volumes in LVM.- Example:
sudo lvmdiskscan
- 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:
sudo lvremove /dev/mapper/myvg-mylv
- Example:
-
vgremove
: Deletes a volume group. You must remove all logical volumes from the VG first.- Example:
sudo vgremove myvg
- Example:
-
pvremove
: Removes a physical volume from LVM control, effectively undoing thepvcreate
command.- Example:
sudo pvremove /dev/sdb1
- 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.sudo vgs
Filesystem resizing¶
lvextend
: Increases the size of LV
When extending an LV, ensure that the underlying filesystem is resized accordingly.
- For
ext4
orext3
filesystems:sudo resize2fs /dev/mapper/myvg-mylv
- For
xfs
filesystems:sudo xfs_growfs /mount/point
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.
sudo lvcreate --thinpool thinpool --size 100G myvg sudo lvcreate --thin --virtualsize 50G --name thinlv myvg/thinpool
Reverting a Logical Volume back to Raw Disks¶
-
Backup data. Removing a logical volume is destructive and irreversible.
-
Identify the LV, VG, and PGs
lvdisplay vgdisplay pvdisplay
-
Unmount the LV
Also remove the entry inumount /dev/mapper/myvg-mylv # or umount /dev/myvg/mylv
/etc/fstab
-
Remove the LV
lvremove /dev/mapper/myvg-mylv # or lvremove /dev/myvg/mylv
-
Remove the VG
vgremove myvg
-
Remove the PVs
pvremove /dev/xda # Replace with the PVs you want to remove
-
(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.
wipefs -a /dev/xda