Ben the DBA

How to Expand a Filesystem Using LVM

Add the New Disk

First, physically attach the new disk to your server or virtual machine. Once the disk is connected, you can use the lsblk or fdisk -l command to identify the new disk. It will typically appear as /dev/sdb, /dev/sdc, etc., depending on your system.


lsblk

Create a Partition on the New Disk

Next, you need to create a partition on the new disk. In this example I we use fdisk but you can try cfdisk.


fdisk /dev/sdb

Follow these steps in fdisk:

Now, the new partition should be available as /dev/sdb1.

Create a Physical Volume (PV)


pvcreate /dev/sdb1

This command initializes the partition /dev/sdb1 as a PV.

Add the New PV to an Existing Volume Group (VG)

Now that you have a new PV, you can add it to an existing Volume Group (VG). First, check the name of your current VG with:


vgdisplay

Assume your VG is named vg_data. You would add the new PV to this VG with the following command:


vgextend vg_data /dev/sdb1

This command extends the vg_data volume group to include the new physical volume.

Extend the Logical Volume (LV)

After adding the PV to the VG, you can extend the Logical Volume (LV) that the filesystem is on. First, identify the LV that you want to extend:


lvdisplay

Suppose the LV is /dev/vg_data/lv_data. You can extend it using the lvextend command:


lvextend -l +100%FREE /dev/vg_data/lv_data

This command increases the size of the LV by using all the free space available in the VG. If you want to extend the logical volume by an additional 10 GB, you can use:


lvextend -L +10G /dev/vg_data/lv_data

Resize the Filesystem

Finally, you need to resize the filesystem to use the new space added to the LV. The command you use depends on the filesystem type:

For ext4:


resize2fs /dev/vg_data/lv_data

For xfs:


xfs_growfs /mount/point

If your filesystem is mounted, these commands can be run while the filesystem is online.

Verify the Changes

After resizing the filesystem, it’s good practice to verify that everything is as expected. You can use df -h to check the size of the filesystem:


df -h

TOC

Add the New Disk
Create a Partition on the New Disk
Create a Physical Volume (PV)
Add the New PV to an Existing Volume Group (VG)
Extend the Logical Volume (LV)
Resize the Filesystem
Verify the Changes