This topic describes how to extend partitions and file systems on a disk of a Linux Elastic Compute Service (ECS) instance. The disk can be the system disk or a data disk.
Scenarios
When you resize a disk on an instance in the ECS console, the partitions and file systems on the disk are not extended together with the disk. For the new disk capacity to take effect, you must connect to the instance, and then extend partitions and file systems on the disk. You can extend existing partitions or create partitions based on the increased disk space.
- You have resized a disk on a Linux instance and want to extend existing partitions on the disk into the added disk space. Note You can create partitions based on the increased disk space instead of extending existing partitions. For more information, see Resize partitions and file systems of Linux data disks.
- A raw disk (unpartitioned disk) is attached to a Linux instance. In this scenario, proceed to Step 2: Extend file systems.
Prerequisites
- A Linux instance that runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, CentOS 6 or later, Debian 8 or later, or Ubuntu 14 or later is created. Note If the instance runs an operating system that is not of the preceding versions, perform the operations described in Resize partitions and file systems of Linux data disks to extend partitions and file systems.
- A disk on the Linux instance is resized based on the instructions described in Step 1: Resize a disk to extend its capacity.
- (Recommended) Snapshots are created for the disk to back up data. For more information, see Create a snapshot of a disk.
If you did not create snapshots for the disk when you perform Step 1: Resize a disk to extend its capacity, we recommend that you create snapshots for the disk to back up data before you extend partitions and file systems on the disk.
Preparations: Check disk and partition information
Before you extend partitions and file systems on the disk, check disk and partition information to determine the operations that need to be performed.
- Connect to the instance. For more information, see Connect to a Linux instance by using a password or key.
- Run the following command to check disk and partition information:
fdisk -lu
The command output shown in the preceding figure indicates that the instance has two disks: the system disk
/dev/vda
and the data disk/dev/vdb
. In the following examples,/dev/vdb
is used. Take note of the following information that is displayed in the command output:- ①: the device name of the disk.
- ②: indicates whether the disk is partitioned and the names of the partitions.
- Partitioned: If the disk is partitioned, partition information is displayed in the ② section. Device indicates the partition name. In this example, the partition name is
/dev/vdb1
. The value1
in the partition name indicates the partition number. You must extend partitions and file systems on the disk. - Unpartitioned (raw device): If the disk is a raw device that is not partitioned, no partition information is displayed in the ② section. You do not need to extend partitions. Proceed to Step 2: Extend file systems on the disk.
- Partitioned: If the disk is partitioned, partition information is displayed in the ② section. Device indicates the partition name. In this example, the partition name is
- ③: the partition style of the disk.
If the value of
Disk label type
isdos
, it indicates the Master Boot Record (MBR) partition style. If the value of this parameter isgpt
, it indicates the GUID Partition Table (GPT) partition style.Note The values vary based on operating systems. If theDisk label type
parameter is not included in the command output, you can check theSystem
parameter to determine the partition style of the disk. If the value ofSystem
isLinux
, it indicates the MBR partition style. If the value of this parameter isGPT
, it indicates the GPT partition style. - ④: the total size of partitions on the disk. In the preceding command output, the total size of partitions on the disk is 125829120. If the
End
value of the last partition (/dev/vdb1
in the preceding command output) is significantly different from the total size of partitions on the disk, the disk space is not fully occupied by the partition. In the preceding command output, the End value of /dev/vdb1 is 83886079. You must extend the partition. You can also run thelsblk
command to determine the partition that needs to be extended.
Step 1: Extend partitions
This section describes how to extend a disk partition from 40 GB to 60 GB on an instance after the disk is extended to 60 GB in the ECS console. In the following examples, the /dev/vdb1
partition and an instance that runs the Alibaba Cloud Linux 2.1903 LTS 64-bit public image are used. Operations vary based on actual configurations.
- Optional: If the information obtained in the Preparations: Check disk and partition information section indicates that your disk is an MBR disk and is extended to more than 2 TiB in the ECS console, convert the disk into a GPT disk before you perform the next step. If the preceding condition for the operation is not met, skip this step. For more information, see Convert a data disk on a Linux instance from MBR to GPT.
- Install tools.
- If the instance runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 6 or later, run the following commands.
- Install the growpart utility.
type growpart || yum install -y cloud-utils-growpart
- (Optional) Install the gdisk tool if your disk is a GPT disk based on the information that is obtained in the Preparations: Check disk and partition information section:
type sgdisk || yum install -y gdisk
Note CentOS 6 and CentOS 8 reached their end of life (EOL). If your instance runs CentOS 6 or CentOS 8, change the repository addresses of the operating system. For more information, see Change the CentOS 6 source address and Change CentOS 8 repository addresses. - Install the growpart utility.
- If the instance runs Debian 8 or later, or Ubuntu 14 or later, run the following commands.
- Update the software repository.
apt-get update
- Install cloud-guest-utils.
type growpart || apt-get install -y cloud-guest-utils
- (Optional) Install the gdisk tool if your disk is a GPT disk based on the information that is obtained in the Preparations: Check disk and partition information section:
type sgdisk || apt-get install -y gdisk
- Update the software repository.
- If the instance runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 6 or later, run the following commands.
- Extend partitions. In this example, the
/dev/vdb1
disk partition is used. Run the following command to extend the /dev/vdb1 partition.1
specifies the partition number. Use a space to separate/dev/vdb
from1
.LC_ALL=en_US.UTF-8 growpart /dev/vdb 1
LC_ALL=en_US.UTF-8
specifies an environment variable, which ensures compatibility with Chinese character sets.Important- If a single disk contains consecutive partitions, you only need to extend the last partition. For example, assume that the data disk
/dev/vdb
contains three partitions named/dev/vdb1
,/dev/vdb2
, and/dev/vdb3
. You only need to run thegrowpart /dev/vdb 3
command to extend the /dev/vdb3 partition. - If an error is reported when you use the growpart utility, fix the error. For more information, see the FAQ section of this topic.
IfCHANGED
is displayed as shown in the following figure, the partition is extended. - If a single disk contains consecutive partitions, you only need to extend the last partition. For example, assume that the data disk
- Optional:Run the
reboot
command to restart the instance if the instance runs CentOS 6 or earlier.Typically, you do not need to restart instances after you extend the disk partitions on the instances. If you extend a partition on the system disk of a CentOS 6 instance or if the operating system is of an early version and does not recognize the new partition size, restart the instance for the new partition size to take effect. We recommend that you restart instances during off-peak hours to ensure service continuity.
Step 2: Extend file systems
- Run the following command to check the file system type:
df -Th
Type indicates the file system type. Mounted on indicates the mount directory of the partition. In the command output shown in the following figure, the file system on the
/dev/vdb1
partition is an ext4 file system, and the partition is mounted to /mnt.If the Filesystem value does not contain a digit as a suffix, the disk is a raw device. Example:
/dev/vdc
. - Extend file systems.
Commands that are used to extend file systems vary based on file system types. Run one of the following commands to extend a file system based on the file system type that you obtained in the preceding step:
- ext file system including ext2, ext3, and ext4
- For example, if you want to extend the ext file system of the
/dev/vdb1
partition, run the following command:resize2fs /dev/vdb1
- For example, if you want to extend the ext file system of the
/dev/vdc
raw device, run the following command:resize2fs /dev/vdc
- For example, if you want to extend the ext file system of the
- xfs file systemFor example, if you want to extend the xfs file system that is mounted to /mnt, run the following command:
type xfs_growfs || yum install -y xfsprogs xfs_growfs /mnt
- btrfs file systemFor example, if you want to extend the btrfs file system that is mounted to /mnt, run the following command:
btrfs filesystem resize max /mnt
- ext file system including ext2, ext3, and ext4
- Run the following command to check whether the file system is extended:
df -Th
If the file system size displayed in the command output is the same as the file system size displayed in the ECS console, the file system is extended.
The file system size of the/dev/vdb1
partition shown in the following figure is basically the same as the file system size (60 GB) displayed in the ECS console. This indicates that the file system is extended.After you extend file systems, check whether the file system sizes are the same as the specified values.- If the file system sizes are increased to the specified values and business applications on the instance can run as expected, the file systems are extended.
- If the file system sizes are not increased to the specified values, use the snapshots that you created to roll back the disk. For more information, see Roll back a disk by using a snapshot.
FAQ
FAQ about growpart
- Why does the
unexpected output in sfdisk --version [sfdisk, from util-linux 2.23.2]
error message appear when I run a growpart command to extend a partition?Cause: The Chinese operating system of the instance uses a character encoding type that is not en_US.UTF-8.
Solution:- Run the
locale
command to check the character encoding type of the instance. If the character encoding type is not en_US.UTF-8, change the type to en_US.UTF-8.- Run the following command to change the character encoding type:
export LANG=en_US.UTF-8
- If the problem persists, run the following command to change the character encoding type:
export LC_ALL=en_US.UTF-8
- Run the following command to change the character encoding type:
- If the problem persists, run the
reboot
command to restart the instance.
Important If the partition is extended after you change the character encoding type, we recommend that you switch to the original character encoding type. - Run the
- Why does the
unexpected output in sfdisk --version [sfdisk (util-linux-ng 2.17.2)]
error message appear when I run a growpart command to extend a partition?Cause: The fdisk version is incompatible with the growpart version.
Solution: Downgrade growpart to version 0.27.
- Why does the
FAILED: unable to determine partition type
error message appear when I run a growpart command to extend a partition?Cause: The disk is a raw device and has no partitions. You do not need to extend partitions but you need to extend file systems on the disk.
Solution: Extend file systems on the disk.
- Why does the
WARNING: MBR/dos partitioned disk is larger than 2TB. Additional space will go unused
error message appear when I run a growpart command to extend a partition?Cause: The MBR partition style does not support disks that are larger than 2 TiB in size.
Solution: Convert the disk from MBR to GPT and then extend partitions on the disk. For more information, see Convert MBR partitions into GPT partitions.
- Why does the
Error: Cannot retrieve repository metadata
error message appear when I install the growpart utility on CentOS 6?Cause: CentOS 6 reached EOL. Software packages cannot be installed on CentOS 6 by using the default repositories.
Solution:- Change the YUM repository address for CentOS 6. For more information, see Change the CentOS 6 source address. Note CentOS 6 has reached EOL. To use YUM to install CentOS software packages, you must change the YUM repository address for CentOS 6.
- Run the following command to install the growpart utility:
type growpart || yum install -y cloud-utils-growpart
- If the problem persists, perform the steps that are described in Resize partitions and file systems of Linux data disks to extend partitions.
- Change the YUM repository address for CentOS 6. For more information, see Change the CentOS 6 source address.
FAQ about resize2fs
- Question: Why is the following information displayed when I run a resize2fs command to extend file systems?
orresize2fs: Device or resource busy while trying to open /dev/vda Couldn't find valid filesystem superblock.
open: Is a directory while opening /mnt
Cause: When you run a resize2fs command to extend the file system on a partition, you must specify the partition name, instead of the device name of the disk or the mount directory of the partition.
Solution: Specify correct partition names when you extend file systems.
- Question: Why does the
The filesystem is already ... blocks long. Nothing to do!
error message appear when I run a resize2fs command to extend a file system?Cause: No disk space is available for you to extend file systems. This may be caused by an ineffective partition extend operation.
Solution: If the new partition size is not recognized by the operating system after you extend a partition, the reason may be that the operating system is of an early version. Run the
reboot
command to restart the instance for the new partition size to take effect.