All Products
Search
Document Center

Elastic Compute Service:Extend the partitions and file systems of disks on a Linux instance

Last Updated:Sep 04, 2024

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.

    Important
  • (Recommended) A snapshot of the disk is created to back up disk data. For more information, see Create a snapshot for a disk.

    Note

    In 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:

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

  1. Connect to the ECS instance.

    For more information, see Connect to a Linux instance by using a password or key.

  2. Run the following command to check the disk and partition information:

    sudo fdisk -lu

    image.png

    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 number 1 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 is dos, the MBR partition style is used. If the value is gpt, the GPT partition style is used.

      Note
      • The value varies based on the operating system. If the Disk label type field is not displayed, check the System field to determine the partition style. If the value of System is Linux, the MBR partition style is used. If the value is GPT, 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.

      Note

      You can also run the lsblk command to check which partition needs to be extended.

Method 2: Use the ECS console

Important

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.

  1. 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.

  2. Move the pointer over section ① and click the image icons in sections ② and ③ to view the device name, partition style, partition name, file system type, and mount point of the disk.

    image

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.

  1. (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.

  2. 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.

    Note
    • Separate /dev/vdb and 1 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 
    Note

    CentOS 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
    Important

    Take 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 the growpart /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 the growpart /dev/nvme0n1 1 command instead of the growpart /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 the growpart /dev/vdb 2 command to extend the /dev/vdb2 partition and then run the growpart /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.

    image.png

  3. (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

  1. 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.

    image.png

    If the Filesystem value does not contain a digit as a suffix, the disk is a raw device. Example: /dev/vdc.

  2. 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.

    Note

    If 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
      Note

      To 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
  3. 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. image.png

    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

Note

If issues occur and you cannot resolve the issues, you can join the DingTalk group (ID: 25920014655) to contact Alibaba Cloud engineers.

FAQ about the growpart utility

  • Question 1: 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, switch the character encoding type to en_US.UTF-8.

      • Run the following command to change the character encoding type:

        export LANG=en_US.UTF-8
      • If the issue persists, run the following command to change the character encoding type:

        export LC_ALL=en_US.UTF-8
    • If the issue 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 back to the original character encoding type.

  • Question 2: 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. If the issue persists, perform the steps described in the Extend partitions and file systems on a data disk of a Linux instance offline topic to extend the partition.

  • Question 3: 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 that is not partitioned. You do not need to extend partitions, but you need to extend the file systems of the disk.

    Solution: Perform the operations described in the Step 2: Extend file systems section of this topic.

  • Question 4: 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 of the disk. For more information, see Convert MBR partitions into GPT partitions.

  • Question 5: Why does the mkdir: cannot create directory ... No space left on device error message appear when I run a growpart command to extend a partition?

    Cause: The disk space is insufficient and the growpart command cannot create a temporary directory.

    Solution: Delete files that you no longer use and try again.

  • Question 6: 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. You cannot install CentOS 6 packages by using the default repositories.

    Solution:

  • Question 7: Why does a partition of a disk whose device name starts with /dev/mapper fail to be extended?

    Cause: In most cases, on disks whose device names start with /dev/mapper, LVM is used to manage disk partitions.

    Solution: Use LVM to extend the LVs, and then extend the file systems. For more information, see Extend an LV.

  • Question 8: Why does the NOCHANGE: partition 1 could only be grown by 2015 [fudge=2048] error message appear when I run a growpart command to extend a partition?

    Cause: No space is available when you extend the partition. In most cases, this error message appears because you did not restart the instance in the ECS console or by calling an API operation to make the new disk size take effect after you resize the disk offline in the ECS console.

    Solution: After you resize the disk offline, you must restart the instance in the ECS console or by calling an API operation to make the new disk size take effect. If you run the reboot command to restart the instance from within the instance, the new disk size does not take effect. For more information, see Restart an instance and RebootInstance.

FAQ about the resize2fs tool

  • Question 1: Why does the Couldn't find valid filesystem superblock. or open: Is a directory while opening /mnt error message appear when you run a resize2fs command to extend a file system?

    Cause: When you run a resize2fs command to extend the file system of 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 2: 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 the file system. This may be caused by an ineffective partition extend operation.

    Solution:

    • Check whether a growpart command is run to extend the partition. If not, run a growpart command to extend the partition.

    • If a growpart command is run to extend the partition, CHANGED is displayed, and the preceding error message is still returned when you run a resize2fs command, the operating system of the instance may be of an earlier version that does not allow system partition tables to be updated online. Run the reboot command to restart the instance, and then run a resize2fs command.

  • Question 3: Why does the resize2fs: Permission denied to resize filesystem error message appear when I run a resize2fs command to extend a file system?

    Cause: File system inconsistency may exist.

    Solution:

    • Run the dmesg -T | grep fs command. If an error message similar to There are errors in the filesystem is displayed in the command output, file system inconsistency exists.

    • Back up disk data. For more information, see Create a snapshot.

    • On the premise that business is not affected, run the umount command to unmount the mounted partition or the entire disk based on the actual scenario.

      Note

      If you cannot unmount the system disk by using the umount command, detach the system disk, and then attach the disk to a different instance. For more information, see Detach or attach a system disk.

    • Run the e2fsck command to resolve the issue. For information about how to use the e2fsck command, see the relevant documentation.