[Storage Disk] Expanding Disk Space on CentOS 7.x
Expanding Disk Space on CentOS 7.x
Running out of disk space on your Linux server? It’s a common problem, especially as your applications and data grow. Today, we’ll walk through a practical guide to expand disk space on a CentOS 7.x cloud server. We’ll cover expanding both the root partition and an LVM logical volume. Let’s dive in!
Understanding the Current Disk Setup
First, let’s take a look at our current disk configuration using the lsblk command. This will show us the existing disks, partitions, and mount points.
1 | lsblk |
As you can see from the output above:
We have two disks: vda (100GB) and vdb (500GB).
vda contains a partition vda1 of 40GB, which is mounted as the root filesystem (/).
vdb contains an LVM logical volume named data-data (part of volume group ‘data’), which is 300GB in size and mounted at /www.
Our goal is to fully utilize the available disk space and expand both the root partition and the /www volume.
Expanding the Root Partition (/)
Let’s start by expanding the root partition (/dev/vda1). Here are the steps:
Install growpart: This utility is used to expand partition sizes. If it’s not already installed, you can install it using yum:
1 | sudo yum install cloud-utils-growpart |
Expand the Partition: Use growpart to extend the partition. We are expanding partition number 1 on /dev/vda.
1 | sudo growpart /dev/vda 1 |
Reload the Partition Table: Inform the kernel about the partition table changes using partprobe.
1 | sudo partprobe /dev/vda |
Resize the Filesystem: Finally, we need to resize the filesystem to utilize the newly added space.
For ext4 filesystem: If your root partition is formatted with ext4 (which is common), use resize2fs:
1 | sudo resize2fs /dev/vda1 |
For xfs filesystem: If your root partition uses xfs, use xfs_growfs. Note that for root filesystem, you can simply use / as the mount point.
1 | sudo xfs_growfs / |
Expanding the LVM Logical Volume (/www)
Now, let’s expand the LVM logical volume mounted at /www. This volume is based on /dev/vdb. Here’s how to do it:
Resize the Physical Volume (PV): First, resize the physical volume /dev/vdb to use the full disk space. We assume the entire /dev/vdb is used as a physical volume.
1 | sudo pvresize /dev/vdb |
Check Volume Group (VG) Free Space: Use vgdisplay to check the available free space in the volume group (in this case, likely named ‘data’ based on the logical volume name data-data). Look for “Free PE / Size” in the output.
1 | sudo vgdisplay |
Extend the Logical Volume (LV): Extend the logical volume data-data to use all the available free space in the volume group. +100%FREE tells lvextend to use all the free space.
1 | sudo lvextend -l +100%FREE /dev/mapper/data-data |
Resize the Filesystem: Resize the filesystem on the logical volume to utilize the expanded space.
For ext4 filesystem: If /www is formatted with ext4:
1 | sudo resize2fs /dev/mapper/data-data |
For xfs filesystem: If /www uses xfs, use xfs_growfs, specifying the mount point /www.
1 | sudo xfs_growfs /www |
Conclusion
That’s it! You’ve successfully expanded both your root partition and your LVM logical volume on CentOS 7.x. By following these steps, you can efficiently manage your disk space and ensure your server has the storage it needs to run smoothly. Remember to always double-check your commands and consider backups before making changes to your disk partitions and filesystems.