When you resize a disk attached to an Elastic Compute Service (ECS) instance in the ECS console, the partitions and file systems of the disk are not automatically extended together with the disk. You must manually extend the partitions and file systems of the disk into the additional disk space to make the new disk size take effect. This topic describes how to extend partitions and file systems of a disk attached to a Linux ECS instance.
Prerequisites
A Linux ECS instance that runs one of the following operating systems is created:
Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, CentOS 6 or later, Debian 8 or later, or Ubuntu 14 or later.
ImportantTo extend the size of a disk attached to a CentOS 6 instance to greater than 2 TiB or extend the partitions of the disk in the GUID Partition Table (GPT) partition style, run the
parted
command. For more information, see Extend partitions and file systems on a data disk of a Linux instance offline. Do not use thegrowpart
command described in this topic. Otherwise, the partition table may be damaged. Check whether the disk uses the GPT partition style. For more information, see the Preparation: Obtain disk information section of this topic.For information about how to extend the partitions and file systems of disks attached to an ECS instance that runs a different operating system, see Extend partitions and file systems on a data disk of a Linux instance offline.
(Recommended) A snapshot of the disk is created to back up disk data. For more information, see Create a snapshot for a disk.
NoteIn most cases, data loss does not occur when you extend partitions and file systems. To prevent data loss or exceptions caused by accidental operations, we recommend that you create snapshots to back up disk data.
A disk attached to the instance is resized in the ECS console. For more information, see Step 1: Resize a disk to extend the capacity of the disk.
If you set the Resizing Method parameter to Offline Resizing to resize a disk that is attached to an ECS instance, you must restart the instance in the ECS console or by calling an API operation after you resize the disk to make the new disk size take effect. The new disk size does not take effect if you run the
reboot
command on the instance to restart the instance. For more information, see Restart an instance.
Considerations
This topic is not applicable to scenarios in which Logical Volume Manager (LVM) is used to manage disk partitions.
If you use LVM to manage disk partitions, you must use LVM to extend the logical volumes (LVs) and the file systems after you resize the disk in the ECS console. For more information, see Extend an LV.
This topic is not applicable to scenarios in which the multi-attach feature is enabled for disks and cluster file systems are used.
The multi-attach feature allows you to mount a disk to multiple mount points for access. In this case, if you extend the partitions or file systems at one mount point, the other mount points may be affected, which undermines the consistency or availability of the cluster. For more information, see Multi-attach for cloud disks.
Procedure
The operations that are described in this topic are suitable for the following scenarios:
You resized a disk attached to a Linux instance, and you want to extend existing partitions of the disk into the additional disk space.
NoteYou can also create partitions in the additional disk space instead of extending existing partitions. For more information, see Extend partitions and file systems on a data disk of a Linux instance offline.
A raw disk (unpartitioned disk) is attached to a Linux instance. In this case, skip Step 1: Extend partitions and proceed to Step 2: Extend file systems.
This topic also provides sample commands that you can use to extend partitions and file systems. In the examples, Master Boot Record (MBR) partitions, GPT partitions, and a raw disk are used.
Preparation: Obtain disk information
Before you extend the partitions and file systems of a disk, obtain the disk and partition information.
Method 1: Use the CLI
Connect to the ECS instance.
For more information, see Connect to a Linux instance by using a password or key.
Run the following command to check the disk and partition information:
sudo fdisk -lu
As shown in the preceding figure, the instance has two disks: system disk
/dev/vda
and data disk/dev/vdb
. In this example, the data disk/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 section ②. Device indicates the partition name. In this example, the partition name is
/dev/vdb1
. The number1
indicates the partition number. In this case, you must extend the partitions and file systems of the disk.Not partitioned (raw device): If the disk is a raw device, no partition information is displayed in section ②. In this case, skip Step 1: Extend partitions and proceed to Step 2: Extend file systems.
③: the partition style of the disk.
If the value of
Disk label type
isdos
, the MBR partition style is used. If the value isgpt
, the GPT partition style is used.NoteThe value varies based on the operating system. If the
Disk label type
field is not displayed, check theSystem
field to determine the partition style. If the value ofSystem
isLinux
, the MBR partition style is used. If the value isGPT
, the GPT partition style is used.If you use an earlier version of fdisk, the GPT partition style may not be displayed. To check the partition style, you can run the
gdisk -l /dev/vdb
command.
④: indicates whether the partition needs to be extended.
The capacity of a disk or partition is calculated by using the following formula: Number of sectors of the disk or partition × 512 bytes.
In the preceding figure, the number of sectors of the
/dev/vdb
disk is 125,829,120. The disk capacity is 64.4 GB (or 60 GiB), which is calculated by using the following formula: 125,829,120 × 512 bytes.The number of sectors of the
/dev/vdb1
partition is 83,886,078, which is calculated by using the following formula: Value of End - Value of Start. The partition capacity is 42.9 GB (or 40 GiB), which is calculated by using the following formula: 83,886,078 × 512 bytes.
The capacity of the
/dev/vdb1
partition is smaller than the capacity of the/dev/vdb
disk. The/dev/vdb1
partition needs to be extended.NoteYou can also run the
lsblk
command to check which partition needs to be extended.
Method 2: Use the ECS console
Take note of the following items:
This method is applicable only to specific users. You can go to the ECS console to check whether you can use this method.
Make sure that Cloud Assistant Agent is installed on the ECS instance to which the disk is attached. For more information, see Install Cloud Assistant Agent.
Go to the details page of the ECS instance, click the Block Storage (Disks) tab, and then find the disk whose partitions and file systems need to be extended.
Move the pointer over section ① and click the icons in sections ② and ③ to view the device name, partition style, partition name, file system type, and mount point of the disk.
Step 1: Extend partitions
In this example, the instance uses an Alibaba Cloud Linux 3.2104 LTS 64-bit public image, the /dev/vdb disk of the instance is resized to 60 GiB, and the /dev/vdb1
partition of the disk is extended from 40 GiB to 60 GiB.
(Conditionally required) If the information obtained in the Preparation: Obtain disk information section of this topic indicates that the partition style of the disk is MBR, and the disk is resized in the ECS console to a size larger than 2 TiB, change the partition style to GPT.
For information about how to change the partition style of a disk, see Convert MBR partitions into GPT partitions.
Extend a specific partition. Run the following commands to extend the
/dev/vdb1
partition based on the operating system of the instance and partition style of the disk.NoteSeparate
/dev/vdb
and1
with a space in the command.1
specifies the number of the partition.If you want to extend a partition of the system disk, replace
/dev/vdb
in the command with/dev/vda
.
Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 6 or later
MBR partition style
type growpart || sudo yum install -y cloud-utils-growpart sudo yum update cloud-utils-growpart sudo LC_ALL=en_US.UTF-8 growpart /dev/vdb 1
GPT partition style (gDisk must be installed)
type growpart || sudo yum install -y cloud-utils-growpart sudo yum update cloud-utils-growpart type sgdisk || sudo yum install -y gdisk sudo LC_ALL=en_US.UTF-8 growpart /dev/vdb 1
NoteCentOS 6 and CentOS 8 reached 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 How do I change CentOS 6 repository addresses? or Change CentOS 8 repository addresses.
Debian 8 or later, or Ubuntu 14 or later
MBR partition style
sudo apt-get update sudo apt-get install -y cloud-guest-utils sudo LC_ALL=en_US.UTF-8 growpart /dev/vdb 1
GPT partition style (gDisk must be installed)
sudo apt-get update sudo apt-get install -y cloud-guest-utils type sgdisk || sudo apt-get install -y gdisk sudo LC_ALL=en_US.UTF-8 growpart /dev/vdb 1
ImportantTake note of the following items:
Set the
LC_ALL
environment variable to en_US.UTF-8 to ensure compatibility with Chinese character sets.If a single disk has successive partitions, you can extend only the last partition. For example, the
/dev/vdb
data disk has three partitions named/dev/vdb1
,/dev/vdb2
, and/dev/vdb3
. You can extend only the last partition (/dev/vdb3
). To extend the partition, run thegrowpart /dev/vdb 3
command.If the disk is a Nonvolatile Memory Express (NVMe) disk whose device name is
/dev/nvme0n1
and you want to extend the/dev/nvme0n1p1
partition, run thegrowpart /dev/nvme0n1 1
command instead of thegrowpart /dev/nvme0n1p1 1
command.If you want to extend a logical partition (whose partition number is greater than or equal to 5 in most cases) of an extended MBR partition, you must first extend the extended MBR partition. For example, the following partitions exist:
/dev/vdb1
(primary),/dev/vdb2
(extended), and/dev/vdb5
(logical). You must run thegrowpart /dev/vdb 2
command to extend the /dev/vdb2 partition and then run thegrowpart /dev/vdb 5
command to extend the /dev/vdb5 partition.If you use the growpart utility and errors are reported, see the FAQ section of this topic for troubleshooting.
If
CHANGED
is displayed as shown in the following figure, the partition is extended.(Conditionally required) If the instance runs CentOS 6 or earlier, run the
reboot
command to restart the instance for the new partition size to take effect.In most cases, you do not need to restart instances after you extend 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 earlier 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 and the directory on which the file system is mounted:
df -Th
Type
specifies the file system type.Mounted on
specifies 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 the file system.
Commands that are used to extend file systems vary based on file system types. Run one of the following commands to extend file systems based on the file system type that you obtained in the preceding step.
NoteIf you use the resize2fs tool to extend file systems while the file systems and the associated disk are being used, data issues may occur in the file systems. The issues are related to the image kernel version and the version of the resize2fs tool. The issues are not related to the disk resize operation. We recommend that you create snapshots of the disk to back up data in advance. For information about how to create snapshots of disks to back up disk data, see Create a snapshot.
Ext file system, such as Ext2, Ext3, or Ext4
For example, to extend the Ext file system of the
/dev/vdb1
partition, run the following command:sudo resize2fs /dev/vdb1
NoteTo extend the file system of the partition of the system disk, replace
/dev/vdb1
in the command with/dev/vda1
.For example, to extend the Ext file system of the
/dev/vdc
raw device, run the following command:sudo resize2fs /dev/vdc
XFS file system
For example, to extend the XFS file system that is mounted to /mnt, run one of the following commands based on the instance operating system:
Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 6 or later
type xfs_growfs || sudo yum install -y xfsprogs sudo xfs_growfs /mnt
Debian 8 or later, or Ubuntu 14 or later
type xfs_growfs || sudo apt-get install -y xfsprogs sudo xfs_growfs /mnt
Btrfs file system
For example, to extend the Btrfs file system that is mounted to /mnt, run the following command:
sudo btrfs filesystem resize max /mnt
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 (59 GiB) of the
/dev/vdb1
partition shown in the following figure is close to the file system size (60 GiB) displayed in the ECS console, which indicates that the file system is extended. The size difference exists because the file system information uses storage space.After you extend file systems, check the file system sizes.
If the file system sizes are increased to the specified values and business applications on the instance 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.
Examples
This section provides sample commands that you can use to extend partitions and file systems. In the examples, MBR partition, GPT partitions, and a raw disk are used. The commands are only for reference. The actual commands may vary based on your environment.
Example 1: Extend an MBR partition and the Ext4 file system of the partition
Device name: /dev/vda (system disk)
Partition: /dev/vda1
File system type: Ext4
Operating system: Alibaba Cloud Linux 3
Run the following commands to extend the /dev/vda1 partition and the Ext4 file system of the partition:
type growpart || sudo yum install -y cloud-utils-growpart
sudo yum update cloud-utils-growpart
sudo LC_ALL=en_US.UTF-8 growpart /dev/vda 1
sudo resize2fs /dev/vda1
Example 2: Extend a GPT partition and the XFS file system of the partition
Device name: /dev/vdb (data disk)
Two partitions: /dev/vdb1 and /dev/vdb2
File system type: XFS
Directory to which /dev/vdb2 is mounted: /mnt
Operating system: Debian 12
Run the following commands to extend the /dev/vdb2 partition and the XFS file system of the partition:
sudo apt-get update
sudo apt-get install -y cloud-guest-utils
type sgdisk || sudo apt-get install -y gdisk
sudo LC_ALL=en_US.UTF-8 growpart /dev/vdb 2
type xfs_growfs || sudo apt-get install -y xfsprogs
sudo xfs_growfs /mnt
Example 3: Extend the file system of a raw data disk
Device name: /dev/vdc (raw data disk that is not partitioned)
File system type: Ext4
Operating system: CentOS 7
Run the following command to extend the Ext4 file system of the /dev/vdc disk:
sudo resize2fs /dev/vdc
FAQ
If issues occur and you cannot resolve the issues, you can join the DingTalk group (ID: 25920014655) to contact Alibaba Cloud engineers.