
A Step-by-Step Guide to Safely Reducing an LVM Physical Volume in Linux
Managing disk space is a fundamental task for any system administrator. As needs change, you may find yourself needing to reclaim space from a large partition managed by the Logical Volume Manager (LVM). Shrinking a Physical Volume (PV) is a powerful but potentially risky operation. This guide provides a clear, step-by-step process to help you safely reduce the size of an LVM Physical Volume and reclaim that valuable disk space.
Before you begin any of the steps below, you must perform a full and verified backup of all data on the volume group. This process involves modifying low-level disk structures, and a mistake can lead to catastrophic data loss. Do not proceed without a reliable backup.
Understanding the LVM Hierarchy
To successfully shrink a Physical Volume, it’s essential to understand the LVM stack. The process works from the top down:
- Filesystem: The layer where your files and directories reside (e.g., ext4, XFS).
- Logical Volume (LV): A virtual partition that contains the filesystem.
- Volume Group (VG): A pool of storage made up of one or more Physical Volumes.
- Physical Volume (PV): A physical disk or partition that has been initialized for LVM use.
You cannot shrink a lower layer without first shrinking the layers above it. Therefore, the process involves shrinking the filesystem, then the Logical Volume, and finally the Physical Volume.
Step 1: Shrink the Filesystem
First, you must reduce the size of the filesystem to be smaller than your intended new Logical Volume size. This ensures no data is cut off when you shrink the LV.
Before resizing, it’s crucial to run a filesystem check to ensure its integrity. Unmount the filesystem first if possible.
# Unmount the filesystem (recommended)
umount /dev/your_volume_group/your_logical_volume
# Run a filesystem check (e.g., for ext4)
e2fsck -f /dev/your_volume_group/your_logical_volume
Once the check is complete, you can resize the filesystem. For an ext4 filesystem, use the resize2fs command. It’s wise to shrink the filesystem to a size slightly smaller than your final target size for the LV to leave a small margin of safety.
# Shrink the filesystem to a specific size, e.g., 95GB
resize2fs /dev/your_volume_group/your_logical_volume 95G
If you cannot unmount the filesystem (e.g., the root volume), some filesystems like ext4 support online shrinking, but the risk is higher.
Step 2: Reduce the Logical Volume (LV)
With the filesystem safely shrunk, you can now reduce the size of the Logical Volume that contains it. Use the lvreduce command. The new size of the LV should be slightly larger than the new filesystem size you set in the previous step.
# Reduce the LV size to 100GB
lvreduce -L 100G /dev/your_volume_group/your_logical_volume
You can also reduce the size by a specific amount. For example, to shrink it by 50GB:
# Reduce the LV size by 50GB
lvreduce -L -50G /dev/your_volume_group/your_logical_volume
After this step, you can mount the filesystem again and run df -h to verify that it is accessible and reporting the correct new size.
Step 3: Identify and Move Data from the End of the Physical Volume
This is the most critical part of the process. You cannot simply shrink a Physical Volume if it has data allocated at the end of it. You must first move the data—or more accurately, the LVM physical extents (PEs)—from the end of the PV to free space elsewhere in the Volume Group.
First, determine the current size of your PV and how many extents it uses with pvs.
pvs
Next, use the pvmove command. This powerful utility can relocate physical extents from one part of a PV to another or to a different PV entirely. To free up space at the end of the disk, you need to move the extents from the end of the device.
Let’s say your PV /dev/sda2 is 200GB and you want to shrink it to 150GB. You must move all allocated data from the 150GB mark onwards.
# Move all allocated extents from the end of /dev/sda2
# The source PV and destination PV are the same in this case
pvmove /dev/sda2
For more complex scenarios, you may need to specify the exact extent ranges to move them to another PV within the same VG.
Step 4: Shrink the Physical Volume (PV)
Once you are certain that no data resides on the part of the PV you intend to cut off, you can use pvresize to inform LVM of the new, smaller size.
This command does not check for data; it simply updates LVM’s metadata. If you perform this step without moving the data first (Step 3), you will corrupt your Logical Volumes and lose data.
# Resize the PV /dev/sda2 to its new size of 150GB
pvresize --setphysicalvolumesize 150G /dev/sda2
After running this command, use pvs and vgs to confirm that LVM now recognizes the new, smaller size of the Physical Volume and that the Volume Group’s free space has been adjusted accordingly.
Step 5: Reclaim the Space by Resizing the Partition (Optional)
Shrinking the PV only tells LVM to stop using the end of the device. The underlying disk partition is still the same size. To make that space available to the operating system for other uses (like creating a new partition), you must also shrink the partition itself using a tool like fdisk or parted.
This is another high-risk operation. The general process is:
- Use
fdisk /dev/sdato enter the partitioning tool. - Print the partition table (
p) and write down the exact starting sector of the partition you are shrinking. - Delete the partition (
d). - Re-create the partition (
n) with the exact same starting sector but a new, smaller ending sector that matches the new PV size. - Ensure the partition type is set back to “Linux LVM” (type 8e).
- Write the changes to the disk (
w).
A system reboot is often required for the kernel to recognize the new partition table.
Final Security and Best Practices
- Test First: If possible, always test this entire procedure in a virtual machine or on a non-production system before attempting it on live data.
- Work in a Maintenance Window: Perform these actions during a period of low system activity to minimize disruption and allow time for recovery from backups if needed.
- Double-Check Calculations: Be meticulous when calculating new sizes for your filesystem, LV, and PV. A simple typo can have severe consequences.
- Verify at Every Step: After each major command (
resize2fs,lvreduce,pvresize), use verification commands (df -h,lvs,pvs) to ensure the previous step completed as expected before moving to the next.
By following these steps carefully and methodically, you can effectively shrink an LVM Physical Volume, providing the flexibility needed to adapt your storage layout to evolving requirements.
Source: https://kifarunix.com/how-to-reduce-or-shrink-physical-volume-in-linux/


