Extend an lvm partition

Learn how to expand your Logical Volume Management (LVM) partitions with ease in our comprehensive guide. This article provides clear, step-by-step instructions for extending LVM partitions, allowing you to dynamically adjust your storage space to meet changing requirements.

Extend an lvm partition

To bring this tutorial to life, let's dive into a real-world scenario.

Imagine you're in charge of a server, and you start noticing a flurry of errors cropping up. Not only that, but the server's response time has slowed down drasticlaly.

After a quick investigation, your detective work reveals a crucial piece of the puzzle – the /var partition has reached its storage limit, bringing the server to its knees.

Display filesystem usage

Using the command df -h check which FS (File System) is full

  • -h : Display the size in human-readable format
root@FRNASRSUP01 (PROD):~ # df -h
Filesystem                                    Size  Used Avail Use% Mounted on
udev                                          972M     0  972M   0% /dev
tmpfs                                         197M  876K  196M   1% /run
/dev/mapper/vg0-lv0                           7.6G  2.2G  5.1G  30% /
tmpfs                                         982M     0  982M   0% /dev/shm
tmpfs                                         5.0M     0  5.0M   0% /run/lock
/dev/mapper/vg0-lv_var                           2.1G  2.0G     0 100% /var
/dev/mapper/vg0-lv_tmp                        488M  116K  452M   1% /tmp
/dev/mapper/vg0-lv_var_lib_mysql              4.9G  443M  4.2G  10% /var/lib/mysql
/dev/mapper/vg0-lv_var_cache_centreon_backup  974M   24K  907M   1% /var/cache/centreon/backup
tmpfs                                         197M     0  197M   0% /run/user/1007

Check if your volume group has some free space

Using the command vgs, check how much free space you have on your VG (Volume Group).

Perfect, here we have 18.5Go of free space available to extend an existing LV (Logical Volume) or to create a new one.


root@FRNASRSUP01 (PROD):~ # vgs
  VG  #PV #LV #SN Attr   VSize  VFree
  vg0   2   5   0 wz--n- 34.99g 18.50g

In our case, we'd like to give some extra space to our /var partition.

Extend the Logical Volume

Now that we know wich LV to extend (name in the first column of the df command) and that we have some space left on our VG, let's extend it.

To do this use the following command: lvextend -L+2G /dev/mapper/vg0-lv_var
This will add 2G to our logical volume /dev/mapper/vg0-lv_var.

root@FRNASRSUP01 (PROD):~ # lvextend -L+2G /dev/mapper/vg0-lv_var
  Size of logical volume vg0/lv_var changed from 2.20 GiB (564 extents) to 4.20 GiB (1076 extents).
  Logical volume vg0/lv_var successfully resized.

Extend your file system

Now, you also need to make sure that the FS (File System) inside our Logical Volume gets bigger to match the new size of your partition.

To do this, you can use the command resize2fs /dev/mapper/vg0-lv_var.

This command will stretch the file system to match the expanded storage capacity.

root@FRNASRSUP01 (PROD):~ # resize2fs /dev/mapper/vg0-lv_var
resize2fs 1.46.2 (28-Feb-2021)
Filesystem at /dev/mapper/vg0-lv_var is mounted on /var; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/vg0-lv_var is now 1101824 (4k) blocks long.

root@FRNASRSUP01 (PROD):~ # df -h
Filesystem                                    Size  Used Avail Use% Mounted on
udev                                          972M     0  972M   0% /dev
tmpfs                                         197M  876K  196M   1% /run
/dev/mapper/vg0-lv0                           7.6G  2.2G  5.1G  30% /
tmpfs                                         982M     0  982M   0% /dev/shm
tmpfs                                         5.0M     0  5.0M   0% /run/lock
/dev/mapper/vg0-lv_var                        4.1G  2.0G  1.9G  52% /var
/dev/mapper/vg0-lv_tmp                        488M  116K  452M   1% /tmp
/dev/mapper/vg0-lv_var_lib_mysql              4.9G  443M  4.2G  10% /var/lib/mysql
/dev/mapper/vg0-lv_var_cache_centreon_backup  974M   24K  907M   1% /var/cache/centreon/backup
tmpfs                                         197M     0  197M   0% /run/user/1007

Conclusion

Now we have increased our LV size by 2Go. As you can see, all of these commands can be done on a live system without unmounting or stopping anything.

LVM is a device mapper framework that is truly powerful and has a tonne of advantages:

  • Flexible Storage Management
  • Snapshot Support
  • Easy RAID Configuration
  • Efficient Disk Space Allocation
  • No Need to Reformat
  • Dynamic Volume Management
  • Easy to backup and restore
  • and more...