Running out of disk space in Linux? If you have partitions created on LVM, you can free up space by shrinking the logical volumes. In this detailed guide, I will walk you through safely how to reduce LVM partition size in Linux (RHEL | Ubuntu)
Initial Setup & Assumptions
- We will reduce /home partition by 2GB
- The partition is on LVM and formatted as ext4
- These steps work for both ext3 and ext4 filesystems
Note: These steps are applicable only when you have LVM based partition and formatted either as ext4 or ext3. We can not reduce XFS file system created on LVM.
Let’s assume, we want to reduce /home by 2GB which is on LVM partition & formatted as ext4.
[root@cloud ~]# df -h /home/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_cloud-LogVol00 12G 9.2G 1.9G 84% /home
1) Create a Backup
This is crucial – LVM operations can be risky. It is recommended to backup on some external drive or on other file system.
Backup important data from /home using the following tar command
[root@cloud ~]# tar -czf /root/home_backup.tar.gz /home
2) Umount the file system
Once the backup is created then use the beneath umount command
[root@cloud ~]# umount /home/
3) Check Filesystem Integrity
Run e2fsck command to check whether filesystem has any error or not. When we use ‘-f’ flag in e2fsck command then it will forcefully check or scan the file system even if the file system us clean.
[root@cloud ~]# e2fsck -f /dev/mapper/vg_cloud-LogVol00 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/mapper/vg_cloud-LogVol00: 12/770640 files (0.0% non-contiguous), 2446686/3084288 blocks
4) Reduce or Shrink the size of /home to desire size
As shown in the above scenario, size of /home is 12 GB , so by reducing it by 2GB , then the size will become 10GB. Run the following resize2fs command followed by the file system and size.
[root@cloud ~]# resize2fs /dev/mapper/vg_cloud-LogVol00 10G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/vg_cloud-LogVol00 to 2621440 (4k) blocks. The filesystem on /dev/mapper/vg_cloud-LogVol00 is now 2621440 blocks long.
5) Reduce LVM size using lvreduce command
Execute the following lvreduce command,
[root@cloud ~]# lvreduce -L 10G /dev/mapper/vg_cloud-LogVol00 WARNING: Reducing active logical volume to 10.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce LogVol00? [y/n]: y Reducing logical volume LogVol00 to 10.00 GiB Logical volume LogVol00 successfully resized
6) Final Filesystem Check
Again run the e2fsck to check file system for errors after size reduction.
[root@cloud ~]# e2fsck -f /dev/mapper/vg_cloud-LogVol00 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/mapper/vg_cloud-LogVol00: 12/648960 files (0.0% non-contiguous), 2438425/2621440 blocks
7) Remount the File system and verify its size
Now, run mount command to remount the /home file system and then run df command to verify it’s size.
[root@cloud ~]# mount /home/ [root@cloud ~]# df -h /home/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_cloud-LogVol00 9.9G 9.2G 208M 98% /home
Perfect, above commands output confirms that /home size is reduced by 2GB, now the new size of file system is 10GB. That’s all from this guide, please do post your queries and feedback in below comments section.
Important Notes:
- Always back up your data before performing LVM operations
- Never reduce a mounted filesystem
- The filesystem must be shrunk before the logical volume
- Keep some extra space for safety margin
- If using XFS filesystem, note that it cannot be shrunk – only grown
Also Read : How to Create Partitions in Linux Using fdisk Command
thanks, I understand how to reduce size of lvm.
thanks a lot , very helpful .
Thanks for the article. Is there no way to reduce Linux file system without unmounting i.e. to avoid downtime ?
During LVM reduction, It is always recommended to umount file system otherwise there can be corruption on the file system, though you try reducing the LVM partition online without umounting.
Why have to unmount the file system takes place before reduce size of lvm
There are chances that your file system might get corrupted if you don’t umount file system during lvm reduce.
Thank you so much!!!
Hi, good post!
One question, in step 4 it says “WARNING: Reducing active logical volume to 10.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)”. So it means that the data in /home are destroyed while de resize is performed?
In the production environment, it is not recommended to reduce files system as it might lead to file system corruption and data destruction but there are some situation where we need to reduce it.
So in Step:4 we will not loose any data as file system usage is 9.2 GB and we are reducing it from 12 GB to 10 GB and before reduction we are doing files system check with e2fsck command and resizing it with resize2fs command and in last we are reducing the file system.
I am trying to reduce root partition size. It’s showing below error:
“on-line resizing required resize2fs: On-line shrinking not supported”
Thanks, but this is directions on how to reduce the size of a logical volume, not a partition (e.g. a physical volume).
BTW. If you want to don’t want the volume unmounted you can create a snapshot, and use that while reducing the partition size. Only you need enough free space in your group so you can allocate the snapshot to have enough space to for the changing of the file system. But with USB sticks of 256G or greater being easily affordable, it is quite practical to put some physical volumes on a USB stick for your snapshot volume.
The command I was looking for is pvresize. That allows your to resize an lvm parttion.