How to reduce LVM size & reuse free space on the fly

Reducing LVM disk size is possible using lvreduce on the fly without a reboot. Make sure that data is backed up for the disk to be resized so that FREE space can be reused. LVM (Logical Volume Management) is very efficient when it comes to mounting/partitioning disks in Linux. You need to be ROOT to carry out this process.

In this scenario we would be reducing /dev/mapper/VolGroup01-lvtest mounted at /mnt/lvdisk from 100GB to 50GB.

[root@viz-jump ~]# df -h | grep /mnt/lvdisk
/dev/mapper/VolGroup01-lvtest
                       99G   60M   94G   1% /mnt/lvdisk

First check whether the target partition is in use otherwise it might give you "device busy" errors.
Run this command

# [root@viz-jump ~]# lsof | grep /mnt/lvdisk

If any processes running on the partition, kill or shutdown the service or process. In our case it was Postgres and had to shutdown the service.

# service postgresql stop

NOTE: Please Backup your data to be on a safe side as this process will UNMOUNT the partition and resize it.

Now proceed with reducing the size of the LVM disk

# lvreduce -r -L 50G /dev/mapper/VolGroup01-lvtest

Output:

[root@viz-jump ~]# lvreduce -r -L 50G /dev/mapper/VolGroup01-lvtest
Do you want to unmount "/mnt/lvdisk"? [Y|n] y
fsck from util-linux-ng 2.17.2
/dev/mapper/VolGroup01-lvtest: 11/6553600 files (0.0% non-contiguous), 459349/26213376 blocks
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/VolGroup01-lvtest to 13107200 (4k) blocks.
The filesystem on /dev/mapper/VolGroup01-lvtest is now 13107200 blocks long.
Size of logical volume VolGroup01/lvtest changed from 100.00 GiB (25599 extents) to 50.00 GiB (12800 extents).
Logical volume lvtest successfully resized

NOTE:As you can see above it shows 100 GB is now reduced to 50GB.

Now verify the size by running "df -h"

root@viz-jump ~]# df -h | grep /mnt/lvdisk
                       50G   52M   47G   1% /mnt/lvdisk

Now if you check the Volume Group size i.e. VolGroup01, it will show you that 50% is allocated and remaining is FREE.

[root@viz-jump ~]# vgdisplay VolGroup01 | grep PE
  PE Size               4.00 MiB
  Total PE              25599
  Alloc PE / Size       12800 / 50.00 GiB
  Free  PE / Size       12799 / 50.00 GiB

Next step would be to reuse the FREE space and mount it and make it persistent across reboot. First create a Logical Volume

# lvcreate -n dbbackupLV -l +100%FREE VolGroup01

Output:

[root@viz-jump ~]# lvcreate -n testLV -l +100%FREE VolGroup01
  Logical volume "testLV" created

Now format the partition to ext4. You can make your choice on the filesystem.

# mkfs.ext4 /dev/mapper/VolGroup01-testLV

Output:

[root@viz-jump ~]# mkfs.ext4 /dev/mapper/VolGroup01-testLV
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13106176 blocks
655308 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Now create a directory so that you can mount the new partition

# mkdir /mnt/lvdisk01

And then add a entry in fstab to make the mount persistent across reboot.
1st find the UUID of the logical Volume created.

# blkid | grep testLV

Output:

[root@viz-jump mnt]# blkid | grep testLV
/dev/mapper/VolGroup01-testLV: UUID="d3347cbe-b0a0-4388-b191-796984d3e85d" TYPE="ext4"

Copy the UUID = d3347cbe-b0a0-4388-b191-796984d3e85d

Edit fstab

# vi /etc/fstab
# Add this line for the new partition
UUID=d3347cbe-b0a0-4388-b191-796984d3e85d /mnt/lvdisk01 ext4    defaults        1 2

Now either of the mount commands will work

# mount -a (mounts all partitions given in /etc/fstab)

(OR)

# mount /mnt/lvdisk01 (To mount a specifc partition)

That should be it and now ready to use the new space. To verify run "df -h"

[root@viz-jump mnt]# df -h | grep /mnt
                       50G   52M   47G   1% /mnt/lvdisk
                       50G   52M   47G   1% /mnt/lvdisk01

If you encounter any errors during the process, let us know via Contact Us and will try our best to help.

Most Read Articles

Add/Detect a new disk in ...

Written By Farooq Mohammed Ahmed on Sunday, 11 January 2015 19:22
Add/Detect a new disk in ...

Replace SSL Certificates ...

Written By Farooq Mohammed Ahmed on Friday, 16 December 2016 08:05
Replace SSL Certificates ...

Using awk in Alias Comman...

Written By Farooq Mohammed Ahmed on Friday, 19 February 2016 14:22
Using awk in Alias Comman...

Script to Monitor Nagios ...

Written By Farooq Mohammed Ahmed on Monday, 18 July 2016 22:48
Script to Monitor Nagios ...

Generate/Create a SHA2/SH...

Written By Farooq Mohammed Ahmed on Tuesday, 25 October 2016 13:56
Generate/Create a SHA2/SH...