How to Expand the Disk Capacity of a Linux Cloud Server?

22-01-2024 01:51:09

This help guide explains how to expand the disk capacity of a Linux cloud server. When we upgrade the cloud server's disk to a larger capacity, the system does not automatically expand the disk size. These steps are required to complete the expansion. This guide is applicable to operating systems like CentOS, Ubuntu, Debian, etc.

Viewing Disk Partitions

First, remotely log in to the Linux cloud server and use the fdisk command to view the disk partition status. As shown in the following figure, you can see that this cloud server has three disks: /dev/vda, /dev/vdb, and /dev/vdc, each with a size of 21.5G. The system disk /dev/vda has already created the /dev/vda1 partition, while the other two data disks have not yet created partitions.

fdisk -l 

Expanding the System Disk

1.Install the growpart Tool

Some cloud server images come with growpart pre-installed. For others, follow these steps to install it.

  • CentOS
yum install -y epel-release
yum install -y cloud-utils
  • Ubuntu
apt-get install cloud-initramfs-growroot

2.Expand the Partition Table

Execute the following command to expand the partition table of the system disk /dev/vda. For CentOS 6 and Debian 8 operating systems, you may encounter a kernel that does not support hot-reloading of the partition table. In this case, after expanding the partition table, a restart of the operating system is required.

LANG=en_US.UTF-8
growpart /dev/vda 1

3.Expand the File System

  • For xfs file system (default in CentOS 7)

    xfs_growfs /
  • For ext4 file system (default in CentOS 6)

    resize2fs /dev/vda1

4.Confirm Completion of Expansion

Use the df command to check if the Avail column for the /dev/vda1 partition has expanded to the new capacity.

df -lh

Creating Data Disks

If there were no data disks before changing configurations, and data disks were created after, follow these steps to create and mount the data disks. Assume the data disk is mounted to the /data directory.

  • For xfs file system (default in CentOS 7)
    mkfs.xfs /dev/vdb
    mount -t xfs /dev/vdb /data

Edit /etc/fstab and add the following content.

/dev/vdb  /data  xfs  defaults,noatime 0 0  
  • For ext4 file system (default in CentOS 6)
    mkfs -t ext4 /dev/vdb 
    mount /dev/vdb /data/

Edit /etc/fstab and add the following content.

/dev/vdb  /data  ext4  defaults,noatime 0 0

Expanding Data Disks

If the data disk already existed before changing configurations and has been upgraded, follow these steps to expand the data disk. Assume the data disk is mounted to the /data directory.

First, use the df command to check the file system type of the data disk, and execute the corresponding expansion command based on the file system type.

df -ihT

  • For xfs file system (default in CentOS 7)

    xfs_repair /dev/vdb
    xfs_growfs /data
  • For ext4 file system (default in CentOS 6)

    e2fsck -f /dev/vdb
    resize2fs /dev/vdb