Home Linux Disk Management How To Resize LVM Partitions In Linux

How To Resize LVM Partitions In Linux

Extend Or Reduce Volume Group And Logical Volume's Size In Linux

By Karthick
Published: Updated: 38K views

In the previous article, we have covered what is LVM and how to create Volume group and Logical volumes in Linux. This article will teach you how to resize LVM partitions i.e. extend a Volume Group and Logical Volume's size and reduce/shrink Logical Volume's size in Linux.

When you want to increase the size of your logical volume, you should check if there are any unallocated spaces in the free pool (i.e. Volume Group).

If there is no space left in the free pool, then you have to go with any of the below steps to increase the space.

  • Check if any volume group has space that can be utilized. In this case, add the space back to the volume group and increase the new volume size.
  • Add a disk, initialize it as physical volume and add it to the volume group using which you can extend the logical volume size.

My Existing Setup

For demonstration purpose, I have created an Ubuntu 20.04 test machine using Vagrant and Virtualbox. I have two disks that are initialized as a physical volume.

$ sudo pvscan

Sample Output:

PV /dev/sdc   VG ostechnix_files   lvm2 [<5.00 GiB / 96.00 MiB free]
PV /dev/sdd                        lvm2 [10.00 GiB]
Total: 2 [<15.00 GiB] / in use: 1 [<5.00 GiB] / in no VG: 1 [10.00 GiB]

Among these two disks, only the /dev/sdc partition is added to the volume group ostechnix_files and created logical volume from that.

$ sudo lvdisplay

Sample Output:

  --- Logical volume ---
  LV Path                /dev/ostechnix_files/guides
  LV Name                guides
  VG Name                ostechnix_files
  LV UUID                dc1gzT-cyLX-axdh-0EpD-GHBB-un8s-B5JAAc
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2022-12-04 15:33:59 +0000
  LV Status              available
  # open                 1
  LV Size                4.90 GiB
  Current LE             1255
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

Extending Volume Group and Logical Volume

I have no space left on the free pool (VG) to increase the size of the logical volume, so I have to add space to the volume group first to extend logical volumes.

$ sudo vgdisplay ostechnix_files | grep -i Free
  Free  PE / Size       24 / 96.00 MiB

I already have a disk of 10GB initialized as a physical volume. You can find the steps to create PV in the LVM introduction article.

$ sudo pvdisplay /dev/sdd
  "/dev/sdd" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd
  VG Name
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               obm8bR-pIUs-VmWc-BN61-xAXQ-WZmm-WtCsOT

Extend Volume Group

We can use vgextend command to add the new initialized space to the volume group (i.e. ostechnix_files).

##Syntax
$ vgextend <volume-group-name> <physical-volume>

Now we run the vgextend command to add the space to the volume group ostechnix_files.

$ sudo vgextend ostechnix_files /dev/sdd
  Volume group "ostechnix_files" successfully extended

You can run any of the following commands to get the logical volume path for which you wish to extend the size.

$ sudo lvdisplay
$ sudo lvdisplay | grep -i path
  LV Path                /dev/ostechnix_files/guides

Extend Logical Volume

We can use the lvextend command to increase the logical volume size.

I am extending the size from 4.9 GB to 6.9 GB by adding 2GB from the free pool (VG).

$ sudo lvextend -L +2G /dev/ostechnix_files/guides

Here, +2G indicates that we are increasing the size by 2 GB.

  Size of logical volume ostechnix_files/guides changed from 4.90 GiB (1255 extents) to 6.90 GiB (1767 extents).
  Logical volume ostechnix_files/guides successfully resized.

Now you can check the LV Size by running any of the following commands.

$ sudo lvdisplay
$ sudo lvdisplay | grep -i size
  LV Size                6.90 GiB

Please note that resizing has only increased the volume size but not the file system size. We can verify it using df command:

$ df -h /dev/ostechnix_files/guides

Sample Output:

Filesystem                          Size  Used Avail Use% Mounted on
/dev/mapper/ostechnix_files-guides  4.8G   20M  4.5G   1% /mnt

We can increase the filesystem size using the resize2fs command. Resizing can be done without unmounting the filesystem.

$ sudo resize2fs /dev/mapper/ostechnix_files-guides

Sample Output:

resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/mapper/ostechnix_files-guides is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/ostechnix_files-guides is now 1809408 (4k) blocks long.

Now let us verify if the partition size is really extended with df command:

$ df -h /dev/mapper/ostechnix_files-guides

Sample Output:

Filesystem                          Size  Used Avail Use% Mounted on
/dev/mapper/ostechnix_files-guides  6.8G   23M  6.4G   1% /mnt

As you can see in the above output, the Volume Group is extended to 6.8 GB.

Reduce or Shrink Logical Volume

As stated already in the previous section, sometimes you may need to borrow some space from an existing logical volume and add it to some other volumes. For this, you have to first shrink the logical volume and add the space back to the volume group. Then you will extend the size of the logical volume from the free pool(VG).

The first step is to unmount the file system. You cannot shrink the volume or file system when the file system is mounted.

$ umount <file-system-path>

To make sure there are no issues with the volume, run the fsck command.

$ sudo fsck /dev/mapper/ostechnix_files-guides

Sample Output:

fsck from util-linux 2.34
e2fsck 1.45.5 (07-Jan-2020)
/dev/mapper/ostechnix_files-guides: clean, 11/449792 files, 50275/1809408 blocks

Now the file system size can be reduced. I am going to resize the filesystem to 4 GB.

To do so, we use resize2fs command:

$ sudo resize2fs /dev/mapper/ostechnix_files-guides 4G

Sample Output:

resize2fs 1.45.5 (07-Jan-2020)
Resizing the filesystem on /dev/mapper/ostechnix_files-guides to 1048576 (4k) blocks.
The filesystem on /dev/mapper/ostechnix_files-guides is now 1048576 (4k) blocks long.

Now run the lvreduce command to shrink the logical volume and add it back to the volume group. Here I am reclaiming 2 GB from the LV.

$ sudo lvreduce -L -2G /dev/ostechnix_files/guides

Here, -2G indicates that we are decreasing the size by 2 GB.

Sample Output:

  WARNING: Reducing active logical volume to 4.90 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce ostechnix_files/guides? [y/n]: y
  Size of logical volume ostechnix_files/guides changed from 6.90 GiB (1767 extents) to 4.90 GiB (1255 extents).
  Logical volume ostechnix_files/guides successfully resized.

Now the space is added back to the volume group. We can verify it using vgdisplay command:

$ vgdisplay ostechnix_files | grep -i "/ Size"

Sample Output:

  Alloc PE / Size       1255 / 4.90 GiB
  Free  PE / Size       2583 / <10.09 GiB

We have to remount the existing file system which we unmounted to shrink the file system. It is safe to run the fsck command again to check for any issues.

$ sudo fsck /dev/mapper/ostechnix_files-guides
$ sudo resize2fs /dev/mapper/ostechnix_files-guides

Now mount the filesystem using command:

$ mount /dev/mapper/ostechnix_files-guides /mnt/ 

Conclusion

At this stage, you should be having a fair amount of understanding on resizing LVM partitions if you followed this article along with hands-on.

As you can see, Extending Volume Group and Logical Volume's size is simple and straightforward. However, shrinking the Logical Volume's size should be done with extra caution as it may lead to corrupting your existing data.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More