How to shrink a filesystem in Linux
This does not happen often, but you may run into a situation where you need to reduce the size of the Linux filesystem. Well, in my case I wanted to clone my Red Hat Enterprise Linux server which was running in Vmware ESXi. The root filesystem was originally built too large and I wanted it to be smaller. As always take the necessary precaution to backup your files first before attempting something like this.
Here are the quick steps to shrink the filesystem in Red Hat linux.
(1) Activate all volume group using the command lvm vgchange.
(2) Check the second extended filesystem using e2fsck.
(3) Reduce the filesystem using resize2fs.
(4) Reduce the logical volume using the command lvm lvreduce.
The first step is the activate all volume groups.
# lvm vgchange -ay
Check the filesystem and resolve any error. The option -f forces checking even if the file system seems clean. -y assume an answer of `yes’ to all questions; allows e2fsck to be used non-interactively.
# e2fsck -fy /dev/VolGrooup00/LogVol00
Reduce the capacity of the filesystem. You must specify the required final size of the filesystem when shrinking it. The suffixes K, M and G may be used to specify a size in kilobytes, megabytes or gigabytes. These are interpreted as traditional (binary) units, so 1G=1024M, 1M=1024K and 1K=1024 bytes.
# resize2fs -f /dev/VolGroup00/LogVol00 5G
Reduce the size of the logical volume.
-L, –size [-]LogicalVolumeSize[kKmMgGtT]
Reduce or set the logical volume size in units of megabyte by default. A size suffix of k for kilobyte, m for megabyte, g for gigabyte or t for terabyte is optional. With the – sign the value will be subtracted from the logical volume’s actual sizeand without it it will be taken as an absolute size.
# lvm lvreduce -L 5G /dev/VolGroup00/LogVol00