Extending the /var Logical Volume on Linux: A Step-by-Step Guide

In the dynamic world of Linux system administration, managing disk space efficiently is crucial for maintaining system stability and performance. A common scenario involves extending the /var logical volume (LV) to accommodate increasing data, such as logs, mail, and cache files. This guide will walk you through the process of recognizing the extended /dev/sdb disk and extending the /var LV using Logical Volume Manager (LVM), tailored for systems like CentOS.

Prerequisites

Ensure you have a backup of your data before proceeding. Extending volumes and working with disk partitions come with risks, and having a backup can prevent data loss.

Step 1: Resize the Physical Volume

If you’ve recently extended the storage of /dev/sdb (for example, from 50G to 80G), the first step is to make the LVM aware of the new disk size. Use the pvresize command to update the physical volume (PV) without any data loss.

sudo pvresize /dev/sdb

This command updates LVM with the new size of /dev/sdb, expanding the underlying physical volume to utilize the full capacity of the disk.

Step 2: Verify Free Space in the Volume Group

After resizing the PV, check the free space in your volume group (VG) to confirm that the additional space is recognized and available for allocation.

sudo vgs

or

sudo vgdisplay rootVG

Look for the “VFree” or “Free PE / Size” in the output to verify the increase in available space.

Step 3: Extend the Logical Volume

With the VG now having additional space, you can proceed to extend the /var LV. To add a specific amount of space, such as 20GB, use the lvextend command.

sudo lvextend -L +20G /dev/rootVG/varLV

This command increases the size of the /var LV by 20GB, utilizing the newly available space in the VG.

Step 4: Resize the Filesystem

After extending the LV, resize the filesystem to utilize the new space. The command depends on the type of filesystem you’re using.

For XFS (default in CentOS 8):

sudo xfs_growfs /var

For ext4:

sudo resize2fs /dev/rootVG/varLV

Step 5: Verify the Changes

To ensure the filesystem has been successfully resized and to check the new size of the /var LV, use:

df -h /var

This command displays the size and usage of the /var filesystem, allowing you to confirm the extension was successful.

Conclusion

Extending the /var logical volume on Linux systems, especially those utilizing LVM, can significantly enhance your server’s capability to handle growing data without compromising performance or stability. Always remember to back up your data before performing disk or volume operations to safeguard against potential data loss.