How to Resize & Reduce for Shrinking Lvm Partition on Redhat/CentOS

November 5, 2012 | By
| 2 Replies More

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. Before shrinking the volume make sure you have taken the backup of the mount point. Shrinking volume has a chance of data corruption. 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. The example given in this post applies to centos 7.

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
  • 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 each step mentioned above.

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 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 file-system check if not there might be some issue with your filesystem.

3. Resizing filesystem

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. We will reduce the filesystem to 800MB. We will use -p option to print out a percentage completion bars for each resize2fs operation during an offline resize

# resize2fs -p /dev/vg-01/lv_stripe 800M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg-01/lv_stripe to 204800 (4k) blocks.
Begin pass 3 (max = 8)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/vg-01/lv_stripe is now 204800 blocks long.

4. Reducing the LV size

After shrinking the residing filesystem, we can proceed with reducing LV as follows.

# lvreduce -L 800M /dev/vg-01/lv_stripe 
  WARNING: Reducing active logical volume to 800.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_stripe? [y/n]: y
  Size of logical volume vg-01/lv_stripe changed from 1.00 GiB (256 extents) to 800.00 MiB (200 extents).
  Logical volume lv_stripe successfully resized.

This will reduce the logical volume size to 800MB. Note that when resizing the logical volume, use the exact size (800MB). 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 which indicates the new size. You can also 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

Important remark

Note that we resize the filesystem wth resize2fs command before reducing the logical volume with lvreduce command. resize2fs makes the filesystem use only the first size bytes of the storage. It does this by moving both filesystem metadata and your data around. After it completes, there will be unused storage at the end of the block device (logical volume), unused by the filesystem. lvextend and lvreduce commands change the size of the logical volume. They can additionally change the size of the filesystem if given the -r option that calls resize2fs for you, which is probably the right way to go, especially with reducing. Accidentally giving the wrong size to lvreduce is an unfortunately easy way to lose data; -r prevents this (by ensuring that resize2fs is told the same size).

So you can bypass resize2fs step by using directly lvreduce with -r option. For example, we will reduce our logical volume by following the steps above without resize2fs step and by ending with lvreduce -r command as below:

# umount /mnt/lv_stripe/
# 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/57344 files (7.1% non-contiguous), 17567/204800 blocks
# lvreduce -r -L 500M /dev/vg-01/lv_stripe
  Rounding size (125 extents) up to stripe boundary size for segment (126 extents)
fsck from util-linux 2.23.2
/dev/mapper/vg--01-lv_stripe: clean, 14/57344 files, 17567/204800 blocks
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vg--01-lv_stripe to 129024 (4k) blocks.
The filesystem on /dev/mapper/vg--01-lv_stripe is now 129024 blocks long.

  Size of logical volume vg-01/lv_stripe changed from 800.00 MiB (200 extents) to 504.00 MiB (126 extents).
  Logical volume lv_stripe successfully resized.

Now let's check the new size of our logical volume

# lvdisplay /dev/vg-01/lv_stripe | grep "Size"
 LV Size 504.00 MiB

You can see a new size. We didn't need to resize the filesystem.

Conclusion

LVM offers a great possibility for some operations on our partition. We have now successfully shrunk a file system and corresponding LVM logical volume. Notice that root volumes can only be shrunk by unmounting the file system which requires booting into a Live CD to complete the work. For non-root volumes, the file system must first be unmounted so that you can shrink the volume, check and then reduce it.

Filed Under : HOWTOS, LINUX HOWTO, LVM

Tagged With :

Free Linux Ebook to Download

Comments (2)

Trackback URL | Comments RSS Feed

  1. Andrew says:

    I do everything with the instructions but at the lvreduce gets an error;

    Unable to reduce RAID LV - operation not implemented.

    RHEL7.2

    Help

Leave a Reply

All comments are subject to moderation.