Today, CentOS is still common. We can also use the growpart and xfsprogs expansion tools to achieve lossless expansion when we need to expand disks, but not all disk types support it. For example, we need to ensure that the hard disk of the physical server or virtual machine has increased space, or adjust the disk size in the ECS. The file system type used (xfs, ext4, etc.) requires different tools to perform extension operations.
Confirm Disk Space
Check the current partition:
Run the following command to check the current size and partition information of the disk:
lsblk
Or:
df -h
These commands display the size and mount point information of disks and partitions. Suppose you need to expand the disk where the root partition (/) is located.
Expand partitions using the growpart tool
To install the growpart tool:
If growpart is not installed on your system, you need to install it first. You can use the following command to install:
sudo yum install cloud-utils-growpart
Extended partition:
Assuming your partition is/dev/sda1, run the following command to extend it:
sudo growpart /dev/sda 1
Where/dev/sda is the disk and 1 is the partition number. After running, it will expand the partition to the maximum space on the disk.
Extended File System
If the file system is xfs:
If your system uses the xfs file system, you can use the xfs_growfs tool to extend the file system. Run the following command on the root partition:
sudo xfs_growfs /
This command will automatically expand the root partition to the new disk space.
If the file system is ext4:
Use the following command to check whether the file system is normal:
sudo e2fsck -f /dev/sda1
Extend ext4 file system:
Use the resize2fs command to expand the size of the file system:
sudo resize2fs /dev/sda1
This will expand the file system to accommodate the new partition size.
Check whether the disk expansion is successful
Run the following command to check the disk space again to confirm whether it has been expanded successfully:
df -h
You should be able to see that the root partition (or your extended partition) has added new space.