In this guide, we demonstrate how to shrink an LVM volume or partition in Linux by resizing the file system followed by resizing the logical volume.
Note: This example is based on CentOS 8. Commands may vary in different Linux distributions. As of CentOS 8, the default file system is XFS, which cannot be shrunk. This guide focuses on the ext4 file system.
Overview of Logical Volume Manager (LVM):
Before diving into the resizing process, it’s crucial to understand some basic concepts:
- Physical Volume (PV): Created on a physical disk or Linux partition.
- Volume Group (VG): Consists of one or more physical volumes.
- Logical Volume (LV): Also known as the partition, resides within a volume group and hosts a file system.
- File System: e.g., ext4, exists on the logical volume.
How to Decrease or Shrink the Logical Volume:
To reduce the size of an LVM partition, you must initially decrease the file system within to prevent potential data corruption. Due to the risk involved in mistyping commands, it’s highly advised to create a full backup of your data beforehand. Shrinking a logical volume creates additional space in the volume group, allowing you to potentially extend another logical volume using this newfound space.
The initial step varies based on whether you intend to shrink an LVM root volume or a non-root volume.
Shrinking a root volume: The root volume is typically the logical volume mounted to ‘/’. It cannot be unmounted for shrinking as it’s actively used by the operating system. Therefore, you’ll need to boot from a Live CD to perform this operation. After booting into the Live CD, you may need to execute the command below to detect LVM volumes. Although this detection usually occurs during boot, running this command is recommended to ensure proper recognition.
vgchange -a y
Shrinking a non-root volume: If you’re shrinking a non-root volume, which refers to any volume not mounted to the root of the file system, you’ll need to unmount the volume. Keep in mind that when you unmount the volume, the data becomes inaccessible. Therefore, it’s advisable to schedule downtime and halt any applications using data from it before proceeding with unmounting. You can unmount by specifying either the logical volume or its current mount location. In the example below, we specify the logical volume, typically found at /dev/(vg-name)/(lv-name).
umount /dev/centos/var
The following steps are applicable to both root and non-root volumes.
Before attempting to shrink the size of an LVM volume, it’s essential to perform a file system check on it. Failure to do so will result in an error message, halting the process. This step is imperative as resizing a file system in a compromised state could lead to data corruption. Utilize the ‘-f’ flag to run the check even if the file system appears clean. Additionally, the ‘-y’ flag assumes yes to all questions and responds accordingly if asked to fix a problem.
e2fsck -fy /dev/centos/var
Subsequently, you’ll need to reduce the size of the file system. For safety, we’ll shrink the file system to a size lower than what the logical volume will shrink to. This precaution prevents accidental shrinking of the logical volume to a size smaller than the file system, which could lead to corruption and data loss. Rest assured, we’ll reclaim the space later in the process.
The following command will shrink the file system to a total size of 4G. It’s important to note that the specified size for shrinking must have available free space within the file system. If there isn’t enough free space, data deletion may be necessary before proceeding.
resize2fs /dev/centos/var 4G
After reducing the file system, proceed to shrink the size of the logical volume using the lvreduce command. Adjust it to the desired volume size by specifying the -L flag. Alternatively, if you prefer reducing it by a specific size, prepend a ‘-‘ before the size. Both options are provided below for clarity, though you only need to execute one.
For example, to decrease it to 5G:
lvreduce -L 5G /dev/vg/disk-name
To reduce by 5G
lvreduce -L -5G /dev/vg/disk-name
After executing the lvreduce command, you’ll receive a warning indicating the chosen size for reduction. Take this opportunity to ensure that you’re shrinking the logical volume to a size that is not smaller than the size you previously reduced the file system to. Once you’ve confirmed, proceed by entering ‘y’ and pressing enter.
Once the logical volume has been decreased to the desired size, execute resize2fs on the volume. This action extends the file system to utilize all available space within the logical volume, ensuring that none of the previously shrunk file system’s free space is wasted.
resize2fs /dev/centos/var
Now, the final step is to mount the volume. If it’s a root volume and you’re operating within a Live CD environment, just reboot into your primary Linux operating system.
For a non-root volume that was unmounted during the reduction process, simply remount it. You can accomplish this by using the command ‘mount -a’, assuming the configuration is already set in /etc/fstab. Alternatively, specify the logical volume and its mount point. In this example, we’re manually mounting it to /mnt for testing purposes.
mount /dev/centos/var /mnt
Once you’ve either rebooted into the primary operating system or completed the mount, verify the decreased space by using the ‘df’ command. This ensures that the reduction has been successfully applied as anticipated.
[root@hostname /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 9.8G 1.4G 8.5G 14% /
devtmpfs 908M 0 908M 0% /dev
tmpfs 914M 0 914M 0% /dev/shm
tmpfs 914M 8.6M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 96M 402M 20% /boot
/dev/mapper/centos-var 4.8G 20M 4.6G 1% /mnt
*In this instance, /dev/centos/var accurately reflects the reduction from the initial 10G size.
By following these simple steps you can shrink your LVM volume.