What You’ll Learn
- Installing LVM utilities
- Key LVM components: PV, VG, LV, PE
- Creating Physical Volumes (PV)
- Building and extending Volume Groups (VG)
- Allocating and resizing Logical Volumes (LV)
- Formatting and resizing filesystems
- Common commands and handy tips
Prerequisites
We’ve attached three new 5 GiB virtual disks to our CentOS VM. If you haven’t installed the LVM tools yet, run:Most LVM operations require root privileges. Prepend
sudo or switch to the root user before proceeding.LVM Abbreviations
| Resource | Description | Command Prefix |
|---|---|---|
| Physical Volume (PV) | A disk or partition managed by LVM | pv* |
| Volume Group (VG) | A pool of one or more PVs | vg* |
| Logical Volume (LV) | A virtual partition carved from a VG | lv* |
| Physical Extent (PE) | The smallest allocation unit in LVM | n/a |
1. Creating Physical Volumes (PV)
Physical Volumes are the foundation of LVM. You can use entire disks or partitions.-
Scan for available disks and existing PVs:
Sample output:
-
Initialize
/dev/sdcand/dev/sddas new PVs: -
Verify all PVs:
PFree indicates unallocated space ready for LVs.
2. Building a Volume Group (VG)
Volume Groups aggregate one or more PVs into a single storage pool.-
Create a VG named my_volume:
-
To extend the VG later, initialize another PV:
-
Add it to my_volume:
-
List all VGs:
Removing a PV from a VG
- Detach
/dev/sde: - Wipe its metadata:
3. Allocating Logical Volumes (LV)
Logical Volumes are like partitions within a VG.- Create a 2 GiB LV called partition1:
- Create a 6 GiB LV called partition2:
- View all LVs:
- Check remaining VG space:
4. Resizing Logical Volumes
LVM data is allocated in Physical Extents (PE). You can grow or shrink LVs by adjusting their extents.Grow to Fill Free Space
Expand partition1 to consume all free extents:Shrink Back
Reduce partition1 back to 2 GiB:Shrinking a filesystem without shrinking the filesystem itself first can lead to data loss. Always unmount, run a filesystem check, and resize the filesystem before reducing an LV.
5. Formatting the Filesystem
Before storing data, format your LV. For example, to use XFS:6. Resizing with an Existing Filesystem
To grow both the LV and its filesystem in one command:Note: Not all filesystems support online shrinking. Check your filesystem’s documentation.
7. Handy Tips & Resources
- Browse the full LVM manual:
- Use tab-completion to explore subcommands:
- Always back up critical data before performing volume operations.