Shrink LVM volume is one option to increase the capacity of the logical volume manager. This always involves risk if the logical volume contains a filesystem.
In this tutorial, we learn how to safely shrink lvm volume in Linux.
How to Shrink an LVM Volume
To reduce the size of a logical volume, first unmount the file system. You can then use the lvreduce command to shrink the volume. After shrinking the volume, remount the file system.
In our example, the logical volume named /dev/vg-01/lv_stripe
has 1Gb size. We want to reduce the LV size to 800MB. 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.
- 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 800MB, we need to follow the below steps.
- Unmount the filesystem (if its root volume which requires booting into a Live CD to complete the work)
- Use fsck command to check the filesystem before resizing it.
- Resize the filesystem to 800MB before reducing the LV size
- Reduce the size of the logical Volume 800MB.
- Mount the filesystem
Now, we can go through steps to shrink LVM an volume in Linux.
1. Unmounting filesystem
In the example below, we have a logical volume /dev/vg-01/lv_stripe
mounted on the mount point./mnt/lv_stripe
We can check if the volume is mounted with df -hP
command
# df -hP
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 5.2G 13G 30% /
devtmpfs 897M 0 897M 0% /dev
tmpfs 912M 156K 912M 1% /dev/shm
tmpfs 912M 9.0M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 497M 189M 309M 38% /boot
tmpfs 183M 24K 183M 1% /run/user/1000
/dev/mapper/vg--01-lv_stripe 1008M 55M 902M 6% /mnt/lv_stripe
You can see in the last line, the logical volume is mounted. Before running fsck on the filesystem, it should be unmounted. You can unmount the filesystem /mnt/lv_stripe
as follows.
# umount /mnt/lv_stripe
Check with the df command
# df -hP
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 5.2G 13G 30% /
devtmpfs 897M 0 897M 0% /dev
tmpfs 912M 156K 912M 1% /dev/shm
tmpfs 912M 9.0M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 497M 189M 309M 38% /boot
tmpfs 183M 24K 183M 1% /run/user/1000
2. Performing filesystem check
Before proceeding with reducing filesystem, fsck command should be done in order to avoid inconsistency of filesystem data. We will force checking even if the file system seems clean with -f
option. This can be done as follows.
# e2fsck -f /dev/vg-01/lv_stripe
e2fsck 1.42.9 (28-Dec-2013)
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/vg-01/lv_stripe: 14/65536 files (7.1% non-contiguous), 18146/262144 blocks
This operation must pass in every 5 steps of the filesystem check if not there might be some issue with your filesystem.
Note: fsck.ext4 is now recommended for new versions of Ubuntu as it uses ext4 filesystem by default.
3. Shrink the Logical Volume
Use lvreduce command to reduce the size of logical volume. You can pass -r or --resizefs option to attempt to reduce the filesystem before shrinking the volume which is safer.
XFS or GFS2 filesystems doesnt support lvreduce.
This will reduce the logical volume size to 800MB
lvreduce -r -L 800M /dev/vg-01/lv_stripe
We can check the new size of the logical volume
# lvdisplay /dev/vg-01/lv_stripe
--- Logical volume ---
LV Path /dev/vg-01/lv_stripe
LV Name lv_stripe
VG Name vg-01
LV UUID moX8R0-ME2Q-UFHd-HLIP-rmij-SmTm-3Zupem
LV Write Access read/write
LV Creation host, time centos7-srv, 2017-05-01 16:02:21 +0100
LV Status available
# open 0
LV Size 800.00 MiB
Current LE 200
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
You can see the LV Size 800.00 Mib
line indicates the new size.
Mount the filesystem and check it using the command df -Ph
command
# mount /dev/vg-01/lv_stripe /mnt/lv_stripe/
# df -Ph
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 5.2G 13G 30% /
devtmpfs 897M 0 897M 0% /dev
tmpfs 912M 156K 912M 1% /dev/shm
tmpfs 912M 9.0M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 497M 189M 309M 38% /boot
tmpfs 183M 20K 183M 1% /run/user/1000
/dev/mapper/vg--01-lv_linear 9.7G 3.2M 9.2G 1% /mnt/lv_linear
/dev/mapper/vg--01-lv_mirror 992M 2.8M 938M 1% /mnt/lv_mirror
/dev/mapper/vg--01-lv_stripe 786M 55M 692M 8% /mnt/lv_stripe
Conclusion
In this guide, we learned how to shrink LVM volume in Linux.
Thanks for reading, please leave your feedback and suggestion in the below comment section.
Comments