Logical Volume Manager(LVM) allows the Linux kernel to manage large disk drives effectively. This allows users to create partitions from more than one disk and allows them to extend the filesystem size online within few seconds. In this article, we will see, how we can shrink the size of an LVM partition without losing data.
In our example, the logical volume named “test” has 4Gb size. We want to reduce the LV size to 1GB. Note down the following points before proceeding with the shrinking of filesystem.
• Make sure the current disk usage of the filesystem is less than the size to which you are going to reduce the logical volume. Here, my logical volume has only less than 200MB data, so reducing to 1GB is quite fine.
• Always take a backup of filesystem data before doing any size change in LVM as a simple mistake in command can cause filesystem corruption and hence loss of data.
In order to reduce the LV size to 1Gb, we need to follow the below steps.
a. Unmount the filesystem
b. Use “fsck” to check the filesystem before resizing it.
c. Resize the filesystem to 1GB before reducing the LV size
d. Reduce the size of the logical Volume 1GB.
e. Mount the filesystem
Now, we can go through each steps mentioned above.
Before running fsck on the filesystem, it should be unmounted. You can unmount the filesystem “/test” as follows.
# umount /test
Performing filesystem check
Before proceeding with reducing filesystem, “fsck” should be done in order to avoid inconsistency of filesytem data. This can be done as follows.
# e2fsck –f /dev/vgname/lvname
Before reducing the size of the Logical Volume, we need to reduce the filesystem in it. The command “resize2fs” can be used for this as follows.
# resize2fs –p /dev/vgname/lvname 1G
This will shrink the filesystem to 1GB
Reducing the LV size
After shrinking the residing filesystem, we can proceed with reducing LV as follows.
# lvreduce –L 1G /dev/vgname/lvname
This will reduce the logical volume size to 1GB.
Now, you can mount the filesystem and check it using the command “df –h”. The filesystem has reduced to 1GB without losing any data.